channels: power_down TEC when thermistor is not connected to ADC

pull/20/head
Astro 2020-09-25 00:01:08 +02:00
parent 20059aff5c
commit a2caac0fe5
4 changed files with 41 additions and 12 deletions

View File

@ -28,12 +28,10 @@ pub struct Channel<C: ChannelPins> {
}
impl<C: ChannelPins> Channel<C> {
pub fn new(mut pins: ChannelPinSet<C>, adc_calibration: ad7172::ChannelCalibration) -> Self {
pub fn new(pins: ChannelPinSet<C>, 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<C: ChannelPins> Channel<C> {
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();
}
}

View File

@ -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<ElectricPotential> {
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

View File

@ -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::<ampere>(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::<volt>(0.0));
}
// power up TEC
pub fn power_up<I: Into<usize>>(&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<I: Into<usize>>(&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<P: hal::PwmPin<Duty=u16>>(pin: &P) -> f64 {
let duty = pin.get_duty();

View File

@ -328,6 +328,7 @@ fn main() -> ! {
leds.g3.off();
let current = ElectricCurrent::new::<ampere>(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,