From fb5c7a84e97d2b0175934c96eefa6053dfeca0bb Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 17 May 2020 00:13:52 +0200 Subject: [PATCH] add units::Volts, use for stm32f4 adc --- src/channels.rs | 53 ++++++++++++++++++++++++++++++++----------------- src/main.rs | 6 +++--- src/units.rs | 10 ++++++++++ 3 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 src/units.rs diff --git a/src/channels.rs b/src/channels.rs index 18a93a5..a21f63e 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -5,6 +5,7 @@ use crate::{ channel::{Channel, Channel0, Channel1}, channel_state::ChannelState, pins, + units::Volts, }; pub const CHANNELS: usize = 2; @@ -82,30 +83,46 @@ impl Channels { } } - pub fn read_dac_loopback(&mut self, channel: usize) -> u16 { + pub fn read_dac_loopback(&mut self, channel: usize) -> Volts { match channel { - 0 => self.channel0.adc.convert( - &self.channel0.dac_loopback_pin, - stm32f4xx_hal::adc::config::SampleTime::Cycles_480 - ), - 1 => self.channel1.adc.convert( - &self.channel1.dac_loopback_pin, - stm32f4xx_hal::adc::config::SampleTime::Cycles_480 - ), + 0 => { + let sample = self.channel0.adc.convert( + &self.channel0.dac_loopback_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.dac_loopback_pin, + stm32f4xx_hal::adc::config::SampleTime::Cycles_480 + ); + let mv = self.channel1.adc.sample_to_millivolts(sample); + Volts(mv as f64 / 1000.0) + } _ => unreachable!(), } } - pub fn read_itec(&mut self, channel: usize) -> u16 { + pub fn read_itec(&mut self, channel: usize) -> Volts { match channel { - 0 => self.channel0.adc.convert( - &self.channel0.itec_pin, - stm32f4xx_hal::adc::config::SampleTime::Cycles_480 - ), - 1 => self.channel1.adc.convert( - &self.channel1.itec_pin, - stm32f4xx_hal::adc::config::SampleTime::Cycles_480 - ), + 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 + ); + let mv = self.channel1.adc.sample_to_millivolts(sample); + Volts(mv as f64 / 1000.0) + } _ => unreachable!(), } } diff --git a/src/main.rs b/src/main.rs index ebdbc71..9ead14b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ use session::{Session, SessionOutput}; mod command_parser; use command_parser::{Command, ShowCommand, PwmPin}; mod timer; +mod units; mod pid; mod steinhart_hart; mod channels; @@ -143,14 +144,13 @@ fn main() -> ! { if let Some(adc_data) = channels.channel_state(channel).adc_data { let dac_loopback = channels.read_dac_loopback(channel); let itec = channels.read_itec(channel); - let dcc_u = 5.0; - let itec_u = dcc_u * (itec as f64) / (0xFFF as f64); + let itec_u = itec.0; let tec_u = (itec_u - 1.5) / 8.0; let tec_r = 5.0; let tec_i = tec_u / tec_r; let state = channels.channel_state(channel); let _ = writeln!( - socket, "t={} raw{}=0x{:06X} dac_loopback=0x{:X} itec=0x{:X} tec={:.2}V/{:.2}A", + socket, "t={} raw{}=0x{:06X} dac_loopback={} itec={} tec={:.3}V/{:.3}A", state.adc_time, channel, adc_data, dac_loopback, itec, tec_u, tec_i, diff --git a/src/units.rs b/src/units.rs new file mode 100644 index 0000000..3a894cb --- /dev/null +++ b/src/units.rs @@ -0,0 +1,10 @@ +use core::fmt; + +#[derive(Debug, Clone, PartialEq, PartialOrd)] +pub struct Volts(pub f64); + +impl fmt::Display for Volts { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:.3}V", self.0) + } +}