From 419586583f5f195dbd7c44292cb2ca07d67b7f30 Mon Sep 17 00:00:00 2001 From: atse Date: Tue, 20 Aug 2024 17:42:44 +0800 Subject: [PATCH] Add command for flipping output polarity Needed for IDC cable connections, since the IDC connector and front panel connectors have flipped polarities. --- src/channel_state.rs | 2 ++ src/channels.rs | 14 +++++++++++--- src/command_handler.rs | 10 ++++++++++ src/command_parser.rs | 24 ++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/channel_state.rs b/src/channel_state.rs index 4820131..fdcd26f 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -35,6 +35,7 @@ pub struct ChannelState { pub pid_engaged: bool, pub pid: pid::Controller, pub sh: sh::Parameters, + pub polarity_swapped: bool, } impl ChannelState { @@ -51,6 +52,7 @@ impl ChannelState { pid_engaged: false, pid: pid::Controller::new(pid::Parameters::default()), sh: sh::Parameters::default(), + polarity_swapped: false, } } diff --git a/src/channels.rs b/src/channels.rs index a428dd1..83dd357 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -152,7 +152,11 @@ impl Channels { } pub fn set_i(&mut self, channel: usize, i_set: ElectricCurrent) -> ElectricCurrent { - let i_set = i_set.min(MAX_TEC_I).max(-MAX_TEC_I); + let mut i_set = i_set.min(MAX_TEC_I).max(-MAX_TEC_I); + self.channel_state(channel).i_set = i_set; + if self.channel_state(channel).polarity_swapped { + i_set = -i_set; + } let vref_meas = match channel.into() { 0 => self.channel0.vref_meas, 1 => self.channel1.vref_meas, @@ -163,7 +167,6 @@ impl Channels { let voltage = i_set * 10.0 * r_sense + center_point; let voltage = self.set_dac(channel, voltage); let i_set = (voltage - center_point) / (10.0 * r_sense); - self.channel_state(channel).i_set = i_set; i_set } @@ -407,7 +410,12 @@ impl Channels { // Get current passing through TEC pub fn get_tec_i(&mut self, channel: usize) -> ElectricCurrent { - (self.adc_read(channel, PinsAdcReadTarget::ITec, 16) - self.adc_read(channel, PinsAdcReadTarget::VREF, 16)) / ElectricalResistance::new::(0.4) + let tec_i = (self.adc_read(channel, PinsAdcReadTarget::ITec, 16) - self.adc_read(channel, PinsAdcReadTarget::VREF, 16)) / ElectricalResistance::new::(0.4); + if self.channel_state(channel).polarity_swapped { + -tec_i + } else { + tec_i + } } // Get voltage across TEC diff --git a/src/command_handler.rs b/src/command_handler.rs index 3f30083..01a6fec 100644 --- a/src/command_handler.rs +++ b/src/command_handler.rs @@ -181,6 +181,15 @@ impl Handler { Ok(Handler::Handled) } + fn swap_polarity (socket: &mut TcpSocket, channels: &mut Channels, channel: usize, swapped: bool) -> Result { + channels.channel_state(channel).polarity_swapped = swapped; + let channel_state = channels.channel_state(channel); + let i_set = channel_state.i_set; + channels.set_i(channel, i_set); + send_line(socket, b"{}"); + Ok(Handler::Handled) + } + fn set_pwm (socket: &mut TcpSocket, channels: &mut Channels, channel: usize, pin: PwmPin, value: f64) -> Result { match pin { PwmPin::ISet => { @@ -424,6 +433,7 @@ impl Handler { Command::Show(ShowCommand::PostFilter) => Handler::show_post_filter(socket, channels), Command::Show(ShowCommand::Ipv4) => Handler::show_ipv4(socket, ipv4_config), Command::PwmPid { channel } => Handler::engage_pid(socket, channels, channel), + Command::PwmPolaritySwapped { channel, swapped } => Handler::swap_polarity(socket, channels, channel, swapped), Command::Pwm { channel, pin, value } => Handler::set_pwm(socket, channels, channel, pin, value), Command::CenterPoint { channel, center } => Handler::set_center_point(socket, channels, channel, center), Command::Pid { channel, parameter, value } => Handler::set_pid(socket, channels, channel, parameter, value), diff --git a/src/command_parser.rs b/src/command_parser.rs index b9e70bb..0e802f8 100644 --- a/src/command_parser.rs +++ b/src/command_parser.rs @@ -159,6 +159,10 @@ pub enum Command { PwmPid { channel: usize, }, + PwmPolaritySwapped { + channel: usize, + swapped: bool, + }, CenterPoint { channel: usize, center: CenterPoint, @@ -239,6 +243,12 @@ fn off_on(input: &[u8]) -> IResult<&[u8], bool> { ))(input) } +fn boolean(input: &[u8]) -> IResult<&[u8], bool> { + alt((value(false, tag("false")), + value(true, tag("true")) + ))(input) +} + fn channel(input: &[u8]) -> IResult<&[u8], usize> { map(one_of("01"), |c| (c as usize) - ('0' as usize))(input) } @@ -321,6 +331,16 @@ fn pwm_pid(input: &[u8]) -> IResult<&[u8], ()> { value((), tag("pid"))(input) } +fn pwm_polarity_swapped(input: &[u8]) -> IResult<&[u8], bool> { + preceded( + tag("polarity_swapped"), + preceded( + whitespace, + boolean, + ) + )(input) +} + fn pwm(input: &[u8]) -> IResult<&[u8], Result> { let (input, _) = tag("pwm")(input)?; alt(( @@ -333,6 +353,10 @@ fn pwm(input: &[u8]) -> IResult<&[u8], Result> { let (input, ()) = pwm_pid(input)?; Ok((input, Ok(Command::PwmPid { channel }))) }, + |input| { + let (input, swapped) = pwm_polarity_swapped(input)?; + Ok((input, Ok(Command::PwmPolaritySwapped { channel, swapped }))) + }, |input| { let (input, config) = pwm_setup(input)?; match config {