Skip to content

Commit 23f440e

Browse files
committed
Float pkg: Decouple ki config value from the loop time.
Going into the future the ki will "feel" like as it would have if the loop frequency was 832 Hz - the default loop frequency of Float pkg.
1 parent 717b5ac commit 23f440e

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

float/float/float.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ typedef struct {
161161
float mc_max_temp_fet, mc_max_temp_mot;
162162
float mc_current_max, mc_current_min, current_max, current_min, max_continuous_current;
163163
float surge_angle, surge_angle2, surge_angle3, surge_adder;
164+
float ki; // config ki scaled taking account the loop frequency.
164165
bool overcurrent;
165166
bool surge_enable;
166167
bool current_beeping;
@@ -393,6 +394,11 @@ static void app_init(data *d) {
393394
d->odometer = VESC_IF->mc_get_odometer();
394395
}
395396

397+
static float normalized_to_iteration_ki(float normalized_ki, int loop_freq_hz) {
398+
// Default frequency of Float thread used to be 832 Hz so we retain that scale.
399+
return normalized_ki * (832.0 / loop_freq_hz);
400+
}
401+
396402
static void configure(data *d) {
397403
d->debug_render_1 = 2;
398404
d->debug_render_2 = 4;
@@ -418,6 +424,7 @@ static void configure(data *d) {
418424
d->turntilt_step_size = d->float_conf.turntilt_speed / d->float_conf.hertz;
419425
d->noseangling_step_size = d->float_conf.noseangling_speed / d->float_conf.hertz;
420426
d->inputtilt_step_size = d->float_conf.inputtilt_speed / d->float_conf.hertz;
427+
d->ki = normalized_to_iteration_ki(d->float_conf.ki, d->float_conf.hertz);
421428

422429
d->surge_angle = d->float_conf.surge_angle;
423430
d->surge_angle2 = d->float_conf.surge_angle * 2;
@@ -2011,7 +2018,7 @@ static void float_thd(void *arg) {
20112018
bool tail_down = SIGN(d->proportional) != SIGN(d->erpm);
20122019

20132020
// Resume real PID maths
2014-
d->pid_integral = d->pid_integral + d->proportional * d->float_conf.ki;
2021+
d->pid_integral = d->pid_integral + d->proportional * d->ki;
20152022

20162023
// Apply I term Filter
20172024
if (d->float_conf.ki_limit > 0 && fabsf(d->pid_integral) > d->float_conf.ki_limit) {
@@ -2729,6 +2736,7 @@ static void cmd_runtime_tune(data *d, unsigned char *cfg, int len)
27292736
d->float_conf.ki = 0.005;
27302737
else if (h1 > 1)
27312738
d->float_conf.ki = ((float)(h1 - 1)) / 100;
2739+
d->ki = normalized_to_iteration_ki(d->float_conf.ki, d->float_conf.hertz);
27322740
d->float_conf.ki_limit = h2 + 19;
27332741
if (h2 == 0)
27342742
d->float_conf.ki_limit = 0;

0 commit comments

Comments
 (0)