Integral rescaling

This commit is contained in:
topquark12 2021-01-08 11:25:01 +08:00
parent cc0126636c
commit 5c84b7438b
2 changed files with 12 additions and 2 deletions

View File

@ -333,7 +333,7 @@ fn main() -> ! {
KP =>
pid.parameters.kp = value as f32,
KI =>
pid.parameters.ki = value as f32,
pid.update_ki(value as f32),
KD =>
pid.parameters.kd = value as f32,
OutputMin =>

View File

@ -109,6 +109,16 @@ impl Controller {
integral: self.integral,
}
}
pub fn update_ki(&mut self, new_ki: f32) {
if new_ki == 0.0 {
self.integral = 0.0;
} else {
// Rescale integral with changes to kI, aka "Bumpless operation"
self.integral = f64::from(self.parameters.ki) * self.integral / f64::from(new_ki);
}
self.parameters.ki = new_ki;
}
}
#[derive(Clone, Serialize, Deserialize)]