2020-09-24 15:49:13 +08:00
|
|
|
use postcard::{from_bytes, to_slice};
|
2020-09-25 05:04:29 +08:00
|
|
|
use serde::{Serialize, Deserialize};
|
2020-10-01 07:34:46 +08:00
|
|
|
use smoltcp::wire::Ipv4Address;
|
2020-09-25 05:04:29 +08:00
|
|
|
use stm32f4xx_hal::i2c;
|
2020-09-25 03:10:27 +08:00
|
|
|
use uom::si::{
|
|
|
|
electric_potential::volt,
|
|
|
|
electric_current::ampere,
|
2020-09-25 03:31:30 +08:00
|
|
|
electrical_resistance::ohm,
|
2020-09-25 05:04:29 +08:00
|
|
|
f64::{ElectricCurrent, ElectricPotential, ElectricalResistance, ThermodynamicTemperature},
|
2020-09-25 03:31:30 +08:00
|
|
|
thermodynamic_temperature::degree_celsius,
|
2020-09-25 03:10:27 +08:00
|
|
|
};
|
2020-09-24 07:18:33 +08:00
|
|
|
use crate::{
|
2020-09-26 07:40:01 +08:00
|
|
|
ad7172::PostFilter,
|
2020-09-24 07:18:33 +08:00
|
|
|
channels::{CHANNELS, Channels},
|
|
|
|
command_parser::CenterPoint,
|
2020-09-25 05:04:29 +08:00
|
|
|
EEPROM_SIZE, EEPROM_PAGE_SIZE,
|
2020-09-24 07:18:33 +08:00
|
|
|
pid,
|
2020-09-25 05:04:29 +08:00
|
|
|
pins,
|
2020-09-24 07:18:33 +08:00
|
|
|
steinhart_hart,
|
|
|
|
};
|
|
|
|
|
2020-09-25 05:04:29 +08:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Eeprom(eeprom24x::Error<i2c::Error>),
|
|
|
|
Encode(postcard::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<eeprom24x::Error<i2c::Error>> for Error {
|
|
|
|
fn from(e: eeprom24x::Error<i2c::Error>) -> Self {
|
|
|
|
Error::Eeprom(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<postcard::Error> for Error {
|
|
|
|
fn from(e: postcard::Error) -> Self {
|
|
|
|
Error::Encode(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 07:18:33 +08:00
|
|
|
/// Just for encoding/decoding, actual state resides in ChannelState
|
2020-09-25 03:35:15 +08:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2020-09-24 07:18:33 +08:00
|
|
|
pub struct Config {
|
|
|
|
channels: [ChannelConfig; CHANNELS],
|
2020-10-01 07:34:46 +08:00
|
|
|
pub ipv4_address: [u8; 4],
|
2020-09-24 07:18:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2020-10-01 07:34:46 +08:00
|
|
|
pub fn new(channels: &mut Channels, ipv4_address: Ipv4Address) -> Self {
|
2020-09-24 07:18:33 +08:00
|
|
|
Config {
|
|
|
|
channels: [
|
2020-09-25 03:10:27 +08:00
|
|
|
ChannelConfig::new(channels, 0),
|
|
|
|
ChannelConfig::new(channels, 1),
|
2020-09-24 07:18:33 +08:00
|
|
|
],
|
2020-10-01 07:34:46 +08:00
|
|
|
ipv4_address: ipv4_address.0,
|
2020-09-24 07:18:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:10:35 +08:00
|
|
|
/// apply loaded config to system
|
|
|
|
pub fn apply(&self, channels: &mut Channels) {
|
|
|
|
for i in 0..CHANNELS {
|
|
|
|
self.channels[i].apply(channels, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load(eeprom: &mut pins::Eeprom) -> Result<Self, Error> {
|
|
|
|
let mut buffer = [0; EEPROM_SIZE];
|
|
|
|
eeprom.read_data(0, &mut buffer)?;
|
|
|
|
log::info!("load: {:?}", buffer);
|
|
|
|
let config = from_bytes(&mut buffer)?;
|
|
|
|
Ok(config)
|
2020-09-24 08:06:53 +08:00
|
|
|
}
|
|
|
|
|
2020-09-25 05:10:35 +08:00
|
|
|
pub fn save(&self, eeprom: &mut pins::Eeprom) -> Result<(), Error> {
|
|
|
|
let mut buffer = [0; EEPROM_SIZE];
|
|
|
|
let config_buffer = to_slice(self, &mut buffer)?;
|
|
|
|
log::info!("save: {:?}", config_buffer);
|
|
|
|
|
|
|
|
let mut addr = 0;
|
|
|
|
for chunk in config_buffer.chunks(EEPROM_PAGE_SIZE) {
|
|
|
|
'write_retry: loop {
|
|
|
|
match eeprom.write_page(addr, chunk) {
|
|
|
|
Ok(()) => break 'write_retry,
|
|
|
|
Err(eeprom24x::Error::I2C(i2c::Error::NACK)) => {},
|
|
|
|
Err(e) => Err(e)?,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr += chunk.len() as u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-09-24 07:18:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:35:15 +08:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2020-09-24 07:18:33 +08:00
|
|
|
pub struct ChannelConfig {
|
|
|
|
center: CenterPoint,
|
|
|
|
pid: pid::Parameters,
|
2020-09-25 02:45:07 +08:00
|
|
|
pid_target: f32,
|
2020-09-25 03:31:30 +08:00
|
|
|
sh: SteinhartHartConfig,
|
2020-09-25 03:10:27 +08:00
|
|
|
pwm: PwmLimits,
|
2020-09-26 07:40:01 +08:00
|
|
|
/// uses variant `PostFilter::Invalid` instead of `None` to save space
|
|
|
|
adc_postfilter: PostFilter,
|
2020-09-24 07:18:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ChannelConfig {
|
2020-09-25 03:10:27 +08:00
|
|
|
pub fn new(channels: &mut Channels, channel: usize) -> Self {
|
|
|
|
let pwm = PwmLimits::new(channels, channel);
|
2020-09-26 07:40:01 +08:00
|
|
|
|
|
|
|
let adc_postfilter = channels.adc.get_postfilter(channel as u8)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap_or(PostFilter::Invalid);
|
|
|
|
|
2020-09-25 03:10:27 +08:00
|
|
|
let state = channels.channel_state(channel);
|
2020-09-24 07:18:33 +08:00
|
|
|
ChannelConfig {
|
|
|
|
center: state.center.clone(),
|
|
|
|
pid: state.pid.parameters.clone(),
|
2020-09-25 02:45:07 +08:00
|
|
|
pid_target: state.pid.target as f32,
|
2020-09-25 03:31:30 +08:00
|
|
|
sh: (&state.sh).into(),
|
2020-09-25 03:10:27 +08:00
|
|
|
pwm,
|
2020-09-26 07:40:01 +08:00
|
|
|
adc_postfilter,
|
2020-09-24 07:18:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:04:29 +08:00
|
|
|
pub fn apply(&self, channels: &mut Channels, channel: usize) {
|
|
|
|
let state = channels.channel_state(channel);
|
|
|
|
state.center = self.center.clone();
|
|
|
|
state.pid.parameters = self.pid.clone();
|
|
|
|
state.pid.target = self.pid_target.into();
|
|
|
|
state.sh = (&self.sh).into();
|
2020-09-26 07:40:01 +08:00
|
|
|
|
2020-09-25 05:04:29 +08:00
|
|
|
self.pwm.apply(channels, channel);
|
2020-09-26 07:40:01 +08:00
|
|
|
|
|
|
|
let adc_postfilter = match self.adc_postfilter {
|
|
|
|
PostFilter::Invalid => None,
|
|
|
|
adc_postfilter => Some(adc_postfilter),
|
|
|
|
};
|
|
|
|
let _ = channels.adc.set_postfilter(channel as u8, adc_postfilter);
|
2020-09-25 05:04:29 +08:00
|
|
|
}
|
2020-09-24 07:18:33 +08:00
|
|
|
}
|
2020-09-25 02:59:04 +08:00
|
|
|
|
2020-09-25 03:35:15 +08:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2020-09-25 03:31:30 +08:00
|
|
|
struct SteinhartHartConfig {
|
|
|
|
t0: f32,
|
|
|
|
r0: f32,
|
|
|
|
b: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&steinhart_hart::Parameters> for SteinhartHartConfig {
|
|
|
|
fn from(sh: &steinhart_hart::Parameters) -> Self {
|
|
|
|
SteinhartHartConfig {
|
|
|
|
t0: sh.t0.get::<degree_celsius>() as f32,
|
|
|
|
r0: sh.r0.get::<ohm>() as f32,
|
|
|
|
b: sh.b as f32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:04:29 +08:00
|
|
|
impl Into<steinhart_hart::Parameters> for &SteinhartHartConfig {
|
|
|
|
fn into(self) -> steinhart_hart::Parameters {
|
|
|
|
steinhart_hart::Parameters {
|
|
|
|
t0: ThermodynamicTemperature::new::<degree_celsius>(self.t0.into()),
|
|
|
|
r0: ElectricalResistance::new::<ohm>(self.r0.into()),
|
|
|
|
b: self.b.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:35:15 +08:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2020-09-25 03:10:27 +08:00
|
|
|
struct PwmLimits {
|
|
|
|
max_v: f32,
|
|
|
|
max_i_pos: f32,
|
|
|
|
max_i_neg: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PwmLimits {
|
|
|
|
pub fn new(channels: &mut Channels, channel: usize) -> Self {
|
|
|
|
let (max_v, _) = channels.get_max_v(channel);
|
|
|
|
let (max_i_pos, _) = channels.get_max_i_pos(channel);
|
|
|
|
let (max_i_neg, _) = channels.get_max_i_neg(channel);
|
|
|
|
PwmLimits {
|
|
|
|
max_v: max_v.get::<volt>() as f32,
|
|
|
|
max_i_pos: max_i_pos.get::<ampere>() as f32,
|
|
|
|
max_i_neg: max_i_neg.get::<ampere>() as f32,
|
|
|
|
}
|
|
|
|
}
|
2020-09-25 05:04:29 +08:00
|
|
|
|
|
|
|
pub fn apply(&self, channels: &mut Channels, channel: usize) {
|
|
|
|
channels.set_max_v(channel, ElectricPotential::new::<volt>(self.max_v.into()));
|
|
|
|
channels.set_max_i_pos(channel, ElectricCurrent::new::<ampere>(self.max_i_pos.into()));
|
|
|
|
channels.set_max_i_neg(channel, ElectricCurrent::new::<ampere>(self.max_i_neg.into()));
|
|
|
|
}
|
2020-09-25 03:10:27 +08:00
|
|
|
}
|
|
|
|
|
2020-09-25 02:59:04 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2020-10-01 07:34:46 +08:00
|
|
|
use crate::DEFAULT_IPV4_ADDRESS;
|
2020-09-25 02:59:04 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fit_eeprom() {
|
|
|
|
let channel_config = ChannelConfig {
|
|
|
|
center: CenterPoint::Override(1.5),
|
|
|
|
pid: pid::Parameters::default(),
|
|
|
|
pid_target: 93.7,
|
2020-09-25 03:31:30 +08:00
|
|
|
sh: (&steinhart_hart::Parameters::default()).into(),
|
2020-09-25 03:10:27 +08:00
|
|
|
pwm: PwmLimits {
|
|
|
|
max_v: 1.65,
|
|
|
|
max_i_pos: 2.1,
|
|
|
|
max_i_neg: 2.25,
|
|
|
|
},
|
2020-09-26 07:40:01 +08:00
|
|
|
adc_postfilter: PostFilter::F21SPS,
|
2020-09-25 02:59:04 +08:00
|
|
|
};
|
|
|
|
let config = Config {
|
|
|
|
channels: [
|
|
|
|
channel_config.clone(),
|
|
|
|
channel_config.clone(),
|
|
|
|
],
|
2020-10-01 07:34:46 +08:00
|
|
|
ipv4_address: DEFAULT_IPV4_ADDRESS.0,
|
2020-09-25 02:59:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut buffer = [0; EEPROM_SIZE];
|
2020-09-25 05:21:54 +08:00
|
|
|
let buffer = to_slice(&config, &mut buffer).unwrap();
|
2020-09-25 02:59:04 +08:00
|
|
|
assert!(buffer.len() <= EEPROM_SIZE);
|
|
|
|
}
|
2020-09-25 03:35:15 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_encode_decode() {
|
|
|
|
let channel_config = ChannelConfig {
|
|
|
|
center: CenterPoint::Override(1.5),
|
|
|
|
pid: pid::Parameters::default(),
|
|
|
|
pid_target: 93.7,
|
|
|
|
sh: (&steinhart_hart::Parameters::default()).into(),
|
|
|
|
pwm: PwmLimits {
|
|
|
|
max_v: 1.65,
|
|
|
|
max_i_pos: 2.1,
|
|
|
|
max_i_neg: 2.25,
|
|
|
|
},
|
2020-09-26 07:40:01 +08:00
|
|
|
adc_postfilter: PostFilter::F21SPS,
|
2020-09-25 03:35:15 +08:00
|
|
|
};
|
|
|
|
let config = Config {
|
|
|
|
channels: [
|
|
|
|
channel_config.clone(),
|
|
|
|
channel_config.clone(),
|
|
|
|
],
|
2020-10-01 07:34:46 +08:00
|
|
|
ipv4_address: DEFAULT_IPV4_ADDRESS.0,
|
2020-09-25 03:35:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut buffer = [0; EEPROM_SIZE];
|
2020-09-25 05:21:54 +08:00
|
|
|
to_slice(&config, &mut buffer).unwrap();
|
|
|
|
let decoded: Config = from_bytes(&buffer).unwrap();
|
2020-09-25 03:35:15 +08:00
|
|
|
assert_eq!(decoded, config);
|
|
|
|
}
|
2020-09-25 02:59:04 +08:00
|
|
|
}
|