Compare commits

..

3 Commits

Author SHA1 Message Date
a506876995 Move PwmLimits into its own file 2024-10-21 16:04:07 +08:00
9848c65de5 Fix command parser failing test due to changes 2024-10-21 15:51:21 +08:00
069d791802 Rename all Steinhart-Hart references to B-param
The Steinhart-Hart equation was changed in code long ago to the
B-parameter equation. Rename references to it and the interface
accordingly. The `s-h` command is now `b-p`.

The reason the name "B-Parameter" equation was chosen over
"Beta-Parameter" was due to its easier searchability.
2024-10-21 15:44:46 +08:00
11 changed files with 75 additions and 84 deletions

View File

@ -111,8 +111,8 @@ formatted as line-delimited JSON.
| `pid <0/1> kd <value>` | Set differential gain | | `pid <0/1> kd <value>` | Set differential gain |
| `pid <0/1> output_min <amp>` | Set mininum output | | `pid <0/1> output_min <amp>` | Set mininum output |
| `pid <0/1> output_max <amp>` | Set maximum output | | `pid <0/1> output_max <amp>` | Set maximum output |
| `s-h` | Show Steinhart-Hart equation parameters | | `b-p` | Show B-Parameter equation parameters |
| `s-h <0/1> <t0/b/r0> <value>` | Set Steinhart-Hart parameter for a channel | | `b-p <0/1> <t0/b/r0> <value>` | Set B-Parameter for a channel |
| `postfilter` | Show postfilter settings | | `postfilter` | Show postfilter settings |
| `postfilter <0/1> off` | Disable postfilter | | `postfilter <0/1> off` | Disable postfilter |
| `postfilter <0/1> rate <rate>` | Set postfilter output data rate | | `postfilter <0/1> rate <rate>` | Set postfilter output data rate |
@ -144,22 +144,22 @@ output will be truncated when USB buffers are full.
Connect the thermistor with the SENS pins of the Connect the thermistor with the SENS pins of the
device. Temperature-depending resistance is measured by the AD7172 device. Temperature-depending resistance is measured by the AD7172
ADC. To prepare conversion to a temperature, set the Beta parameters ADC. To prepare conversion to a temperature, set the parameters
for the Steinhart-Hart equation. for the B-Parameter equation.
Set the base temperature in degrees celsius for the channel 0 thermistor: Set the base temperature in degrees celsius for the channel 0 thermistor:
``` ```
s-h 0 t0 20 b-p 0 t0 20
``` ```
Set the resistance in Ohms measured at the base temperature t0: Set the resistance in Ohms measured at the base temperature t0:
``` ```
s-h 0 r0 10000 b-p 0 r0 10000
``` ```
Set the Beta parameter: Set the Beta parameter:
``` ```
s-h 0 b 3800 b-p 0 b 3800
``` ```
### 50/60 Hz filtering ### 50/60 Hz filtering
@ -260,7 +260,7 @@ with the following keys.
| `interval` | Seconds | Time elapsed since last report update on channel | | `interval` | Seconds | Time elapsed since last report update on channel |
| `adc` | Volts | AD7172 input | | `adc` | Volts | AD7172 input |
| `sens` | Ohms | Thermistor resistance derived from `adc` | | `sens` | Ohms | Thermistor resistance derived from `adc` |
| `temperature` | Degrees Celsius | Steinhart-Hart conversion result derived from `sens` | | `temperature` | Degrees Celsius | B-Parameter conversion result derived from `sens` |
| `pid_engaged` | Boolean | `true` if in closed-loop mode | | `pid_engaged` | Boolean | `true` if in closed-loop mode |
| `i_set` | Amperes | TEC output current | | `i_set` | Amperes | TEC output current |
| `dac_value` | Volts | AD5680 output derived from `i_set` | | `dac_value` | Volts | AD5680 output derived from `i_set` |

View File

@ -1,11 +1,11 @@
from pytec.client import Client from pytec.client import Client
tec = Client() #(host="localhost", port=6667) tec = Client() #(host="localhost", port=6667)
tec.set_param("s-h", 1, "t0", 20) tec.set_param("b-p", 1, "t0", 20)
print(tec.get_output()) print(tec.get_output())
print(tec.get_pid()) print(tec.get_pid())
print(tec.get_output()) print(tec.get_output())
print(tec.get_postfilter()) print(tec.get_postfilter())
print(tec.get_steinhart_hart()) print(tec.get_b_parameter())
for data in tec.report_mode(): for data in tec.report_mode():
print(data) print(data)

View File

@ -92,14 +92,14 @@ class Client:
""" """
return self._get_conf("pid") return self._get_conf("pid")
def get_steinhart_hart(self): def get_b_parameter(self):
"""Retrieve Steinhart-Hart parameters for resistance to temperature conversion """Retrieve B-Parameter equation parameters for resistance to temperature conversion
Example:: Example::
[{'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 0}, [{'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 0},
{'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 1}] {'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 1}]
""" """
return self._get_conf("s-h") return self._get_conf("b-p")
def get_postfilter(self): def get_postfilter(self):
"""Retrieve DAC postfilter configuration """Retrieve DAC postfilter configuration
@ -146,7 +146,7 @@ class Client:
Examples:: Examples::
tec.set_param("output", 0, "max_v", 2.0) tec.set_param("output", 0, "max_v", 2.0)
tec.set_param("pid", 1, "output_max", 2.5) tec.set_param("pid", 1, "output_max", 2.5)
tec.set_param("s-h", 0, "t0", 20.0) tec.set_param("b-p", 0, "t0", 20.0)
tec.set_param("center", 0, "vref") tec.set_param("center", 0, "vref")
tec.set_param("postfilter", 1, 21) tec.set_param("postfilter", 1, 21)

View File

@ -8,14 +8,14 @@ use uom::si::{
thermodynamic_temperature::{degree_celsius, kelvin}, thermodynamic_temperature::{degree_celsius, kelvin},
}; };
/// Steinhart-Hart equation parameters /// B-Parameter equation parameters
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Parameters { pub struct Parameters {
/// Base temperature /// Base temperature
pub t0: ThermodynamicTemperature, pub t0: ThermodynamicTemperature,
/// Resistance at base temperature /// Thermistor resistance at base temperature
pub r0: ElectricalResistance, pub r0: ElectricalResistance,
/// Beta /// Beta (average slope of the function ln R vs. 1/T)
pub b: TemperatureInterval, pub b: TemperatureInterval,
} }

View File

@ -1,9 +1,8 @@
use crate::{ use crate::{
ad7172, ad7172, b_parameter as bp,
command_parser::{CenterPoint, Polarity}, command_parser::{CenterPoint, Polarity},
pid, pid,
pwm_limits::PwmLimits, pwm_limits::PwmLimits,
steinhart_hart as sh,
}; };
use num_traits::Zero; use num_traits::Zero;
use smoltcp::time::{Duration, Instant}; use smoltcp::time::{Duration, Instant};
@ -33,7 +32,7 @@ pub struct ChannelState {
pub pwm_limits: PwmLimits, pub pwm_limits: PwmLimits,
pub pid_engaged: bool, pub pid_engaged: bool,
pub pid: pid::Controller, pub pid: pid::Controller,
pub sh: sh::Parameters, pub bp: bp::Parameters,
pub polarity: Polarity, pub polarity: Polarity,
} }
@ -55,7 +54,7 @@ impl ChannelState {
}, },
pid_engaged: false, pid_engaged: false,
pid: pid::Controller::new(pid::Parameters::default()), pid: pid::Controller::new(pid::Parameters::default()),
sh: sh::Parameters::default(), bp: bp::Parameters::default(),
polarity: Polarity::Normal, polarity: Polarity::Normal,
} }
} }
@ -101,7 +100,7 @@ impl ChannelState {
pub fn get_temperature(&self) -> Option<ThermodynamicTemperature> { pub fn get_temperature(&self) -> Option<ThermodynamicTemperature> {
let r = self.get_sens()?; let r = self.get_sens()?;
let temperature = self.sh.get_temperature(r); let temperature = self.bp.get_temperature(r);
Some(temperature) Some(temperature)
} }
} }

View File

@ -1,12 +1,11 @@
use crate::timer::sleep; use crate::timer::sleep;
use crate::{ use crate::{
ad5680, ad7172, ad5680, ad7172, b_parameter,
channel::{Channel, Channel0, Channel1}, channel::{Channel, Channel0, Channel1},
channel_state::ChannelState, channel_state::ChannelState,
command_handler::JsonBuffer, command_handler::JsonBuffer,
command_parser::{CenterPoint, Polarity, PwmPin}, command_parser::{CenterPoint, Polarity, PwmPin},
pins::{self, Channel0VRef, Channel1VRef}, pins::{self, Channel0VRef, Channel1VRef},
steinhart_hart,
}; };
use core::marker::PhantomData; use core::marker::PhantomData;
use heapless::{consts::U2, Vec}; use heapless::{consts::U2, Vec};
@ -558,17 +557,17 @@ impl Channels {
serde_json_core::to_vec(&summaries) serde_json_core::to_vec(&summaries)
} }
fn steinhart_hart_summary(&mut self, channel: usize) -> SteinhartHartSummary { fn b_parameter_summary(&mut self, channel: usize) -> BParameterSummary {
let params = self.channel_state(channel).sh.clone(); let params = self.channel_state(channel).bp.clone();
SteinhartHartSummary { channel, params } BParameterSummary { channel, params }
} }
pub fn steinhart_hart_summaries_json( pub fn b_parameter_summaries_json(
&mut self, &mut self,
) -> Result<JsonBuffer, serde_json_core::ser::Error> { ) -> Result<JsonBuffer, serde_json_core::ser::Error> {
let mut summaries = Vec::<_, U2>::new(); let mut summaries = Vec::<_, U2>::new();
for channel in 0..CHANNELS { for channel in 0..CHANNELS {
let _ = summaries.push(self.steinhart_hart_summary(channel)); let _ = summaries.push(self.b_parameter_summary(channel));
} }
serde_json_core::to_vec(&summaries) serde_json_core::to_vec(&summaries)
} }
@ -647,7 +646,7 @@ pub struct PostFilterSummary {
} }
#[derive(Serialize)] #[derive(Serialize)]
pub struct SteinhartHartSummary { pub struct BParameterSummary {
channel: usize, channel: usize,
params: steinhart_hart::Parameters, params: b_parameter::Parameters,
} }

View File

@ -2,7 +2,7 @@ use super::{
ad7172, ad7172,
channels::{Channels, CHANNELS}, channels::{Channels, CHANNELS},
command_parser::{ command_parser::{
CenterPoint, Command, Ipv4Config, PidParameter, Polarity, PwmPin, ShParameter, ShowCommand, BpParameter, CenterPoint, Command, Ipv4Config, PidParameter, Polarity, PwmPin, ShowCommand,
}, },
config::ChannelConfig, config::ChannelConfig,
dfu, dfu,
@ -113,16 +113,13 @@ impl Handler {
Ok(Handler::Handled) Ok(Handler::Handled)
} }
fn show_steinhart_hart( fn show_b_parameter(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
socket: &mut TcpSocket, match channels.b_parameter_summaries_json() {
channels: &mut Channels,
) -> Result<Handler, Error> {
match channels.steinhart_hart_summaries_json() {
Ok(buf) => { Ok(buf) => {
send_line(socket, &buf); send_line(socket, &buf);
} }
Err(e) => { Err(e) => {
error!("unable to serialize steinhart-hart summaries: {:?}", e); error!("unable to serialize b parameter summaries: {:?}", e);
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
return Err(Error::Report); return Err(Error::Report);
} }
@ -241,19 +238,19 @@ impl Handler {
Ok(Handler::Handled) Ok(Handler::Handled)
} }
fn set_steinhart_hart( fn set_b_parameter(
socket: &mut TcpSocket, socket: &mut TcpSocket,
channels: &mut Channels, channels: &mut Channels,
channel: usize, channel: usize,
parameter: ShParameter, parameter: BpParameter,
value: f64, value: f64,
) -> Result<Handler, Error> { ) -> Result<Handler, Error> {
let sh = &mut channels.channel_state(channel).sh; let bp = &mut channels.channel_state(channel).bp;
use super::command_parser::ShParameter::*; use super::command_parser::BpParameter::*;
match parameter { match parameter {
T0 => sh.t0 = ThermodynamicTemperature::new::<degree_celsius>(value), T0 => bp.t0 = ThermodynamicTemperature::new::<degree_celsius>(value),
B => sh.b = TemperatureInterval::new::<kelvin>(value), B => bp.b = TemperatureInterval::new::<kelvin>(value),
R0 => sh.r0 = ElectricalResistance::new::<ohm>(value), R0 => bp.r0 = ElectricalResistance::new::<ohm>(value),
} }
send_line(socket, b"{}"); send_line(socket, b"{}");
Ok(Handler::Handled) Ok(Handler::Handled)
@ -480,9 +477,7 @@ impl Handler {
Command::Show(ShowCommand::Input) => Handler::show_report(socket, channels), Command::Show(ShowCommand::Input) => Handler::show_report(socket, channels),
Command::Show(ShowCommand::Pid) => Handler::show_pid(socket, channels), Command::Show(ShowCommand::Pid) => Handler::show_pid(socket, channels),
Command::Show(ShowCommand::Output) => Handler::show_pwm(socket, channels), Command::Show(ShowCommand::Output) => Handler::show_pwm(socket, channels),
Command::Show(ShowCommand::SteinhartHart) => { Command::Show(ShowCommand::BParameter) => Handler::show_b_parameter(socket, channels),
Handler::show_steinhart_hart(socket, channels)
}
Command::Show(ShowCommand::PostFilter) => Handler::show_post_filter(socket, channels), Command::Show(ShowCommand::PostFilter) => Handler::show_post_filter(socket, channels),
Command::Show(ShowCommand::Ipv4) => Handler::show_ipv4(socket, ipv4_config), Command::Show(ShowCommand::Ipv4) => Handler::show_ipv4(socket, ipv4_config),
Command::OutputPid { channel } => Handler::engage_pid(socket, channels, channel), Command::OutputPid { channel } => Handler::engage_pid(socket, channels, channel),
@ -502,11 +497,11 @@ impl Handler {
parameter, parameter,
value, value,
} => Handler::set_pid(socket, channels, channel, parameter, value), } => Handler::set_pid(socket, channels, channel, parameter, value),
Command::SteinhartHart { Command::BParameter {
channel, channel,
parameter, parameter,
value, value,
} => Handler::set_steinhart_hart(socket, channels, channel, parameter, value), } => Handler::set_b_parameter(socket, channels, channel, parameter, value),
Command::PostFilter { Command::PostFilter {
channel, channel,
rate: None, rate: None,

View File

@ -93,7 +93,7 @@ pub enum ShowCommand {
Input, Input,
Output, Output,
Pid, Pid,
SteinhartHart, BParameter,
PostFilter, PostFilter,
Ipv4, Ipv4,
} }
@ -108,9 +108,9 @@ pub enum PidParameter {
OutputMax, OutputMax,
} }
/// Steinhart-Hart equation parameter /// B-Parameter equation parameter
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum ShParameter { pub enum BpParameter {
T0, T0,
B, B,
R0, R0,
@ -172,9 +172,9 @@ pub enum Command {
parameter: PidParameter, parameter: PidParameter,
value: f64, value: f64,
}, },
SteinhartHart { BParameter {
channel: usize, channel: usize,
parameter: ShParameter, parameter: BpParameter,
value: f64, value: f64,
}, },
PostFilter { PostFilter {
@ -366,18 +366,18 @@ fn pid(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
))(input) ))(input)
} }
/// `s-h <0-1> <parameter> <value>` /// `b-p <0-1> <parameter> <value>`
fn steinhart_hart_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> { fn b_parameter_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
let (input, channel) = channel(input)?; let (input, channel) = channel(input)?;
let (input, _) = whitespace(input)?; let (input, _) = whitespace(input)?;
let (input, parameter) = alt(( let (input, parameter) = alt((
value(ShParameter::T0, tag("t0")), value(BpParameter::T0, tag("t0")),
value(ShParameter::B, tag("b")), value(BpParameter::B, tag("b")),
value(ShParameter::R0, tag("r0")), value(BpParameter::R0, tag("r0")),
))(input)?; ))(input)?;
let (input, _) = whitespace(input)?; let (input, _) = whitespace(input)?;
let (input, value) = float(input)?; let (input, value) = float(input)?;
let result = value.map(|value| Command::SteinhartHart { let result = value.map(|value| Command::BParameter {
channel, channel,
parameter, parameter,
value, value,
@ -385,12 +385,12 @@ fn steinhart_hart_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Erro
Ok((input, result)) Ok((input, result))
} }
/// `s-h` | `s-h <steinhart_hart_parameter>` /// `b-p` | `b-p <b_parameter_parameter>`
fn steinhart_hart(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> { fn b_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
let (input, _) = tag("s-h")(input)?; let (input, _) = tag("b-p")(input)?;
alt(( alt((
preceded(whitespace, steinhart_hart_parameter), preceded(whitespace, b_parameter_parameter),
value(Ok(Command::Show(ShowCommand::SteinhartHart)), end), value(Ok(Command::Show(ShowCommand::BParameter)), end),
))(input) ))(input)
} }
@ -572,7 +572,7 @@ fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
output, output,
center_point, center_point,
pid, pid,
steinhart_hart, b_parameter,
postfilter, postfilter,
value(Ok(Command::Dfu), tag("dfu")), value(Ok(Command::Dfu), tag("dfu")),
fan, fan,
@ -678,7 +678,7 @@ mod test {
#[test] #[test]
fn parse_output_polarity() { fn parse_output_polarity() {
let command = Command::parse(b"pwm 0 polarity reversed"); let command = Command::parse(b"output 0 polarity reversed");
assert_eq!( assert_eq!(
command, command,
Ok(Command::OutputPolarity { Ok(Command::OutputPolarity {
@ -753,19 +753,19 @@ mod test {
} }
#[test] #[test]
fn parse_steinhart_hart() { fn parse_b_parameter() {
let command = Command::parse(b"s-h"); let command = Command::parse(b"b-p");
assert_eq!(command, Ok(Command::Show(ShowCommand::SteinhartHart))); assert_eq!(command, Ok(Command::Show(ShowCommand::BParameter)));
} }
#[test] #[test]
fn parse_steinhart_hart_set() { fn parse_b_parameter_set() {
let command = Command::parse(b"s-h 1 t0 23.05"); let command = Command::parse(b"b-p 1 t0 23.05");
assert_eq!( assert_eq!(
command, command,
Ok(Command::SteinhartHart { Ok(Command::BParameter {
channel: 1, channel: 1,
parameter: ShParameter::T0, parameter: BpParameter::T0,
value: 23.05, value: 23.05,
}) })
); );

View File

@ -1,10 +1,10 @@
use crate::{ use crate::{
ad7172::PostFilter, ad7172::PostFilter,
b_parameter,
channels::Channels, channels::Channels,
command_parser::{CenterPoint, Polarity}, command_parser::{CenterPoint, Polarity},
pid, pid,
pwm_limits::PwmLimits, pwm_limits::PwmLimits,
steinhart_hart,
}; };
use num_traits::Zero; use num_traits::Zero;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -18,7 +18,7 @@ pub struct ChannelConfig {
pid_engaged: bool, pid_engaged: bool,
i_set: ElectricCurrent, i_set: ElectricCurrent,
polarity: Polarity, polarity: Polarity,
sh: steinhart_hart::Parameters, bp: b_parameter::Parameters,
pwm: PwmLimits, pwm: PwmLimits,
/// uses variant `PostFilter::Invalid` instead of `None` to save space /// uses variant `PostFilter::Invalid` instead of `None` to save space
adc_postfilter: PostFilter, adc_postfilter: PostFilter,
@ -47,7 +47,7 @@ impl ChannelConfig {
pid_engaged: state.pid_engaged, pid_engaged: state.pid_engaged,
i_set, i_set,
polarity: state.polarity.clone(), polarity: state.polarity.clone(),
sh: state.sh.clone(), bp: state.bp.clone(),
pwm, pwm,
adc_postfilter, adc_postfilter,
} }
@ -59,7 +59,7 @@ impl ChannelConfig {
state.pid.parameters = self.pid.clone(); state.pid.parameters = self.pid.clone();
state.pid.target = self.pid_target.into(); state.pid.target = self.pid_target.into();
state.pid_engaged = self.pid_engaged; state.pid_engaged = self.pid_engaged;
state.sh = self.sh.clone(); state.bp = self.bp.clone();
self.pwm.apply(channels, channel); self.pwm.apply(channels, channel);

View File

@ -35,10 +35,10 @@ mod session;
use session::{Session, SessionInput}; use session::{Session, SessionInput};
mod command_parser; mod command_parser;
use command_parser::Ipv4Config; use command_parser::Ipv4Config;
mod b_parameter;
mod channels; mod channels;
mod pid; mod pid;
mod pwm_limits; mod pwm_limits;
mod steinhart_hart;
mod timer; mod timer;
use channels::{Channels, CHANNELS}; use channels::{Channels, CHANNELS};
mod channel; mod channel;

View File

@ -1,8 +1,6 @@
use crate::channels::Channels; use crate::channels::Channels;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uom::si::{ use uom::si::f64::{ElectricCurrent, ElectricPotential};
f64::{ElectricCurrent, ElectricPotential},
};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PwmLimits { pub struct PwmLimits {
@ -25,4 +23,4 @@ impl PwmLimits {
channels.set_max_i_pos(channel, self.max_i_pos); channels.set_max_i_pos(channel, self.max_i_pos);
channels.set_max_i_neg(channel, self.max_i_neg); channels.set_max_i_neg(channel, self.max_i_neg);
} }
} }