add units::Volts, use for stm32f4 adc

This commit is contained in:
Astro 2020-05-17 00:13:52 +02:00
parent 7f8dd62a36
commit fb5c7a84e9
3 changed files with 48 additions and 21 deletions

View File

@ -5,6 +5,7 @@ use crate::{
channel::{Channel, Channel0, Channel1}, channel::{Channel, Channel0, Channel1},
channel_state::ChannelState, channel_state::ChannelState,
pins, pins,
units::Volts,
}; };
pub const CHANNELS: usize = 2; 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 { match channel {
0 => self.channel0.adc.convert( 0 => {
let sample = self.channel0.adc.convert(
&self.channel0.dac_loopback_pin, &self.channel0.dac_loopback_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480 stm32f4xx_hal::adc::config::SampleTime::Cycles_480
), );
1 => self.channel1.adc.convert( 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, &self.channel1.dac_loopback_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480 stm32f4xx_hal::adc::config::SampleTime::Cycles_480
), );
let mv = self.channel1.adc.sample_to_millivolts(sample);
Volts(mv as f64 / 1000.0)
}
_ => unreachable!(), _ => unreachable!(),
} }
} }
pub fn read_itec(&mut self, channel: usize) -> u16 { pub fn read_itec(&mut self, channel: usize) -> Volts {
match channel { match channel {
0 => self.channel0.adc.convert( 0 => {
let sample = self.channel0.adc.convert(
&self.channel0.itec_pin, &self.channel0.itec_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480 stm32f4xx_hal::adc::config::SampleTime::Cycles_480
), );
1 => self.channel1.adc.convert( 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, &self.channel1.itec_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480 stm32f4xx_hal::adc::config::SampleTime::Cycles_480
), );
let mv = self.channel1.adc.sample_to_millivolts(sample);
Volts(mv as f64 / 1000.0)
}
_ => unreachable!(), _ => unreachable!(),
} }
} }

View File

@ -42,6 +42,7 @@ use session::{Session, SessionOutput};
mod command_parser; mod command_parser;
use command_parser::{Command, ShowCommand, PwmPin}; use command_parser::{Command, ShowCommand, PwmPin};
mod timer; mod timer;
mod units;
mod pid; mod pid;
mod steinhart_hart; mod steinhart_hart;
mod channels; mod channels;
@ -143,14 +144,13 @@ fn main() -> ! {
if let Some(adc_data) = channels.channel_state(channel).adc_data { if let Some(adc_data) = channels.channel_state(channel).adc_data {
let dac_loopback = channels.read_dac_loopback(channel); let dac_loopback = channels.read_dac_loopback(channel);
let itec = channels.read_itec(channel); let itec = channels.read_itec(channel);
let dcc_u = 5.0; let itec_u = itec.0;
let itec_u = dcc_u * (itec as f64) / (0xFFF as f64);
let tec_u = (itec_u - 1.5) / 8.0; let tec_u = (itec_u - 1.5) / 8.0;
let tec_r = 5.0; let tec_r = 5.0;
let tec_i = tec_u / tec_r; let tec_i = tec_u / tec_r;
let state = channels.channel_state(channel); let state = channels.channel_state(channel);
let _ = writeln!( 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, state.adc_time, channel, adc_data,
dac_loopback, itec, dac_loopback, itec,
tec_u, tec_i, tec_u, tec_i,

10
src/units.rs Normal file
View File

@ -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)
}
}