2024-02-02 14:07:25 +08:00
|
|
|
use core::fmt::Debug;
|
2024-02-05 15:14:23 +08:00
|
|
|
use miniconf::{JsonCoreSlash, Tree};
|
2024-02-02 14:07:25 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-02-05 15:14:23 +08:00
|
|
|
use uom::si::{
|
|
|
|
electric_current::{ampere, milliampere, ElectricCurrent},
|
|
|
|
electric_potential::{volt, ElectricPotential}
|
|
|
|
};
|
2024-02-02 14:07:25 +08:00
|
|
|
use crate::laser_diode::laser_diode::LdDrive;
|
2024-02-05 15:14:23 +08:00
|
|
|
use crate::thermostat::thermostat::Thermostat;
|
2024-02-02 14:07:25 +08:00
|
|
|
use log::info;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)]
|
|
|
|
enum LdCmdEnum {
|
|
|
|
#[default]
|
|
|
|
Reserved,
|
|
|
|
// LD Drive Related
|
|
|
|
PowerUp,
|
|
|
|
PowerDown,
|
|
|
|
LdTermsShort,
|
|
|
|
LdTermsOpen,
|
|
|
|
SetI,
|
|
|
|
SetISoftLimit,
|
|
|
|
// PD Mon Related
|
|
|
|
SetPdResponsitivity,
|
|
|
|
SetPdDarkCurrent,
|
|
|
|
SetPdILimit,
|
|
|
|
SetLdPwrLimit,
|
|
|
|
ClearAlarmStatus,
|
|
|
|
// Report Related
|
|
|
|
GetModInTermStatus,
|
|
|
|
GetLdStatus,
|
|
|
|
GetAlramStatus,
|
|
|
|
}
|
|
|
|
|
2024-02-05 15:14:23 +08:00
|
|
|
#[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)]
|
|
|
|
enum ThermostatCmdEnum {
|
|
|
|
#[default]
|
|
|
|
Reserved,
|
|
|
|
PowerUp,
|
|
|
|
PowerDown,
|
|
|
|
// TEC
|
|
|
|
SetTecMaxV,
|
|
|
|
SetTecMaxIPos,
|
|
|
|
SetTecMaxINeg,
|
|
|
|
SetTecIOut, // Constant Current Mode
|
|
|
|
// Control MOde
|
|
|
|
SetConstantCurrentMode,
|
|
|
|
SetPidControlMode,
|
|
|
|
// PID
|
|
|
|
SetPidKp,
|
|
|
|
SetPidKi,
|
|
|
|
SetPidKd,
|
|
|
|
SetPidOutMin,
|
|
|
|
SetPidOutMax,
|
|
|
|
SetPidUpdateInterval, // Update Interval is set based on the sampling rate of ADC
|
|
|
|
// Steinhart-Hart Equation
|
|
|
|
SetShT0,
|
|
|
|
SetShR0,
|
|
|
|
SetShBeta,
|
|
|
|
// Report Related
|
|
|
|
GetTecStatus,
|
|
|
|
GetPidStatus,
|
|
|
|
GetShParams,
|
|
|
|
}
|
|
|
|
|
2024-02-02 14:07:25 +08:00
|
|
|
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
|
|
|
|
pub struct CmdJsonObj{
|
|
|
|
rev: u8,
|
2024-02-05 15:14:23 +08:00
|
|
|
laser_diode_cmd: Option<LdCmdEnum>,
|
|
|
|
thermostat_cmd: Option<ThermostatCmdEnum>,
|
2024-02-02 14:07:25 +08:00
|
|
|
data_f32: Option<f32>,
|
|
|
|
data_f64: Option<f64>,
|
|
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
|
|
|
|
pub struct Cmd {
|
|
|
|
json: CmdJsonObj
|
|
|
|
}
|
|
|
|
|
2024-02-05 15:14:23 +08:00
|
|
|
pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mut tec: Thermostat)->(LdDrive, Thermostat){
|
2024-02-02 14:07:25 +08:00
|
|
|
let mut cmd = Cmd {
|
|
|
|
json: CmdJsonObj::default()
|
|
|
|
};
|
|
|
|
match cmd.set_json("/json", &buffer[0..buffer_size]){
|
|
|
|
Ok(_) => {
|
|
|
|
info!("############ Command Received {:?}", cmd.json.laser_diode_cmd);
|
|
|
|
|
|
|
|
match cmd.json.laser_diode_cmd {
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::PowerUp) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
laser.power_up()
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::PowerDown) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
laser.power_down()
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::LdTermsShort) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
laser.ld_short();
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::LdTermsOpen) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
laser.ld_open();
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::SetI) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
match cmd.json.data_f64 {
|
|
|
|
Some(val) => {
|
|
|
|
laser.ld_set_i(ElectricCurrent::new::<milliampere>(val));
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
info!("Wrong Data type is received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::SetISoftLimit) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
match cmd.json.data_f64 {
|
|
|
|
Some(val) => {
|
|
|
|
laser.set_ld_drive_current_limit(ElectricCurrent::new::<milliampere>(val))
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
info!("Wrong Data type is received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::SetPdResponsitivity) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::SetPdDarkCurrent) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::SetPdILimit) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
match cmd.json.data_f64 {
|
|
|
|
Some(val) => {
|
|
|
|
laser.set_pd_i_limit(ElectricCurrent::new::<milliampere>(val))
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
info!("Wrong Data type is received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::SetLdPwrLimit) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::ClearAlarmStatus) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
laser.pd_mon_clear_alarm()
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::GetModInTermStatus) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::GetLdStatus) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
Some(LdCmdEnum::GetAlramStatus) => {
|
2024-02-02 14:07:25 +08:00
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
None => { /* Do Nothing*/ }
|
2024-02-02 14:07:25 +08:00
|
|
|
_ => {
|
|
|
|
info!("Unimplemented Command")
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
|
|
|
|
match cmd.json.thermostat_cmd {
|
|
|
|
Some(ThermostatCmdEnum::PowerUp) => {
|
|
|
|
tec.power_up()
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::PowerDown) => {
|
|
|
|
tec.power_down()
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetTecMaxV) => {
|
|
|
|
match cmd.json.data_f64 {
|
|
|
|
Some(val) => {
|
|
|
|
tec.set_max_v(ElectricPotential::new::<volt>(val));
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
info!("Wrong Data type is received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetTecMaxIPos) => {
|
|
|
|
match cmd.json.data_f64 {
|
|
|
|
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_f64 {
|
|
|
|
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_f64 {
|
|
|
|
Some(val) => {
|
|
|
|
tec.set_i(ElectricCurrent::new::<milliampere>(val));
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
info!("Wrong Data type is received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetConstantCurrentMode) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetPidControlMode) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetPidKp) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetPidKi) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetPidKd) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetPidOutMin) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetPidOutMax) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetPidUpdateInterval) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetShT0) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetShR0) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::SetShBeta) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::GetTecStatus) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::GetPidStatus) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
Some(ThermostatCmdEnum::GetShParams) => {
|
|
|
|
info!("Not supported Yet")
|
|
|
|
}
|
|
|
|
None => { /* Do Nothing*/ }
|
|
|
|
_ => {
|
|
|
|
info!("Unimplemented Command")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-02-02 14:07:25 +08:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
info!("Invalid Command: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 15:14:23 +08:00
|
|
|
(laser, tec)
|
2024-02-02 14:07:25 +08:00
|
|
|
}
|