2020-05-13 06:04:55 +08:00
|
|
|
use smoltcp::time::Instant;
|
2020-05-19 06:15:39 +08:00
|
|
|
use log::info;
|
2020-05-13 05:16:57 +08:00
|
|
|
use crate::{
|
2020-05-19 06:15:39 +08:00
|
|
|
ad5680,
|
2020-05-13 05:16:57 +08:00
|
|
|
ad7172,
|
|
|
|
channel::{Channel, Channel0, Channel1},
|
2020-05-13 06:04:55 +08:00
|
|
|
channel_state::ChannelState,
|
2020-05-13 05:16:57 +08:00
|
|
|
pins,
|
2020-05-17 06:13:52 +08:00
|
|
|
units::Volts,
|
2020-05-13 05:16:57 +08:00
|
|
|
};
|
|
|
|
|
2020-05-13 06:04:55 +08:00
|
|
|
pub const CHANNELS: usize = 2;
|
|
|
|
|
2020-05-19 03:38:13 +08:00
|
|
|
// TODO: -pub
|
2020-05-13 05:16:57 +08:00
|
|
|
pub struct Channels {
|
2020-05-21 03:27:22 +08:00
|
|
|
channel0: Channel<Channel0>,
|
|
|
|
channel1: Channel<Channel1>,
|
2020-05-13 05:16:57 +08:00
|
|
|
pub adc: ad7172::Adc<pins::AdcSpi, pins::AdcNss>,
|
2020-05-21 03:27:22 +08:00
|
|
|
tec_u_meas_adc: pins::TecUMeasAdc,
|
2020-05-13 05:16:57 +08:00
|
|
|
pub pwm: pins::PwmPins,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Channels {
|
|
|
|
pub fn new(pins: pins::Pins) -> Self {
|
|
|
|
let channel0 = Channel::new(pins.channel0);
|
|
|
|
let channel1 = Channel::new(pins.channel1);
|
2020-05-19 03:38:13 +08:00
|
|
|
let tec_u_meas_adc = pins.tec_u_meas_adc;
|
2020-05-13 05:16:57 +08:00
|
|
|
let pwm = pins.pwm;
|
|
|
|
|
|
|
|
let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap();
|
|
|
|
// Feature not used
|
|
|
|
adc.set_sync_enable(false).unwrap();
|
2020-05-17 08:11:53 +08:00
|
|
|
|
|
|
|
// Calibrate ADC channels individually
|
|
|
|
adc.disable_all_channels().unwrap();
|
|
|
|
|
|
|
|
adc.setup_channel(0, ad7172::Input::Ain0, ad7172::Input::Ain1).unwrap();
|
|
|
|
adc.calibrate().unwrap();
|
|
|
|
adc.disable_channel(0).unwrap();
|
|
|
|
|
|
|
|
adc.setup_channel(1, ad7172::Input::Ain2, ad7172::Input::Ain3).unwrap();
|
|
|
|
adc.calibrate().unwrap();
|
|
|
|
adc.disable_channel(1).unwrap();
|
|
|
|
|
|
|
|
// Setup channels and start ADC
|
2020-05-13 05:16:57 +08:00
|
|
|
adc.setup_channel(0, ad7172::Input::Ain0, ad7172::Input::Ain1).unwrap();
|
|
|
|
adc.setup_channel(1, ad7172::Input::Ain2, ad7172::Input::Ain3).unwrap();
|
2020-05-17 08:11:53 +08:00
|
|
|
adc.start_continuous_conversion().unwrap();
|
2020-05-13 05:16:57 +08:00
|
|
|
|
2020-05-19 03:38:13 +08:00
|
|
|
Channels { channel0, channel1, adc, tec_u_meas_adc, pwm }
|
2020-05-13 05:16:57 +08:00
|
|
|
}
|
2020-05-13 06:04:55 +08:00
|
|
|
|
|
|
|
pub fn channel_state<I: Into<usize>>(&mut self, channel: I) -> &mut ChannelState {
|
|
|
|
match channel.into() {
|
|
|
|
0 => &mut self.channel0.state,
|
|
|
|
1 => &mut self.channel1.state,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ADC input + PID processing
|
|
|
|
pub fn poll_adc(&mut self, instant: Instant) -> Option<u8> {
|
|
|
|
self.adc.data_ready().unwrap().map(|channel| {
|
|
|
|
let data = self.adc.read_data().unwrap();
|
|
|
|
|
|
|
|
let dac_value = {
|
|
|
|
let state = self.channel_state(channel);
|
2020-05-14 04:11:25 +08:00
|
|
|
let pid_output = state.update_pid(instant, data);
|
2020-05-13 06:04:55 +08:00
|
|
|
|
|
|
|
if state.pid_engaged {
|
2020-05-14 04:11:25 +08:00
|
|
|
Some(pid_output)
|
2020-05-13 06:04:55 +08:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(dac_value) = dac_value {
|
|
|
|
// Forward PID output to i_set DAC
|
2020-05-17 07:23:35 +08:00
|
|
|
self.set_dac(channel.into(), Volts(dac_value));
|
2020-05-13 06:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
channel
|
|
|
|
})
|
|
|
|
}
|
2020-05-13 06:15:29 +08:00
|
|
|
|
2020-05-14 03:02:26 +08:00
|
|
|
/// i_set DAC
|
2020-05-17 07:23:35 +08:00
|
|
|
pub fn set_dac(&mut self, channel: usize, voltage: Volts) {
|
2020-05-19 06:15:39 +08:00
|
|
|
let dac_factor = match channel.into() {
|
|
|
|
0 => self.channel0.dac_factor,
|
|
|
|
1 => self.channel1.dac_factor,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let value = (voltage.0 * dac_factor) as u32;
|
2020-05-14 03:02:26 +08:00
|
|
|
match channel {
|
|
|
|
0 => {
|
2020-05-19 06:15:39 +08:00
|
|
|
self.channel0.dac.set(value).unwrap();
|
2020-05-17 07:23:35 +08:00
|
|
|
self.channel0.state.dac_value = voltage;
|
2020-05-14 03:02:26 +08:00
|
|
|
}
|
|
|
|
1 => {
|
2020-05-19 06:15:39 +08:00
|
|
|
self.channel1.dac.set(value).unwrap();
|
2020-05-17 07:23:35 +08:00
|
|
|
self.channel1.state.dac_value = voltage;
|
2020-05-14 03:02:26 +08:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 08:18:25 +08:00
|
|
|
pub fn read_dac_feedback(&mut self, channel: usize) -> Volts {
|
2020-05-13 06:15:29 +08:00
|
|
|
match channel {
|
2020-05-17 06:13:52 +08:00
|
|
|
0 => {
|
|
|
|
let sample = self.channel0.adc.convert(
|
2020-05-17 08:18:25 +08:00
|
|
|
&self.channel0.dac_feedback_pin,
|
2020-05-17 06:13:52 +08:00
|
|
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
|
|
|
);
|
|
|
|
let mv = self.channel0.adc.sample_to_millivolts(sample);
|
|
|
|
Volts(mv as f64 / 1000.0)
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
let sample = self.channel1.adc.convert(
|
2020-05-17 08:18:25 +08:00
|
|
|
&self.channel1.dac_feedback_pin,
|
2020-05-17 06:13:52 +08:00
|
|
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
|
|
|
);
|
|
|
|
let mv = self.channel1.adc.sample_to_millivolts(sample);
|
|
|
|
Volts(mv as f64 / 1000.0)
|
|
|
|
}
|
2020-05-13 06:15:29 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 05:59:31 +08:00
|
|
|
|
2020-05-21 05:07:31 +08:00
|
|
|
pub fn read_dac_feedback_until_stable(&mut self, channel: usize, tolerance: f64) -> Volts {
|
|
|
|
let mut prev = self.read_dac_feedback(channel);
|
|
|
|
loop {
|
|
|
|
let current = self.read_dac_feedback(channel);
|
|
|
|
use num_traits::float::Float;
|
|
|
|
if (current - prev).0.abs() < tolerance {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
prev = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 06:13:52 +08:00
|
|
|
pub fn read_itec(&mut self, channel: usize) -> Volts {
|
2020-05-17 05:59:31 +08:00
|
|
|
match channel {
|
2020-05-17 06:13:52 +08:00
|
|
|
0 => {
|
|
|
|
let sample = self.channel0.adc.convert(
|
|
|
|
&self.channel0.itec_pin,
|
|
|
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
|
|
|
);
|
|
|
|
let mv = self.channel0.adc.sample_to_millivolts(sample);
|
|
|
|
Volts(mv as f64 / 1000.0)
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
let sample = self.channel1.adc.convert(
|
|
|
|
&self.channel1.itec_pin,
|
|
|
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
|
|
|
);
|
2020-05-19 04:43:44 +08:00
|
|
|
let mv = self.channel1.adc.sample_to_millivolts(sample);
|
|
|
|
Volts(mv as f64 / 1000.0)
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 06:15:39 +08:00
|
|
|
/// should be 1.5V
|
2020-05-19 04:43:44 +08:00
|
|
|
pub fn read_vref(&mut self, channel: usize) -> Volts {
|
|
|
|
match channel {
|
|
|
|
0 => {
|
|
|
|
let sample = self.channel0.adc.convert(
|
|
|
|
&self.channel0.vref_pin,
|
|
|
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
|
|
|
);
|
|
|
|
let mv = self.channel0.adc.sample_to_millivolts(sample);
|
|
|
|
Volts(mv as f64 / 1000.0)
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
let sample = self.channel1.adc.convert(
|
|
|
|
&self.channel1.vref_pin,
|
|
|
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
|
|
|
);
|
2020-05-17 06:13:52 +08:00
|
|
|
let mv = self.channel1.adc.sample_to_millivolts(sample);
|
|
|
|
Volts(mv as f64 / 1000.0)
|
|
|
|
}
|
2020-05-17 05:59:31 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 03:38:13 +08:00
|
|
|
|
|
|
|
pub fn read_tec_u_meas(&mut self, channel: usize) -> Volts {
|
|
|
|
match channel {
|
|
|
|
0 => {
|
|
|
|
let sample = self.tec_u_meas_adc.convert(
|
|
|
|
&self.channel0.tec_u_meas_pin,
|
|
|
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
|
|
|
);
|
|
|
|
let mv = self.tec_u_meas_adc.sample_to_millivolts(sample);
|
|
|
|
Volts(mv as f64 / 1000.0)
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
let sample = self.tec_u_meas_adc.convert(
|
|
|
|
&self.channel1.tec_u_meas_pin,
|
|
|
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
|
|
|
);
|
|
|
|
let mv = self.tec_u_meas_adc.sample_to_millivolts(sample);
|
|
|
|
Volts(mv as f64 / 1000.0)
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 06:15:39 +08:00
|
|
|
|
|
|
|
/// for i_set
|
|
|
|
pub fn calibrate_dac_value(&mut self, channel: usize) {
|
|
|
|
let vref = self.read_vref(channel);
|
2020-05-21 05:07:31 +08:00
|
|
|
let value = self.calibrate_dac_value_for_voltage(channel, vref);
|
|
|
|
info!("best dac value for {}: {}", vref, value);
|
|
|
|
|
|
|
|
let dac_factor = value as f64 / vref.0;
|
|
|
|
match channel {
|
|
|
|
0 => self.channel0.dac_factor = dac_factor,
|
|
|
|
1 => self.channel1.dac_factor = dac_factor,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn calibrate_dac_value_for_voltage(&mut self, channel: usize, voltage: Volts) -> u32 {
|
2020-05-19 06:15:39 +08:00
|
|
|
let mut best_value = 0;
|
|
|
|
let mut best_error = Volts(100.0);
|
2020-05-21 05:07:31 +08:00
|
|
|
|
|
|
|
for step in (1..=12).rev() {
|
|
|
|
for value in (best_value..=ad5680::MAX_VALUE).step_by(2usize.pow(step)) {
|
|
|
|
match channel {
|
|
|
|
0 => {
|
|
|
|
self.channel0.dac.set(value).unwrap();
|
|
|
|
// self.channel0.shdn.set_high().unwrap();
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
self.channel1.dac.set(value).unwrap();
|
|
|
|
// self.channel1.shdn.set_high().unwrap();
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2020-05-19 06:15:39 +08:00
|
|
|
}
|
|
|
|
|
2020-05-21 05:07:31 +08:00
|
|
|
let dac_feedback = self.read_dac_feedback_until_stable(channel, 0.001);
|
|
|
|
let error = voltage - dac_feedback;
|
|
|
|
if error < Volts(0.0) {
|
|
|
|
break;
|
|
|
|
} else if error < best_error {
|
|
|
|
best_value = value;
|
|
|
|
best_error = error;
|
|
|
|
}
|
2020-05-19 06:15:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.set_dac(channel, Volts(0.0));
|
2020-05-21 05:07:31 +08:00
|
|
|
best_value
|
2020-05-19 06:15:39 +08:00
|
|
|
}
|
2020-05-13 05:16:57 +08:00
|
|
|
}
|