kirdy/src/net/cmd_handler.rs

562 lines
21 KiB
Rust

use core::{fmt::Debug, marker::PhantomData};
use miniconf::{JsonCoreSlash, Tree};
use serde::{Deserialize, Serialize};
use uom::si::{
electric_current::{ampere, microampere, milliampere, ElectricCurrent},
electric_potential::{volt, ElectricPotential},
electrical_resistance::{ohm, ElectricalResistance},
power::{milliwatt, Power},
thermodynamic_temperature::{degree_celsius, ThermodynamicTemperature}
};
use crate::{laser_diode::{laser_diode::{
LdDrive, LdSettingsSummary, StatusReport as LdStatusReport},
pd_responsitivity::ResponsitivityUnit
},
net::net,
thermostat::{ad7172::FilterType, thermostat::{StatusReport as TecStatusReport, TempAdcFilter}}
};
use crate::thermostat::thermostat::{Thermostat, ThermostatSettingsSummary};
use crate::thermostat::pid_state::PidSettings::*;
use crate::device::{dfu, sys_timer};
use log::info;
use crate::{DeviceSettings, State, IpSettings};
#[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)]
enum DeviceCmd {
#[default]
Reserved,
SetIPSettings,
SetActiveReportMode,
GetStatusReport,
GetSettingsSummary,
Dfu,
SaveFlashSettings,
LoadFlashSettings,
HardReset,
}
#[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)]
enum LdCmdEnum {
#[default]
Reserved,
// LD Drive Related
SetDefaultPowerOn,
PowerUp,
PowerDown,
LdTermsShort,
LdTermsOpen,
SetI,
SetISoftLimit,
// PD Mon Related
SetPdResponsitivity,
SetPdDarkCurrent,
SetLdPwrLimit,
ClearAlarm,
}
#[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)]
enum ThermostatCmdEnum {
#[default]
Reserved,
SetDefaultPowerOn,
PowerUp,
PowerDown,
// TEC
SetTecMaxV,
SetTecMaxIPos,
SetTecMaxINeg,
SetTecIOut, // Constant Current Mode
SetTemperatureSetpoint,
// PID
SetPidEngage,
SetPidDisEngage,
SetPidKp,
SetPidKi,
SetPidKd,
SetPidOutMin,
SetPidOutMax,
SetPidUpdateInterval, // Update Interval is set based on the sampling rate of ADC
// Temperature ADC
ConfigTempAdcFilter,
// TempMon
SetTempMonUpperLimit,
SetTempMonLowerLimit,
ClearAlarm,
// Steinhart-Hart Equation
SetShT0,
SetShR0,
SetShBeta,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct CmdJsonObj{
laser_diode_cmd: Option<LdCmdEnum>,
thermostat_cmd: Option<ThermostatCmdEnum>,
device_cmd: Option<DeviceCmd>,
data_bool: Option<bool>,
data_f32: Option<f32>,
data_f64: Option<f64>,
temp_adc_filter: Option<TempAdcFilter>,
ip_settings: Option<IpSettings>,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct Cmd {
json: CmdJsonObj
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct StatusReport {
ts: u32,
laser: LdStatusReport,
tec: TecStatusReport,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct StatusReportObj {
json: StatusReport
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct SettingsSummary {
laser: LdSettingsSummary,
thermostat: ThermostatSettingsSummary,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct SettingsSummaryObj {
json: SettingsSummary
}
pub fn send_settings_summary(buffer: &mut [u8], laser: &mut LdDrive, tec: &mut Thermostat){
let settings_summary = SettingsSummaryObj {
json: SettingsSummary {
laser: laser.get_settings_summary(),
thermostat: tec.get_settings_summary(),
}
};
let num_bytes = settings_summary.get_json("/json", buffer).unwrap();
net::eth_send(buffer, num_bytes);
}
pub fn send_status_report(buffer: &mut [u8], laser: &mut LdDrive, tec: &mut Thermostat){
let status_report = StatusReportObj {
json: StatusReport {
ts: sys_timer::now(),
laser: laser.get_status_report(),
tec: tec.get_status_report(),
}
};
let num_bytes = status_report.get_json("/json", buffer).unwrap();
net::eth_send(buffer, num_bytes);
}
// Use a minimal struct for high speed cmd ctrl to reduce processing overhead
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct TecSetICmdJson {
tec_set_i: f32
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct TecSetICmd {
json: TecSetICmdJson
}
/// Miniconf is very slow in debug builds (~3-4ms of cmd decoding time).
/// Make sure kirdy's firmware is flashed with release builds.
/// The received message must contain only one json cmd. TCP client should set TCP_NODELAY or equivalent flag in its TCP Socket
/// Settings to avoid unwanted buffering on TX Data and minimize TX latency.
pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mut tec: Thermostat, mut state: State, mut device_settings: DeviceSettings)->(LdDrive, Thermostat, State, DeviceSettings){
let mut cmd = TecSetICmd {
json: TecSetICmdJson::default()
};
match cmd.set_json("/json", &buffer[0..buffer_size]){
Ok(_) => {
tec.set_i(ElectricCurrent::new::<milliampere>(cmd.json.tec_set_i));
return (laser, tec, state, device_settings);
}
Err(_) => { /* Do Nothing */}
}
let mut cmd = Cmd {
json: CmdJsonObj::default()
};
match cmd.set_json("/json", &buffer[0..buffer_size]){
Ok(_) => {
info!("############ Laser Diode Command Received {:?}", cmd.json.laser_diode_cmd);
info!("############ Thermostat Command Received {:?}", cmd.json.thermostat_cmd);
info!("############ Device Command Received {:?}", cmd.json.device_cmd);
match cmd.json.device_cmd {
Some(DeviceCmd::SetIPSettings) => {
match cmd.json.ip_settings {
Some(val) => {
device_settings.ip_settings = val;
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(DeviceCmd::Dfu) => {
unsafe {
dfu::set_dfu_trigger();
}
}
Some(DeviceCmd::SetActiveReportMode) => {
match cmd.json.data_bool{
Some(val) => {
device_settings.report_readings = val;
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(DeviceCmd::GetStatusReport) => {
send_status_report(buffer, &mut laser, &mut tec);
}
Some(DeviceCmd::GetSettingsSummary) => {
send_settings_summary(buffer, &mut laser, &mut tec);
}
Some(DeviceCmd::SaveFlashSettings) => {
state = State::SaveFlashSettings;
}
Some(DeviceCmd::LoadFlashSettings) => {
state = State::LoadFlashSettings;
}
Some(DeviceCmd::HardReset) => {
state = State::HardReset;
}
None => { /* Do Nothing */}
_ => {
info!("Unimplemented Command")
}
}
match cmd.json.laser_diode_cmd {
Some(LdCmdEnum::SetDefaultPowerOn) => {
match cmd.json.data_bool {
Some(val) => {
laser.set_default_pwr_on(val);
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(LdCmdEnum::PowerUp) => {
laser.power_up()
}
Some(LdCmdEnum::PowerDown) => {
laser.power_down()
}
Some(LdCmdEnum::LdTermsShort) => {
laser.ld_short();
}
Some(LdCmdEnum::LdTermsOpen) => {
laser.ld_open();
}
Some(LdCmdEnum::SetI) => {
match cmd.json.data_f32 {
Some(val) => {
laser.ld_set_i(ElectricCurrent::new::<milliampere>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(LdCmdEnum::SetISoftLimit) => {
match cmd.json.data_f32 {
Some(val) => {
laser.set_ld_drive_current_limit(ElectricCurrent::new::<milliampere>(val))
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(LdCmdEnum::SetPdResponsitivity) => {
match cmd.json.data_f32 {
Some(val) => {
laser.set_pd_responsitivity(ResponsitivityUnit {dimension: PhantomData, units: PhantomData, value: val})
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(LdCmdEnum::SetPdDarkCurrent) => {
match cmd.json.data_f32 {
Some(val) => {
laser.set_pd_dark_current(ElectricCurrent::new::<microampere>(val))
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(LdCmdEnum::SetLdPwrLimit) => {
match cmd.json.data_f32 {
Some(val) => {
laser.set_ld_power_limit(Power::new::<milliwatt>(val))
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(LdCmdEnum::ClearAlarm) => {
laser.pd_mon_clear_alarm()
}
None => { /* Do Nothing*/ }
_ => {
info!("Unimplemented Command")
}
}
match cmd.json.thermostat_cmd {
Some(ThermostatCmdEnum::SetDefaultPowerOn) => {
match cmd.json.data_bool {
Some(val) => {
tec.set_default_pwr_on(val);
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::PowerUp) => {
tec.power_up()
}
Some(ThermostatCmdEnum::PowerDown) => {
tec.power_down()
}
Some(ThermostatCmdEnum::SetTecMaxV) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_max_v(ElectricPotential::new::<volt>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetTecMaxIPos) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_max_i_pos(ElectricCurrent::new::<ampere>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetTecMaxINeg) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_max_i_pos(ElectricCurrent::new::<milliampere>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetTecIOut) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_i(ElectricCurrent::new::<milliampere>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetTemperatureSetpoint) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_temperature_setpoint(ThermodynamicTemperature::new::<degree_celsius>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetPidEngage) => {
tec.set_pid_engaged(true);
}
Some(ThermostatCmdEnum::SetPidDisEngage) => {
tec.set_pid_engaged(false);
}
Some(ThermostatCmdEnum::SetPidKp) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_pid(Kp, val);
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetPidKi) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_pid(Ki, val);
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetPidKd) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_pid(Kd, val);
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetPidOutMin) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_pid(Min, val);
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetPidOutMax) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_pid(Max, val);
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetPidUpdateInterval) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::ConfigTempAdcFilter) => {
match cmd.json.temp_adc_filter {
Some(val) => {
match val.filter_type {
FilterType::Sinc5Sinc1With50hz60HzRejection => {
match val.sinc5sinc1postfilter {
Some(val2) => {
tec.set_temp_adc_sinc5_sinc1_with_postfilter(0, val2);
}
None => {
info!("sinc5sinc1postfilter field needs to be set for configuration");
}
}
}
FilterType::Sinc5Sinc1 => {
match val.sinc5sinc1odr {
Some(val2) => {
tec.set_temp_adc_sinc5_sinc1_filter(0, val2);
}
None => {
info!("sinc5sinc1odr field needs to be set for configuration");
}
}
}
FilterType::Sinc3WithFineODR => {
match val.sinc3fineodr {
Some(val2) => {
tec.set_temp_adc_sinc3_fine_filter(0, val2);
}
None => {
info!("data_f32 field needs to be set for configuration");
}
}
}
FilterType::Sinc3 => {
match val.sinc3odr {
Some(val2) => {
tec.set_temp_adc_sinc3_filter(0, val2);
}
None => {
info!("sinc3_filter field needs to be set for configuration");
}
}
}
}
}
None => {
info!("temp_adc_filter needs to be set for configuration")
}
}
}
Some(ThermostatCmdEnum::SetTempMonUpperLimit) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_temp_mon_upper_limit(ThermodynamicTemperature::new::<degree_celsius>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetTempMonLowerLimit) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_temp_mon_lower_limit(ThermodynamicTemperature::new::<degree_celsius>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::ClearAlarm) => {
tec.clear_temp_mon_alarm();
}
Some(ThermostatCmdEnum::SetShT0) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_sh_t0(ThermodynamicTemperature::new::<degree_celsius>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetShR0) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_sh_r0(ElectricalResistance::new::<ohm>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetShBeta) => {
match cmd.json.data_f32 {
Some(val) => {
tec.set_sh_beta(val);
}
None => {
info!("Wrong Data type is received")
}
}
}
None => { /* Do Nothing*/ }
_ => {
info!("Unimplemented Command")
}
}
}
Err(err) => {
info!("Invalid Command: {:?}", err);
}
}
(laser, tec, state, device_settings)
}