Skip to content

Commit fe27429

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 f077e81 commit fe27429

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
@@ -152,21 +152,21 @@ static void main_freq_update_reconfigure(float frequency) {
152152
Data *d = (Data *) ARG;
153153

154154
motor_data_configure(&d->motor, d->float_conf.atr_filter, frequency);
155-
pid_configure(&d->pid, frequency);
156155
motor_control_configure(&d->motor_control, &d->float_conf, frequency);
157156

158157
atr_configure(&d->atr, &d->float_conf, frequency);
159158
turn_tilt_configure(&d->turn_tilt, &d->float_conf, frequency);
160-
booster_configure(&d->booster, frequency);
161-
162-
ema_configure(&d->balance_current, 25.0f, frequency);
163159

164160
reverse_stop_configure(&d->reverse_stop, frequency);
165161
}
166162

167163
// Reconfigures all components dependent on the IMU loop frequency.
168164
static void imu_freq_update_reconfigure(float frequency) {
169165
Data *d = (Data *) ARG;
166+
167+
pid_configure(&d->pid, frequency);
168+
booster_configure(&d->booster, frequency);
169+
ema_configure(&d->balance_current, 25.0f, frequency);
170170
}
171171

172172
static void reconfigure(Data *d) {
@@ -764,11 +764,25 @@ static void pid_control(Data *d, float dt) {
764764

765765
static void imu_ref_callback(float *acc, float *gyro, float *mag, float dt) {
766766
unused(mag);
767-
768767
Data *d = (Data *) ARG;
768+
769+
time_t time = vesc_system_time_ticks();
770+
769771
balance_filter_update(&d->balance_filter, gyro, acc, dt);
770772

771773
frequency_tracker_update(&d->imu_freq_tracker, dt);
774+
775+
imu_update(&d->imu, &d->balance_filter, &d->state);
776+
777+
if (d->state.state == STATE_RUNNING) {
778+
pid_control(d, dt);
779+
}
780+
781+
motor_control_apply(
782+
&d->motor_control, d->motor.abs_erpm_smooth.value, d->state.state, &d->time
783+
);
784+
785+
data_recorder_sample(&d->data_record, d, time);
772786
}
773787

774788
static void refloat_thd(void *arg) {
@@ -790,8 +804,6 @@ static void refloat_thd(void *arg) {
790804

791805
time_update(&d->time, d->state.state);
792806

793-
imu_update(&d->imu, &d->balance_filter, &d->state);
794-
795807
beeper_update(d);
796808

797809
charging_timeout(&d->charging, &d->state);
@@ -949,8 +961,6 @@ static void refloat_thd(void *arg) {
949961
}
950962
}
951963

952-
pid_control(d, dt);
953-
954964
break;
955965
case (STATE_READY):
956966
if (d->state.mode == MODE_FLYWHEEL) {
@@ -1071,15 +1081,9 @@ static void refloat_thd(void *arg) {
10711081
break;
10721082
}
10731083

1074-
motor_control_apply(
1075-
&d->motor_control, d->motor.abs_erpm_smooth.value, d->state.state, &d->time
1076-
);
1077-
10781084
int32_t ticks =
10791085
lrintf(VESC_IF->timer_seconds_elapsed_since(loop_timer) * SYSTEM_TICK_RATE_HZ);
10801086
sleep_ticks = max(d->main_loop_ticks - ticks, 1);
1081-
1082-
data_recorder_sample(&d->data_record, d, d->time.now);
10831087
}
10841088
}
10851089

0 commit comments

Comments
 (0)