forked from M-Labs/thermostat
Compare commits
22 Commits
97f8faa646
...
8c3bf9ef3f
Author | SHA1 | Date | |
---|---|---|---|
8c3bf9ef3f | |||
9d89104f50 | |||
136c7a0b52 | |||
5000cae1b1 | |||
78ec77509f | |||
52aa3890c1 | |||
1ae6a6fdd4 | |||
7333d2cea5 | |||
44e9130010 | |||
5b0c6f7018 | |||
1007982b48 | |||
925601f4f5 | |||
8c1cb3117c | |||
1fcfe41a63 | |||
9fce19a418 | |||
00d5feaa8d | |||
09be55e12a | |||
76547be90a | |||
8b975e656e | |||
ae3d8b51d4 | |||
17edae44fb | |||
03b4561142 |
158
Cargo.lock
generated
158
Cargo.lock
generated
@ -62,6 +62,21 @@ version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
|
||||
|
||||
[[package]]
|
||||
name = "built"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38d17f4d6e4dc36d1a02fbedc2753a096848e7c1b0772f7654eab8e2c927dd53"
|
||||
dependencies = [
|
||||
"git2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.3.4"
|
||||
@ -74,6 +89,16 @@ version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
|
||||
dependencies = [
|
||||
"jobserver",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
@ -204,6 +229,15 @@ dependencies = [
|
||||
"void",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456"
|
||||
dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.12.3"
|
||||
@ -232,6 +266,19 @@ dependencies = [
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "git2"
|
||||
version = "0.18.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fbf97ba92db08df386e10c8ede66a2a0369bd277090afd8710e19e38de9ec0cd"
|
||||
dependencies = [
|
||||
"bitflags 2.4.2",
|
||||
"libc",
|
||||
"libgit2-sys",
|
||||
"log",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hash32"
|
||||
version = "0.1.1"
|
||||
@ -254,12 +301,61 @@ dependencies = [
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6"
|
||||
dependencies = [
|
||||
"unicode-bidi",
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jobserver"
|
||||
version = "0.1.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8c37f63953c4c63420ed5fd3d6d398c719489b9f872b9fa683262f8edd363c7d"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.152"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7"
|
||||
|
||||
[[package]]
|
||||
name = "libgit2-sys"
|
||||
version = "0.16.1+1.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2a2bb3680b094add03bb3732ec520ece34da31a8cd2d633d1389d0f0fb60d0c"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
"libz-sys",
|
||||
"pkg-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libm"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a"
|
||||
|
||||
[[package]]
|
||||
name = "libz-sys"
|
||||
version = "1.1.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "037731f5d3aaa87a5675e895b63ddff1a87624bc29f77004ea829809654e48f6"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
"pkg-config",
|
||||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.11"
|
||||
@ -342,6 +438,18 @@ dependencies = [
|
||||
"cortex-m-semihosting",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb"
|
||||
|
||||
[[package]]
|
||||
name = "postcard"
|
||||
version = "0.5.1"
|
||||
@ -469,7 +577,7 @@ version = "0.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3e4a069bef843d170df47e7c0a8bf8d037f217d9f5b325865acc3e466ffe40d3"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 1.2.1",
|
||||
"byteorder",
|
||||
"log",
|
||||
"managed",
|
||||
@ -553,6 +661,7 @@ version = "0.0.0"
|
||||
dependencies = [
|
||||
"bare-metal 1.0.0",
|
||||
"bit_field",
|
||||
"built",
|
||||
"byteorder",
|
||||
"cortex-m 0.6.7",
|
||||
"cortex-m-log",
|
||||
@ -576,12 +685,42 @@ dependencies = [
|
||||
"usbd-serial",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
|
||||
dependencies = [
|
||||
"tinyvec_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec_macros"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-bidi"
|
||||
version = "0.3.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-normalization"
|
||||
version = "0.1.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
|
||||
dependencies = [
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
@ -599,6 +738,17 @@ dependencies = [
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633"
|
||||
dependencies = [
|
||||
"form_urlencoded",
|
||||
"idna",
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "usb-device"
|
||||
version = "0.2.7"
|
||||
@ -622,6 +772,12 @@ version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "876e32dcadfe563a4289e994f7cb391197f362b6315dc45e8ba4aa6f564a4b3c"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.2"
|
||||
|
@ -38,6 +38,9 @@ heapless = "0.5"
|
||||
serde-json-core = "0.1"
|
||||
sfkv = "0.1"
|
||||
|
||||
[build-dependencies]
|
||||
built = { version = "0.7", features = ["git2"] }
|
||||
|
||||
[features]
|
||||
semihosting = ["panic-semihosting", "cortex-m-log/semihosting"]
|
||||
|
||||
|
17
README.md
17
README.md
@ -45,7 +45,7 @@ There are several options for flashing Thermostat. DFU requires only a micro-USB
|
||||
|
||||
### dfu-util on Linux
|
||||
* Install the DFU USB tool (dfu-util).
|
||||
* Convert firmware from ELF to BIN: `arm-none-eabi-objcopy -O binary thermostat thermostat.bin` (you can skip this step if using the BIN from Hydra)
|
||||
* Convert firmware from ELF to BIN: `llvm-objcopy -O binary target/thumbv7em-none-eabihf/release/thermostat thermostat.bin` (you can skip this step if using the BIN from Hydra)
|
||||
* Connect to the Micro USB connector to Thermostat below the RJ45.
|
||||
* Add jumper to Thermostat v2.0 across 2-pin jumper adjacent to JTAG connector.
|
||||
* Cycle board power to put it in DFU update mode
|
||||
@ -271,18 +271,19 @@ with the following keys.
|
||||
| `tec_u_meas` | Volts | Measurement of the voltage across the TEC |
|
||||
| `pid_output` | Amperes | PID control output |
|
||||
|
||||
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].
|
||||
|
||||
## PID Tuning
|
||||
|
||||
The thermostat implements a PID control loop for each of the TEC channels, more details on setting up the PID control loop can be found [here](./doc/PID%20tuning.md).
|
||||
|
||||
## Fan control
|
||||
|
||||
Fan control is available for the thermostat revisions with integrated fan system. For this purpose four commands are available:
|
||||
Fan control commands are available for thermostat revisions with an integrated fan system:
|
||||
1. `fan` - show fan stats: `fan_pwm`, `abs_max_tec_i`, `auto_mode`, `k_a`, `k_b`, `k_c`.
|
||||
2. `fan auto` - enable auto speed controller mode, which correlates with fan curve `fcurve`.
|
||||
3. `fan <value>` - set the fan power with the value from `1` to `100` and disable auto mode. There is no way to disable the fan.
|
||||
2. `fan auto` - enable auto speed controller mode, where fan speed is controlled by the fan curve `fcurve`.
|
||||
3. `fan <value>` - set the fan power with the value from `1` to `100` and disable auto mode. There is no way to completely disable the fan.
|
||||
Please note that power doesn't correlate with the actual speed linearly.
|
||||
4. `fcurve <a> <b> <c>` - set coefficients of the controlling curve `a*x^2 + b*x + c`, where `x` is `abs_max_tec_i/MAX_TEC_I`,
|
||||
i.e. receives values from 0 to 1 linearly tied to the maximum current. The controlling curve should produce values from 0 to 1,
|
||||
as below and beyond values would be substituted by 0 and 1 respectively.
|
||||
5. `fcurve default` - restore fan curve settings to defaults: `a = 1.0, b = 0.0, c = 0.0`.
|
||||
4. `fcurve <a> <b> <c>` - set coefficients of the controlling curve `a*x^2 + b*x + c`, where `x` is `abs_max_tec_i/MAX_TEC_I`, a normalized value in range [0,1],
|
||||
i.e. the (linear) proportion of current output capacity used, on the channel with the largest current flow. The controlling curve is also clamped to [0,1].
|
||||
5. `fcurve default` - restore fan curve coefficients to defaults: `a = 1.0, b = 0.0, c = 0.0`.
|
||||
|
2
build.rs
2
build.rs
@ -15,4 +15,6 @@ fn main() {
|
||||
// Only re-run the build script when memory.x is changed,
|
||||
// instead of when any part of the source code changes.
|
||||
println!("cargo:rerun-if-changed=memory.x");
|
||||
|
||||
built::write_built_file().expect("Failed to acquire build-time information");
|
||||
}
|
||||
|
48
flake.lock
generated
48
flake.lock
generated
@ -1,41 +1,45 @@
|
||||
{
|
||||
"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": {
|
||||
"locked": {
|
||||
"lastModified": 1691421349,
|
||||
"narHash": "sha256-RRJyX0CUrs4uW4gMhd/X4rcDG8PTgaaCQM5rXEJOx6g=",
|
||||
"lastModified": 1722791413,
|
||||
"narHash": "sha256-rCTrlCWvHzMCNcKxPE3Z/mMK2gDZ+BvvpEVyRM4tKmU=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "011567f35433879aae5024fc6ec53f2a0568a6c4",
|
||||
"rev": "8b5b6723aca5a51edf075936439d9cd3947b7b2c",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-23.05",
|
||||
"ref": "nixos-24.05",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"mozilla-overlay": "mozilla-overlay",
|
||||
"nixpkgs": "nixpkgs"
|
||||
"nixpkgs": "nixpkgs",
|
||||
"rust-overlay": "rust-overlay"
|
||||
}
|
||||
},
|
||||
"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"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
43
flake.nix
43
flake.nix
@ -1,32 +1,25 @@
|
||||
{
|
||||
description = "Firmware for the Sinara 8451 Thermostat";
|
||||
|
||||
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-23.05;
|
||||
inputs.mozilla-overlay = { url = github:mozilla/nixpkgs-mozilla; flake = false; };
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
|
||||
inputs.rust-overlay = {
|
||||
url = "github:oxalica/rust-overlay";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, mozilla-overlay }:
|
||||
outputs = { self, nixpkgs, rust-overlay }:
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import mozilla-overlay) ]; };
|
||||
rustManifest = pkgs.fetchurl {
|
||||
url = "https://static.rust-lang.org/dist/2022-12-15/channel-rust-stable.toml";
|
||||
hash = "sha256-S7epLlflwt0d1GZP44u5Xosgf6dRrmr8xxC+Ml2Pq7c=";
|
||||
};
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import rust-overlay) ]; };
|
||||
|
||||
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 = pkgs.rust-bin.stable."1.66.0".default.override {
|
||||
extensions = [ "rust-src" ];
|
||||
targets = [ "thumbv7em-none-eabihf" ];
|
||||
};
|
||||
rust = rustChannelOfTargets "stable" null targets;
|
||||
rustPlatform = pkgs.recurseIntoAttrs (pkgs.makeRustPlatform {
|
||||
rustPlatform = pkgs.makeRustPlatform {
|
||||
rustc = rust;
|
||||
cargo = rust;
|
||||
});
|
||||
};
|
||||
|
||||
thermostat = rustPlatform.buildRustPackage {
|
||||
name = "thermostat";
|
||||
version = "0.0.0";
|
||||
@ -54,24 +47,26 @@
|
||||
'';
|
||||
|
||||
dontFixup = true;
|
||||
auditable = false;
|
||||
};
|
||||
in {
|
||||
packages.x86_64-linux = {
|
||||
inherit thermostat;
|
||||
default = thermostat;
|
||||
};
|
||||
|
||||
hydraJobs = {
|
||||
inherit thermostat;
|
||||
};
|
||||
|
||||
devShell.x86_64-linux = pkgs.mkShell {
|
||||
devShells.x86_64-linux.default = pkgs.mkShellNoCC {
|
||||
name = "thermostat-dev-shell";
|
||||
buildInputs = with pkgs; [
|
||||
rust openocd dfu-util
|
||||
packages = with pkgs; [
|
||||
rust llvm
|
||||
openocd dfu-util
|
||||
] ++ (with python3Packages; [
|
||||
numpy matplotlib
|
||||
]);
|
||||
};
|
||||
defaultPackage.x86_64-linux = thermostat;
|
||||
};
|
||||
}
|
@ -2,11 +2,13 @@ use smoltcp::time::{Duration, Instant};
|
||||
use uom::si::{
|
||||
f64::{
|
||||
ElectricPotential,
|
||||
ElectricCurrent,
|
||||
ElectricalResistance,
|
||||
ThermodynamicTemperature,
|
||||
Time,
|
||||
},
|
||||
electric_potential::volt,
|
||||
electric_current::ampere,
|
||||
electrical_resistance::ohm,
|
||||
thermodynamic_temperature::degree_celsius,
|
||||
time::millisecond,
|
||||
@ -29,6 +31,7 @@ pub struct ChannelState {
|
||||
/// i_set 0A center point
|
||||
pub center: CenterPoint,
|
||||
pub dac_value: ElectricPotential,
|
||||
pub i_set: ElectricCurrent,
|
||||
pub pid_engaged: bool,
|
||||
pub pid: pid::Controller,
|
||||
pub sh: sh::Parameters,
|
||||
@ -44,6 +47,7 @@ impl ChannelState {
|
||||
adc_interval: Duration::from_millis(100),
|
||||
center: CenterPoint::Vref,
|
||||
dac_value: ElectricPotential::new::<volt>(0.0),
|
||||
i_set: ElectricCurrent::new::<ampere>(0.0),
|
||||
pid_engaged: false,
|
||||
pid: pid::Controller::new(pid::Parameters::default()),
|
||||
sh: sh::Parameters::default(),
|
||||
|
265
src/channels.rs
265
src/channels.rs
@ -1,5 +1,6 @@
|
||||
use core::cmp::max_by;
|
||||
use core::marker::PhantomData;
|
||||
use heapless::{consts::U2, Vec};
|
||||
use num_traits::Zero;
|
||||
use serde::{Serialize, Serializer};
|
||||
use smoltcp::time::Instant;
|
||||
use stm32f4xx_hal::hal;
|
||||
@ -18,15 +19,38 @@ use crate::{
|
||||
channel_state::ChannelState,
|
||||
command_parser::{CenterPoint, PwmPin},
|
||||
command_handler::JsonBuffer,
|
||||
pins,
|
||||
pins::{self, Channel0VRef, Channel1VRef},
|
||||
steinhart_hart,
|
||||
};
|
||||
|
||||
pub enum PinsAdcReadTarget {
|
||||
VREF,
|
||||
DacVfb,
|
||||
ITec,
|
||||
VTec,
|
||||
}
|
||||
|
||||
pub const CHANNELS: usize = 2;
|
||||
pub const R_SENSE: f64 = 0.05;
|
||||
// DAC chip outputs 0-5v, which is then passed through a resistor dividor to provide 0-3v range
|
||||
const DAC_OUT_V_MAX: f64 = 3.0;
|
||||
|
||||
// From design specs
|
||||
pub const MAX_TEC_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
|
||||
const DAC_OUT_V_MAX: ElectricPotential = ElectricPotential {
|
||||
dimension: PhantomData,
|
||||
units: PhantomData,
|
||||
value: 3.0,
|
||||
};
|
||||
// TODO: -pub
|
||||
pub struct Channels {
|
||||
channel0: Channel<Channel0>,
|
||||
@ -98,7 +122,7 @@ impl Channels {
|
||||
pub fn get_center(&mut self, channel: usize) -> ElectricPotential {
|
||||
match self.channel_state(channel).center {
|
||||
CenterPoint::Vref =>
|
||||
self.read_vref(channel),
|
||||
self.adc_read(channel, PinsAdcReadTarget::VREF, 8),
|
||||
CenterPoint::Override(center_point) =>
|
||||
ElectricPotential::new::<volt>(center_point.into()),
|
||||
}
|
||||
@ -111,16 +135,13 @@ impl Channels {
|
||||
}
|
||||
|
||||
pub fn get_i(&mut self, channel: usize) -> ElectricCurrent {
|
||||
let center_point = self.get_center(channel);
|
||||
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
||||
let voltage = self.get_dac(channel);
|
||||
let i_tec = (voltage - center_point) / (10.0 * r_sense);
|
||||
i_tec
|
||||
let i_set = self.channel_state(channel).i_set;
|
||||
i_set
|
||||
}
|
||||
|
||||
/// i_set DAC
|
||||
fn set_dac(&mut self, channel: usize, voltage: ElectricPotential) -> ElectricPotential {
|
||||
let value = ((voltage / ElectricPotential::new::<volt>(DAC_OUT_V_MAX)).get::<ratio>() * (ad5680::MAX_VALUE as f64)) as u32 ;
|
||||
let value = ((voltage / DAC_OUT_V_MAX).get::<ratio>() * (ad5680::MAX_VALUE as f64)) as u32 ;
|
||||
match channel {
|
||||
0 => self.channel0.dac.set(value).unwrap(),
|
||||
1 => self.channel1.dac.set(value).unwrap(),
|
||||
@ -130,7 +151,8 @@ impl Channels {
|
||||
voltage
|
||||
}
|
||||
|
||||
pub fn set_i(&mut self, channel: usize, i_tec: ElectricCurrent) -> ElectricCurrent {
|
||||
pub fn set_i(&mut self, channel: usize, i_set: ElectricCurrent) -> ElectricCurrent {
|
||||
let i_set = i_set.min(MAX_TEC_I).max(-MAX_TEC_I);
|
||||
let vref_meas = match channel.into() {
|
||||
0 => self.channel0.vref_meas,
|
||||
1 => self.channel1.vref_meas,
|
||||
@ -138,38 +160,119 @@ impl Channels {
|
||||
};
|
||||
let center_point = vref_meas;
|
||||
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
||||
let voltage = i_tec * 10.0 * r_sense + center_point;
|
||||
let voltage = i_set * 10.0 * r_sense + center_point;
|
||||
let voltage = self.set_dac(channel, voltage);
|
||||
let i_tec = (voltage - center_point) / (10.0 * r_sense);
|
||||
i_tec
|
||||
let i_set = (voltage - center_point) / (10.0 * r_sense);
|
||||
self.channel_state(channel).i_set = i_set;
|
||||
i_set
|
||||
}
|
||||
|
||||
pub fn read_dac_feedback(&mut self, channel: usize) -> ElectricPotential {
|
||||
/// AN4073: ADC Reading Dispersion can be reduced through Averaging
|
||||
pub fn adc_read(&mut self, channel: usize, adc_read_target: PinsAdcReadTarget, avg_pt: u16) -> ElectricPotential {
|
||||
let mut sample: u32 = 0;
|
||||
match channel {
|
||||
0 => {
|
||||
let sample = self.pins_adc.convert(
|
||||
&self.channel0.dac_feedback_pin,
|
||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||
);
|
||||
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||
sample = match adc_read_target {
|
||||
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 as u32}
|
||||
}
|
||||
}
|
||||
PinsAdcReadTarget::DacVfb => {
|
||||
for _ in (0..avg_pt).rev() {
|
||||
sample += self
|
||||
.pins_adc
|
||||
.convert(&self.channel0.dac_feedback_pin,stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
||||
as u32;
|
||||
}
|
||||
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)
|
||||
}
|
||||
1 => {
|
||||
let sample = self.pins_adc.convert(
|
||||
&self.channel1.dac_feedback_pin,
|
||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||
);
|
||||
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||
sample = match adc_read_target {
|
||||
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 as u32}
|
||||
}
|
||||
}
|
||||
PinsAdcReadTarget::DacVfb => {
|
||||
for _ in (0..avg_pt).rev() {
|
||||
sample += self
|
||||
.pins_adc
|
||||
.convert(&self.channel1.dac_feedback_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
||||
as u32;
|
||||
}
|
||||
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)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_dac_feedback_until_stable(&mut self, channel: usize, tolerance: ElectricPotential) -> ElectricPotential {
|
||||
let mut prev = self.read_dac_feedback(channel);
|
||||
let mut prev = self.adc_read(channel, PinsAdcReadTarget::DacVfb, 1);
|
||||
loop {
|
||||
let current = self.read_dac_feedback(channel);
|
||||
let current = self.adc_read(channel, PinsAdcReadTarget::DacVfb, 1);
|
||||
if (current - prev).abs() < tolerance {
|
||||
return current;
|
||||
}
|
||||
@ -177,73 +280,6 @@ 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.
|
||||
///
|
||||
/// The thermostat DAC applies a control voltage signal to the CTLI pin of MAX driver chip to control its output current.
|
||||
@ -291,7 +327,7 @@ impl Channels {
|
||||
best_error = error;
|
||||
start_value = prev_value;
|
||||
|
||||
let vref = (value as f64 / ad5680::MAX_VALUE as f64) * ElectricPotential::new::<volt>(DAC_OUT_V_MAX);
|
||||
let vref = (value as f64 / ad5680::MAX_VALUE as f64) * DAC_OUT_V_MAX;
|
||||
match channel {
|
||||
0 => self.channel0.vref_meas = vref,
|
||||
1 => self.channel1.vref_meas = vref,
|
||||
@ -351,32 +387,32 @@ impl Channels {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_max_v(&mut self, channel: usize) -> ElectricPotential {
|
||||
pub fn get_max_v(&mut self, channel: usize) -> (ElectricPotential, ElectricPotential) {
|
||||
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
||||
let duty = self.get_pwm(channel, PwmPin::MaxV);
|
||||
duty * max
|
||||
(duty * max, MAX_TEC_V)
|
||||
}
|
||||
|
||||
pub fn get_max_i_pos(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||
let duty = self.get_pwm(channel, PwmPin::MaxIPos);
|
||||
(duty * max, max)
|
||||
(duty * max, MAX_TEC_I)
|
||||
}
|
||||
|
||||
pub fn get_max_i_neg(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||
let duty = self.get_pwm(channel, PwmPin::MaxINeg);
|
||||
(duty * max, max)
|
||||
(duty * max, MAX_TEC_I)
|
||||
}
|
||||
|
||||
// Get current passing through TEC
|
||||
pub fn get_tec_i(&mut self, channel: usize) -> ElectricCurrent {
|
||||
(self.read_itec(channel) - self.read_vref(channel)) / ElectricalResistance::new::<ohm>(0.4)
|
||||
(self.adc_read(channel, PinsAdcReadTarget::ITec, 16) - self.adc_read(channel, PinsAdcReadTarget::VREF, 16)) / ElectricalResistance::new::<ohm>(0.4)
|
||||
}
|
||||
|
||||
// Get voltage across TEC
|
||||
pub fn get_tec_v(&mut self, channel: usize) -> ElectricPotential {
|
||||
(self.read_tec_u_meas(channel) - ElectricPotential::new::<volt>(1.5)) * 4.0
|
||||
(self.adc_read(channel, PinsAdcReadTarget::VTec, 16) - ElectricPotential::new::<volt>(1.5)) * 4.0
|
||||
}
|
||||
|
||||
fn set_pwm(&mut self, channel: usize, pin: PwmPin, duty: f64) -> f64 {
|
||||
@ -408,28 +444,28 @@ impl Channels {
|
||||
|
||||
pub fn set_max_v(&mut self, channel: usize, max_v: ElectricPotential) -> (ElectricPotential, ElectricPotential) {
|
||||
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
||||
let duty = (max_v / max).get::<ratio>();
|
||||
let duty = (max_v.min(MAX_TEC_V).max(ElectricPotential::zero()) / max).get::<ratio>();
|
||||
let duty = self.set_pwm(channel, PwmPin::MaxV, duty);
|
||||
(duty * max, max)
|
||||
}
|
||||
|
||||
pub fn set_max_i_pos(&mut self, channel: usize, max_i_pos: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||
let duty = (max_i_pos / max).get::<ratio>();
|
||||
let duty = (max_i_pos.min(MAX_TEC_I).max(ElectricCurrent::zero()) / max).get::<ratio>();
|
||||
let duty = self.set_pwm(channel, PwmPin::MaxIPos, duty);
|
||||
(duty * max, max)
|
||||
}
|
||||
|
||||
pub fn set_max_i_neg(&mut self, channel: usize, max_i_neg: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||
let duty = (max_i_neg / max).get::<ratio>();
|
||||
let duty = (max_i_neg.min(MAX_TEC_I).max(ElectricCurrent::zero()) / max).get::<ratio>();
|
||||
let duty = self.set_pwm(channel, PwmPin::MaxINeg, duty);
|
||||
(duty * max, max)
|
||||
}
|
||||
|
||||
fn report(&mut self, channel: usize) -> Report {
|
||||
let i_set = self.get_i(channel);
|
||||
let i_tec = self.read_itec(channel);
|
||||
let i_tec = self.adc_read(channel, PinsAdcReadTarget::ITec, 16);
|
||||
let tec_i = self.get_tec_i(channel);
|
||||
let dac_value = self.get_dac(channel);
|
||||
let state = self.channel_state(channel);
|
||||
@ -445,7 +481,7 @@ impl Channels {
|
||||
pid_engaged: state.pid_engaged,
|
||||
i_set,
|
||||
dac_value,
|
||||
dac_feedback: self.read_dac_feedback(channel),
|
||||
dac_feedback: self.adc_read(channel, PinsAdcReadTarget::DacVfb, 1),
|
||||
i_tec,
|
||||
tec_i,
|
||||
tec_u_meas: self.get_tec_v(channel),
|
||||
@ -482,8 +518,8 @@ impl Channels {
|
||||
PwmSummary {
|
||||
channel,
|
||||
center: CenterPointJson(self.channel_state(channel).center.clone()),
|
||||
i_set: (self.get_i(channel), ElectricCurrent::new::<ampere>(3.0)).into(),
|
||||
max_v: (self.get_max_v(channel), ElectricPotential::new::<volt>(5.0)).into(),
|
||||
i_set: (self.get_i(channel), MAX_TEC_I).into(),
|
||||
max_v: self.get_max_v(channel).into(),
|
||||
max_i_pos: self.get_max_i_pos(channel).into(),
|
||||
max_i_neg: self.get_max_i_neg(channel).into(),
|
||||
}
|
||||
@ -524,10 +560,11 @@ impl Channels {
|
||||
serde_json_core::to_vec(&summaries)
|
||||
}
|
||||
|
||||
pub fn current_abs_max_tec_i(&mut self) -> f64 {
|
||||
max_by(self.get_tec_i(0).abs().get::<ampere>(),
|
||||
self.get_tec_i(1).abs().get::<ampere>(),
|
||||
|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal))
|
||||
pub fn current_abs_max_tec_i(&mut self) -> ElectricCurrent {
|
||||
(0..CHANNELS)
|
||||
.map(|channel| self.get_tec_i(channel).abs())
|
||||
.max_by(|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal))
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@ use super::{
|
||||
session::Session,
|
||||
FanCtrl,
|
||||
hw_rev::HWRev,
|
||||
firmware_ver,
|
||||
};
|
||||
|
||||
use uom::{
|
||||
@ -57,6 +58,10 @@ pub enum Error {
|
||||
FlashError
|
||||
}
|
||||
|
||||
mod built_info {
|
||||
include!(concat!(env!("OUT_DIR"), "/built.rs"));
|
||||
}
|
||||
|
||||
pub type JsonBuffer = Vec<u8, U1024>;
|
||||
|
||||
fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool {
|
||||
@ -207,11 +212,11 @@ impl Handler {
|
||||
}
|
||||
|
||||
fn set_center_point(socket: &mut TcpSocket, channels: &mut Channels, channel: usize, center: CenterPoint) -> Result<Handler, Error> {
|
||||
let i_tec = channels.get_i(channel);
|
||||
let i_set = channels.get_i(channel);
|
||||
let state = channels.channel_state(channel);
|
||||
state.center = center;
|
||||
if !state.pid_engaged {
|
||||
channels.set_i(channel, i_tec);
|
||||
channels.set_i(channel, i_set);
|
||||
}
|
||||
send_line(socket, b"{}");
|
||||
Ok(Handler::Handled)
|
||||
@ -345,7 +350,7 @@ impl Handler {
|
||||
|
||||
fn set_fan(socket: &mut TcpSocket, fan_pwm: u32, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
||||
if !fan_ctrl.fan_available() {
|
||||
send_line(socket, b"{ \"warning\": \"this thermostat doesn't have fan!\" }");
|
||||
send_line(socket, b"{ \"warning\": \"this thermostat doesn't have a fan!\" }");
|
||||
return Ok(Handler::Handled);
|
||||
}
|
||||
fan_ctrl.set_auto_mode(false);
|
||||
@ -374,7 +379,7 @@ impl Handler {
|
||||
|
||||
fn fan_auto(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
||||
if !fan_ctrl.fan_available() {
|
||||
send_line(socket, b"{ \"warning\": \"this thermostat doesn't have fan!\" }");
|
||||
send_line(socket, b"{ \"warning\": \"this thermostat doesn't have a fan!\" }");
|
||||
return Ok(Handler::Handled);
|
||||
}
|
||||
fan_ctrl.set_auto_mode(true);
|
||||
@ -412,6 +417,20 @@ impl Handler {
|
||||
}
|
||||
}
|
||||
|
||||
fn show_firmware_version(socket: &mut TcpSocket) -> Result<Handler, Error> {
|
||||
match firmware_ver::summary() {
|
||||
Ok(buf) => {
|
||||
send_line(socket, &buf);
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Err(e) => {
|
||||
error!("unable to serialize FirmwareVer summary: {:?}", e);
|
||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||
Err(Error::ReportError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_command(command: Command, socket: &mut TcpSocket, channels: &mut Channels, session: &Session, store: &mut FlashStore, ipv4_config: &mut Ipv4Config, fan_ctrl: &mut FanCtrl, hwrev: HWRev) -> Result<Self, Error> {
|
||||
match command {
|
||||
Command::Quit => Ok(Handler::CloseSocket),
|
||||
@ -441,6 +460,7 @@ impl Handler {
|
||||
Command::FanCurve { k_a, k_b, k_c } => Handler::fan_curve(socket, fan_ctrl, k_a, k_b, k_c),
|
||||
Command::FanCurveDefaults => Handler::fan_defaults(socket, fan_ctrl),
|
||||
Command::ShowHWRev => Handler::show_hwrev(socket, hwrev),
|
||||
Command::ShowFirmwareVer => Handler::show_firmware_version(socket),
|
||||
}
|
||||
}
|
||||
}
|
@ -191,6 +191,7 @@ pub enum Command {
|
||||
},
|
||||
FanCurveDefaults,
|
||||
ShowHWRev,
|
||||
ShowFirmwareVer,
|
||||
}
|
||||
|
||||
fn end(input: &[u8]) -> IResult<&[u8], ()> {
|
||||
@ -600,6 +601,7 @@ fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
||||
fan,
|
||||
fan_curve,
|
||||
value(Ok(Command::ShowHWRev), tag("hwrev")),
|
||||
value(Ok(Command::ShowFirmwareVer), tag("version")),
|
||||
))(input)
|
||||
}
|
||||
|
||||
@ -861,4 +863,10 @@ mod test {
|
||||
let command = Command::parse(b"hwrev");
|
||||
assert_eq!(command, Ok(Command::ShowHWRev));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_firmware_ver() {
|
||||
let command = Command::parse(b"version");
|
||||
assert_eq!(command, Ok(Command::ShowFirmwareVer));
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use num_traits::Zero;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use uom::si::{
|
||||
electric_potential::volt,
|
||||
@ -18,6 +19,7 @@ pub struct ChannelConfig {
|
||||
pid: pid::Parameters,
|
||||
pid_target: f32,
|
||||
pid_engaged: bool,
|
||||
i_set: ElectricCurrent,
|
||||
sh: steinhart_hart::Parameters,
|
||||
pwm: PwmLimits,
|
||||
/// uses variant `PostFilter::Invalid` instead of `None` to save space
|
||||
@ -33,11 +35,17 @@ impl ChannelConfig {
|
||||
.unwrap_or(PostFilter::Invalid);
|
||||
|
||||
let state = channels.channel_state(channel);
|
||||
let i_set = if state.pid_engaged {
|
||||
ElectricCurrent::zero()
|
||||
} else {
|
||||
state.i_set
|
||||
};
|
||||
ChannelConfig {
|
||||
center: state.center.clone(),
|
||||
pid: state.pid.parameters.clone(),
|
||||
pid_target: state.pid.target as f32,
|
||||
pid_engaged: state.pid_engaged,
|
||||
i_set: i_set,
|
||||
sh: state.sh.clone(),
|
||||
pwm,
|
||||
adc_postfilter,
|
||||
@ -59,6 +67,7 @@ impl ChannelConfig {
|
||||
adc_postfilter => Some(adc_postfilter),
|
||||
};
|
||||
let _ = channels.adc.set_postfilter(channel as u8, adc_postfilter);
|
||||
let _ = channels.set_i(channel, self.i_set);
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,7 +80,7 @@ struct PwmLimits {
|
||||
|
||||
impl PwmLimits {
|
||||
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_neg, _) = channels.get_max_i_neg(channel);
|
||||
PwmLimits {
|
||||
|
@ -4,17 +4,18 @@ use stm32f4xx_hal::{
|
||||
pwm::{self, PwmChannels},
|
||||
pac::TIM8,
|
||||
};
|
||||
|
||||
use uom::si::{
|
||||
f64::ElectricCurrent,
|
||||
electric_current::ampere,
|
||||
};
|
||||
use crate::{
|
||||
hw_rev::HWSettings,
|
||||
command_handler::JsonBuffer,
|
||||
channels::MAX_TEC_I,
|
||||
};
|
||||
|
||||
pub type FanPin = PwmChannels<TIM8, pwm::C4>;
|
||||
|
||||
// as stated in the schematics
|
||||
const MAX_TEC_I: f32 = 3.0;
|
||||
|
||||
const MAX_USER_FAN_PWM: f32 = 100.0;
|
||||
const MIN_USER_FAN_PWM: f32 = 1.0;
|
||||
|
||||
@ -50,10 +51,10 @@ impl FanCtrl {
|
||||
fan_ctrl
|
||||
}
|
||||
|
||||
pub fn cycle(&mut self, abs_max_tec_i: f32) {
|
||||
self.abs_max_tec_i = abs_max_tec_i;
|
||||
pub fn cycle(&mut self, abs_max_tec_i: ElectricCurrent) {
|
||||
self.abs_max_tec_i = abs_max_tec_i.get::<ampere>() as f32;
|
||||
if self.fan_auto && self.hw_settings.fan_available {
|
||||
let scaled_current = self.abs_max_tec_i / MAX_TEC_I;
|
||||
let scaled_current = self.abs_max_tec_i / MAX_TEC_I.get::<ampere>() as f32;
|
||||
// do not limit upper bound, as it will be limited in the set_pwm()
|
||||
let pwm = (MAX_USER_FAN_PWM * (scaled_current * (scaled_current * self.k_a + self.k_b) + self.k_c)) as u32;
|
||||
self.set_pwm(pwm);
|
||||
|
20
src/firmware_ver.rs
Normal file
20
src/firmware_ver.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use serde::Serialize;
|
||||
use crate::command_handler::JsonBuffer;
|
||||
|
||||
mod built_info {
|
||||
include!(concat!(env!("OUT_DIR"), "/built.rs"));
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct FirmwareSummary {
|
||||
git_hash: Option<&'static str>,
|
||||
is_dirty: Option<bool>,
|
||||
}
|
||||
|
||||
pub fn summary() -> Result<JsonBuffer, serde_json_core::ser::Error> {
|
||||
let summary = FirmwareSummary {
|
||||
git_hash: built_info::GIT_COMMIT_HASH,
|
||||
is_dirty: built_info::GIT_DIRTY,
|
||||
};
|
||||
serde_json_core::to_vec(&summary)
|
||||
}
|
@ -55,6 +55,7 @@ use command_handler::Handler;
|
||||
mod fan_ctrl;
|
||||
use fan_ctrl::FanCtrl;
|
||||
mod hw_rev;
|
||||
mod firmware_ver;
|
||||
|
||||
const HSE: MegaHertz = MegaHertz(8);
|
||||
#[cfg(not(feature = "semihosting"))]
|
||||
@ -185,7 +186,7 @@ fn main() -> ! {
|
||||
server.for_each(|_, session| session.set_report_pending(channel.into()));
|
||||
}
|
||||
|
||||
fan_ctrl.cycle(channels.current_abs_max_tec_i() as f32);
|
||||
fan_ctrl.cycle(channels.current_abs_max_tec_i());
|
||||
|
||||
if channels.pid_engaged() {
|
||||
leds.g3.on();
|
||||
|
@ -54,15 +54,13 @@ impl Controller {
|
||||
// + x0 * (kp + ki + kd)
|
||||
// - x1 * (kp + 2kd)
|
||||
// + x2 * kd
|
||||
// + kp * (u0 - u1)
|
||||
// y0 = clip(y0', ymin, ymax)
|
||||
pub fn update(&mut self, input: f64) -> f64 {
|
||||
|
||||
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)
|
||||
- self.x1 * f64::from(self.parameters.kp + 2.0 * self.parameters.kd)
|
||||
+ self.x2 * f64::from(self.parameters.kd)
|
||||
+ f64::from(self.parameters.kp) * (self.target - self.u1);
|
||||
+ self.x2 * f64::from(self.parameters.kd);
|
||||
if output < self.parameters.output_min.into() {
|
||||
output = self.parameters.output_min.into();
|
||||
}
|
||||
|
26
src/pins.rs
26
src/pins.rs
@ -66,21 +66,31 @@ pub trait ChannelPins {
|
||||
type TecUMeasPin;
|
||||
}
|
||||
|
||||
pub enum Channel0VRef {
|
||||
Analog(PA0<Analog>),
|
||||
Disabled(PA0<Input<Floating>>),
|
||||
}
|
||||
|
||||
impl ChannelPins for Channel0 {
|
||||
type DacSpi = Dac0Spi;
|
||||
type DacSync = PE4<Output<PushPull>>;
|
||||
type Shdn = PE10<Output<PushPull>>;
|
||||
type VRefPin = PA0<Analog>;
|
||||
type VRefPin = Channel0VRef;
|
||||
type ItecPin = PA6<Analog>;
|
||||
type DacFeedbackPin = PA4<Analog>;
|
||||
type TecUMeasPin = PC2<Analog>;
|
||||
}
|
||||
|
||||
pub enum Channel1VRef {
|
||||
Analog(PA3<Analog>),
|
||||
Disabled(PA3<Input<Floating>>),
|
||||
}
|
||||
|
||||
impl ChannelPins for Channel1 {
|
||||
type DacSpi = Dac1Spi;
|
||||
type DacSync = PF6<Output<PushPull>>;
|
||||
type Shdn = PE15<Output<PushPull>>;
|
||||
type VRefPin = PA3<Analog>;
|
||||
type VRefPin = Channel1VRef;
|
||||
type ItecPin = PB0<Analog>;
|
||||
type DacFeedbackPin = PA5<Analog>;
|
||||
type TecUMeasPin = PC3<Analog>;
|
||||
@ -150,13 +160,17 @@ impl Pins {
|
||||
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 mut shdn0 = gpioe.pe10.into_push_pull_output();
|
||||
let _ = shdn0.set_low();
|
||||
let vref0_pin = gpioa.pa0.into_analog();
|
||||
let vref0_pin = if hwrev.major > 2 {Channel0VRef::Analog(gpioa.pa0.into_analog())} else {Channel0VRef::Disabled(gpioa.pa0)};
|
||||
let itec0_pin = gpioa.pa6.into_analog();
|
||||
let dac_feedback0_pin = gpioa.pa4.into_analog();
|
||||
let tec_u_meas0_pin = gpioc.pc2.into_analog();
|
||||
@ -176,7 +190,7 @@ impl Pins {
|
||||
);
|
||||
let mut shdn1 = gpioe.pe15.into_push_pull_output();
|
||||
let _ = shdn1.set_low();
|
||||
let vref1_pin = gpioa.pa3.into_analog();
|
||||
let vref1_pin = if hwrev.major > 2 {Channel1VRef::Analog(gpioa.pa3.into_analog())} else {Channel1VRef::Disabled(gpioa.pa3)};
|
||||
let itec1_pin = gpiob.pb0.into_analog();
|
||||
let dac_feedback1_pin = gpioa.pa5.into_analog();
|
||||
let tec_u_meas1_pin = gpioc.pc3.into_analog();
|
||||
@ -198,10 +212,6 @@ impl Pins {
|
||||
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(gpiod.pd9, gpiod.pd10.into_push_pull_output(), gpiod.pd11.into_push_pull_output());
|
||||
|
||||
let eeprom_scl = gpiob.pb8.into_alternate().set_open_drain();
|
||||
|
Loading…
Reference in New Issue
Block a user