forked from M-Labs/thermostat
channels: power_down TEC when thermistor is not connected to ADC
This commit is contained in:
parent
20059aff5c
commit
a2caac0fe5
@ -28,12 +28,10 @@ pub struct Channel<C: ChannelPins> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<C: ChannelPins> Channel<C> {
|
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 state = ChannelState::new(adc_calibration);
|
||||||
let mut dac = ad5680::Dac::new(pins.dac_spi, pins.dac_sync);
|
let mut dac = ad5680::Dac::new(pins.dac_spi, pins.dac_sync);
|
||||||
let _ = dac.set(0);
|
let _ = dac.set(0);
|
||||||
// power up TEC
|
|
||||||
let _ = pins.shdn.set_high();
|
|
||||||
// sensible dummy preset. calibrate_i_set() must be used.
|
// sensible dummy preset. calibrate_i_set() must be used.
|
||||||
let dac_factor = ad5680::MAX_VALUE as f64 / 5.0;
|
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,
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,12 @@ impl ChannelState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, now: Instant, adc_data: u32) {
|
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;
|
self.adc_time = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,13 +68,7 @@ impl ChannelState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_adc(&self) -> Option<ElectricPotential> {
|
pub fn get_adc(&self) -> Option<ElectricPotential> {
|
||||||
let data = self.adc_data?;
|
Some(self.adc_calibration.convert_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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get `SENS[01]` input resistance
|
/// Get `SENS[01]` input resistance
|
||||||
|
@ -70,13 +70,16 @@ impl Channels {
|
|||||||
let data = self.adc.read_data().unwrap();
|
let data = self.adc.read_data().unwrap();
|
||||||
|
|
||||||
let state = self.channel_state(channel);
|
let state = self.channel_state(channel);
|
||||||
|
|
||||||
state.update(instant, data);
|
state.update(instant, data);
|
||||||
match state.update_pid() {
|
match state.update_pid() {
|
||||||
Some(pid_output) if state.pid_engaged => {
|
Some(pid_output) if state.pid_engaged => {
|
||||||
log::info!("PID: {:.3} A", pid_output);
|
log::info!("PID: {:.3} A", pid_output);
|
||||||
// Forward PID output to i_set DAC
|
// Forward PID output to i_set DAC
|
||||||
self.set_i(channel.into(), ElectricCurrent::new::<ampere>(pid_output));
|
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));
|
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_pwm(&self, channel: usize, pin: PwmPin) -> f64 {
|
||||||
fn get<P: hal::PwmPin<Duty=u16>>(pin: &P) -> f64 {
|
fn get<P: hal::PwmPin<Duty=u16>>(pin: &P) -> f64 {
|
||||||
let duty = pin.get_duty();
|
let duty = pin.get_duty();
|
||||||
|
@ -328,6 +328,7 @@ fn main() -> ! {
|
|||||||
leds.g3.off();
|
leds.g3.off();
|
||||||
let current = ElectricCurrent::new::<ampere>(value);
|
let current = ElectricCurrent::new::<ampere>(value);
|
||||||
let (current, max) = channels.set_i(channel, current);
|
let (current, max) = channels.set_i(channel, current);
|
||||||
|
channels.power_up(channel);
|
||||||
let _ = writeln!(
|
let _ = writeln!(
|
||||||
socket, "channel {}: i_set DAC output set to {:.3} / {:.3}",
|
socket, "channel {}: i_set DAC output set to {:.3} / {:.3}",
|
||||||
channel,
|
channel,
|
||||||
|
Loading…
Reference in New Issue
Block a user