forked from M-Labs/thermostat
Compare commits
43 Commits
gui-paramt
...
master
Author | SHA1 | Date |
---|---|---|
linuswck | ad54842c43 | |
atse | b336c4f993 | |
linuswck | 680193b34b | |
atse | ae4bea0c8a | |
atse | 1f2de942e4 | |
atse | 1041d3ecbb | |
atse | c6040899dd | |
atse | 9d89104f50 | |
atse | 136c7a0b52 | |
atse | 5000cae1b1 | |
atse | 78ec77509f | |
atse | 52aa3890c1 | |
atse | 1ae6a6fdd4 | |
atse | 7333d2cea5 | |
atse | 44e9130010 | |
linuswck | 5b0c6f7018 | |
linuswck | 1007982b48 | |
linuswck | 925601f4f5 | |
linuswck | 8c1cb3117c | |
linuswck | 1fcfe41a63 | |
linuswck | 9fce19a418 | |
atse | 00d5feaa8d | |
atse | 09be55e12a | |
atse | 76547be90a | |
atse | 8b975e656e | |
atse | ae3d8b51d4 | |
atse | 17edae44fb | |
atse | 03b4561142 | |
atse | 631a10938d | |
atse | 6cd6a6a2c2 | |
atse | b93e2fbb7b | |
atse | 76b95f66e0 | |
atse | 8008870bc1 | |
atse | 7646ff9037 | |
atse | 6f81a63d12 | |
atse | 78012f6fdd | |
atse | bb4f43fe1c | |
atse | 9df0fe406f | |
topquark12 | 5ba74c6d9b | |
atse | 6f0acc73b8 | |
atse | f29e86310d | |
atse | b04a61c414 | |
atse | cd680dd6cd |
|
@ -1,2 +1,5 @@
|
||||||
target/
|
target/
|
||||||
result
|
result
|
||||||
|
*.bin
|
||||||
|
|
||||||
|
__pycache__/
|
||||||
|
|
|
@ -327,10 +327,10 @@ dependencies = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "panic-abort"
|
name = "panic-halt"
|
||||||
version = "0.3.2"
|
version = "0.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4e20e6499bbbc412f280b04a42346b356c6fa0753d5fd22b7bd752ff34c778ee"
|
checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "panic-semihosting"
|
name = "panic-semihosting"
|
||||||
|
@ -563,7 +563,7 @@ dependencies = [
|
||||||
"nb 1.0.0",
|
"nb 1.0.0",
|
||||||
"nom",
|
"nom",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"panic-abort",
|
"panic-halt",
|
||||||
"panic-semihosting",
|
"panic-semihosting",
|
||||||
"serde",
|
"serde",
|
||||||
"serde-json-core",
|
"serde-json-core",
|
||||||
|
|
|
@ -7,14 +7,14 @@ authors = ["Astro <astro@spaceboyz.net>"]
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
keywords = ["thermostat", "laser", "physics"]
|
keywords = ["thermostat", "laser", "physics"]
|
||||||
repository = "https://git.m-labs.hk/M-Labs/thermostat"
|
repository = "https://git.m-labs.hk/M-Labs/thermostat"
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = []
|
features = []
|
||||||
default-target = "thumbv7em-none-eabihf"
|
default-target = "thumbv7em-none-eabihf"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
panic-abort = "0.3"
|
panic-halt = "0.2"
|
||||||
panic-semihosting = { version = "0.5", optional = true }
|
panic-semihosting = { version = "0.5", optional = true }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
bare-metal = "1"
|
bare-metal = "1"
|
||||||
|
|
35
README.md
35
README.md
|
@ -29,7 +29,7 @@ Alternatively, you can install the Rust toolchain without Nix using rustup; see
|
||||||
|
|
||||||
Connect SWDIO/SWCLK/RST/GND to a programmer such as ST-Link v2.1. Run OpenOCD:
|
Connect SWDIO/SWCLK/RST/GND to a programmer such as ST-Link v2.1. Run OpenOCD:
|
||||||
```shell
|
```shell
|
||||||
openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg
|
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg
|
||||||
```
|
```
|
||||||
|
|
||||||
You may need to power up the programmer before powering the device.
|
You may need to power up the programmer before powering the device.
|
||||||
|
@ -45,7 +45,7 @@ There are several options for flashing Thermostat. DFU requires only a micro-USB
|
||||||
|
|
||||||
### dfu-util on Linux
|
### dfu-util on Linux
|
||||||
* Install the DFU USB tool (dfu-util).
|
* 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.
|
* 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.
|
* Add jumper to Thermostat v2.0 across 2-pin jumper adjacent to JTAG connector.
|
||||||
* Cycle board power to put it in DFU update mode
|
* Cycle board power to put it in DFU update mode
|
||||||
|
@ -64,7 +64,7 @@ On a Windows machine install [st.com](https://st.com) DfuSe USB device firmware
|
||||||
|
|
||||||
### OpenOCD
|
### OpenOCD
|
||||||
```shell
|
```shell
|
||||||
openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg -c "program target/thumbv7em-none-eabihf/release/thermostat verify reset;exit"
|
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c "program target/thumbv7em-none-eabihf/release/thermostat verify reset;exit"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Network
|
## Network
|
||||||
|
@ -84,9 +84,7 @@ invalidate the first line of input.
|
||||||
|
|
||||||
### Reading ADC input
|
### Reading ADC input
|
||||||
|
|
||||||
Set report mode to `on` for a continuous stream of input data.
|
ADC input data is provided in reports. Query for the latest report with the command `report`. See the *Reports* section below.
|
||||||
|
|
||||||
The scope of this setting is per TCP session.
|
|
||||||
|
|
||||||
|
|
||||||
### TCP commands
|
### TCP commands
|
||||||
|
@ -97,8 +95,6 @@ formatted as line-delimited JSON.
|
||||||
| Syntax | Function |
|
| Syntax | Function |
|
||||||
|----------------------------------|-------------------------------------------------------------------------------|
|
|----------------------------------|-------------------------------------------------------------------------------|
|
||||||
| `report` | Show current input |
|
| `report` | Show current input |
|
||||||
| `report mode` | Show current report mode |
|
|
||||||
| `report mode <off/on>` | Set report mode |
|
|
||||||
| `pwm` | Show current PWM settings |
|
| `pwm` | Show current PWM settings |
|
||||||
| `pwm <0/1> max_i_pos <amp>` | Set maximum positive output current |
|
| `pwm <0/1> max_i_pos <amp>` | Set maximum positive output current |
|
||||||
| `pwm <0/1> max_i_neg <amp>` | Set maximum negative output current |
|
| `pwm <0/1> max_i_neg <amp>` | Set maximum negative output current |
|
||||||
|
@ -184,7 +180,7 @@ postfilter rate can be tuned with the `postfilter` command.
|
||||||
- Connect TEC module device 1 to TEC1- and TEC1+.
|
- Connect TEC module device 1 to TEC1- and TEC1+.
|
||||||
- The GND pin is for shielding not for sinking TEC module currents.
|
- The GND pin is for shielding not for sinking TEC module currents.
|
||||||
|
|
||||||
When using a TEC module with the Thermostat, the Thermostat expects the thermal load (where the thermistor is located) to heat up with a positive software current set point, and cool down with a negative current set point.
|
When using a TEC module with the Thermostat, the Thermostat expects the thermal load (where the thermistor is located) to cool down with a positive software current set point, and heat up with a negative current set point.
|
||||||
|
|
||||||
Testing heat flow direction with a low set current is recommended before installation of the TEC module.
|
Testing heat flow direction with a low set current is recommended before installation of the TEC module.
|
||||||
|
|
||||||
|
@ -251,20 +247,18 @@ pwm 0 pid
|
||||||
|
|
||||||
## Reports
|
## Reports
|
||||||
|
|
||||||
Use the bare `report` command to obtain a single report. Enable
|
Use the bare `report` command to obtain a single report. Reports are JSON objects
|
||||||
continuous reporting with `report mode on`. Reports are JSON objects
|
|
||||||
with the following keys.
|
with the following keys.
|
||||||
|
|
||||||
| Key | Unit | Description |
|
| Key | Unit | Description |
|
||||||
| --- | :---: | --- |
|
| --- | :---: | --- |
|
||||||
| `channel` | Integer | Channel `0`, or `1` |
|
| `channel` | Integer | Channel `0`, or `1` |
|
||||||
| `time` | Milliseconds | Temperature measurement time |
|
| `time` | Seconds | Temperature measurement time |
|
||||||
| `adc` | Volts | AD7172 input |
|
| `adc` | Volts | AD7172 input |
|
||||||
| `sens` | Ohms | Thermistor resistance derived from `adc` |
|
| `sens` | Ohms | Thermistor resistance derived from `adc` |
|
||||||
| `temperature` | Degrees Celsius | Steinhart-Hart conversion result derived from `sens` |
|
| `temperature` | Degrees Celsius | Steinhart-Hart conversion result derived from `sens` |
|
||||||
| `pid_engaged` | Boolean | `true` if in closed-loop mode |
|
| `pid_engaged` | Boolean | `true` if in closed-loop mode |
|
||||||
| `i_set` | Amperes | TEC output current |
|
| `i_set` | Amperes | TEC output current |
|
||||||
| `vref` | Volts | MAX1968 VREF (1.5 V) |
|
|
||||||
| `dac_value` | Volts | AD5680 output derived from `i_set` |
|
| `dac_value` | Volts | AD5680 output derived from `i_set` |
|
||||||
| `dac_feedback` | Volts | ADC measurement of the AD5680 output |
|
| `dac_feedback` | Volts | ADC measurement of the AD5680 output |
|
||||||
| `i_tec` | Volts | MAX1968 TEC current monitor |
|
| `i_tec` | Volts | MAX1968 TEC current monitor |
|
||||||
|
@ -272,18 +266,19 @@ with the following keys.
|
||||||
| `tec_u_meas` | Volts | Measurement of the voltage across the TEC |
|
| `tec_u_meas` | Volts | Measurement of the voltage across the TEC |
|
||||||
| `pid_output` | Amperes | PID control output |
|
| `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
|
## 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).
|
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
|
||||||
|
|
||||||
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`.
|
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`.
|
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 disable the fan.
|
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.
|
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`,
|
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. receives values from 0 to 1 linearly tied to the maximum current. The controlling curve should produce values from 0 to 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].
|
||||||
as below and beyond values would be substituted by 0 and 1 respectively.
|
5. `fcurve default` - restore fan curve coefficients to defaults: `a = 1.0, b = 0.0, c = 0.0`.
|
||||||
5. `fcurve default` - restore fan curve settings to defaults: `a = 1.0, b = 0.0, c = 0.0`.
|
|
||||||
|
|
48
flake.lock
48
flake.lock
|
@ -1,41 +1,45 @@
|
||||||
{
|
{
|
||||||
"nodes": {
|
"nodes": {
|
||||||
"mozilla-overlay": {
|
|
||||||
"flake": false,
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1638887313,
|
|
||||||
"narHash": "sha256-FMYV6rVtvSIfthgC1sK1xugh3y7muoQcvduMdriz4ag=",
|
|
||||||
"owner": "mozilla",
|
|
||||||
"repo": "nixpkgs-mozilla",
|
|
||||||
"rev": "7c1e8b1dd6ed0043fb4ee0b12b815256b0b9de6f",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "mozilla",
|
|
||||||
"repo": "nixpkgs-mozilla",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1641870998,
|
"lastModified": 1722791413,
|
||||||
"narHash": "sha256-6HkxR2WZsm37VoQS7jgp6Omd71iw6t1kP8bDbaqCDuI=",
|
"narHash": "sha256-rCTrlCWvHzMCNcKxPE3Z/mMK2gDZ+BvvpEVyRM4tKmU=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "386234e2a61e1e8acf94dfa3a3d3ca19a6776efb",
|
"rev": "8b5b6723aca5a51edf075936439d9cd3947b7b2c",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"ref": "nixos-21.11",
|
"ref": "nixos-24.05",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"root": {
|
"root": {
|
||||||
"inputs": {
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
49
flake.nix
49
flake.nix
|
@ -1,33 +1,26 @@
|
||||||
{
|
{
|
||||||
description = "Firmware for the Sinara 8451 Thermostat";
|
description = "Firmware for the Sinara 8451 Thermostat";
|
||||||
|
|
||||||
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11;
|
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
|
||||||
inputs.mozilla-overlay = { url = github:mozilla/nixpkgs-mozilla; flake = false; };
|
inputs.rust-overlay = {
|
||||||
|
url = "github:oxalica/rust-overlay";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
|
||||||
outputs = { self, nixpkgs, mozilla-overlay }:
|
outputs = { self, nixpkgs, rust-overlay }:
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import mozilla-overlay) ]; };
|
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import rust-overlay) ]; };
|
||||||
rustManifest = pkgs.fetchurl {
|
|
||||||
url = "https://static.rust-lang.org/dist/2021-10-26/channel-rust-nightly.toml";
|
|
||||||
sha256 = "sha256-1hLbypXA+nuH7o3AHCokzSBZAvQxvef4x9+XxO3aBao=";
|
|
||||||
};
|
|
||||||
|
|
||||||
targets = [
|
rust = pkgs.rust-bin.stable."1.66.0".default.override {
|
||||||
"thumbv7em-none-eabihf"
|
extensions = [ "rust-src" ];
|
||||||
];
|
targets = [ "thumbv7em-none-eabihf" ];
|
||||||
rustChannelOfTargets = _channel: _date: targets:
|
};
|
||||||
(pkgs.lib.rustLib.fromManifestFile rustManifest {
|
rustPlatform = pkgs.makeRustPlatform {
|
||||||
inherit (pkgs) stdenv lib fetchurl patchelf;
|
|
||||||
}).rust.override {
|
|
||||||
inherit targets;
|
|
||||||
extensions = ["rust-src"];
|
|
||||||
};
|
|
||||||
rust = rustChannelOfTargets "nightly" null targets;
|
|
||||||
rustPlatform = pkgs.recurseIntoAttrs (pkgs.makeRustPlatform {
|
|
||||||
rustc = rust;
|
rustc = rust;
|
||||||
cargo = rust;
|
cargo = rust;
|
||||||
});
|
};
|
||||||
thermostat = rustPlatform.buildRustPackage rec {
|
|
||||||
|
thermostat = rustPlatform.buildRustPackage {
|
||||||
name = "thermostat";
|
name = "thermostat";
|
||||||
version = "0.0.0";
|
version = "0.0.0";
|
||||||
|
|
||||||
|
@ -54,26 +47,26 @@
|
||||||
'';
|
'';
|
||||||
|
|
||||||
dontFixup = true;
|
dontFixup = true;
|
||||||
|
auditable = false;
|
||||||
};
|
};
|
||||||
in {
|
in {
|
||||||
packages.x86_64-linux = {
|
packages.x86_64-linux = {
|
||||||
inherit thermostat;
|
inherit thermostat;
|
||||||
|
default = thermostat;
|
||||||
};
|
};
|
||||||
|
|
||||||
hydraJobs = {
|
hydraJobs = {
|
||||||
inherit thermostat;
|
inherit thermostat;
|
||||||
};
|
};
|
||||||
|
|
||||||
devShell.x86_64-linux = pkgs.mkShell {
|
devShells.x86_64-linux.default = pkgs.mkShellNoCC {
|
||||||
name = "thermostat-dev-shell";
|
name = "thermostat-dev-shell";
|
||||||
buildInputs = with pkgs; [
|
packages = with pkgs; [
|
||||||
rustPlatform.rust.rustc
|
rust llvm
|
||||||
rustPlatform.rust.cargo
|
openocd dfu-util rlwrap
|
||||||
openocd dfu-util
|
|
||||||
] ++ (with python3Packages; [
|
] ++ (with python3Packages; [
|
||||||
numpy matplotlib
|
numpy matplotlib
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
defaultPackage.x86_64-linux = thermostat;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import socket
|
import socket
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
class CommandError(Exception):
|
class CommandError(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -126,9 +127,8 @@ class Client:
|
||||||
'tec_u_meas': 2.5340000000000003,
|
'tec_u_meas': 2.5340000000000003,
|
||||||
'pid_output': 2.067581958092247}
|
'pid_output': 2.067581958092247}
|
||||||
"""
|
"""
|
||||||
self._command("report mode", "on")
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
self._socket.sendall("report\n".encode('utf-8'))
|
||||||
line = self._read_line()
|
line = self._read_line()
|
||||||
if not line:
|
if not line:
|
||||||
break
|
break
|
||||||
|
@ -136,6 +136,7 @@ class Client:
|
||||||
yield json.loads(line)
|
yield json.loads(line)
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
pass
|
pass
|
||||||
|
time.sleep(0.05)
|
||||||
|
|
||||||
def set_param(self, topic, channel, field="", value=""):
|
def set_param(self, topic, channel, field="", value=""):
|
||||||
"""Set configuration parameters
|
"""Set configuration parameters
|
||||||
|
|
|
@ -24,7 +24,7 @@ pub struct Channel<C: ChannelPins> {
|
||||||
pub vref_meas: ElectricPotential,
|
pub vref_meas: ElectricPotential,
|
||||||
pub shdn: C::Shdn,
|
pub shdn: C::Shdn,
|
||||||
pub vref_pin: C::VRefPin,
|
pub vref_pin: C::VRefPin,
|
||||||
pub itec_pin: C::ItecPin,
|
pub itec_pin: C::ITecPin,
|
||||||
/// feedback from `dac` output
|
/// feedback from `dac` output
|
||||||
pub dac_feedback_pin: C::DacFeedbackPin,
|
pub dac_feedback_pin: C::DacFeedbackPin,
|
||||||
pub tec_u_meas_pin: C::TecUMeasPin,
|
pub tec_u_meas_pin: C::TecUMeasPin,
|
||||||
|
|
|
@ -2,11 +2,13 @@ use smoltcp::time::{Duration, Instant};
|
||||||
use uom::si::{
|
use uom::si::{
|
||||||
f64::{
|
f64::{
|
||||||
ElectricPotential,
|
ElectricPotential,
|
||||||
|
ElectricCurrent,
|
||||||
ElectricalResistance,
|
ElectricalResistance,
|
||||||
ThermodynamicTemperature,
|
ThermodynamicTemperature,
|
||||||
Time,
|
Time,
|
||||||
},
|
},
|
||||||
electric_potential::volt,
|
electric_potential::volt,
|
||||||
|
electric_current::ampere,
|
||||||
electrical_resistance::ohm,
|
electrical_resistance::ohm,
|
||||||
thermodynamic_temperature::degree_celsius,
|
thermodynamic_temperature::degree_celsius,
|
||||||
time::millisecond,
|
time::millisecond,
|
||||||
|
@ -26,11 +28,10 @@ pub struct ChannelState {
|
||||||
pub adc_calibration: ad7172::ChannelCalibration,
|
pub adc_calibration: ad7172::ChannelCalibration,
|
||||||
pub adc_time: Instant,
|
pub adc_time: Instant,
|
||||||
pub adc_interval: Duration,
|
pub adc_interval: Duration,
|
||||||
/// VREF for the TEC (1.5V)
|
|
||||||
pub vref: ElectricPotential,
|
|
||||||
/// 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,
|
||||||
|
@ -44,10 +45,9 @@ impl ChannelState {
|
||||||
adc_time: Instant::from_secs(0),
|
adc_time: Instant::from_secs(0),
|
||||||
// default: 10 Hz
|
// default: 10 Hz
|
||||||
adc_interval: Duration::from_millis(100),
|
adc_interval: Duration::from_millis(100),
|
||||||
// updated later with Channels.read_vref()
|
|
||||||
vref: ElectricPotential::new::<volt>(1.5),
|
|
||||||
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(),
|
||||||
|
|
311
src/channels.rs
311
src/channels.rs
|
@ -1,5 +1,6 @@
|
||||||
use core::cmp::max_by;
|
use core::marker::PhantomData;
|
||||||
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;
|
||||||
|
@ -18,15 +19,43 @@ use crate::{
|
||||||
channel_state::ChannelState,
|
channel_state::ChannelState,
|
||||||
command_parser::{CenterPoint, PwmPin},
|
command_parser::{CenterPoint, PwmPin},
|
||||||
command_handler::JsonBuffer,
|
command_handler::JsonBuffer,
|
||||||
pins,
|
pins::{self, Channel0VRef, Channel1VRef},
|
||||||
steinhart_hart,
|
steinhart_hart,
|
||||||
};
|
};
|
||||||
|
use crate::timer::sleep;
|
||||||
|
|
||||||
|
pub enum PinsAdcReadTarget {
|
||||||
|
VREF,
|
||||||
|
DacVfb,
|
||||||
|
ITec,
|
||||||
|
VTec,
|
||||||
|
}
|
||||||
|
|
||||||
pub const CHANNELS: usize = 2;
|
pub const CHANNELS: usize = 2;
|
||||||
pub const R_SENSE: f64 = 0.05;
|
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,
|
||||||
|
};
|
||||||
|
const MAX_TEC_I_DUTY_TO_CURRENT_RATE: ElectricCurrent = ElectricCurrent {
|
||||||
|
dimension: PhantomData,
|
||||||
|
units: PhantomData,
|
||||||
|
value: 1.0 / (10.0 * R_SENSE / 3.3),
|
||||||
|
};
|
||||||
|
// 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
|
// TODO: -pub
|
||||||
pub struct Channels {
|
pub struct Channels {
|
||||||
channel0: Channel<Channel0>,
|
channel0: Channel<Channel0>,
|
||||||
|
@ -58,7 +87,6 @@ impl Channels {
|
||||||
let pwm = pins.pwm;
|
let pwm = pins.pwm;
|
||||||
let mut channels = Channels { channel0, channel1, adc, pins_adc, pwm };
|
let mut channels = Channels { channel0, channel1, adc, pins_adc, pwm };
|
||||||
for channel in 0..CHANNELS {
|
for channel in 0..CHANNELS {
|
||||||
channels.channel_state(channel).vref = channels.read_vref(channel);
|
|
||||||
channels.calibrate_dac_value(channel);
|
channels.calibrate_dac_value(channel);
|
||||||
channels.set_i(channel, ElectricCurrent::new::<ampere>(0.0));
|
channels.set_i(channel, ElectricCurrent::new::<ampere>(0.0));
|
||||||
}
|
}
|
||||||
|
@ -98,11 +126,8 @@ 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 => {
|
CenterPoint::Vref =>
|
||||||
let vref = self.read_vref(channel);
|
self.adc_read(channel, PinsAdcReadTarget::VREF, 8),
|
||||||
self.channel_state(channel).vref = vref;
|
|
||||||
vref
|
|
||||||
},
|
|
||||||
CenterPoint::Override(center_point) =>
|
CenterPoint::Override(center_point) =>
|
||||||
ElectricPotential::new::<volt>(center_point.into()),
|
ElectricPotential::new::<volt>(center_point.into()),
|
||||||
}
|
}
|
||||||
|
@ -115,16 +140,13 @@ impl Channels {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_i(&mut self, channel: usize) -> ElectricCurrent {
|
pub fn get_i(&mut self, channel: usize) -> ElectricCurrent {
|
||||||
let center_point = self.get_center(channel);
|
let i_set = self.channel_state(channel).i_set;
|
||||||
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
i_set
|
||||||
let voltage = self.get_dac(channel);
|
|
||||||
let i_tec = (voltage - center_point) / (10.0 * r_sense);
|
|
||||||
i_tec
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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 / 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 {
|
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(),
|
||||||
|
@ -134,7 +156,8 @@ impl Channels {
|
||||||
voltage
|
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() {
|
let vref_meas = match channel.into() {
|
||||||
0 => self.channel0.vref_meas,
|
0 => self.channel0.vref_meas,
|
||||||
1 => self.channel1.vref_meas,
|
1 => self.channel1.vref_meas,
|
||||||
|
@ -142,109 +165,112 @@ impl Channels {
|
||||||
};
|
};
|
||||||
let center_point = vref_meas;
|
let center_point = vref_meas;
|
||||||
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
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 voltage = self.set_dac(channel, voltage);
|
||||||
let i_tec = (voltage - center_point) / (10.0 * r_sense);
|
let i_set = (voltage - center_point) / (10.0 * r_sense);
|
||||||
i_tec
|
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 {
|
match channel {
|
||||||
0 => {
|
0 => {
|
||||||
let sample = self.pins_adc.convert(
|
sample = match adc_read_target {
|
||||||
&self.channel0.dac_feedback_pin,
|
PinsAdcReadTarget::VREF => {
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
match &self.channel0.vref_pin {
|
||||||
);
|
Channel0VRef::Analog(vref_pin) => {
|
||||||
let mv = self.pins_adc.sample_to_millivolts(sample);
|
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)
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
}
|
}
|
||||||
1 => {
|
1 => {
|
||||||
let sample = self.pins_adc.convert(
|
sample = match adc_read_target {
|
||||||
&self.channel1.dac_feedback_pin,
|
PinsAdcReadTarget::VREF => {
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
match &self.channel1.vref_pin {
|
||||||
);
|
Channel1VRef::Analog(vref_pin) => {
|
||||||
let mv = self.pins_adc.sample_to_millivolts(sample);
|
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)
|
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);
|
|
||||||
loop {
|
|
||||||
let current = self.read_dac_feedback(channel);
|
|
||||||
if (current - prev).abs() < tolerance {
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
prev = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,8 +300,7 @@ impl Channels {
|
||||||
let mut start_value = 1;
|
let mut start_value = 1;
|
||||||
let mut best_error = ElectricPotential::new::<volt>(100.0);
|
let mut best_error = ElectricPotential::new::<volt>(100.0);
|
||||||
|
|
||||||
for step in (0..18).rev() {
|
for step in (5..18).rev() {
|
||||||
let mut prev_value = start_value;
|
|
||||||
for value in (start_value..=ad5680::MAX_VALUE).step_by(1 << step) {
|
for value in (start_value..=ad5680::MAX_VALUE).step_by(1 << step) {
|
||||||
match channel {
|
match channel {
|
||||||
0 => {
|
0 => {
|
||||||
|
@ -286,24 +311,23 @@ impl Channels {
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
|
sleep(10);
|
||||||
|
|
||||||
let dac_feedback = self.read_dac_feedback_until_stable(channel, ElectricPotential::new::<volt>(0.001));
|
let dac_feedback = self.adc_read(channel, PinsAdcReadTarget::DacVfb, 64);
|
||||||
let error = target_voltage - dac_feedback;
|
let error = target_voltage - dac_feedback;
|
||||||
if error < ElectricPotential::new::<volt>(0.0) {
|
if error < ElectricPotential::new::<volt>(0.0) {
|
||||||
break;
|
break;
|
||||||
} else if error < best_error {
|
} else if error < best_error {
|
||||||
best_error = error;
|
best_error = error;
|
||||||
start_value = prev_value;
|
start_value = 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 {
|
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,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
prev_value = value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -355,32 +379,30 @@ 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 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
|
(duty * max, MAX_TEC_V)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 duty = self.get_pwm(channel, PwmPin::MaxIPos);
|
let duty = self.get_pwm(channel, PwmPin::MaxIPos);
|
||||||
(duty * max, max)
|
(duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, MAX_TEC_I)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 duty = self.get_pwm(channel, PwmPin::MaxINeg);
|
let duty = self.get_pwm(channel, PwmPin::MaxINeg);
|
||||||
(duty * max, max)
|
(duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, MAX_TEC_I)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current passing through TEC
|
// Get current passing through TEC
|
||||||
pub fn get_tec_i(&mut self, channel: usize) -> ElectricCurrent {
|
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
|
// Get voltage across TEC
|
||||||
pub fn get_tec_v(&mut self, channel: usize) -> ElectricPotential {
|
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 {
|
fn set_pwm(&mut self, channel: usize, pin: PwmPin, duty: f64) -> f64 {
|
||||||
|
@ -412,29 +434,28 @@ impl Channels {
|
||||||
|
|
||||||
pub fn set_max_v(&mut self, channel: usize, max_v: ElectricPotential) -> (ElectricPotential, ElectricPotential) {
|
pub fn set_max_v(&mut self, channel: usize, max_v: 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 / 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);
|
let duty = self.set_pwm(channel, PwmPin::MaxV, duty);
|
||||||
(duty * max, max)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_max_i_pos(&mut self, channel: usize, max_i_pos: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
pub fn set_max_i_pos(&mut self, channel: usize, max_i_pos: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
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_TEC_I_DUTY_TO_CURRENT_RATE).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_TEC_I_DUTY_TO_CURRENT_RATE, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_max_i_neg(&mut self, channel: usize, max_i_neg: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
pub fn set_max_i_neg(&mut self, channel: usize, max_i_neg: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
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_TEC_I_DUTY_TO_CURRENT_RATE).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_TEC_I_DUTY_TO_CURRENT_RATE, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn report(&mut self, channel: usize) -> Report {
|
fn report(&mut self, channel: usize) -> Report {
|
||||||
let vref = self.channel_state(channel).vref;
|
|
||||||
let i_set = self.get_i(channel);
|
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 tec_i = self.get_tec_i(channel);
|
||||||
let dac_value = self.get_dac(channel);
|
let dac_value = self.get_dac(channel);
|
||||||
let state = self.channel_state(channel);
|
let state = self.channel_state(channel);
|
||||||
|
@ -449,9 +470,8 @@ impl Channels {
|
||||||
.map(|temperature| temperature.get::<degree_celsius>()),
|
.map(|temperature| temperature.get::<degree_celsius>()),
|
||||||
pid_engaged: state.pid_engaged,
|
pid_engaged: state.pid_engaged,
|
||||||
i_set,
|
i_set,
|
||||||
vref,
|
|
||||||
dac_value,
|
dac_value,
|
||||||
dac_feedback: self.read_dac_feedback(channel),
|
dac_feedback: self.adc_read(channel, PinsAdcReadTarget::DacVfb, 1),
|
||||||
i_tec,
|
i_tec,
|
||||||
tec_i,
|
tec_i,
|
||||||
tec_u_meas: self.get_tec_v(channel),
|
tec_u_meas: self.get_tec_v(channel),
|
||||||
|
@ -475,12 +495,21 @@ impl Channels {
|
||||||
serde_json_core::to_vec(&summaries)
|
serde_json_core::to_vec(&summaries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn pid_engaged(&mut self) -> bool {
|
||||||
|
for channel in 0..CHANNELS {
|
||||||
|
if self.channel_state(channel).pid_engaged {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
fn pwm_summary(&mut self, channel: usize) -> PwmSummary {
|
fn pwm_summary(&mut self, channel: usize) -> PwmSummary {
|
||||||
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(channel), ElectricCurrent::new::<ampere>(3.0)).into(),
|
i_set: (self.get_i(channel), MAX_TEC_I).into(),
|
||||||
max_v: (self.get_max_v(channel), ElectricPotential::new::<volt>(5.0)).into(),
|
max_v: self.get_max_v(channel).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(),
|
||||||
}
|
}
|
||||||
|
@ -521,10 +550,11 @@ impl Channels {
|
||||||
serde_json_core::to_vec(&summaries)
|
serde_json_core::to_vec(&summaries)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_abs_max_tec_i(&mut self) -> f64 {
|
pub fn current_abs_max_tec_i(&mut self) -> ElectricCurrent {
|
||||||
max_by(self.get_tec_i(0).abs().get::<ampere>(),
|
(0..CHANNELS)
|
||||||
self.get_tec_i(1).abs().get::<ampere>(),
|
.map(|channel| self.get_tec_i(channel).abs())
|
||||||
|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal))
|
.max_by(|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal))
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -538,7 +568,6 @@ pub struct Report {
|
||||||
temperature: Option<f64>,
|
temperature: Option<f64>,
|
||||||
pid_engaged: bool,
|
pid_engaged: bool,
|
||||||
i_set: ElectricCurrent,
|
i_set: ElectricCurrent,
|
||||||
vref: ElectricPotential,
|
|
||||||
dac_value: ElectricPotential,
|
dac_value: ElectricPotential,
|
||||||
dac_feedback: ElectricPotential,
|
dac_feedback: ElectricPotential,
|
||||||
i_tec: ElectricPotential,
|
i_tec: ElectricPotential,
|
||||||
|
|
|
@ -13,7 +13,6 @@ use super::{
|
||||||
PwmPin,
|
PwmPin,
|
||||||
ShParameter
|
ShParameter
|
||||||
},
|
},
|
||||||
leds::Leds,
|
|
||||||
ad7172,
|
ad7172,
|
||||||
CHANNEL_CONFIG_KEY,
|
CHANNEL_CONFIG_KEY,
|
||||||
channels::{
|
channels::{
|
||||||
|
@ -23,7 +22,6 @@ use super::{
|
||||||
config::ChannelConfig,
|
config::ChannelConfig,
|
||||||
dfu,
|
dfu,
|
||||||
flash_store::FlashStore,
|
flash_store::FlashStore,
|
||||||
session::Session,
|
|
||||||
FanCtrl,
|
FanCtrl,
|
||||||
hw_rev::HWRev,
|
hw_rev::HWRev,
|
||||||
};
|
};
|
||||||
|
@ -88,16 +86,6 @@ fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool {
|
||||||
|
|
||||||
impl Handler {
|
impl Handler {
|
||||||
|
|
||||||
fn reporting(socket: &mut TcpSocket) -> Result<Handler, Error> {
|
|
||||||
send_line(socket, b"{}");
|
|
||||||
Ok(Handler::Handled)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn show_report_mode(socket: &mut TcpSocket, session: &Session) -> Result<Handler, Error> {
|
|
||||||
let _ = writeln!(socket, "{{ \"report\": {:?} }}", session.reporting());
|
|
||||||
Ok(Handler::Handled)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn show_report(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
fn show_report(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
||||||
match channels.reports_json() {
|
match channels.reports_json() {
|
||||||
Ok(buf) => {
|
Ok(buf) => {
|
||||||
|
@ -176,18 +164,16 @@ impl Handler {
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn engage_pid (socket: &mut TcpSocket, channels: &mut Channels, leds: &mut Leds, channel: usize) -> Result<Handler, Error> {
|
fn engage_pid (socket: &mut TcpSocket, channels: &mut Channels, channel: usize) -> Result<Handler, Error> {
|
||||||
channels.channel_state(channel).pid_engaged = true;
|
channels.channel_state(channel).pid_engaged = true;
|
||||||
leds.g3.on();
|
|
||||||
send_line(socket, b"{}");
|
send_line(socket, b"{}");
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_pwm (socket: &mut TcpSocket, channels: &mut Channels, leds: &mut Leds, channel: usize, pin: PwmPin, value: f64) -> Result<Handler, Error> {
|
fn set_pwm (socket: &mut TcpSocket, channels: &mut Channels, channel: usize, pin: PwmPin, value: f64) -> Result<Handler, Error> {
|
||||||
match pin {
|
match pin {
|
||||||
PwmPin::ISet => {
|
PwmPin::ISet => {
|
||||||
channels.channel_state(channel).pid_engaged = false;
|
channels.channel_state(channel).pid_engaged = false;
|
||||||
leds.g3.off();
|
|
||||||
let current = ElectricCurrent::new::<ampere>(value);
|
let current = ElectricCurrent::new::<ampere>(value);
|
||||||
channels.set_i(channel, current);
|
channels.set_i(channel, current);
|
||||||
channels.power_up(channel);
|
channels.power_up(channel);
|
||||||
|
@ -210,11 +196,11 @@ impl Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_center_point(socket: &mut TcpSocket, channels: &mut Channels, channel: usize, center: CenterPoint) -> Result<Handler, Error> {
|
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);
|
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_tec);
|
channels.set_i(channel, i_set);
|
||||||
}
|
}
|
||||||
send_line(socket, b"{}");
|
send_line(socket, b"{}");
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
|
@ -348,7 +334,7 @@ impl Handler {
|
||||||
|
|
||||||
fn set_fan(socket: &mut TcpSocket, fan_pwm: u32, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
fn set_fan(socket: &mut TcpSocket, fan_pwm: u32, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
||||||
if !fan_ctrl.fan_available() {
|
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);
|
return Ok(Handler::Handled);
|
||||||
}
|
}
|
||||||
fan_ctrl.set_auto_mode(false);
|
fan_ctrl.set_auto_mode(false);
|
||||||
|
@ -377,7 +363,7 @@ impl Handler {
|
||||||
|
|
||||||
fn fan_auto(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
fn fan_auto(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
||||||
if !fan_ctrl.fan_available() {
|
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);
|
return Ok(Handler::Handled);
|
||||||
}
|
}
|
||||||
fan_ctrl.set_auto_mode(true);
|
fan_ctrl.set_auto_mode(true);
|
||||||
|
@ -415,19 +401,17 @@ impl Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_command(command: Command, socket: &mut TcpSocket, channels: &mut Channels, session: &Session, leds: &mut Leds, store: &mut FlashStore, ipv4_config: &mut Ipv4Config, fan_ctrl: &mut FanCtrl, hwrev: HWRev) -> Result<Self, Error> {
|
pub fn handle_command(command: Command, socket: &mut TcpSocket, channels: &mut Channels, store: &mut FlashStore, ipv4_config: &mut Ipv4Config, fan_ctrl: &mut FanCtrl, hwrev: HWRev) -> Result<Self, Error> {
|
||||||
match command {
|
match command {
|
||||||
Command::Quit => Ok(Handler::CloseSocket),
|
Command::Quit => Ok(Handler::CloseSocket),
|
||||||
Command::Reporting(_reporting) => Handler::reporting(socket),
|
|
||||||
Command::Show(ShowCommand::Reporting) => Handler::show_report_mode(socket, session),
|
|
||||||
Command::Show(ShowCommand::Input) => Handler::show_report(socket, channels),
|
Command::Show(ShowCommand::Input) => Handler::show_report(socket, channels),
|
||||||
Command::Show(ShowCommand::Pid) => Handler::show_pid(socket, channels),
|
Command::Show(ShowCommand::Pid) => Handler::show_pid(socket, channels),
|
||||||
Command::Show(ShowCommand::Pwm) => Handler::show_pwm(socket, channels),
|
Command::Show(ShowCommand::Pwm) => Handler::show_pwm(socket, channels),
|
||||||
Command::Show(ShowCommand::SteinhartHart) => Handler::show_steinhart_hart(socket, channels),
|
Command::Show(ShowCommand::SteinhartHart) => Handler::show_steinhart_hart(socket, channels),
|
||||||
Command::Show(ShowCommand::PostFilter) => Handler::show_post_filter(socket, channels),
|
Command::Show(ShowCommand::PostFilter) => Handler::show_post_filter(socket, channels),
|
||||||
Command::Show(ShowCommand::Ipv4) => Handler::show_ipv4(socket, ipv4_config),
|
Command::Show(ShowCommand::Ipv4) => Handler::show_ipv4(socket, ipv4_config),
|
||||||
Command::PwmPid { channel } => Handler::engage_pid(socket, channels, leds, channel),
|
Command::PwmPid { channel } => Handler::engage_pid(socket, channels, channel),
|
||||||
Command::Pwm { channel, pin, value } => Handler::set_pwm(socket, channels, leds, channel, pin, value),
|
Command::Pwm { channel, pin, value } => Handler::set_pwm(socket, channels, channel, pin, value),
|
||||||
Command::CenterPoint { channel, center } => Handler::set_center_point(socket, channels, channel, center),
|
Command::CenterPoint { channel, center } => Handler::set_center_point(socket, channels, channel, center),
|
||||||
Command::Pid { channel, parameter, value } => Handler::set_pid(socket, channels, channel, parameter, value),
|
Command::Pid { channel, parameter, value } => Handler::set_pid(socket, channels, channel, parameter, value),
|
||||||
Command::SteinhartHart { channel, parameter, value } => Handler::set_steinhart_hart(socket, channels, channel, parameter, value),
|
Command::SteinhartHart { channel, parameter, value } => Handler::set_steinhart_hart(socket, channels, channel, parameter, value),
|
||||||
|
|
|
@ -96,7 +96,6 @@ pub struct Ipv4Config {
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum ShowCommand {
|
pub enum ShowCommand {
|
||||||
Input,
|
Input,
|
||||||
Reporting,
|
|
||||||
Pwm,
|
Pwm,
|
||||||
Pid,
|
Pid,
|
||||||
SteinhartHart,
|
SteinhartHart,
|
||||||
|
@ -148,7 +147,6 @@ pub enum Command {
|
||||||
Reset,
|
Reset,
|
||||||
Ipv4(Ipv4Config),
|
Ipv4(Ipv4Config),
|
||||||
Show(ShowCommand),
|
Show(ShowCommand),
|
||||||
Reporting(bool),
|
|
||||||
/// PWM parameter setting
|
/// PWM parameter setting
|
||||||
Pwm {
|
Pwm {
|
||||||
channel: usize,
|
channel: usize,
|
||||||
|
@ -233,12 +231,6 @@ fn float(input: &[u8]) -> IResult<&[u8], Result<f64, Error>> {
|
||||||
Ok((input, result))
|
Ok((input, result))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn off_on(input: &[u8]) -> IResult<&[u8], bool> {
|
|
||||||
alt((value(false, tag("off")),
|
|
||||||
value(true, tag("on"))
|
|
||||||
))(input)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn channel(input: &[u8]) -> IResult<&[u8], usize> {
|
fn channel(input: &[u8]) -> IResult<&[u8], usize> {
|
||||||
map(one_of("01"), |c| (c as usize) - ('0' as usize))(input)
|
map(one_of("01"), |c| (c as usize) - ('0' as usize))(input)
|
||||||
}
|
}
|
||||||
|
@ -246,24 +238,8 @@ fn channel(input: &[u8]) -> IResult<&[u8], usize> {
|
||||||
fn report(input: &[u8]) -> IResult<&[u8], Command> {
|
fn report(input: &[u8]) -> IResult<&[u8], Command> {
|
||||||
preceded(
|
preceded(
|
||||||
tag("report"),
|
tag("report"),
|
||||||
alt((
|
// `report` - Report once
|
||||||
preceded(
|
value(Command::Show(ShowCommand::Input), end)
|
||||||
whitespace,
|
|
||||||
preceded(
|
|
||||||
tag("mode"),
|
|
||||||
alt((
|
|
||||||
preceded(
|
|
||||||
whitespace,
|
|
||||||
// `report mode <on | off>` - Switch repoting mode
|
|
||||||
map(off_on, Command::Reporting)
|
|
||||||
),
|
|
||||||
// `report mode` - Show current reporting state
|
|
||||||
value(Command::Show(ShowCommand::Reporting), end)
|
|
||||||
))
|
|
||||||
)),
|
|
||||||
// `report` - Report once
|
|
||||||
value(Command::Show(ShowCommand::Input), end)
|
|
||||||
))
|
|
||||||
)(input)
|
)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -682,24 +658,6 @@ mod test {
|
||||||
assert_eq!(command, Ok(Command::Show(ShowCommand::Input)));
|
assert_eq!(command, Ok(Command::Show(ShowCommand::Input)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_report_mode() {
|
|
||||||
let command = Command::parse(b"report mode");
|
|
||||||
assert_eq!(command, Ok(Command::Show(ShowCommand::Reporting)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_report_mode_on() {
|
|
||||||
let command = Command::parse(b"report mode on");
|
|
||||||
assert_eq!(command, Ok(Command::Reporting(true)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_report_mode_off() {
|
|
||||||
let command = Command::parse(b"report mode off");
|
|
||||||
assert_eq!(command, Ok(Command::Reporting(false)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_pwm_i_set() {
|
fn parse_pwm_i_set() {
|
||||||
let command = Command::parse(b"pwm 1 i_set 16383");
|
let command = Command::parse(b"pwm 1 i_set 16383");
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use num_traits::Zero;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use uom::si::{
|
use uom::si::{
|
||||||
electric_potential::volt,
|
electric_potential::volt,
|
||||||
|
@ -18,6 +19,7 @@ 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,11 +35,17 @@ 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: i_set,
|
||||||
sh: state.sh.clone(),
|
sh: state.sh.clone(),
|
||||||
pwm,
|
pwm,
|
||||||
adc_postfilter,
|
adc_postfilter,
|
||||||
|
@ -59,6 +67,7 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +80,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,3 +1,4 @@
|
||||||
|
use core::arch::asm;
|
||||||
use cortex_m_rt::pre_init;
|
use cortex_m_rt::pre_init;
|
||||||
use stm32f4xx_hal::stm32::{RCC, SYSCFG};
|
use stm32f4xx_hal::stm32::{RCC, SYSCFG};
|
||||||
|
|
||||||
|
|
|
@ -4,17 +4,18 @@ use stm32f4xx_hal::{
|
||||||
pwm::{self, PwmChannels},
|
pwm::{self, PwmChannels},
|
||||||
pac::TIM8,
|
pac::TIM8,
|
||||||
};
|
};
|
||||||
|
use uom::si::{
|
||||||
|
f64::ElectricCurrent,
|
||||||
|
electric_current::ampere,
|
||||||
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
hw_rev::HWSettings,
|
hw_rev::HWSettings,
|
||||||
command_handler::JsonBuffer,
|
command_handler::JsonBuffer,
|
||||||
|
channels::MAX_TEC_I,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type FanPin = PwmChannels<TIM8, pwm::C4>;
|
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 MAX_USER_FAN_PWM: f32 = 100.0;
|
||||||
const MIN_USER_FAN_PWM: f32 = 1.0;
|
const MIN_USER_FAN_PWM: f32 = 1.0;
|
||||||
|
|
||||||
|
@ -50,10 +51,10 @@ impl FanCtrl {
|
||||||
fan_ctrl
|
fan_ctrl
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cycle(&mut self, abs_max_tec_i: f32) {
|
pub fn cycle(&mut self, abs_max_tec_i: ElectricCurrent) {
|
||||||
self.abs_max_tec_i = abs_max_tec_i;
|
self.abs_max_tec_i = abs_max_tec_i.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.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()
|
// 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;
|
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);
|
self.set_pwm(pwm);
|
||||||
|
|
31
src/main.rs
31
src/main.rs
|
@ -1,11 +1,10 @@
|
||||||
#![cfg_attr(not(test), no_std)]
|
#![cfg_attr(not(test), no_std)]
|
||||||
#![cfg_attr(not(test), no_main)]
|
#![cfg_attr(not(test), no_main)]
|
||||||
#![feature(maybe_uninit_extra, asm)]
|
|
||||||
#![cfg_attr(test, allow(unused))]
|
#![cfg_attr(test, allow(unused))]
|
||||||
// TODO: #![deny(warnings, unused)]
|
// TODO: #![deny(warnings, unused)]
|
||||||
|
|
||||||
#[cfg(not(any(feature = "semihosting", test)))]
|
#[cfg(not(any(feature = "semihosting", test)))]
|
||||||
use panic_abort as _;
|
use panic_halt as _;
|
||||||
#[cfg(all(feature = "semihosting", not(test)))]
|
#[cfg(all(feature = "semihosting", not(test)))]
|
||||||
use panic_semihosting as _;
|
use panic_semihosting as _;
|
||||||
|
|
||||||
|
@ -181,12 +180,15 @@ fn main() -> ! {
|
||||||
loop {
|
loop {
|
||||||
let mut new_ipv4_config = None;
|
let mut new_ipv4_config = None;
|
||||||
let instant = Instant::from_millis(i64::from(timer::now()));
|
let instant = Instant::from_millis(i64::from(timer::now()));
|
||||||
let updated_channel = channels.poll_adc(instant);
|
channels.poll_adc(instant);
|
||||||
if let Some(channel) = updated_channel {
|
|
||||||
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();
|
||||||
|
} else {
|
||||||
|
leds.g3.off();
|
||||||
|
}
|
||||||
|
|
||||||
let instant = Instant::from_millis(i64::from(timer::now()));
|
let instant = Instant::from_millis(i64::from(timer::now()));
|
||||||
cortex_m::interrupt::free(net::clear_pending);
|
cortex_m::interrupt::free(net::clear_pending);
|
||||||
|
@ -211,7 +213,7 @@ fn main() -> ! {
|
||||||
// Do nothing and feed more data to the line reader in the next loop cycle.
|
// Do nothing and feed more data to the line reader in the next loop cycle.
|
||||||
Ok(SessionInput::Nothing) => {}
|
Ok(SessionInput::Nothing) => {}
|
||||||
Ok(SessionInput::Command(command)) => {
|
Ok(SessionInput::Command(command)) => {
|
||||||
match Handler::handle_command(command, &mut socket, &mut channels, session, &mut leds, &mut store, &mut ipv4_config, &mut fan_ctrl, hwrev) {
|
match Handler::handle_command(command, &mut socket, &mut channels, &mut store, &mut ipv4_config, &mut fan_ctrl, hwrev) {
|
||||||
Ok(Handler::NewIPV4(ip)) => new_ipv4_config = Some(ip),
|
Ok(Handler::NewIPV4(ip)) => new_ipv4_config = Some(ip),
|
||||||
Ok(Handler::Handled) => {},
|
Ok(Handler::Handled) => {},
|
||||||
Ok(Handler::CloseSocket) => socket.close(),
|
Ok(Handler::CloseSocket) => socket.close(),
|
||||||
|
@ -226,19 +228,6 @@ fn main() -> ! {
|
||||||
Err(_) =>
|
Err(_) =>
|
||||||
socket.close(),
|
socket.close(),
|
||||||
}
|
}
|
||||||
} else if socket.can_send() {
|
|
||||||
if let Some(channel) = session.is_report_pending() {
|
|
||||||
match channels.reports_json() {
|
|
||||||
Ok(buf) => {
|
|
||||||
send_line(&mut socket, &buf[..]);
|
|
||||||
session.mark_report_sent(channel);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("unable to serialize report: {:?}", e);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -54,15 +54,13 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
34
src/pins.rs
34
src/pins.rs
|
@ -61,27 +61,37 @@ pub trait ChannelPins {
|
||||||
type DacSync: OutputPin;
|
type DacSync: OutputPin;
|
||||||
type Shdn: OutputPin;
|
type Shdn: OutputPin;
|
||||||
type VRefPin;
|
type VRefPin;
|
||||||
type ItecPin;
|
type ITecPin;
|
||||||
type DacFeedbackPin;
|
type DacFeedbackPin;
|
||||||
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 = PA0<Analog>;
|
type VRefPin = Channel0VRef;
|
||||||
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 = PA3<Analog>;
|
type VRefPin = Channel1VRef;
|
||||||
type ItecPin = PB0<Analog>;
|
type ITecPin = PB0<Analog>;
|
||||||
type DacFeedbackPin = PA5<Analog>;
|
type DacFeedbackPin = PA5<Analog>;
|
||||||
type TecUMeasPin = PC3<Analog>;
|
type TecUMeasPin = PC3<Analog>;
|
||||||
}
|
}
|
||||||
|
@ -98,7 +108,7 @@ pub struct ChannelPinSet<C: ChannelPins> {
|
||||||
pub dac_sync: C::DacSync,
|
pub dac_sync: C::DacSync,
|
||||||
pub shdn: C::Shdn,
|
pub shdn: C::Shdn,
|
||||||
pub vref_pin: C::VRefPin,
|
pub vref_pin: C::VRefPin,
|
||||||
pub itec_pin: C::ItecPin,
|
pub itec_pin: C::ITecPin,
|
||||||
pub dac_feedback_pin: C::DacFeedbackPin,
|
pub dac_feedback_pin: C::DacFeedbackPin,
|
||||||
pub tec_u_meas_pin: C::TecUMeasPin,
|
pub tec_u_meas_pin: C::TecUMeasPin,
|
||||||
}
|
}
|
||||||
|
@ -150,13 +160,17 @@ impl Pins {
|
||||||
gpioe.pe13, gpioe.pe14
|
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(
|
let (dac0_spi, dac0_sync) = Self::setup_dac0(
|
||||||
clocks, spi4,
|
clocks, spi4,
|
||||||
gpioe.pe2, gpioe.pe4, gpioe.pe6
|
gpioe.pe2, gpioe.pe4, gpioe.pe6
|
||||||
);
|
);
|
||||||
let mut shdn0 = gpioe.pe10.into_push_pull_output();
|
let mut shdn0 = gpioe.pe10.into_push_pull_output();
|
||||||
let _ = shdn0.set_low();
|
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 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();
|
||||||
|
@ -176,7 +190,7 @@ impl Pins {
|
||||||
);
|
);
|
||||||
let mut shdn1 = gpioe.pe15.into_push_pull_output();
|
let mut shdn1 = gpioe.pe15.into_push_pull_output();
|
||||||
let _ = shdn1.set_low();
|
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 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();
|
||||||
|
@ -198,10 +212,6 @@ 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(gpiod.pd9, gpiod.pd10.into_push_pull_output(), gpiod.pd11.into_push_pull_output());
|
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();
|
let eeprom_scl = gpiob.pb8.into_alternate().set_open_drain();
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use core::mem::MaybeUninit;
|
|
||||||
use smoltcp::{
|
use smoltcp::{
|
||||||
iface::EthernetInterface,
|
iface::EthernetInterface,
|
||||||
socket::{SocketSet, SocketHandle, TcpSocket, TcpSocketBuffer, SocketRef},
|
socket::{SocketSet, SocketHandle, TcpSocket, TcpSocketBuffer, SocketRef},
|
||||||
|
@ -13,6 +12,18 @@ pub struct SocketState<S> {
|
||||||
state: S,
|
state: S,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, S: Default> SocketState<S>{
|
||||||
|
fn new(sockets: &mut SocketSet<'a>, tcp_rx_storage: &'a mut [u8; TCP_RX_BUFFER_SIZE], tcp_tx_storage: &'a mut [u8; TCP_TX_BUFFER_SIZE]) -> SocketState<S> {
|
||||||
|
let tcp_rx_buffer = TcpSocketBuffer::new(&mut tcp_rx_storage[..]);
|
||||||
|
let tcp_tx_buffer = TcpSocketBuffer::new(&mut tcp_tx_storage[..]);
|
||||||
|
let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
|
||||||
|
SocketState::<S> {
|
||||||
|
handle: sockets.add(tcp_socket),
|
||||||
|
state: S::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Number of server sockets and therefore concurrent client
|
/// Number of server sockets and therefore concurrent client
|
||||||
/// sessions. Many data structures in `Server::run()` correspond to
|
/// sessions. Many data structures in `Server::run()` correspond to
|
||||||
/// this const.
|
/// this const.
|
||||||
|
@ -35,28 +46,27 @@ impl<'a, 'b, S: Default> Server<'a, 'b, S> {
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Server<'a, '_, S>),
|
F: FnOnce(&mut Server<'a, '_, S>),
|
||||||
{
|
{
|
||||||
let mut sockets_storage: [_; SOCKET_COUNT] = Default::default();
|
macro_rules! create_rtx_storage {
|
||||||
let mut sockets = SocketSet::new(&mut sockets_storage[..]);
|
($rx_storage:ident, $tx_storage:ident) => {
|
||||||
let mut states: [SocketState<S>; SOCKET_COUNT] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
||||||
|
|
||||||
macro_rules! create_socket {
|
|
||||||
($set:ident, $rx_storage:ident, $tx_storage:ident, $target:expr) => {
|
|
||||||
let mut $rx_storage = [0; TCP_RX_BUFFER_SIZE];
|
let mut $rx_storage = [0; TCP_RX_BUFFER_SIZE];
|
||||||
let mut $tx_storage = [0; TCP_TX_BUFFER_SIZE];
|
let mut $tx_storage = [0; TCP_TX_BUFFER_SIZE];
|
||||||
let tcp_rx_buffer = TcpSocketBuffer::new(&mut $rx_storage[..]);
|
|
||||||
let tcp_tx_buffer = TcpSocketBuffer::new(&mut $tx_storage[..]);
|
|
||||||
let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
|
|
||||||
$target = $set.add(tcp_socket);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
create_socket!(sockets, tcp_rx_storage0, tcp_tx_storage0, states[0].handle);
|
|
||||||
create_socket!(sockets, tcp_rx_storage1, tcp_tx_storage1, states[1].handle);
|
|
||||||
create_socket!(sockets, tcp_rx_storage2, tcp_tx_storage2, states[2].handle);
|
|
||||||
create_socket!(sockets, tcp_rx_storage3, tcp_tx_storage3, states[3].handle);
|
|
||||||
|
|
||||||
for state in &mut states {
|
create_rtx_storage!(tcp_rx_storage0, tcp_tx_storage0);
|
||||||
state.state = S::default();
|
create_rtx_storage!(tcp_rx_storage1, tcp_tx_storage1);
|
||||||
}
|
create_rtx_storage!(tcp_rx_storage2, tcp_tx_storage2);
|
||||||
|
create_rtx_storage!(tcp_rx_storage3, tcp_tx_storage3);
|
||||||
|
|
||||||
|
let mut sockets_storage: [_; SOCKET_COUNT] = Default::default();
|
||||||
|
let mut sockets = SocketSet::new(&mut sockets_storage[..]);
|
||||||
|
|
||||||
|
let states: [SocketState<S>; SOCKET_COUNT] = [
|
||||||
|
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage0, &mut tcp_tx_storage0),
|
||||||
|
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage1, &mut tcp_tx_storage1),
|
||||||
|
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage2, &mut tcp_tx_storage2),
|
||||||
|
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage3, &mut tcp_tx_storage3),
|
||||||
|
];
|
||||||
|
|
||||||
let mut server = Server {
|
let mut server = Server {
|
||||||
states,
|
states,
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use super::command_parser::{Command, Error as ParserError};
|
use super::command_parser::{Command, Error as ParserError};
|
||||||
use super::channels::CHANNELS;
|
|
||||||
|
|
||||||
const MAX_LINE_LEN: usize = 64;
|
const MAX_LINE_LEN: usize = 64;
|
||||||
|
|
||||||
|
@ -53,8 +52,6 @@ impl From<Result<Command, ParserError>> for SessionInput {
|
||||||
|
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
reader: LineReader,
|
reader: LineReader,
|
||||||
reporting: bool,
|
|
||||||
report_pending: [bool; CHANNELS],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Session {
|
impl Default for Session {
|
||||||
|
@ -67,43 +64,11 @@ impl Session {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Session {
|
Session {
|
||||||
reader: LineReader::new(),
|
reader: LineReader::new(),
|
||||||
reporting: false,
|
|
||||||
report_pending: [false; CHANNELS],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(&mut self) {
|
pub fn reset(&mut self) {
|
||||||
self.reader = LineReader::new();
|
self.reader = LineReader::new();
|
||||||
self.reporting = false;
|
|
||||||
self.report_pending = [false; CHANNELS];
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn reporting(&self) -> bool {
|
|
||||||
self.reporting
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_report_pending(&mut self, channel: usize) {
|
|
||||||
if self.reporting {
|
|
||||||
self.report_pending[channel] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_report_pending(&self) -> Option<usize> {
|
|
||||||
if ! self.reporting {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
self.report_pending.iter()
|
|
||||||
.enumerate()
|
|
||||||
.fold(None, |result, (channel, report_pending)| {
|
|
||||||
result.or_else(|| {
|
|
||||||
if *report_pending { Some(channel) } else { None }
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mark_report_sent(&mut self, channel: usize) {
|
|
||||||
self.report_pending[channel] = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn feed(&mut self, buf: &[u8]) -> (usize, SessionInput) {
|
pub fn feed(&mut self, buf: &[u8]) -> (usize, SessionInput) {
|
||||||
|
@ -114,12 +79,6 @@ impl Session {
|
||||||
match line {
|
match line {
|
||||||
Some(line) => {
|
Some(line) => {
|
||||||
let command = Command::parse(&line);
|
let command = Command::parse(&line);
|
||||||
match command {
|
|
||||||
Ok(Command::Reporting(reporting)) => {
|
|
||||||
self.reporting = reporting;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
return (buf_bytes, command.into());
|
return (buf_bytes, command.into());
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
|
|
Loading…
Reference in New Issue