forked from M-Labs/thermostat
pid::Parameters, CenterPoint: demote f32 fields to save config space
This commit is contained in:
parent
c5c0ce5625
commit
58e648b5e0
@ -103,7 +103,7 @@ impl Channels {
|
||||
CenterPoint::Vref =>
|
||||
state.vref,
|
||||
CenterPoint::Override(center_point) =>
|
||||
ElectricPotential::new::<volt>(center_point),
|
||||
ElectricPotential::new::<volt>(center_point.into()),
|
||||
};
|
||||
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
||||
let (voltage, max) = self.get_dac(channel);
|
||||
@ -137,7 +137,7 @@ impl Channels {
|
||||
CenterPoint::Vref =>
|
||||
state.vref,
|
||||
CenterPoint::Override(center_point) =>
|
||||
ElectricPotential::new::<volt>(center_point),
|
||||
ElectricPotential::new::<volt>(center_point.into()),
|
||||
};
|
||||
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
||||
let voltage = i_tec * 10.0 * r_sense + center_point;
|
||||
|
@ -126,7 +126,7 @@ pub enum PwmPin {
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum CenterPoint {
|
||||
Vref,
|
||||
Override(f64),
|
||||
Override(f32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@ -312,7 +312,7 @@ fn center_point(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
||||
value(Ok(CenterPoint::Vref), tag("vref")),
|
||||
|input| {
|
||||
let (input, value) = float(input)?;
|
||||
Ok((input, value.map(CenterPoint::Override)))
|
||||
Ok((input, value.map(|value| CenterPoint::Override(value as f32))))
|
||||
}
|
||||
))(input)?;
|
||||
end(input)?;
|
||||
|
@ -37,7 +37,7 @@ impl Config {
|
||||
pub struct ChannelConfig {
|
||||
center: CenterPoint,
|
||||
pid: pid::Parameters,
|
||||
pid_target: f64,
|
||||
pid_target: f32,
|
||||
sh: steinhart_hart::Parameters,
|
||||
// TODO: pwm limits
|
||||
}
|
||||
@ -47,7 +47,7 @@ impl ChannelConfig {
|
||||
ChannelConfig {
|
||||
center: state.center.clone(),
|
||||
pid: state.pid.parameters.clone(),
|
||||
pid_target: state.pid.target,
|
||||
pid_target: state.pid.target as f32,
|
||||
sh: state.sh.clone(),
|
||||
}
|
||||
}
|
||||
|
14
src/main.rs
14
src/main.rs
@ -388,19 +388,19 @@ fn main() -> ! {
|
||||
pid.reset();
|
||||
}
|
||||
KP =>
|
||||
pid.parameters.kp = value,
|
||||
pid.parameters.kp = value as f32,
|
||||
KI =>
|
||||
pid.parameters.ki = value,
|
||||
pid.parameters.ki = value as f32,
|
||||
KD =>
|
||||
pid.parameters.kd = value,
|
||||
pid.parameters.kd = value as f32,
|
||||
OutputMin =>
|
||||
pid.parameters.output_min = value,
|
||||
pid.parameters.output_min = value as f32,
|
||||
OutputMax =>
|
||||
pid.parameters.output_max = value,
|
||||
pid.parameters.output_max = value as f32,
|
||||
IntegralMin =>
|
||||
pid.parameters.integral_min = value,
|
||||
pid.parameters.integral_min = value as f32,
|
||||
IntegralMax =>
|
||||
pid.parameters.integral_max = value,
|
||||
pid.parameters.integral_max = value as f32,
|
||||
}
|
||||
let _ = writeln!(socket, "PID parameter updated");
|
||||
}
|
||||
|
36
src/pid.rs
36
src/pid.rs
@ -2,13 +2,13 @@ use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct Parameters {
|
||||
pub kp: f64,
|
||||
pub ki: f64,
|
||||
pub kd: f64,
|
||||
pub output_min: f64,
|
||||
pub output_max: f64,
|
||||
pub integral_min: f64,
|
||||
pub integral_max: f64
|
||||
pub kp: f32,
|
||||
pub ki: f32,
|
||||
pub kd: f32,
|
||||
pub output_min: f32,
|
||||
pub output_max: f32,
|
||||
pub integral_min: f32,
|
||||
pub integral_max: f32
|
||||
}
|
||||
|
||||
impl Default for Parameters {
|
||||
@ -50,32 +50,32 @@ impl Controller {
|
||||
let error = self.target - input;
|
||||
|
||||
// partial
|
||||
let p = self.parameters.kp * error;
|
||||
let p = f64::from(self.parameters.kp) * error;
|
||||
|
||||
//integral
|
||||
self.integral += error;
|
||||
if self.integral < self.parameters.integral_min {
|
||||
self.integral = self.parameters.integral_min;
|
||||
if self.integral < self.parameters.integral_min.into() {
|
||||
self.integral = self.parameters.integral_min.into();
|
||||
}
|
||||
if self.integral > self.parameters.integral_max {
|
||||
self.integral = self.parameters.integral_max;
|
||||
if self.integral > self.parameters.integral_max.into() {
|
||||
self.integral = self.parameters.integral_max.into();
|
||||
}
|
||||
let i = self.parameters.ki * self.integral;
|
||||
let i = f64::from(self.parameters.ki) * f64::from(self.integral);
|
||||
|
||||
// derivative
|
||||
let d = match self.last_input {
|
||||
None => 0.0,
|
||||
Some(last_input) => self.parameters.kd * (last_input - input)
|
||||
Some(last_input) => f64::from(self.parameters.kd) * (last_input - input)
|
||||
};
|
||||
self.last_input = Some(input);
|
||||
|
||||
// output
|
||||
let mut output = p + i + d;
|
||||
if output < self.parameters.output_min {
|
||||
output = self.parameters.output_min;
|
||||
if output < self.parameters.output_min.into() {
|
||||
output = self.parameters.output_min.into();
|
||||
}
|
||||
if output > self.parameters.output_max {
|
||||
output = self.parameters.output_max;
|
||||
if output > self.parameters.output_max.into() {
|
||||
output = self.parameters.output_max.into();
|
||||
}
|
||||
self.last_output = Some(output);
|
||||
output
|
||||
|
Loading…
Reference in New Issue
Block a user