1
0
Fork 0

Add fns, struct items for getting ld, tec settings

This commit is contained in:
linuswck 2024-03-04 15:48:19 +08:00
parent adfa4f5fa3
commit ef2410f441
4 changed files with 76 additions and 12 deletions

View File

@ -51,6 +51,7 @@ impl Settings{
#[derive(Deserialize, Serialize, Clone, Copy, Debug, Tree)]
struct Settings {
pwr_on: bool,
default_pwr_on: bool,
ld_drive_current: ElectricCurrent,
ld_drive_current_limit: ElectricCurrent,
pd_responsitivity: pd_responsitivity::Parameters,
@ -61,6 +62,7 @@ impl Default for Settings {
fn default() -> Self {
Self {
pwr_on: false,
default_pwr_on: false,
ld_drive_current: ElectricCurrent::new::<milliampere>(0.0),
ld_drive_current_limit: ElectricCurrent::new::<milliampere>(0.0),
pd_responsitivity: pd_responsitivity::Parameters::default(),
@ -71,7 +73,7 @@ impl Default for Settings {
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct StatusReport {
pwr_engaged: bool,
pwr_on: bool,
pwr_excursion: bool,
ld_i_set: ElectricCurrent,
pd_i: ElectricCurrent,
@ -191,13 +193,17 @@ impl LdDrive{
LdPwrExcProtector::set_trigger_threshold_v(i / Settings::PD_MON_TRANSCONDUCTANCE);
}
pub fn set_default_pwr_on(&mut self, pwr_on: bool){
self.settings.default_pwr_on = pwr_on;
}
pub fn get_term_status(&mut self) -> Impedance {
self.ctrl.get_lf_mod_in_impedance()
}
pub fn get_status_report(&mut self) -> StatusReport {
StatusReport {
pwr_engaged: LdPwrExcProtector::get_status().pwr_engaged,
pwr_on: LdPwrExcProtector::get_status().pwr_engaged,
pwr_excursion: LdPwrExcProtector::get_status().pwr_excursion,
ld_i_set: self.settings.ld_drive_current,
pd_i: self.get_pd_i(),
@ -209,7 +215,7 @@ impl LdDrive{
pub fn get_settings_summary(&mut self) -> LdSettingsSummary {
let settings = self.settings;
LdSettingsSummary {
pwr_on: self.settings.pwr_on,
default_pwr_on: self.settings.default_pwr_on,
ld_drive_current: LdSettingsSummaryField { value: settings.ld_drive_current, max: Settings::LD_CURRENT_MAX},
ld_drive_current_limit: LdSettingsSummaryField { value: settings.ld_drive_current_limit, max: Settings::LD_CURRENT_MAX},
pd_responsitivity: settings.pd_responsitivity,
@ -220,7 +226,7 @@ impl LdDrive{
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct LdSettingsSummary {
pwr_on: bool,
default_pwr_on: bool,
ld_drive_current: LdSettingsSummaryField<ElectricCurrent>,
ld_drive_current_limit: LdSettingsSummaryField<ElectricCurrent>,
pd_responsitivity: pd_responsitivity::Parameters,

View File

@ -207,6 +207,10 @@ impl MAX1968 {
self.pins_adc.reference_voltage()
}
pub fn is_powered_on(&mut self) -> bool {
self.phy.shdn.is_set_low()
}
pub fn power_down(&mut self) {
self.phy.shdn.set_low();
}

View File

@ -142,6 +142,10 @@ impl PidState {
Some(temperature)
}
pub fn apply_pid_params(&mut self, pid_params: Parameters){
self.controller.parameters = pid_params;
}
pub fn set_pid_params(&mut self, param: PidSettings, val: f32){
match param {
PidSettings::Kp => {
@ -208,4 +212,8 @@ impl PidState {
pub fn get_sh(&mut self) -> sh::Parameters {
self.sh
}
pub fn apply_sh(&mut self, sh: sh::Parameters) {
self.sh = sh;
}
}

View File

@ -6,6 +6,7 @@ use crate::thermostat::max1968::{MAX1968, AdcReadTarget, PwmPinsEnum};
use crate::thermostat::ad7172::{self, FilterType, PostFilter, SingleChODR};
use crate::thermostat::pid_state::{PidState, PidSettings, Parameters as PidParams};
use crate::thermostat::temp_mon::{TempMon, TempStatus, TempMonSettings};
use crate::thermostat::steinhart_hart::Parameters as Sh_Params;
use serde::{Deserialize, Serialize};
use log::debug;
use uom::si::{
@ -39,7 +40,7 @@ pub struct TempAdcFilter{
#[derive(Deserialize, Serialize, Clone, Copy, Debug, Tree)]
pub struct TecSettings {
pub pwr_on: bool,
pub default_pwr_on: bool,
pub center_pt: ElectricPotential,
pub max_v_set: ElectricPotential,
pub max_i_pos_set: ElectricCurrent,
@ -102,7 +103,7 @@ impl TecSettings{
impl Default for TecSettings {
fn default() -> Self {
Self {
pwr_on: false,
default_pwr_on: false,
center_pt: ElectricPotential::new::<volt>(1.5),
max_v_set: ElectricPotential::new::<volt>(5.0),
max_i_pos_set: ElectricCurrent::new::<ampere>(1.0),
@ -123,9 +124,12 @@ pub struct Thermostat {
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct ThermostatSettingsSummary {
pwr_on: bool,
default_pwr_on: bool,
pid_engaged: bool,
temperature_setpoint: ThermodynamicTemperature,
tec_settings: TecSettingSummary,
pid_params: PidParams,
temp_adc_settings: TempAdcFilter,
temp_mon_settings: TempMonSettings,
thermistor_params: ThermistorParams,
}
@ -210,20 +214,22 @@ impl Thermostat{
pub fn power_up(&mut self){
self.max1968.power_up();
self.tec_settings.pwr_on = true;
}
pub fn power_down(&mut self){
self.max1968.power_down();
self.pid_ctrl_ch0.reset_pid_state();
self.set_i(ElectricCurrent::new::<ampere>(0.0));
self.tec_settings.pwr_on = false;
}
fn set_center_pt(&mut self, value: ElectricPotential){
self.tec_settings.center_pt = value;
}
pub fn set_default_pwr_on(&mut self, pwr_on: bool) {
self.tec_settings.default_pwr_on = pwr_on;
}
pub fn set_i(&mut self, i_tec: ElectricCurrent) -> ElectricCurrent {
let voltage = i_tec * 10.0 * R_SENSE + self.tec_settings.center_pt;
let voltage = self.max1968.set_dac(voltage, TecSettings::DAC_OUT_V_MAX);
@ -335,6 +341,7 @@ impl Thermostat{
pub fn get_status_report(&mut self) -> StatusReport {
let (tec_v, tec_i) = self.get_tec_readings();
StatusReport {
pwr_on: self.max1968.is_powered_on(),
pid_engaged: self.get_pid_engaged(),
temp_mon_status: self.temp_mon.get_status(),
temperature: self.pid_ctrl_ch0.get_temperature(),
@ -367,6 +374,17 @@ impl Thermostat{
}
fn apply_steinhart_hart(&mut self, sh: ThermistorParams) {
self.pid_ctrl_ch0.apply_sh(
Sh_Params {
t0: ThermodynamicTemperature::new::<degree_celsius>(sh.t0),
r0: sh.r0,
b: sh.b,
}
)
}
fn get_tec_settings(&mut self) -> TecSettingSummary {
TecSettingSummary {
i_set: TecSettingsSummaryField { value: self.tec_settings.i_set, max: TecSettings::MAX_I_SET },
@ -402,6 +420,11 @@ impl Thermostat{
self.temp_mon.set_setpoint(t);
}
pub fn apply_temp_mon_settings(&mut self, settings: TempMonSettings){
self.temp_mon.set_upper_limit(ThermodynamicTemperature::new::<degree_celsius>(settings.upper_limit));
self.temp_mon.set_lower_limit(ThermodynamicTemperature::new::<degree_celsius>(settings.lower_limit));
}
pub fn set_temp_mon_upper_limit(&mut self, t: ThermodynamicTemperature) {
self.temp_mon.set_upper_limit(t);
}
@ -422,8 +445,8 @@ impl Thermostat{
self.ad7172.set_sinc5_sinc1_with_50hz_60hz_rejection(index, odr).unwrap();
}
pub fn set_temp_adc_sinc3_fine_filter(&mut self, index: u8, rate: f64) {
self.ad7172.set_sinc3_fine_filter(index, rate as f32).unwrap();
pub fn set_temp_adc_sinc3_fine_filter(&mut self, index: u8, rate: f32) {
self.ad7172.set_sinc3_fine_filter(index, rate).unwrap();
}
pub fn clear_temp_mon_alarm(&mut self) {
@ -435,10 +458,32 @@ impl Thermostat{
}
pub fn get_settings_summary(&mut self) -> ThermostatSettingsSummary {
let temp_adc_filter_type: FilterType;
let update_rate: f32;
match self.ad7172.get_filter_type_and_rate(0) {
Ok((filter_type, rate)) => {
temp_adc_filter_type = filter_type;
update_rate = rate;
}
Err(_) => {
panic!("Cannot read ADC filter type and rate");
}
}
ThermostatSettingsSummary {
pwr_on: self.tec_settings.pwr_on,
default_pwr_on: self.tec_settings.default_pwr_on,
pid_engaged: self.get_pid_engaged(),
temperature_setpoint: self.pid_ctrl_ch0.get_pid_setpoint(),
tec_settings: self.get_tec_settings(),
pid_params: self.get_pid_settings(),
temp_adc_settings: TempAdcFilter{
filter_type : temp_adc_filter_type,
sinc5sinc1odr : None,
sinc3odr : None,
sinc5sinc1postfilter : None,
sinc3fineodr : None,
rate : Some(update_rate),
},
temp_mon_settings: self.get_temp_mon_settings(),
thermistor_params: self.get_steinhart_hart(),
}
@ -447,6 +492,7 @@ impl Thermostat{
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct StatusReport {
pwr_on: bool,
pid_engaged: bool,
temp_mon_status: TempStatus,
temperature: Option<ThermodynamicTemperature>,