main: use steinhart-hart equation for temperature conversion

This commit is contained in:
Astro 2019-10-02 21:28:14 +02:00
parent 83117db8c5
commit 53ff7e70ca
2 changed files with 22 additions and 12 deletions

View File

@ -100,7 +100,7 @@ const DEFAULT_SH_PARAMETERS: sh::Parameters = sh::Parameters {
a: 0.01, a: 0.01,
b: 0.01, b: 0.01,
c: 0.01, c: 0.01,
parallel_r: 100.0, // TODO parallel_r: 55_000.0, // Ohm (TODO: verify)
}; };
const PWM_PID_WIDTH: u16 = 0xffff; const PWM_PID_WIDTH: u16 = 0xffff;
@ -113,8 +113,8 @@ pub const CHANNELS: usize = 2;
/// State per TEC channel /// State per TEC channel
#[derive(Clone)] #[derive(Clone)]
struct ControlState { struct ControlState {
/// Report data (time, data) /// Report data (time, data, temperature)
report: Option<(u64, u32)>, report: Option<(u64, i32, f32)>,
pid_enabled: bool, pid_enabled: bool,
pid: pid::Controller, pid: pid::Controller,
sh: sh::Parameters, sh: sh::Parameters,
@ -258,6 +258,8 @@ fn main() -> ! {
let now = get_time(); let now = get_time();
let data = adc.read_data().unwrap(); let data = adc.read_data().unwrap();
let state = &mut states[usize::from(channel)]; let state = &mut states[usize::from(channel)];
let voltage = VCC * (data as f32) / (0x7FFFFF as f32);
let temperature = state.sh.get_temperature(voltage);
if state.pid_enabled { if state.pid_enabled {
let width = state.pid.update(data as f32) as u16; let width = state.pid.update(data as f32) as u16;
@ -268,7 +270,7 @@ fn main() -> ! {
} }
} }
state.report = Some((now, data)); state.report = Some((now, data, temperature));
for (session, _) in sessions_handles.iter_mut() { for (session, _) in sessions_handles.iter_mut() {
session.set_report_pending(channel.into()); session.set_report_pending(channel.into());
} }
@ -301,8 +303,11 @@ fn main() -> ! {
} }
Command::Show(ShowCommand::Input) => { Command::Show(ShowCommand::Input) => {
for (channel, state) in states.iter().enumerate() { for (channel, state) in states.iter().enumerate() {
state.report.map(|(time, data)| { state.report.map(|(time, data, temp)| {
let _ = writeln!(socket, "t={}, sens{}={}", time, channel, data); let _ = writeln!(
socket, "t={} temp{}={} raw{}=0x{:06X}",
time, channel, temp, channel, data
);
}); });
} }
} }
@ -470,8 +475,11 @@ fn main() -> ! {
} }
if socket.may_send() { if socket.may_send() {
if let Some(channel) = session.is_report_pending() { if let Some(channel) = session.is_report_pending() {
states[channel].report.map(|(time, data)| { states[channel].report.map(|(time, data, temp)| {
let _ = writeln!(socket, "t={} sens{}={:06X}", time, channel, data); let _ = writeln!(
socket, "t={} temp{}={} raw{}=0x{:06X}",
time, channel, temp, channel, data
);
}); });
session.mark_report_sent(channel); session.mark_report_sent(channel);
} }

View File

@ -14,12 +14,14 @@ pub struct Parameters {
} }
impl Parameters { impl Parameters {
/// input: Voltage /// Perform the voltage to temperature conversion.
/// ///
/// Result unit: Kelvin /// Result unit: Kelvin
pub fn get_temperature(&self, input: f32) -> f32 { ///
let r = self.parallel_r * input; /// TODO: verify
let ln_r = r.ln(); pub fn get_temperature(&self, voltage: f32) -> f32 {
let r = self.parallel_r * voltage;
let ln_r = r.abs().ln();
let inv_temp = self.a + let inv_temp = self.a +
self.b * ln_r + self.b * ln_r +
self.c * ln_r * ln_r * ln_r; self.c * ln_r * ln_r * ln_r;