Add swap command

atse 2024-03-06 14:50:33 +08:00
parent 76547be90a
commit 471cfd8157
5 changed files with 31 additions and 3 deletions

View File

@ -35,6 +35,7 @@ pub struct ChannelState {
pub pid_engaged: bool,
pub pid: pid::Controller,
pub sh: sh::Parameters,
pub swap_tec_polarity: bool,
}
impl ChannelState {
@ -51,6 +52,7 @@ impl ChannelState {
pid_engaged: false,
pid: pid::Controller::new(pid::Parameters::default()),
sh: sh::Parameters::default(),
swap_tec_polarity: false,
}
}

View File

@ -114,7 +114,11 @@ impl<'a> Channels<'a> {
pub fn get_i(&mut self, channel: usize) -> ElectricCurrent {
let i_set = self.channel_state(channel).i_set;
i_set
if self.channel_state(channel).swap_tec_polarity {
-i_set
} else {
i_set
}
}
/// i_set DAC
@ -129,12 +133,15 @@ impl<'a> Channels<'a> {
voltage
}
pub fn set_i(&mut self, channel: usize, i_set: ElectricCurrent) -> ElectricCurrent {
pub fn set_i(&mut self, channel: usize, mut i_set: ElectricCurrent) -> ElectricCurrent {
let vref_meas = match channel.into() {
0 => self.channel0.vref_meas,
1 => self.channel1.vref_meas,
_ => unreachable!(),
};
if self.channel_state(channel).swap_tec_polarity {
i_set = -i_set;
}
let center_point = vref_meas;
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
let voltage = i_set * 10.0 * r_sense + center_point;
@ -371,7 +378,12 @@ impl<'a> Channels<'a> {
// Get current passing through TEC
pub fn get_tec_i(&mut self, channel: usize) -> ElectricCurrent {
(self.read_itec(channel) - self.read_vref(channel)) / ElectricalResistance::new::<ohm>(0.4)
let tec_i = (self.read_itec(channel) - self.read_vref(channel)) / ElectricalResistance::new::<ohm>(0.4);
if self.channel_state(channel).swap_tec_polarity {
-tec_i
} else {
tec_i
}
}
// Get voltage across TEC

View File

@ -412,6 +412,14 @@ impl Handler {
}
}
fn swap_tec_polarity (socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
for c in 0..CHANNELS {
channels.channel_state(c).swap_tec_polarity = !channels.channel_state(c).swap_tec_polarity;
}
send_line(socket, b"{}");
Ok(Handler::Handled)
}
pub fn handle_command(command: Command, socket: &mut TcpSocket, channels: &mut Channels, session: &Session, store: &mut FlashStore, ipv4_config: &mut Ipv4Config, fan_ctrl: &mut FanCtrl, hwrev: HWRev) -> Result<Self, Error> {
match command {
Command::Quit => Ok(Handler::CloseSocket),
@ -441,6 +449,7 @@ impl Handler {
Command::FanCurve { k_a, k_b, k_c } => Handler::fan_curve(socket, fan_ctrl, k_a, k_b, k_c),
Command::FanCurveDefaults => Handler::fan_defaults(socket, fan_ctrl),
Command::ShowHWRev => Handler::show_hwrev(socket, hwrev),
Command::SwapTECPolarity => Handler::swap_tec_polarity(socket, channels),
}
}
}

View File

@ -191,6 +191,7 @@ pub enum Command {
},
FanCurveDefaults,
ShowHWRev,
SwapTECPolarity,
}
fn end(input: &[u8]) -> IResult<&[u8], ()> {
@ -600,6 +601,7 @@ fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
fan,
fan_curve,
value(Ok(Command::ShowHWRev), tag("hwrev")),
value(Ok(Command::SwapTECPolarity), tag("swap")),
))(input)
}

View File

@ -22,6 +22,7 @@ pub struct ChannelConfig {
pwm: PwmLimits,
/// uses variant `PostFilter::Invalid` instead of `None` to save space
adc_postfilter: PostFilter,
swap_tec_polarity: bool,
}
impl ChannelConfig {
@ -41,6 +42,7 @@ impl ChannelConfig {
sh: state.sh.clone(),
pwm,
adc_postfilter,
swap_tec_polarity: false,
}
}
@ -51,6 +53,7 @@ impl ChannelConfig {
state.pid.target = self.pid_target.into();
state.pid_engaged = self.pid_engaged;
state.sh = self.sh.clone();
state.swap_tec_polarity = self.swap_tec_polarity;
self.pwm.apply(channels, channel);