forked from M-Labs/thermostat
Compare commits
7 Commits
7e6013b07b
...
3a8e0bddec
Author | SHA1 | Date | |
---|---|---|---|
3a8e0bddec | |||
fb4333e177 | |||
45412cd504 | |||
b44892a7f5 | |||
db14bd8e5c | |||
db0403206e | |||
f0ebdeb337 |
@ -268,7 +268,7 @@ with the following keys.
|
|||||||
| `i_measured` | Amperes | Measured current passing through TEC |
|
| `i_measured` | Amperes | Measured current passing through TEC |
|
||||||
| `v_measured` | Volts | Measured voltage across TEC |
|
| `v_measured` | Volts | Measured voltage across TEC |
|
||||||
|
|
||||||
Note: With Thermostat v2 and below, the voltage and current readouts `i_tec` and `tec_i` are noisy without the hardware fix shown in [this PR][https://git.m-labs.hk/M-Labs/thermostat/pulls/105].
|
Note: With Thermostat v2 and below, the voltage and current readouts `i_tec` and `tec_i` are disabled and null due to faulty hardware that introduces a lot of noise in the signal.
|
||||||
|
|
||||||
## PID Tuning
|
## PID Tuning
|
||||||
|
|
||||||
|
40
flake.lock
generated
40
flake.lock
generated
@ -1,5 +1,21 @@
|
|||||||
{
|
{
|
||||||
"nodes": {
|
"nodes": {
|
||||||
|
"mozilla-overlay": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1690536331,
|
||||||
|
"narHash": "sha256-aRIf2FB2GTdfF7gl13WyETmiV/J7EhBGkSWXfZvlxcA=",
|
||||||
|
"owner": "mozilla",
|
||||||
|
"repo": "nixpkgs-mozilla",
|
||||||
|
"rev": "db89c8707edcffefcd8e738459d511543a339ff5",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "mozilla",
|
||||||
|
"repo": "nixpkgs-mozilla",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1691421349,
|
"lastModified": 1691421349,
|
||||||
@ -18,28 +34,8 @@
|
|||||||
},
|
},
|
||||||
"root": {
|
"root": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"nixpkgs": "nixpkgs",
|
"mozilla-overlay": "mozilla-overlay",
|
||||||
"rust-overlay": "rust-overlay"
|
"nixpkgs": "nixpkgs"
|
||||||
}
|
|
||||||
},
|
|
||||||
"rust-overlay": {
|
|
||||||
"inputs": {
|
|
||||||
"nixpkgs": [
|
|
||||||
"nixpkgs"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1719281921,
|
|
||||||
"narHash": "sha256-LIBMfhM9pMOlEvBI757GOK5l0R58SRi6YpwfYMbf4yc=",
|
|
||||||
"owner": "oxalica",
|
|
||||||
"repo": "rust-overlay",
|
|
||||||
"rev": "b6032d3a404d8a52ecfc8571ff0c26dfbe221d07",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "oxalica",
|
|
||||||
"repo": "rust-overlay",
|
|
||||||
"type": "github"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
39
flake.nix
39
flake.nix
@ -2,24 +2,31 @@
|
|||||||
description = "Firmware for the Sinara 8451 Thermostat";
|
description = "Firmware for the Sinara 8451 Thermostat";
|
||||||
|
|
||||||
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-23.05;
|
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-23.05;
|
||||||
inputs.rust-overlay = {
|
inputs.mozilla-overlay = { url = github:mozilla/nixpkgs-mozilla; flake = false; };
|
||||||
url = "github:oxalica/rust-overlay";
|
|
||||||
inputs.nixpkgs.follows = "nixpkgs";
|
|
||||||
};
|
|
||||||
|
|
||||||
outputs = { self, nixpkgs, rust-overlay }:
|
outputs = { self, nixpkgs, mozilla-overlay }:
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import rust-overlay) ]; };
|
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import mozilla-overlay) ]; };
|
||||||
|
rustManifest = pkgs.fetchurl {
|
||||||
rust = pkgs.rust-bin.stable."1.66.0".default.override {
|
url = "https://static.rust-lang.org/dist/2022-12-15/channel-rust-stable.toml";
|
||||||
extensions = [ "rust-src" ];
|
hash = "sha256-S7epLlflwt0d1GZP44u5Xosgf6dRrmr8xxC+Ml2Pq7c=";
|
||||||
targets = [ "thumbv7em-none-eabihf" ];
|
|
||||||
};
|
};
|
||||||
rustPlatform = pkgs.makeRustPlatform {
|
|
||||||
|
targets = [
|
||||||
|
"thumbv7em-none-eabihf"
|
||||||
|
];
|
||||||
|
rustChannelOfTargets = _channel: _date: targets:
|
||||||
|
(pkgs.lib.rustLib.fromManifestFile rustManifest {
|
||||||
|
inherit (pkgs) stdenv lib fetchurl patchelf;
|
||||||
|
}).rust.override {
|
||||||
|
inherit targets;
|
||||||
|
extensions = ["rust-src"];
|
||||||
|
};
|
||||||
|
rust = rustChannelOfTargets "stable" null targets;
|
||||||
|
rustPlatform = pkgs.recurseIntoAttrs (pkgs.makeRustPlatform {
|
||||||
rustc = rust;
|
rustc = rust;
|
||||||
cargo = rust;
|
cargo = rust;
|
||||||
};
|
});
|
||||||
|
|
||||||
thermostat = rustPlatform.buildRustPackage {
|
thermostat = rustPlatform.buildRustPackage {
|
||||||
name = "thermostat";
|
name = "thermostat";
|
||||||
version = "0.0.0";
|
version = "0.0.0";
|
||||||
@ -51,21 +58,21 @@
|
|||||||
in {
|
in {
|
||||||
packages.x86_64-linux = {
|
packages.x86_64-linux = {
|
||||||
inherit thermostat;
|
inherit thermostat;
|
||||||
default = thermostat;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
hydraJobs = {
|
hydraJobs = {
|
||||||
inherit thermostat;
|
inherit thermostat;
|
||||||
};
|
};
|
||||||
|
|
||||||
devShells.x86_64-linux.default = pkgs.mkShellNoCC {
|
devShell.x86_64-linux = pkgs.mkShell {
|
||||||
name = "thermostat-dev-shell";
|
name = "thermostat-dev-shell";
|
||||||
packages = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
rust llvm
|
rust llvm
|
||||||
openocd dfu-util
|
openocd dfu-util
|
||||||
] ++ (with python3Packages; [
|
] ++ (with python3Packages; [
|
||||||
numpy matplotlib
|
numpy matplotlib
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
defaultPackage.x86_64-linux = thermostat;
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -1,12 +1,9 @@
|
|||||||
use crate::{ad7172, command_parser::CenterPoint, pid, steinhart_hart as sh};
|
use crate::{ad7172, command_parser::CenterPoint, pid, steinhart_hart as sh};
|
||||||
use smoltcp::time::{Duration, Instant};
|
use smoltcp::time::{Duration, Instant};
|
||||||
use uom::si::{
|
use uom::si::{
|
||||||
electric_current::ampere,
|
|
||||||
electric_potential::volt,
|
electric_potential::volt,
|
||||||
electrical_resistance::ohm,
|
electrical_resistance::ohm,
|
||||||
f64::{
|
f64::{ElectricPotential, ElectricalResistance, ThermodynamicTemperature, Time},
|
||||||
ElectricCurrent, ElectricPotential, ElectricalResistance, ThermodynamicTemperature, Time,
|
|
||||||
},
|
|
||||||
thermodynamic_temperature::degree_celsius,
|
thermodynamic_temperature::degree_celsius,
|
||||||
time::millisecond,
|
time::millisecond,
|
||||||
};
|
};
|
||||||
@ -22,7 +19,6 @@ pub struct ChannelState {
|
|||||||
/// i_set 0A center point
|
/// i_set 0A center point
|
||||||
pub center: CenterPoint,
|
pub center: CenterPoint,
|
||||||
pub dac_value: ElectricPotential,
|
pub dac_value: ElectricPotential,
|
||||||
pub i_set: ElectricCurrent,
|
|
||||||
pub pid_engaged: bool,
|
pub pid_engaged: bool,
|
||||||
pub pid: pid::Controller,
|
pub pid: pid::Controller,
|
||||||
pub sh: sh::Parameters,
|
pub sh: sh::Parameters,
|
||||||
@ -38,7 +34,6 @@ impl ChannelState {
|
|||||||
adc_interval: Duration::from_millis(100),
|
adc_interval: Duration::from_millis(100),
|
||||||
center: CenterPoint::Vref,
|
center: CenterPoint::Vref,
|
||||||
dac_value: ElectricPotential::new::<volt>(0.0),
|
dac_value: ElectricPotential::new::<volt>(0.0),
|
||||||
i_set: ElectricCurrent::new::<ampere>(0.0),
|
|
||||||
pid_engaged: false,
|
pid_engaged: false,
|
||||||
pid: pid::Controller::new(pid::Parameters::default()),
|
pid: pid::Controller::new(pid::Parameters::default()),
|
||||||
sh: sh::Parameters::default(),
|
sh: sh::Parameters::default(),
|
||||||
|
277
src/channels.rs
277
src/channels.rs
@ -4,12 +4,10 @@ use crate::{
|
|||||||
channel_state::ChannelState,
|
channel_state::ChannelState,
|
||||||
command_handler::JsonBuffer,
|
command_handler::JsonBuffer,
|
||||||
command_parser::{CenterPoint, PwmPin},
|
command_parser::{CenterPoint, PwmPin},
|
||||||
pins::{self, Channel0VRef, Channel1VRef},
|
hw_rev, pins, steinhart_hart,
|
||||||
steinhart_hart,
|
|
||||||
};
|
};
|
||||||
use core::{cmp::max_by, marker::PhantomData};
|
use core::cmp::max_by;
|
||||||
use heapless::{consts::U2, Vec};
|
use heapless::{consts::U2, Vec};
|
||||||
use num_traits::Zero;
|
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Serialize, Serializer};
|
||||||
use smoltcp::time::Instant;
|
use smoltcp::time::Instant;
|
||||||
use stm32f4xx_hal::hal;
|
use stm32f4xx_hal::hal;
|
||||||
@ -22,46 +20,24 @@ use uom::si::{
|
|||||||
thermodynamic_temperature::degree_celsius,
|
thermodynamic_temperature::degree_celsius,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub enum PinsAdcReadTarget {
|
|
||||||
VREF,
|
|
||||||
DacVfb,
|
|
||||||
ITec,
|
|
||||||
VTec,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const CHANNELS: usize = 2;
|
pub const CHANNELS: usize = 2;
|
||||||
const R_SENSE: f64 = 0.05;
|
const R_SENSE: f64 = 0.05;
|
||||||
|
|
||||||
// From design specs
|
|
||||||
pub const MAX_I: ElectricCurrent = ElectricCurrent {
|
|
||||||
dimension: PhantomData,
|
|
||||||
units: PhantomData,
|
|
||||||
value: 2.0,
|
|
||||||
};
|
|
||||||
pub const MAX_TEC_V: ElectricPotential = ElectricPotential {
|
|
||||||
dimension: PhantomData,
|
|
||||||
units: PhantomData,
|
|
||||||
value: 4.0,
|
|
||||||
};
|
|
||||||
|
|
||||||
// DAC chip outputs 0-5v, which is then passed through a resistor dividor to provide 0-3v range
|
// DAC chip outputs 0-5v, which is then passed through a resistor dividor to provide 0-3v range
|
||||||
const DAC_OUT_V_MAX: ElectricPotential = ElectricPotential {
|
const DAC_OUT_V_MAX: f64 = 3.0;
|
||||||
dimension: PhantomData,
|
|
||||||
units: PhantomData,
|
|
||||||
value: 3.0,
|
|
||||||
};
|
|
||||||
// TODO: -pub
|
// TODO: -pub
|
||||||
pub struct Channels {
|
pub struct Channels<'a> {
|
||||||
channel0: Channel<Channel0>,
|
channel0: Channel<Channel0>,
|
||||||
channel1: Channel<Channel1>,
|
channel1: Channel<Channel1>,
|
||||||
pub adc: ad7172::Adc<pins::AdcSpi, pins::AdcNss>,
|
pub adc: ad7172::Adc<pins::AdcSpi, pins::AdcNss>,
|
||||||
/// stm32f4 integrated adc
|
/// stm32f4 integrated adc
|
||||||
pins_adc: pins::PinsAdc,
|
pins_adc: pins::PinsAdc,
|
||||||
pwm: pins::PwmPins,
|
pwm: pins::PwmPins,
|
||||||
|
hwrev: &'a hw_rev::HWRev,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Channels {
|
impl<'a> Channels<'a> {
|
||||||
pub fn new(pins: pins::Pins) -> Self {
|
pub fn new(pins: pins::Pins, hwrev: &'a hw_rev::HWRev) -> Self {
|
||||||
let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap();
|
let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap();
|
||||||
// Feature not used
|
// Feature not used
|
||||||
adc.set_sync_enable(false).unwrap();
|
adc.set_sync_enable(false).unwrap();
|
||||||
@ -85,6 +61,7 @@ impl Channels {
|
|||||||
adc,
|
adc,
|
||||||
pins_adc,
|
pins_adc,
|
||||||
pwm,
|
pwm,
|
||||||
|
hwrev,
|
||||||
};
|
};
|
||||||
for channel in 0..CHANNELS {
|
for channel in 0..CHANNELS {
|
||||||
channels.calibrate_dac_value(channel);
|
channels.calibrate_dac_value(channel);
|
||||||
@ -126,7 +103,7 @@ impl Channels {
|
|||||||
/// calculate the TEC i_set centerpoint
|
/// calculate the TEC i_set centerpoint
|
||||||
pub fn get_center(&mut self, channel: usize) -> ElectricPotential {
|
pub fn get_center(&mut self, channel: usize) -> ElectricPotential {
|
||||||
match self.channel_state(channel).center {
|
match self.channel_state(channel).center {
|
||||||
CenterPoint::Vref => self.adc_read(channel, PinsAdcReadTarget::VREF, 8),
|
CenterPoint::Vref => self.read_vref(channel),
|
||||||
CenterPoint::Override(center_point) => {
|
CenterPoint::Override(center_point) => {
|
||||||
ElectricPotential::new::<volt>(center_point.into())
|
ElectricPotential::new::<volt>(center_point.into())
|
||||||
}
|
}
|
||||||
@ -140,13 +117,16 @@ impl Channels {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_i_set(&mut self, channel: usize) -> ElectricCurrent {
|
pub fn get_i_set(&mut self, channel: usize) -> ElectricCurrent {
|
||||||
let i_set = self.channel_state(channel).i_set;
|
let center_point = self.get_center(channel);
|
||||||
i_set
|
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
||||||
|
let voltage = self.get_dac(channel);
|
||||||
|
(voltage - center_point) / (10.0 * r_sense)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// i_set DAC
|
/// i_set DAC
|
||||||
fn set_dac(&mut self, channel: usize, voltage: ElectricPotential) -> ElectricPotential {
|
fn set_dac(&mut self, channel: usize, voltage: ElectricPotential) -> ElectricPotential {
|
||||||
let value = ((voltage / DAC_OUT_V_MAX).get::<ratio>() * (ad5680::MAX_VALUE as f64)) as u32;
|
let value = ((voltage / ElectricPotential::new::<volt>(DAC_OUT_V_MAX)).get::<ratio>()
|
||||||
|
* (ad5680::MAX_VALUE as f64)) as u32;
|
||||||
match channel {
|
match channel {
|
||||||
0 => self.channel0.dac.set(value).unwrap(),
|
0 => self.channel0.dac.set(value).unwrap(),
|
||||||
1 => self.channel1.dac.set(value).unwrap(),
|
1 => self.channel1.dac.set(value).unwrap(),
|
||||||
@ -157,7 +137,6 @@ impl Channels {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_i(&mut self, channel: usize, i_set: ElectricCurrent) -> ElectricCurrent {
|
pub fn set_i(&mut self, channel: usize, i_set: ElectricCurrent) -> ElectricCurrent {
|
||||||
let i_set = i_set.min(MAX_I).max(-MAX_I);
|
|
||||||
let vref_meas = match channel {
|
let vref_meas = match channel {
|
||||||
0 => self.channel0.vref_meas,
|
0 => self.channel0.vref_meas,
|
||||||
1 => self.channel1.vref_meas,
|
1 => self.channel1.vref_meas,
|
||||||
@ -167,108 +146,25 @@ impl Channels {
|
|||||||
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
||||||
let voltage = i_set * 10.0 * r_sense + center_point;
|
let voltage = i_set * 10.0 * r_sense + center_point;
|
||||||
let voltage = self.set_dac(channel, voltage);
|
let voltage = self.set_dac(channel, voltage);
|
||||||
let i_set = (voltage - center_point) / (10.0 * r_sense);
|
(voltage - center_point) / (10.0 * r_sense)
|
||||||
self.channel_state(channel).i_set = i_set;
|
|
||||||
i_set
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// AN4073: ADC Reading Dispersion can be reduced through Averaging
|
pub fn read_dac_feedback(&mut self, channel: usize) -> ElectricPotential {
|
||||||
pub fn adc_read(
|
|
||||||
&mut self,
|
|
||||||
channel: usize,
|
|
||||||
adc_read_target: PinsAdcReadTarget,
|
|
||||||
avg_pt: u16,
|
|
||||||
) -> ElectricPotential {
|
|
||||||
let mut sample: u32 = 0;
|
|
||||||
match channel {
|
match channel {
|
||||||
0 => {
|
0 => {
|
||||||
sample = match adc_read_target {
|
let sample = self.pins_adc.convert(
|
||||||
PinsAdcReadTarget::VREF => match &self.channel0.vref_pin {
|
|
||||||
Channel0VRef::Analog(vref_pin) => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self.pins_adc.convert(
|
|
||||||
vref_pin,
|
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
|
||||||
) as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
Channel0VRef::Disabled(_) => 2048_u32,
|
|
||||||
},
|
|
||||||
PinsAdcReadTarget::DacVfb => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self.pins_adc.convert(
|
|
||||||
&self.channel0.dac_feedback_pin,
|
&self.channel0.dac_feedback_pin,
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
||||||
) as u32;
|
);
|
||||||
}
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::ITec => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self.pins_adc.convert(
|
|
||||||
&self.channel0.itec_pin,
|
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
|
||||||
) as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::VTec => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self.pins_adc.convert(
|
|
||||||
&self.channel0.tec_u_meas_pin,
|
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
|
||||||
) as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mv = self.pins_adc.sample_to_millivolts(sample as u16);
|
|
||||||
ElectricPotential::new::<millivolt>(mv as f64)
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
}
|
}
|
||||||
1 => {
|
1 => {
|
||||||
sample = match adc_read_target {
|
let sample = self.pins_adc.convert(
|
||||||
PinsAdcReadTarget::VREF => match &self.channel1.vref_pin {
|
|
||||||
Channel1VRef::Analog(vref_pin) => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self.pins_adc.convert(
|
|
||||||
vref_pin,
|
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
|
||||||
) as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
Channel1VRef::Disabled(_) => 2048_u32,
|
|
||||||
},
|
|
||||||
PinsAdcReadTarget::DacVfb => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self.pins_adc.convert(
|
|
||||||
&self.channel1.dac_feedback_pin,
|
&self.channel1.dac_feedback_pin,
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
||||||
) as u32;
|
);
|
||||||
}
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::ITec => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self.pins_adc.convert(
|
|
||||||
&self.channel1.itec_pin,
|
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
|
||||||
) as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::VTec => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self.pins_adc.convert(
|
|
||||||
&self.channel1.tec_u_meas_pin,
|
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
|
||||||
) as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mv = self.pins_adc.sample_to_millivolts(sample as u16);
|
|
||||||
ElectricPotential::new::<millivolt>(mv as f64)
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
@ -280,9 +176,9 @@ impl Channels {
|
|||||||
channel: usize,
|
channel: usize,
|
||||||
tolerance: ElectricPotential,
|
tolerance: ElectricPotential,
|
||||||
) -> ElectricPotential {
|
) -> ElectricPotential {
|
||||||
let mut prev = self.adc_read(channel, PinsAdcReadTarget::DacVfb, 1);
|
let mut prev = self.read_dac_feedback(channel);
|
||||||
loop {
|
loop {
|
||||||
let current = self.adc_read(channel, PinsAdcReadTarget::DacVfb, 1);
|
let current = self.read_dac_feedback(channel);
|
||||||
if (current - prev).abs() < tolerance {
|
if (current - prev).abs() < tolerance {
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
@ -290,6 +186,73 @@ impl Channels {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_itec(&mut self, channel: usize) -> ElectricPotential {
|
||||||
|
match channel {
|
||||||
|
0 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel0.itec_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel1.itec_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// should be 1.5V
|
||||||
|
pub fn read_vref(&mut self, channel: usize) -> ElectricPotential {
|
||||||
|
match channel {
|
||||||
|
0 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel0.vref_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel1.vref_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_tec_u_meas(&mut self, channel: usize) -> ElectricPotential {
|
||||||
|
match channel {
|
||||||
|
0 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel0.tec_u_meas_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel1.tec_u_meas_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Calibrates the DAC output to match vref of the MAX driver to reduce zero-current offset of the MAX driver output.
|
/// Calibrates the DAC output to match vref of the MAX driver to reduce zero-current offset of the MAX driver output.
|
||||||
///
|
///
|
||||||
/// The thermostat DAC applies a control voltage signal to the CTLI pin of MAX driver chip to control its output current.
|
/// The thermostat DAC applies a control voltage signal to the CTLI pin of MAX driver chip to control its output current.
|
||||||
@ -338,7 +301,8 @@ impl Channels {
|
|||||||
best_error = error;
|
best_error = error;
|
||||||
start_value = prev_value;
|
start_value = prev_value;
|
||||||
|
|
||||||
let vref = (value as f64 / ad5680::MAX_VALUE as f64) * DAC_OUT_V_MAX;
|
let vref = (value as f64 / ad5680::MAX_VALUE as f64)
|
||||||
|
* ElectricPotential::new::<volt>(DAC_OUT_V_MAX);
|
||||||
match channel {
|
match channel {
|
||||||
0 => self.channel0.vref_meas = vref,
|
0 => self.channel0.vref_meas = vref,
|
||||||
1 => self.channel1.vref_meas = vref,
|
1 => self.channel1.vref_meas = vref,
|
||||||
@ -390,35 +354,32 @@ impl Channels {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_max_v(&mut self, channel: usize) -> (ElectricPotential, ElectricPotential) {
|
pub fn get_max_v(&mut self, channel: usize) -> ElectricPotential {
|
||||||
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
||||||
let duty = self.get_pwm(channel, PwmPin::MaxV);
|
let duty = self.get_pwm(channel, PwmPin::MaxV);
|
||||||
(duty * max, MAX_TEC_V)
|
duty * max
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_max_i_pos(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
pub fn get_max_i_pos(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||||
let duty = self.get_pwm(channel, PwmPin::MaxIPos);
|
let duty = self.get_pwm(channel, PwmPin::MaxIPos);
|
||||||
(duty * max, MAX_I)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_max_i_neg(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
pub fn get_max_i_neg(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||||
let duty = self.get_pwm(channel, PwmPin::MaxINeg);
|
let duty = self.get_pwm(channel, PwmPin::MaxINeg);
|
||||||
(duty * max, MAX_I)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Measure current passing through TEC
|
// Measure current passing through TEC
|
||||||
pub fn get_i_measured(&mut self, channel: usize) -> ElectricCurrent {
|
pub fn get_i_measured(&mut self, channel: usize) -> ElectricCurrent {
|
||||||
(self.adc_read(channel, PinsAdcReadTarget::ITec, 16)
|
(self.read_itec(channel) - self.read_vref(channel)) / ElectricalResistance::new::<ohm>(0.4)
|
||||||
- self.adc_read(channel, PinsAdcReadTarget::VREF, 16))
|
|
||||||
/ ElectricalResistance::new::<ohm>(0.4)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Measure voltage across TEC
|
// Measure voltage across TEC
|
||||||
pub fn get_v_measured(&mut self, channel: usize) -> ElectricPotential {
|
pub fn get_v_measured(&mut self, channel: usize) -> ElectricPotential {
|
||||||
(self.adc_read(channel, PinsAdcReadTarget::VTec, 16) - ElectricPotential::new::<volt>(1.5))
|
(self.read_tec_u_meas(channel) - ElectricPotential::new::<volt>(1.5)) * 4.0
|
||||||
* 4.0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_pwm(&mut self, channel: usize, pin: PwmPin, duty: f64) -> f64 {
|
fn set_pwm(&mut self, channel: usize, pin: PwmPin, duty: f64) -> f64 {
|
||||||
@ -446,7 +407,7 @@ impl Channels {
|
|||||||
max_v: ElectricPotential,
|
max_v: ElectricPotential,
|
||||||
) -> (ElectricPotential, ElectricPotential) {
|
) -> (ElectricPotential, ElectricPotential) {
|
||||||
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
||||||
let duty = (max_v.min(MAX_TEC_V).max(ElectricPotential::zero()) / max).get::<ratio>();
|
let duty = (max_v / max).get::<ratio>();
|
||||||
let duty = self.set_pwm(channel, PwmPin::MaxV, duty);
|
let duty = self.set_pwm(channel, PwmPin::MaxV, duty);
|
||||||
(duty * max, max)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
@ -457,7 +418,7 @@ impl Channels {
|
|||||||
max_i_pos: ElectricCurrent,
|
max_i_pos: ElectricCurrent,
|
||||||
) -> (ElectricCurrent, ElectricCurrent) {
|
) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||||
let duty = (max_i_pos.min(MAX_I).max(ElectricCurrent::zero()) / max).get::<ratio>();
|
let duty = (max_i_pos / max).get::<ratio>();
|
||||||
let duty = self.set_pwm(channel, PwmPin::MaxIPos, duty);
|
let duty = self.set_pwm(channel, PwmPin::MaxIPos, duty);
|
||||||
(duty * max, max)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
@ -468,17 +429,13 @@ impl Channels {
|
|||||||
max_i_neg: ElectricCurrent,
|
max_i_neg: ElectricCurrent,
|
||||||
) -> (ElectricCurrent, ElectricCurrent) {
|
) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||||
let duty = (max_i_neg.min(MAX_I).max(ElectricCurrent::zero()) / max).get::<ratio>();
|
let duty = (max_i_neg / max).get::<ratio>();
|
||||||
let duty = self.set_pwm(channel, PwmPin::MaxINeg, duty);
|
let duty = self.set_pwm(channel, PwmPin::MaxINeg, duty);
|
||||||
(duty * max, max)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn report(&mut self, channel: usize) -> Report {
|
fn report(&mut self, channel: usize) -> Report {
|
||||||
let i_set = self.get_i_set(channel);
|
|
||||||
let i_measured = self.get_i_measured(channel);
|
|
||||||
let v_measured = self.get_v_measured(channel);
|
|
||||||
let state = self.channel_state(channel);
|
let state = self.channel_state(channel);
|
||||||
let pid_output = ElectricCurrent::new::<ampere>(state.pid.y1);
|
|
||||||
Report {
|
Report {
|
||||||
channel,
|
channel,
|
||||||
time: state.get_adc_time(),
|
time: state.get_adc_time(),
|
||||||
@ -488,10 +445,14 @@ impl Channels {
|
|||||||
.get_temperature()
|
.get_temperature()
|
||||||
.map(|temperature| temperature.get::<degree_celsius>()),
|
.map(|temperature| temperature.get::<degree_celsius>()),
|
||||||
pid_engaged: state.pid_engaged,
|
pid_engaged: state.pid_engaged,
|
||||||
pid_output,
|
pid_output: ElectricCurrent::new::<ampere>(state.pid.y1),
|
||||||
i_set,
|
i_set: self.get_i_set(channel),
|
||||||
i_measured,
|
i_measured: if self.hwrev.major > 2 {
|
||||||
v_measured,
|
Some(self.get_i_measured(channel))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
v_measured: self.get_v_measured(channel),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -524,8 +485,8 @@ impl Channels {
|
|||||||
PwmSummary {
|
PwmSummary {
|
||||||
channel,
|
channel,
|
||||||
center: CenterPointJson(self.channel_state(channel).center.clone()),
|
center: CenterPointJson(self.channel_state(channel).center.clone()),
|
||||||
i_set: (self.get_i_set(channel), MAX_I).into(),
|
i_set: (self.get_i_set(channel), ElectricCurrent::new::<ampere>(3.0)).into(),
|
||||||
max_v: self.get_max_v(channel).into(),
|
max_v: (self.get_max_v(channel), ElectricPotential::new::<volt>(5.0)).into(),
|
||||||
max_i_pos: self.get_max_i_pos(channel).into(),
|
max_i_pos: self.get_max_i_pos(channel).into(),
|
||||||
max_i_neg: self.get_max_i_neg(channel).into(),
|
max_i_neg: self.get_max_i_neg(channel).into(),
|
||||||
}
|
}
|
||||||
@ -572,9 +533,11 @@ impl Channels {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn max_abs_i_measured(&mut self) -> ElectricCurrent {
|
pub fn max_abs_i_measured(&mut self) -> ElectricCurrent {
|
||||||
max_by(self.get_i_measured(0).abs(), self.get_i_measured(1).abs(), |a, b| {
|
max_by(
|
||||||
a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal)
|
self.get_i_measured(0).abs(),
|
||||||
})
|
self.get_i_measured(1).abs(),
|
||||||
|
|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -588,7 +551,7 @@ pub struct Report {
|
|||||||
pid_engaged: bool,
|
pid_engaged: bool,
|
||||||
pid_output: ElectricCurrent,
|
pid_output: ElectricCurrent,
|
||||||
i_set: ElectricCurrent,
|
i_set: ElectricCurrent,
|
||||||
i_measured: ElectricCurrent,
|
i_measured: Option<ElectricCurrent>,
|
||||||
v_measured: ElectricPotential,
|
v_measured: ElectricPotential,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,9 +35,9 @@ pub enum Handler {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
ReportError,
|
Report,
|
||||||
PostFilterRateError,
|
PostFilterRate,
|
||||||
FlashError,
|
Flash,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type JsonBuffer = Vec<u8, U1024>;
|
pub type JsonBuffer = Vec<u8, U1024>;
|
||||||
@ -87,7 +87,7 @@ impl Handler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to serialize report: {:?}", e);
|
error!("unable to serialize report: {:?}", e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
return Err(Error::ReportError);
|
return Err(Error::Report);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
@ -101,7 +101,7 @@ impl Handler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to serialize pid summary: {:?}", e);
|
error!("unable to serialize pid summary: {:?}", e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
return Err(Error::ReportError);
|
return Err(Error::Report);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
@ -115,7 +115,7 @@ impl Handler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to serialize pwm summary: {:?}", e);
|
error!("unable to serialize pwm summary: {:?}", e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
return Err(Error::ReportError);
|
return Err(Error::Report);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
@ -132,7 +132,7 @@ impl Handler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to serialize steinhart-hart summaries: {:?}", e);
|
error!("unable to serialize steinhart-hart summaries: {:?}", e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
return Err(Error::ReportError);
|
return Err(Error::Report);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
@ -146,7 +146,7 @@ impl Handler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to serialize postfilter summary: {:?}", e);
|
error!("unable to serialize postfilter summary: {:?}", e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
return Err(Error::ReportError);
|
return Err(Error::Report);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
@ -207,11 +207,11 @@ impl Handler {
|
|||||||
channel: usize,
|
channel: usize,
|
||||||
center: CenterPoint,
|
center: CenterPoint,
|
||||||
) -> Result<Handler, Error> {
|
) -> Result<Handler, Error> {
|
||||||
let i_set = channels.get_i_set(channel);
|
let i_tec = channels.get_i_set(channel);
|
||||||
let state = channels.channel_state(channel);
|
let state = channels.channel_state(channel);
|
||||||
state.center = center;
|
state.center = center;
|
||||||
if !state.pid_engaged {
|
if !state.pid_engaged {
|
||||||
channels.set_i(channel, i_set);
|
channels.set_i(channel, i_tec);
|
||||||
}
|
}
|
||||||
send_line(socket, b"{}");
|
send_line(socket, b"{}");
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
@ -287,7 +287,7 @@ impl Handler {
|
|||||||
socket,
|
socket,
|
||||||
b"{{\"error\": \"unable to choose postfilter rate\"}}",
|
b"{{\"error\": \"unable to choose postfilter rate\"}}",
|
||||||
);
|
);
|
||||||
return Err(Error::PostFilterRateError);
|
return Err(Error::PostFilterRate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
@ -313,7 +313,7 @@ impl Handler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to load config from flash: {:?}", e);
|
error!("unable to load config from flash: {:?}", e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
return Err(Error::FlashError);
|
return Err(Error::Flash);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -338,7 +338,7 @@ impl Handler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to save channel {} config to flash: {:?}", c, e);
|
error!("unable to save channel {} config to flash: {:?}", c, e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
return Err(Error::FlashError);
|
return Err(Error::Flash);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -409,7 +409,7 @@ impl Handler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to serialize fan summary: {:?}", e);
|
error!("unable to serialize fan summary: {:?}", e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
Err(Error::ReportError)
|
Err(Error::Report)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -458,7 +458,7 @@ impl Handler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to serialize HWRev summary: {:?}", e);
|
error!("unable to serialize HWRev summary: {:?}", e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
Err(Error::ReportError)
|
Err(Error::Report)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ad7172::PostFilter, channels::Channels, command_parser::CenterPoint, pid, steinhart_hart,
|
ad7172::PostFilter, channels::Channels, command_parser::CenterPoint, pid, steinhart_hart,
|
||||||
};
|
};
|
||||||
use num_traits::Zero;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uom::si::{
|
use uom::si::{
|
||||||
electric_current::ampere,
|
electric_current::ampere,
|
||||||
@ -15,7 +14,6 @@ pub struct ChannelConfig {
|
|||||||
pid: pid::Parameters,
|
pid: pid::Parameters,
|
||||||
pid_target: f32,
|
pid_target: f32,
|
||||||
pid_engaged: bool,
|
pid_engaged: bool,
|
||||||
i_set: ElectricCurrent,
|
|
||||||
sh: steinhart_hart::Parameters,
|
sh: steinhart_hart::Parameters,
|
||||||
pwm: PwmLimits,
|
pwm: PwmLimits,
|
||||||
/// uses variant `PostFilter::Invalid` instead of `None` to save space
|
/// uses variant `PostFilter::Invalid` instead of `None` to save space
|
||||||
@ -33,17 +31,11 @@ impl ChannelConfig {
|
|||||||
.unwrap_or(PostFilter::Invalid);
|
.unwrap_or(PostFilter::Invalid);
|
||||||
|
|
||||||
let state = channels.channel_state(channel);
|
let state = channels.channel_state(channel);
|
||||||
let i_set = if state.pid_engaged {
|
|
||||||
ElectricCurrent::zero()
|
|
||||||
} else {
|
|
||||||
state.i_set
|
|
||||||
};
|
|
||||||
ChannelConfig {
|
ChannelConfig {
|
||||||
center: state.center.clone(),
|
center: state.center.clone(),
|
||||||
pid: state.pid.parameters.clone(),
|
pid: state.pid.parameters.clone(),
|
||||||
pid_target: state.pid.target as f32,
|
pid_target: state.pid.target as f32,
|
||||||
pid_engaged: state.pid_engaged,
|
pid_engaged: state.pid_engaged,
|
||||||
i_set,
|
|
||||||
sh: state.sh.clone(),
|
sh: state.sh.clone(),
|
||||||
pwm,
|
pwm,
|
||||||
adc_postfilter,
|
adc_postfilter,
|
||||||
@ -65,7 +57,6 @@ impl ChannelConfig {
|
|||||||
adc_postfilter => Some(adc_postfilter),
|
adc_postfilter => Some(adc_postfilter),
|
||||||
};
|
};
|
||||||
let _ = channels.adc.set_postfilter(channel as u8, adc_postfilter);
|
let _ = channels.adc.set_postfilter(channel as u8, adc_postfilter);
|
||||||
let _ = channels.set_i(channel, self.i_set);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +69,7 @@ struct PwmLimits {
|
|||||||
|
|
||||||
impl PwmLimits {
|
impl PwmLimits {
|
||||||
pub fn new(channels: &mut Channels, channel: usize) -> Self {
|
pub fn new(channels: &mut Channels, channel: usize) -> Self {
|
||||||
let (max_v, _) = channels.get_max_v(channel);
|
let max_v = channels.get_max_v(channel);
|
||||||
let (max_i_pos, _) = channels.get_max_i_pos(channel);
|
let (max_i_pos, _) = channels.get_max_i_pos(channel);
|
||||||
let (max_i_neg, _) = channels.get_max_i_neg(channel);
|
let (max_i_neg, _) = channels.get_max_i_neg(channel);
|
||||||
PwmLimits {
|
PwmLimits {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::{channels::MAX_I, command_handler::JsonBuffer, hw_rev::HWSettings};
|
use crate::{command_handler::JsonBuffer, hw_rev::HWSettings};
|
||||||
use num_traits::Float;
|
use num_traits::Float;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use stm32f4xx_hal::{
|
use stm32f4xx_hal::{
|
||||||
@ -9,6 +9,9 @@ use uom::si::{electric_current::ampere, f64::ElectricCurrent};
|
|||||||
|
|
||||||
pub type FanPin = PwmChannels<TIM8, pwm::C4>;
|
pub type FanPin = PwmChannels<TIM8, pwm::C4>;
|
||||||
|
|
||||||
|
// as stated in the schematics
|
||||||
|
const MAX_I: f32 = 3.0;
|
||||||
|
|
||||||
const MAX_USER_FAN_PWM: f32 = 100.0;
|
const MAX_USER_FAN_PWM: f32 = 100.0;
|
||||||
const MIN_USER_FAN_PWM: f32 = 1.0;
|
const MIN_USER_FAN_PWM: f32 = 1.0;
|
||||||
|
|
||||||
@ -46,7 +49,7 @@ impl FanCtrl {
|
|||||||
pub fn cycle(&mut self, max_abs_i_measured: ElectricCurrent) {
|
pub fn cycle(&mut self, max_abs_i_measured: ElectricCurrent) {
|
||||||
self.max_abs_i_measured = max_abs_i_measured.get::<ampere>() as f32;
|
self.max_abs_i_measured = max_abs_i_measured.get::<ampere>() as f32;
|
||||||
if self.fan_auto && self.hw_settings.fan_available {
|
if self.fan_auto && self.hw_settings.fan_available {
|
||||||
let scaled_current = self.max_abs_i_measured / MAX_I.get::<ampere>() as f32;
|
let scaled_current = self.max_abs_i_measured / MAX_I;
|
||||||
// do not limit upper bound, as it will be limited in the set_pwm()
|
// do not limit upper bound, as it will be limited in the set_pwm()
|
||||||
let pwm = (MAX_USER_FAN_PWM
|
let pwm = (MAX_USER_FAN_PWM
|
||||||
* (scaled_current * (scaled_current * self.k_a + self.k_b) + self.k_c))
|
* (scaled_current * (scaled_current * self.k_a + self.k_b) + self.k_c))
|
||||||
@ -59,7 +62,7 @@ impl FanCtrl {
|
|||||||
if self.hw_settings.fan_available {
|
if self.hw_settings.fan_available {
|
||||||
let summary = FanSummary {
|
let summary = FanSummary {
|
||||||
fan_pwm: self.get_pwm(),
|
fan_pwm: self.get_pwm(),
|
||||||
abs_max_tec_i: self.max_abs_i_measured,
|
max_abs_i_measured: self.max_abs_i_measured,
|
||||||
auto_mode: self.fan_auto,
|
auto_mode: self.fan_auto,
|
||||||
k_a: self.k_a,
|
k_a: self.k_a,
|
||||||
k_b: self.k_b,
|
k_b: self.k_b,
|
||||||
@ -95,9 +98,7 @@ impl FanCtrl {
|
|||||||
return 0f32;
|
return 0f32;
|
||||||
}
|
}
|
||||||
let fan = self.fan.as_mut().unwrap();
|
let fan = self.fan.as_mut().unwrap();
|
||||||
let fan_pwm = fan_pwm
|
let fan_pwm = fan_pwm.clamp(MIN_USER_FAN_PWM as u32, MAX_USER_FAN_PWM as u32);
|
||||||
.min(MAX_USER_FAN_PWM as u32)
|
|
||||||
.max(MIN_USER_FAN_PWM as u32);
|
|
||||||
let duty = scale_number(
|
let duty = scale_number(
|
||||||
fan_pwm as f32,
|
fan_pwm as f32,
|
||||||
self.hw_settings.min_fan_pwm,
|
self.hw_settings.min_fan_pwm,
|
||||||
@ -156,7 +157,7 @@ fn scale_number(unscaled: f32, to_min: f32, to_max: f32, from_min: f32, from_max
|
|||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct FanSummary {
|
pub struct FanSummary {
|
||||||
fan_pwm: u32,
|
fan_pwm: u32,
|
||||||
abs_max_tec_i: f32,
|
max_abs_i_measured: f32,
|
||||||
auto_mode: bool,
|
auto_mode: bool,
|
||||||
k_a: f32,
|
k_a: f32,
|
||||||
k_b: f32,
|
k_b: f32,
|
||||||
|
@ -147,7 +147,7 @@ fn main() -> ! {
|
|||||||
|
|
||||||
let mut store = flash_store::store(dp.FLASH);
|
let mut store = flash_store::store(dp.FLASH);
|
||||||
|
|
||||||
let mut channels = Channels::new(pins);
|
let mut channels = Channels::new(pins, &hwrev);
|
||||||
for c in 0..CHANNELS {
|
for c in 0..CHANNELS {
|
||||||
match store.read_value::<ChannelConfig>(CHANNEL_CONFIG_KEY[c]) {
|
match store.read_value::<ChannelConfig>(CHANNEL_CONFIG_KEY[c]) {
|
||||||
Ok(Some(config)) => config.apply(&mut channels, c),
|
Ok(Some(config)) => config.apply(&mut channels, c),
|
||||||
@ -280,10 +280,10 @@ fn main() -> ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply new IPv4 address/gateway
|
// Apply new IPv4 address/gateway
|
||||||
new_ipv4_config.take().map(|config| {
|
if let Some(config) = new_ipv4_config.take() {
|
||||||
server.set_ipv4_config(config.clone());
|
server.set_ipv4_config(config.clone());
|
||||||
ipv4_config = config;
|
ipv4_config = config;
|
||||||
});
|
}
|
||||||
|
|
||||||
// Update watchdog
|
// Update watchdog
|
||||||
wd.feed();
|
wd.feed();
|
||||||
|
@ -54,12 +54,14 @@ impl Controller {
|
|||||||
// + x0 * (kp + ki + kd)
|
// + x0 * (kp + ki + kd)
|
||||||
// - x1 * (kp + 2kd)
|
// - x1 * (kp + 2kd)
|
||||||
// + x2 * kd
|
// + x2 * kd
|
||||||
|
// + kp * (u0 - u1)
|
||||||
// y0 = clip(y0', ymin, ymax)
|
// y0 = clip(y0', ymin, ymax)
|
||||||
pub fn update(&mut self, input: f64) -> f64 {
|
pub fn update(&mut self, input: f64) -> f64 {
|
||||||
let mut output: f64 = self.y1 - self.target * f64::from(self.parameters.ki)
|
let mut output: f64 = self.y1 - self.target * f64::from(self.parameters.ki)
|
||||||
+ input * f64::from(self.parameters.kp + self.parameters.ki + self.parameters.kd)
|
+ input * f64::from(self.parameters.kp + self.parameters.ki + self.parameters.kd)
|
||||||
- self.x1 * f64::from(self.parameters.kp + 2.0 * self.parameters.kd)
|
- self.x1 * f64::from(self.parameters.kp + 2.0 * self.parameters.kd)
|
||||||
+ self.x2 * f64::from(self.parameters.kd);
|
+ self.x2 * f64::from(self.parameters.kd)
|
||||||
|
+ f64::from(self.parameters.kp) * (self.target - self.u1);
|
||||||
if output < self.parameters.output_min.into() {
|
if output < self.parameters.output_min.into() {
|
||||||
output = self.parameters.output_min.into();
|
output = self.parameters.output_min.into();
|
||||||
}
|
}
|
||||||
|
42
src/pins.rs
42
src/pins.rs
@ -58,31 +58,21 @@ pub trait ChannelPins {
|
|||||||
type TecUMeasPin;
|
type TecUMeasPin;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Channel0VRef {
|
|
||||||
Analog(PA0<Analog>),
|
|
||||||
Disabled(PA0<Input<Floating>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ChannelPins for Channel0 {
|
impl ChannelPins for Channel0 {
|
||||||
type DacSpi = Dac0Spi;
|
type DacSpi = Dac0Spi;
|
||||||
type DacSync = PE4<Output<PushPull>>;
|
type DacSync = PE4<Output<PushPull>>;
|
||||||
type Shdn = PE10<Output<PushPull>>;
|
type Shdn = PE10<Output<PushPull>>;
|
||||||
type VRefPin = Channel0VRef;
|
type VRefPin = PA0<Analog>;
|
||||||
type ItecPin = PA6<Analog>;
|
type ItecPin = PA6<Analog>;
|
||||||
type DacFeedbackPin = PA4<Analog>;
|
type DacFeedbackPin = PA4<Analog>;
|
||||||
type TecUMeasPin = PC2<Analog>;
|
type TecUMeasPin = PC2<Analog>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Channel1VRef {
|
|
||||||
Analog(PA3<Analog>),
|
|
||||||
Disabled(PA3<Input<Floating>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ChannelPins for Channel1 {
|
impl ChannelPins for Channel1 {
|
||||||
type DacSpi = Dac1Spi;
|
type DacSpi = Dac1Spi;
|
||||||
type DacSync = PF6<Output<PushPull>>;
|
type DacSync = PF6<Output<PushPull>>;
|
||||||
type Shdn = PE15<Output<PushPull>>;
|
type Shdn = PE15<Output<PushPull>>;
|
||||||
type VRefPin = Channel1VRef;
|
type VRefPin = PA3<Analog>;
|
||||||
type ItecPin = PB0<Analog>;
|
type ItecPin = PB0<Analog>;
|
||||||
type DacFeedbackPin = PA5<Analog>;
|
type DacFeedbackPin = PA5<Analog>;
|
||||||
type TecUMeasPin = PC3<Analog>;
|
type TecUMeasPin = PC3<Analog>;
|
||||||
@ -178,22 +168,10 @@ impl Pins {
|
|||||||
clocks, tim1, tim3, gpioc.pc6, gpioc.pc7, gpioe.pe9, gpioe.pe11, gpioe.pe13, gpioe.pe14,
|
clocks, tim1, tim3, gpioc.pc6, gpioc.pc7, gpioe.pe9, gpioe.pe11, gpioe.pe13, gpioe.pe14,
|
||||||
);
|
);
|
||||||
|
|
||||||
let hwrev = HWRev::detect_hw_rev(&HWRevPins {
|
|
||||||
hwrev0: gpiod.pd0,
|
|
||||||
hwrev1: gpiod.pd1,
|
|
||||||
hwrev2: gpiod.pd2,
|
|
||||||
hwrev3: gpiod.pd3,
|
|
||||||
});
|
|
||||||
let hw_settings = hwrev.settings();
|
|
||||||
|
|
||||||
let (dac0_spi, dac0_sync) = Self::setup_dac0(clocks, spi4, gpioe.pe2, gpioe.pe4, gpioe.pe6);
|
let (dac0_spi, dac0_sync) = Self::setup_dac0(clocks, spi4, gpioe.pe2, gpioe.pe4, gpioe.pe6);
|
||||||
let mut shdn0 = gpioe.pe10.into_push_pull_output();
|
let mut shdn0 = gpioe.pe10.into_push_pull_output();
|
||||||
shdn0.set_low();
|
shdn0.set_low();
|
||||||
let vref0_pin = if hwrev.major > 2 {
|
let vref0_pin = gpioa.pa0.into_analog();
|
||||||
Channel0VRef::Analog(gpioa.pa0.into_analog())
|
|
||||||
} else {
|
|
||||||
Channel0VRef::Disabled(gpioa.pa0)
|
|
||||||
};
|
|
||||||
let itec0_pin = gpioa.pa6.into_analog();
|
let itec0_pin = gpioa.pa6.into_analog();
|
||||||
let dac_feedback0_pin = gpioa.pa4.into_analog();
|
let dac_feedback0_pin = gpioa.pa4.into_analog();
|
||||||
let tec_u_meas0_pin = gpioc.pc2.into_analog();
|
let tec_u_meas0_pin = gpioc.pc2.into_analog();
|
||||||
@ -210,11 +188,7 @@ impl Pins {
|
|||||||
let (dac1_spi, dac1_sync) = Self::setup_dac1(clocks, spi5, gpiof.pf7, gpiof.pf6, gpiof.pf9);
|
let (dac1_spi, dac1_sync) = Self::setup_dac1(clocks, spi5, gpiof.pf7, gpiof.pf6, gpiof.pf9);
|
||||||
let mut shdn1 = gpioe.pe15.into_push_pull_output();
|
let mut shdn1 = gpioe.pe15.into_push_pull_output();
|
||||||
shdn1.set_low();
|
shdn1.set_low();
|
||||||
let vref1_pin = if hwrev.major > 2 {
|
let vref1_pin = gpioa.pa3.into_analog();
|
||||||
Channel1VRef::Analog(gpioa.pa3.into_analog())
|
|
||||||
} else {
|
|
||||||
Channel1VRef::Disabled(gpioa.pa3)
|
|
||||||
};
|
|
||||||
let itec1_pin = gpiob.pb0.into_analog();
|
let itec1_pin = gpiob.pb0.into_analog();
|
||||||
let dac_feedback1_pin = gpioa.pa5.into_analog();
|
let dac_feedback1_pin = gpioa.pa5.into_analog();
|
||||||
let tec_u_meas1_pin = gpioc.pc3.into_analog();
|
let tec_u_meas1_pin = gpioc.pc3.into_analog();
|
||||||
@ -237,6 +211,14 @@ impl Pins {
|
|||||||
channel1,
|
channel1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let hwrev = HWRev::detect_hw_rev(&HWRevPins {
|
||||||
|
hwrev0: gpiod.pd0,
|
||||||
|
hwrev1: gpiod.pd1,
|
||||||
|
hwrev2: gpiod.pd2,
|
||||||
|
hwrev3: gpiod.pd3,
|
||||||
|
});
|
||||||
|
let hw_settings = hwrev.settings();
|
||||||
|
|
||||||
let leds = Leds::new(
|
let leds = Leds::new(
|
||||||
gpiod.pd9,
|
gpiod.pd9,
|
||||||
gpiod.pd10.into_push_pull_output(),
|
gpiod.pd10.into_push_pull_output(),
|
||||||
|
@ -103,16 +103,11 @@ impl<'a, 'b, S: Default> Server<'a, 'b, S> {
|
|||||||
fn set_ipv4_address(&mut self, ipv4_address: Ipv4Cidr) {
|
fn set_ipv4_address(&mut self, ipv4_address: Ipv4Cidr) {
|
||||||
self.net.update_ip_addrs(|addrs| {
|
self.net.update_ip_addrs(|addrs| {
|
||||||
for addr in addrs.iter_mut() {
|
for addr in addrs.iter_mut() {
|
||||||
match addr {
|
if let IpCidr::Ipv4(_) = addr {
|
||||||
IpCidr::Ipv4(_) => {
|
|
||||||
*addr = IpCidr::Ipv4(ipv4_address);
|
*addr = IpCidr::Ipv4(ipv4_address);
|
||||||
// done
|
// done
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ => {
|
|
||||||
// skip
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -111,19 +111,13 @@ impl Session {
|
|||||||
for (i, b) in buf.iter().enumerate() {
|
for (i, b) in buf.iter().enumerate() {
|
||||||
buf_bytes = i + 1;
|
buf_bytes = i + 1;
|
||||||
let line = self.reader.feed(*b);
|
let line = self.reader.feed(*b);
|
||||||
match line {
|
if let Some(line) = line {
|
||||||
Some(line) => {
|
|
||||||
let command = Command::parse(line);
|
let command = Command::parse(line);
|
||||||
match command {
|
if let Ok(Command::Reporting(reporting)) = command {
|
||||||
Ok(Command::Reporting(reporting)) => {
|
|
||||||
self.reporting = reporting;
|
self.reporting = reporting;
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
return (buf_bytes, command.into());
|
return (buf_bytes, command.into());
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
(buf_bytes, SessionInput::Nothing)
|
(buf_bytes, SessionInput::Nothing)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user