Skip to content

Commit be21fbe

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 0e50f1b commit be21fbe

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,
@@ -781,11 +781,25 @@ static void pid_control(Data *d, float dt) {
781781

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

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

791805
static void refloat_thd(void *arg) {
@@ -807,8 +821,6 @@ static void refloat_thd(void *arg) {
807821

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

810-
imu_update(&d->imu, &d->balance_filter, &d->state);
811-
812824
beeper_update(d);
813825

814826
charging_timeout(&d->charging, &d->state);
@@ -955,8 +967,6 @@ static void refloat_thd(void *arg) {
955967
}
956968
}
957969

958-
pid_control(d, dt);
959-
960970
break;
961971
case (STATE_READY):
962972
if (d->state.mode == MODE_FLYWHEEL) {
@@ -1076,15 +1086,9 @@ static void refloat_thd(void *arg) {
10761086
break;
10771087
}
10781088

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

0 commit comments

Comments
 (0)