Skip to content

Commit 24813a9

Browse files
committed
Move PID control to the IMU callback
Feature: Move core (PID) balancing control to the IMU callback > This removes a source of timing inaccuracies stemming from an arbitrary delay between a new IMU sample arriving and being used.
1 parent a9da123 commit 24813a9

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

src/main.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,13 @@ static void main_freq_update_reconfigure(float frequency) {
159159
Data *d = (Data *) ARG;
160160

161161
motor_data_configure(&d->motor, d->float_conf.atr_filter, frequency);
162-
pid_configure(&d->pid, frequency);
163162
motor_control_configure(&d->motor_control, &d->float_conf, frequency);
164163

165164
torque_tilt_configure(&d->torque_tilt, &d->float_conf, frequency);
166165
atr_configure(&d->atr, &d->float_conf, frequency);
167166
brake_tilt_configure(&d->brake_tilt, &d->float_conf, frequency);
168167
turn_tilt_configure(&d->turn_tilt, &d->float_conf, frequency);
169168
remote_configure(&d->remote, &d->float_conf, frequency);
170-
booster_configure(&d->booster, frequency);
171-
172-
ema_configure(&d->balance_current, 25.0f, frequency);
173169

174170
reverse_stop_configure(&d->reverse_stop, frequency);
175171

@@ -184,6 +180,10 @@ static void main_freq_update_reconfigure(float frequency) {
184180
static void imu_freq_update_reconfigure(float frequency) {
185181
Data *d = (Data *) ARG;
186182

183+
pid_configure(&d->pid, frequency);
184+
booster_configure(&d->booster, frequency);
185+
ema_configure(&d->balance_current, 25.0f, frequency);
186+
187187
log_msg(
188188
"IMU freq reconfgure old: %dHz new: %dHz",
189189
(int32_t) d->imu_t.filter_frequency,
@@ -782,11 +782,25 @@ static void pid_control(Data *d, float dt) {
782782

783783
static void imu_ref_callback(float *acc, float *gyro, float *mag, float dt) {
784784
unused(mag);
785-
786785
Data *d = (Data *) ARG;
786+
787+
time_t time = vesc_system_time_ticks();
788+
787789
balance_filter_update(&d->balance_filter, gyro, acc, dt);
788790

789791
frequency_tracker_update(&d->imu_t, dt);
792+
793+
imu_update(&d->imu, &d->balance_filter, &d->state);
794+
795+
if (d->state.state == STATE_RUNNING) {
796+
pid_control(d, dt);
797+
}
798+
799+
motor_control_apply(
800+
&d->motor_control, d->motor.abs_erpm_smooth.value, d->state.state, &d->time
801+
);
802+
803+
data_recorder_sample(&d->data_record, d, time);
790804
}
791805

792806
static void refloat_thd(void *arg) {
@@ -808,8 +822,6 @@ static void refloat_thd(void *arg) {
808822

809823
time_update(&d->time, d->state.state);
810824

811-
imu_update(&d->imu, &d->balance_filter, &d->state);
812-
813825
beeper_update(d);
814826

815827
charging_timeout(&d->charging, &d->state);
@@ -956,8 +968,6 @@ static void refloat_thd(void *arg) {
956968
}
957969
}
958970

959-
pid_control(d, dt);
960-
961971
break;
962972
case (STATE_READY):
963973
if (d->state.mode == MODE_FLYWHEEL) {
@@ -1077,15 +1087,9 @@ static void refloat_thd(void *arg) {
10771087
break;
10781088
}
10791089

1080-
motor_control_apply(
1081-
&d->motor_control, d->motor.abs_erpm_smooth.value, d->state.state, &d->time
1082-
);
1083-
10841090
int32_t ticks =
10851091
lrintf(VESC_IF->timer_seconds_elapsed_since(loop_timer) * SYSTEM_TICK_RATE_HZ);
10861092
sleep_ticks = max(d->main_loop_ticks - ticks, 1);
1087-
1088-
data_recorder_sample(&d->data_record, d, d->time.now);
10891093
}
10901094
}
10911095

0 commit comments

Comments
 (0)