From a2caac0fe54ff680ee16327d7c1e5d271474ad4f Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 25 Sep 2020 00:01:08 +0200 Subject: [PATCH] channels: power_down TEC when thermistor is not connected to ADC --- src/channel.rs | 14 +++++++++++--- src/channel_state.rs | 15 +++++++-------- src/channels.rs | 23 ++++++++++++++++++++++- src/main.rs | 1 + 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/channel.rs b/src/channel.rs index c688f0e..035d5c1 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -28,12 +28,10 @@ pub struct Channel { } impl Channel { - pub fn new(mut pins: ChannelPinSet, adc_calibration: ad7172::ChannelCalibration) -> Self { + pub fn new(pins: ChannelPinSet, adc_calibration: ad7172::ChannelCalibration) -> Self { let state = ChannelState::new(adc_calibration); let mut dac = ad5680::Dac::new(pins.dac_spi, pins.dac_sync); let _ = dac.set(0); - // power up TEC - let _ = pins.shdn.set_high(); // sensible dummy preset. calibrate_i_set() must be used. let dac_factor = ad5680::MAX_VALUE as f64 / 5.0; @@ -47,4 +45,14 @@ impl Channel { tec_u_meas_pin: pins.tec_u_meas_pin, } } + + // power up TEC + pub fn power_up(&mut self) { + let _ = self.shdn.set_high(); + } + + // power down TEC + pub fn power_down(&mut self) { + let _ = self.shdn.set_low(); + } } diff --git a/src/channel_state.rs b/src/channel_state.rs index aab68d4..c4d6cbc 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -50,7 +50,12 @@ impl ChannelState { } pub fn update(&mut self, now: Instant, adc_data: u32) { - self.adc_data = Some(adc_data); + self.adc_data = if adc_data == ad7172::MAX_VALUE { + // this means there is no thermistor plugged into the ADC. + None + } else { + Some(adc_data) + }; self.adc_time = now; } @@ -63,13 +68,7 @@ impl ChannelState { } pub fn get_adc(&self) -> Option { - let data = self.adc_data?; - if data == ad7172::MAX_VALUE { - // this means there is no thermistor plugged into the ADC. - None - } else { - Some(self.adc_calibration.convert_data(data)) - } + Some(self.adc_calibration.convert_data(self.adc_data?)) } /// Get `SENS[01]` input resistance diff --git a/src/channels.rs b/src/channels.rs index c02364d..14cd06d 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -70,13 +70,16 @@ impl Channels { let data = self.adc.read_data().unwrap(); let state = self.channel_state(channel); - state.update(instant, data); match state.update_pid() { Some(pid_output) if state.pid_engaged => { log::info!("PID: {:.3} A", pid_output); // Forward PID output to i_set DAC self.set_i(channel.into(), ElectricCurrent::new::(pid_output)); + self.power_up(channel); + } + None if state.pid_engaged => { + self.power_down(channel); } _ => {} } @@ -293,6 +296,24 @@ impl Channels { self.set_dac(channel, ElectricPotential::new::(0.0)); } + // power up TEC + pub fn power_up>(&mut self, channel: I) { + match channel.into() { + 0 => self.channel0.power_up(), + 1 => self.channel1.power_up(), + _ => unreachable!(), + } + } + + // power down TEC + pub fn power_down>(&mut self, channel: I) { + match channel.into() { + 0 => self.channel0.power_down(), + 1 => self.channel1.power_down(), + _ => unreachable!(), + } + } + fn get_pwm(&self, channel: usize, pin: PwmPin) -> f64 { fn get>(pin: &P) -> f64 { let duty = pin.get_duty(); diff --git a/src/main.rs b/src/main.rs index f5d1d23..946fd2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -328,6 +328,7 @@ fn main() -> ! { leds.g3.off(); let current = ElectricCurrent::new::(value); let (current, max) = channels.set_i(channel, current); + channels.power_up(channel); let _ = writeln!( socket, "channel {}: i_set DAC output set to {:.3} / {:.3}", channel,