2020-09-24 07:18:33 +08:00
|
|
|
use serde::{Serialize, Deserialize};
|
2020-09-24 15:49:13 +08:00
|
|
|
use postcard::{from_bytes, to_slice};
|
2020-09-24 07:18:33 +08:00
|
|
|
use crate::{
|
|
|
|
channel_state::ChannelState,
|
|
|
|
channels::{CHANNELS, Channels},
|
|
|
|
command_parser::CenterPoint,
|
|
|
|
pid,
|
|
|
|
steinhart_hart,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Just for encoding/decoding, actual state resides in ChannelState
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Config {
|
|
|
|
channels: [ChannelConfig; CHANNELS],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
pub fn new(channels: &mut Channels) -> Self {
|
|
|
|
Config {
|
|
|
|
channels: [
|
|
|
|
ChannelConfig::new(channels.channel_state(0usize)),
|
|
|
|
ChannelConfig::new(channels.channel_state(1usize)),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 08:06:53 +08:00
|
|
|
pub fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], postcard::Error> {
|
|
|
|
to_slice(self, buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decode(buffer: &[u8]) -> Result<Self, postcard::Error> {
|
|
|
|
from_bytes(buffer)
|
2020-09-24 07:18:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct ChannelConfig {
|
|
|
|
center: CenterPoint,
|
|
|
|
pid: pid::Parameters,
|
|
|
|
pid_target: f64,
|
|
|
|
sh: steinhart_hart::Parameters,
|
|
|
|
// TODO: pwm limits
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChannelConfig {
|
|
|
|
pub fn new(state: &ChannelState) -> Self {
|
|
|
|
ChannelConfig {
|
|
|
|
center: state.center.clone(),
|
|
|
|
pid: state.pid.parameters.clone(),
|
|
|
|
pid_target: state.pid.target,
|
|
|
|
sh: state.sh.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|