Compare commits
1 Commits
master
...
correct-cl
Author | SHA1 | Date | |
---|---|---|---|
43c4da084d |
45
README.md
45
README.md
@ -111,8 +111,8 @@ formatted as line-delimited JSON.
|
|||||||
| `pid <0/1> kd <value>` | Set differential gain |
|
| `pid <0/1> kd <value>` | Set differential gain |
|
||||||
| `pid <0/1> output_min <amp>` | Set mininum output |
|
| `pid <0/1> output_min <amp>` | Set mininum output |
|
||||||
| `pid <0/1> output_max <amp>` | Set maximum output |
|
| `pid <0/1> output_max <amp>` | Set maximum output |
|
||||||
| `b-p` | Show B-Parameter equation parameters |
|
| `s-h` | Show Steinhart-Hart equation parameters |
|
||||||
| `b-p <0/1> <t0/b/r0> <value>` | Set B-Parameter for a channel |
|
| `s-h <0/1> <t0/b/r0> <value>` | Set Steinhart-Hart parameter for a channel |
|
||||||
| `postfilter` | Show postfilter settings |
|
| `postfilter` | Show postfilter settings |
|
||||||
| `postfilter <0/1> off` | Disable postfilter |
|
| `postfilter <0/1> off` | Disable postfilter |
|
||||||
| `postfilter <0/1> rate <rate>` | Set postfilter output data rate |
|
| `postfilter <0/1> rate <rate>` | Set postfilter output data rate |
|
||||||
@ -144,22 +144,22 @@ output will be truncated when USB buffers are full.
|
|||||||
|
|
||||||
Connect the thermistor with the SENS pins of the
|
Connect the thermistor with the SENS pins of the
|
||||||
device. Temperature-depending resistance is measured by the AD7172
|
device. Temperature-depending resistance is measured by the AD7172
|
||||||
ADC. To prepare conversion to a temperature, set the parameters
|
ADC. To prepare conversion to a temperature, set the Beta parameters
|
||||||
for the B-Parameter equation.
|
for the Steinhart-Hart equation.
|
||||||
|
|
||||||
Set the base temperature in degrees celsius for the channel 0 thermistor:
|
Set the base temperature in degrees celsius for the channel 0 thermistor:
|
||||||
```
|
```
|
||||||
b-p 0 t0 20
|
s-h 0 t0 20
|
||||||
```
|
```
|
||||||
|
|
||||||
Set the resistance in Ohms measured at the base temperature t0:
|
Set the resistance in Ohms measured at the base temperature t0:
|
||||||
```
|
```
|
||||||
b-p 0 r0 10000
|
s-h 0 r0 10000
|
||||||
```
|
```
|
||||||
|
|
||||||
Set the Beta parameter:
|
Set the Beta parameter:
|
||||||
```
|
```
|
||||||
b-p 0 b 3800
|
s-h 0 b 3800
|
||||||
```
|
```
|
||||||
|
|
||||||
### 50/60 Hz filtering
|
### 50/60 Hz filtering
|
||||||
@ -189,30 +189,31 @@ Testing heat flow direction with a low set current is recommended before install
|
|||||||
|
|
||||||
### Limits
|
### Limits
|
||||||
|
|
||||||
Each channel has maximum value settings, for setting
|
Each MAX1968 TEC driver has analog/PWM inputs for setting
|
||||||
output limits.
|
output limits.
|
||||||
|
|
||||||
Use the `output` command to see them.
|
Use the `output` command to see current settings and maximum values.
|
||||||
|
|
||||||
| Limit | Unit | Description |
|
| Limit | Unit | Description |
|
||||||
| --- | :---: | --- |
|
| --- | :---: | --- |
|
||||||
| `max_v` | Volts | Maximum voltage |
|
| `max_v` | Volts | Maximum voltage |
|
||||||
| `max_i_pos` | Amperes | Maximum positive current |
|
| `max_i_pos` | Amperes | Maximum positive current |
|
||||||
| `max_i_neg` | Amperes | Maximum negative current |
|
| `max_i_neg` | Amperes | Maximum negative current |
|
||||||
|
| `i_set` | Amperes | (Not a limit; Open-loop mode) |
|
||||||
|
|
||||||
Example: set the maximum voltage of channel 0 to 1.5 V.
|
Example: set the maximum voltage of channel 0 to 1.5 V.
|
||||||
```
|
```
|
||||||
output 0 max_v 1.5
|
output 0 max_v 1.5
|
||||||
```
|
```
|
||||||
|
|
||||||
Example: set the maximum negative current of channel 0 to -2 A.
|
Example: set the maximum negative current of channel 0 to -3 A.
|
||||||
```
|
```
|
||||||
output 0 max_i_neg 2
|
output 0 max_i_neg 3
|
||||||
```
|
```
|
||||||
|
|
||||||
Example: set the maximum positive current of channel 1 to 2 A.
|
Example: set the maximum positive current of channel 1 to 3 A.
|
||||||
```
|
```
|
||||||
output 1 max_i_pos 2
|
output 0 max_i_pos 3
|
||||||
```
|
```
|
||||||
|
|
||||||
### Open-loop mode
|
### Open-loop mode
|
||||||
@ -239,22 +240,6 @@ of channel 0 to the PID algorithm:
|
|||||||
output 0 pid
|
output 0 pid
|
||||||
```
|
```
|
||||||
|
|
||||||
### PID output clamping
|
|
||||||
|
|
||||||
It is possible to clamp the PID algorithm output independently of channel output limits. This is desirable when e.g. there is a need to keep the current value above a certain threshold in closed-loop mode.
|
|
||||||
|
|
||||||
Note that the actual output will still ultimately be limited by the `max_i_pos` and `max_i_neg` values.
|
|
||||||
|
|
||||||
Set PID maximum output of channel 0 to 1.5 A.
|
|
||||||
```
|
|
||||||
pid 0 output_max 1.5
|
|
||||||
```
|
|
||||||
|
|
||||||
Set PID minimum output of channel 0 to 0.1 A.
|
|
||||||
```
|
|
||||||
pid 0 output_min 0.1
|
|
||||||
```
|
|
||||||
|
|
||||||
## LED indicators
|
## LED indicators
|
||||||
|
|
||||||
| Name | Color | Meaning |
|
| Name | Color | Meaning |
|
||||||
@ -275,7 +260,7 @@ with the following keys.
|
|||||||
| `interval` | Seconds | Time elapsed since last report update on channel |
|
| `interval` | Seconds | Time elapsed since last report update on channel |
|
||||||
| `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 | B-Parameter 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 |
|
||||||
| `dac_value` | Volts | AD5680 output derived from `i_set` |
|
| `dac_value` | Volts | AD5680 output derived from `i_set` |
|
||||||
|
@ -13,7 +13,7 @@ When tuning Thermostat PID parameters, it is helpful to view the temperature, PI
|
|||||||
To use the Python real-time plotting utility, run
|
To use the Python real-time plotting utility, run
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
python pythermostat/pythermostat/plot.py
|
python pytec/plot.py
|
||||||
```
|
```
|
||||||
|
|
||||||
![default view](./assets/default%20view.png)
|
![default view](./assets/default%20view.png)
|
||||||
@ -44,12 +44,12 @@ Below are some general guidelines for manually tuning PID loops. Note that every
|
|||||||
|
|
||||||
## Auto Tuning
|
## Auto Tuning
|
||||||
|
|
||||||
A PID auto tuning utility is provided in the PyThermostat library. The auto tuning utility drives the the load to a controlled oscillation, observes the ultimate gain and oscillation period and calculates a set of PID parameters.
|
A PID auto tuning utility is provided in the Pytec library. The auto tuning utility drives the the load to a controlled oscillation, observes the ultimate gain and oscillation period and calculates a set of PID parameters.
|
||||||
|
|
||||||
To run the auto tuning utility, run
|
To run the auto tuning utility, run
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
python pythermostat/pythermostat/autotune.py
|
python pytec/autotune.py
|
||||||
```
|
```
|
||||||
|
|
||||||
After some time, the auto tuning utility will output the auto tuning results, below is a sample output
|
After some time, the auto tuning utility will output the auto tuning results, below is a sample output
|
||||||
|
55
flake.nix
55
flake.nix
@ -2,22 +2,14 @@
|
|||||||
description = "Firmware for the Sinara 8451 Thermostat";
|
description = "Firmware for the Sinara 8451 Thermostat";
|
||||||
|
|
||||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
|
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
|
||||||
inputs.rust-overlay = {
|
inputs.rust-overlay = {
|
||||||
url = "github:oxalica/rust-overlay";
|
url = "github:oxalica/rust-overlay";
|
||||||
inputs.nixpkgs.follows = "nixpkgs";
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs =
|
outputs = { self, nixpkgs, rust-overlay }:
|
||||||
{
|
|
||||||
self,
|
|
||||||
nixpkgs,
|
|
||||||
rust-overlay,
|
|
||||||
}:
|
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs {
|
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import rust-overlay) ]; };
|
||||||
system = "x86_64-linux";
|
|
||||||
overlays = [ (import rust-overlay) ];
|
|
||||||
};
|
|
||||||
|
|
||||||
rust = pkgs.rust-bin.stable."1.66.0".default.override {
|
rust = pkgs.rust-bin.stable."1.66.0".default.override {
|
||||||
extensions = [ "rust-src" ];
|
extensions = [ "rust-src" ];
|
||||||
@ -33,7 +25,7 @@
|
|||||||
version = "0.0.0";
|
version = "0.0.0";
|
||||||
|
|
||||||
src = self;
|
src = self;
|
||||||
cargoLock = {
|
cargoLock = {
|
||||||
lockFile = ./Cargo.lock;
|
lockFile = ./Cargo.lock;
|
||||||
outputHashes = {
|
outputHashes = {
|
||||||
"stm32-eth-0.2.0" = "sha256-48RpZgagUqgVeKm7GXdk3Oo0v19ScF9Uby0nTFlve2o=";
|
"stm32-eth-0.2.0" = "sha256-48RpZgagUqgVeKm7GXdk3Oo0v19ScF9Uby0nTFlve2o=";
|
||||||
@ -57,23 +49,9 @@
|
|||||||
dontFixup = true;
|
dontFixup = true;
|
||||||
auditable = false;
|
auditable = false;
|
||||||
};
|
};
|
||||||
|
in {
|
||||||
pythermostat = pkgs.python3Packages.buildPythonPackage {
|
|
||||||
pname = "pythermostat";
|
|
||||||
version = "0.0.0";
|
|
||||||
format = "pyproject";
|
|
||||||
src = "${self}/pythermostat";
|
|
||||||
|
|
||||||
propagatedBuildInputs =
|
|
||||||
with pkgs.python3Packages; [
|
|
||||||
numpy
|
|
||||||
matplotlib
|
|
||||||
];
|
|
||||||
};
|
|
||||||
in
|
|
||||||
{
|
|
||||||
packages.x86_64-linux = {
|
packages.x86_64-linux = {
|
||||||
inherit thermostat pythermostat;
|
inherit thermostat;
|
||||||
default = thermostat;
|
default = thermostat;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -83,21 +61,12 @@
|
|||||||
|
|
||||||
devShells.x86_64-linux.default = pkgs.mkShellNoCC {
|
devShells.x86_64-linux.default = pkgs.mkShellNoCC {
|
||||||
name = "thermostat-dev-shell";
|
name = "thermostat-dev-shell";
|
||||||
packages =
|
packages = with pkgs; [
|
||||||
with pkgs;
|
rust llvm
|
||||||
[
|
openocd dfu-util rlwrap
|
||||||
rust
|
] ++ (with python3Packages; [
|
||||||
llvm
|
numpy matplotlib
|
||||||
openocd
|
|
||||||
dfu-util
|
|
||||||
rlwrap
|
|
||||||
]
|
|
||||||
++ (with python3Packages; [
|
|
||||||
numpy
|
|
||||||
matplotlib
|
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt-rfc-style;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -1,10 +1,9 @@
|
|||||||
import math
|
import math
|
||||||
import logging
|
import logging
|
||||||
import time
|
|
||||||
from collections import deque, namedtuple
|
from collections import deque, namedtuple
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
from pythermostat.client import Client
|
from pytec.client import Client
|
||||||
|
|
||||||
# Based on hirshmann pid-autotune libiary
|
# Based on hirshmann pid-autotune libiary
|
||||||
# See https://github.com/hirschmann/pid-autotune
|
# See https://github.com/hirschmann/pid-autotune
|
||||||
@ -237,14 +236,13 @@ def main():
|
|||||||
|
|
||||||
tec = Client()
|
tec = Client()
|
||||||
|
|
||||||
data = tec.get_report()
|
data = next(tec.report_mode())
|
||||||
ch = data[channel]
|
ch = data[channel]
|
||||||
|
|
||||||
tuner = PIDAutotune(target_temperature, output_step,
|
tuner = PIDAutotune(target_temperature, output_step,
|
||||||
lookback, noiseband, ch['interval'])
|
lookback, noiseband, ch['interval'])
|
||||||
|
|
||||||
while True:
|
for data in tec.report_mode():
|
||||||
data = tec.get_report()
|
|
||||||
|
|
||||||
ch = data[channel]
|
ch = data[channel]
|
||||||
|
|
||||||
@ -257,8 +255,6 @@ def main():
|
|||||||
|
|
||||||
tec.set_param("output", channel, "i_set", tuner_out)
|
tec.set_param("output", channel, "i_set", tuner_out)
|
||||||
|
|
||||||
time.sleep(0.05)
|
|
||||||
|
|
||||||
tec.set_param("output", channel, "i_set", 0)
|
tec.set_param("output", channel, "i_set", 0)
|
||||||
|
|
||||||
|
|
11
pytec/example.py
Normal file
11
pytec/example.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from pytec.client import Client
|
||||||
|
|
||||||
|
tec = Client() #(host="localhost", port=6667)
|
||||||
|
tec.set_param("s-h", 1, "t0", 20)
|
||||||
|
print(tec.get_output())
|
||||||
|
print(tec.get_pid())
|
||||||
|
print(tec.get_output())
|
||||||
|
print(tec.get_postfilter())
|
||||||
|
print(tec.get_steinhart_hart())
|
||||||
|
for data in tec.report_mode():
|
||||||
|
print(data)
|
128
pytec/plot.py
Normal file
128
pytec/plot.py
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import matplotlib.animation as animation
|
||||||
|
from threading import Thread, Lock
|
||||||
|
from pytec.client import Client
|
||||||
|
|
||||||
|
TIME_WINDOW = 300.0
|
||||||
|
|
||||||
|
tec = Client()
|
||||||
|
target_temperature = tec.get_pid()[0]['target']
|
||||||
|
print("Channel 0 target temperature: {:.3f}".format(target_temperature))
|
||||||
|
|
||||||
|
class Series:
|
||||||
|
def __init__(self, conv=lambda x: x):
|
||||||
|
self.conv = conv
|
||||||
|
self.x_data = []
|
||||||
|
self.y_data = []
|
||||||
|
|
||||||
|
def append(self, x, y):
|
||||||
|
self.x_data.append(x)
|
||||||
|
self.y_data.append(self.conv(y))
|
||||||
|
|
||||||
|
def clip(self, min_x):
|
||||||
|
drop = 0
|
||||||
|
while drop < len(self.x_data) and self.x_data[drop] < min_x:
|
||||||
|
drop += 1
|
||||||
|
self.x_data = self.x_data[drop:]
|
||||||
|
self.y_data = self.y_data[drop:]
|
||||||
|
|
||||||
|
series = {
|
||||||
|
# 'adc': Series(),
|
||||||
|
# 'sens': Series(lambda x: x * 0.0001),
|
||||||
|
'temperature': Series(),
|
||||||
|
# 'i_set': Series(),
|
||||||
|
'pid_output': Series(),
|
||||||
|
# 'vref': Series(),
|
||||||
|
# 'dac_value': Series(),
|
||||||
|
# 'dac_feedback': Series(),
|
||||||
|
# 'i_tec': Series(),
|
||||||
|
'tec_i': Series(),
|
||||||
|
'tec_u_meas': Series(),
|
||||||
|
# 'interval': Series(),
|
||||||
|
}
|
||||||
|
series_lock = Lock()
|
||||||
|
|
||||||
|
quit = False
|
||||||
|
|
||||||
|
def recv_data(tec):
|
||||||
|
global last_packet_time
|
||||||
|
for data in tec.report_mode():
|
||||||
|
ch0 = data[0]
|
||||||
|
series_lock.acquire()
|
||||||
|
try:
|
||||||
|
for k, s in series.items():
|
||||||
|
if k in ch0:
|
||||||
|
v = ch0[k]
|
||||||
|
if type(v) is float:
|
||||||
|
s.append(ch0['time'], v)
|
||||||
|
finally:
|
||||||
|
series_lock.release()
|
||||||
|
|
||||||
|
if quit:
|
||||||
|
break
|
||||||
|
|
||||||
|
thread = Thread(target=recv_data, args=(tec,))
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
fig, ax = plt.subplots()
|
||||||
|
|
||||||
|
for k, s in series.items():
|
||||||
|
s.plot, = ax.plot([], [], label=k)
|
||||||
|
legend = ax.legend()
|
||||||
|
|
||||||
|
def animate(i):
|
||||||
|
min_x, max_x, min_y, max_y = None, None, None, None
|
||||||
|
|
||||||
|
series_lock.acquire()
|
||||||
|
try:
|
||||||
|
for k, s in series.items():
|
||||||
|
s.plot.set_data(s.x_data, s.y_data)
|
||||||
|
if len(s.y_data) > 0:
|
||||||
|
s.plot.set_label("{}: {:.3f}".format(k, s.y_data[-1]))
|
||||||
|
|
||||||
|
if len(s.x_data) > 0:
|
||||||
|
min_x_ = min(s.x_data)
|
||||||
|
if min_x is None:
|
||||||
|
min_x = min_x_
|
||||||
|
else:
|
||||||
|
min_x = min(min_x, min_x_)
|
||||||
|
max_x_ = max(s.x_data)
|
||||||
|
if max_x is None:
|
||||||
|
max_x = max_x_
|
||||||
|
else:
|
||||||
|
max_x = max(max_x, max_x_)
|
||||||
|
if len(s.y_data) > 0:
|
||||||
|
min_y_ = min(s.y_data)
|
||||||
|
if min_y is None:
|
||||||
|
min_y = min_y_
|
||||||
|
else:
|
||||||
|
min_y = min(min_y, min_y_)
|
||||||
|
max_y_ = max(s.y_data)
|
||||||
|
if max_y is None:
|
||||||
|
max_y = max_y_
|
||||||
|
else:
|
||||||
|
max_y = max(max_y, max_y_)
|
||||||
|
|
||||||
|
if min_x and max_x - TIME_WINDOW > min_x:
|
||||||
|
for s in series.values():
|
||||||
|
s.clip(max_x - TIME_WINDOW)
|
||||||
|
finally:
|
||||||
|
series_lock.release()
|
||||||
|
|
||||||
|
if min_x != max_x:
|
||||||
|
ax.set_xlim(min_x, max_x)
|
||||||
|
if min_y != max_y:
|
||||||
|
margin_y = 0.01 * (max_y - min_y)
|
||||||
|
ax.set_ylim(min_y - margin_y, max_y + margin_y)
|
||||||
|
|
||||||
|
global legend
|
||||||
|
legend.remove()
|
||||||
|
legend = ax.legend()
|
||||||
|
|
||||||
|
ani = animation.FuncAnimation(
|
||||||
|
fig, animate, interval=1, blit=False, save_count=50)
|
||||||
|
|
||||||
|
plt.show()
|
||||||
|
quit = True
|
||||||
|
thread.join()
|
@ -1,7 +1,7 @@
|
|||||||
import socket
|
import socket
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
class CommandError(Exception):
|
class CommandError(Exception):
|
||||||
pass
|
pass
|
||||||
@ -12,10 +12,6 @@ class Client:
|
|||||||
self._lines = [""]
|
self._lines = [""]
|
||||||
self._check_zero_limits()
|
self._check_zero_limits()
|
||||||
|
|
||||||
def disconnect(self):
|
|
||||||
self._socket.shutdown(socket.SHUT_RDWR)
|
|
||||||
self._socket.close()
|
|
||||||
|
|
||||||
def _check_zero_limits(self):
|
def _check_zero_limits(self):
|
||||||
output_report = self.get_output()
|
output_report = self.get_output()
|
||||||
for output_channel in output_report:
|
for output_channel in output_report:
|
||||||
@ -96,14 +92,14 @@ class Client:
|
|||||||
"""
|
"""
|
||||||
return self._get_conf("pid")
|
return self._get_conf("pid")
|
||||||
|
|
||||||
def get_b_parameter(self):
|
def get_steinhart_hart(self):
|
||||||
"""Retrieve B-Parameter equation parameters for resistance to temperature conversion
|
"""Retrieve Steinhart-Hart parameters for resistance to temperature conversion
|
||||||
|
|
||||||
Example::
|
Example::
|
||||||
[{'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 0},
|
[{'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 0},
|
||||||
{'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 1}]
|
{'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 1}]
|
||||||
"""
|
"""
|
||||||
return self._get_conf("b-p")
|
return self._get_conf("s-h")
|
||||||
|
|
||||||
def get_postfilter(self):
|
def get_postfilter(self):
|
||||||
"""Retrieve DAC postfilter configuration
|
"""Retrieve DAC postfilter configuration
|
||||||
@ -114,18 +110,18 @@ class Client:
|
|||||||
"""
|
"""
|
||||||
return self._get_conf("postfilter")
|
return self._get_conf("postfilter")
|
||||||
|
|
||||||
def get_report(self):
|
def report_mode(self):
|
||||||
"""Obtain one-time report on measurement values
|
"""Start reporting measurement values
|
||||||
|
|
||||||
Example of yielded data::
|
Example of yielded data::
|
||||||
{'channel': 0,
|
{'channel': 0,
|
||||||
'time': 2302524,
|
'time': 2302524,
|
||||||
'interval': 0.12
|
|
||||||
'adc': 0.6199188965423515,
|
'adc': 0.6199188965423515,
|
||||||
'sens': 6138.519310282602,
|
'sens': 6138.519310282602,
|
||||||
'temperature': 36.87032392655527,
|
'temperature': 36.87032392655527,
|
||||||
'pid_engaged': True,
|
'pid_engaged': True,
|
||||||
'i_set': 2.0635816680889123,
|
'i_set': 2.0635816680889123,
|
||||||
|
'vref': 1.494,
|
||||||
'dac_value': 2.527790834044456,
|
'dac_value': 2.527790834044456,
|
||||||
'dac_feedback': 2.523,
|
'dac_feedback': 2.523,
|
||||||
'i_tec': 2.331,
|
'i_tec': 2.331,
|
||||||
@ -133,19 +129,16 @@ class Client:
|
|||||||
'tec_u_meas': 2.5340000000000003,
|
'tec_u_meas': 2.5340000000000003,
|
||||||
'pid_output': 2.067581958092247}
|
'pid_output': 2.067581958092247}
|
||||||
"""
|
"""
|
||||||
return self._get_conf("report")
|
while True:
|
||||||
|
self._socket.sendall("report\n".encode('utf-8'))
|
||||||
def get_ipv4(self):
|
line = self._read_line()
|
||||||
"""Get the IPv4 settings of the Thermostat"""
|
if not line:
|
||||||
return self._command("ipv4")
|
break
|
||||||
|
try:
|
||||||
def get_fan(self):
|
yield json.loads(line)
|
||||||
"""Get Thermostat current fan settings"""
|
except json.decoder.JSONDecodeError:
|
||||||
return self._command("fan")
|
pass
|
||||||
|
time.sleep(0.05)
|
||||||
def get_hwrev(self):
|
|
||||||
"""Get Thermostat hardware revision"""
|
|
||||||
return self._command("hwrev")
|
|
||||||
|
|
||||||
def set_param(self, topic, channel, field="", value=""):
|
def set_param(self, topic, channel, field="", value=""):
|
||||||
"""Set configuration parameters
|
"""Set configuration parameters
|
||||||
@ -153,7 +146,7 @@ class Client:
|
|||||||
Examples::
|
Examples::
|
||||||
tec.set_param("output", 0, "max_v", 2.0)
|
tec.set_param("output", 0, "max_v", 2.0)
|
||||||
tec.set_param("pid", 1, "output_max", 2.5)
|
tec.set_param("pid", 1, "output_max", 2.5)
|
||||||
tec.set_param("b-p", 0, "t0", 20.0)
|
tec.set_param("s-h", 0, "t0", 20.0)
|
||||||
tec.set_param("center", 0, "vref")
|
tec.set_param("center", 0, "vref")
|
||||||
tec.set_param("postfilter", 1, 21)
|
tec.set_param("postfilter", 1, 21)
|
||||||
|
|
||||||
@ -170,38 +163,10 @@ class Client:
|
|||||||
self.set_param("pid", channel, "target", value=target)
|
self.set_param("pid", channel, "target", value=target)
|
||||||
self.set_param("output", channel, "pid")
|
self.set_param("output", channel, "pid")
|
||||||
|
|
||||||
def save_config(self, channel=""):
|
def save_config(self):
|
||||||
"""Save current configuration to EEPROM"""
|
"""Save current configuration to EEPROM"""
|
||||||
self._command("save", channel)
|
self._command("save")
|
||||||
if channel != "":
|
|
||||||
self._read_line() # read the extra {}
|
|
||||||
|
|
||||||
def load_config(self, channel=""):
|
def load_config(self):
|
||||||
"""Load current configuration from EEPROM"""
|
"""Load current configuration from EEPROM"""
|
||||||
self._command("load", channel)
|
self._command("load")
|
||||||
if channel != "":
|
|
||||||
self._read_line() # read the extra {}
|
|
||||||
|
|
||||||
def reset(self):
|
|
||||||
"""Reset the device"""
|
|
||||||
self._socket.sendall("reset".encode("utf-8"))
|
|
||||||
self.disconnect() # resetting ends the TCP session, disconnect anyway
|
|
||||||
|
|
||||||
def enter_dfu_mode(self):
|
|
||||||
"""Reset device and enters USB device firmware update (DFU) mode"""
|
|
||||||
self._socket.sendall("dfu".encode("utf-8"))
|
|
||||||
self.disconnect() # resetting ends the TCP session, disconnect anyway
|
|
||||||
|
|
||||||
def set_ipv4(self, address, netmask, gateway=""):
|
|
||||||
"""Configure IPv4 address, netmask length, and optional default gateway"""
|
|
||||||
self._command("ipv4", f"{address}/{netmask}", gateway)
|
|
||||||
|
|
||||||
def set_fan(self, power=None):
|
|
||||||
"""Set fan power with values from 1 to 100. If omitted, set according to fcurve"""
|
|
||||||
if power is None:
|
|
||||||
power = "auto"
|
|
||||||
self._command("fan", power)
|
|
||||||
|
|
||||||
def set_fcurve(self, a=1.0, b=0.0, c=0.0):
|
|
||||||
"""Set fan controller curve coefficients"""
|
|
||||||
self._command("fcurve", a, b, c)
|
|
12
pytec/setup.py
Normal file
12
pytec/setup.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name="pytec",
|
||||||
|
version="0.0",
|
||||||
|
author="M-Labs",
|
||||||
|
url="https://git.m-labs.hk/M-Labs/thermostat",
|
||||||
|
description="Control TEC",
|
||||||
|
license="GPLv3",
|
||||||
|
install_requires=["setuptools"],
|
||||||
|
packages=find_packages(),
|
||||||
|
)
|
@ -1,13 +0,0 @@
|
|||||||
import time
|
|
||||||
from pythermostat.client import Client
|
|
||||||
|
|
||||||
tec = Client() #(host="localhost", port=6667)
|
|
||||||
tec.set_param("b-p", 1, "t0", 20)
|
|
||||||
print(tec.get_output())
|
|
||||||
print(tec.get_pid())
|
|
||||||
print(tec.get_output())
|
|
||||||
print(tec.get_postfilter())
|
|
||||||
print(tec.get_b_parameter())
|
|
||||||
while True:
|
|
||||||
print(tec.get_report())
|
|
||||||
time.sleep(0.05)
|
|
@ -1,18 +0,0 @@
|
|||||||
[build-system]
|
|
||||||
requires = ["setuptools"]
|
|
||||||
build-backend = "setuptools.build_meta"
|
|
||||||
|
|
||||||
[project]
|
|
||||||
name = "pythermostat"
|
|
||||||
version = "0.0"
|
|
||||||
authors = [{name = "M-Labs"}]
|
|
||||||
description = "Python utilities for the Sinara 8451 Thermostat"
|
|
||||||
urls.Repository = "https://git.m-labs.hk/M-Labs/thermostat"
|
|
||||||
license = {text = "GPLv3"}
|
|
||||||
|
|
||||||
[project.gui-scripts]
|
|
||||||
thermostat_plot = "pythermostat.plot:main"
|
|
||||||
|
|
||||||
[project.scripts]
|
|
||||||
thermostat_autotune = "pythermostat.autotune:main"
|
|
||||||
thermostat_test = "pythermostat.test:main"
|
|
@ -1,137 +0,0 @@
|
|||||||
import time
|
|
||||||
import numpy as np
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
import matplotlib.animation as animation
|
|
||||||
from threading import Thread, Lock
|
|
||||||
from pythermostat.client import Client
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
TIME_WINDOW = 300.0
|
|
||||||
|
|
||||||
tec = Client()
|
|
||||||
target_temperature = tec.get_pid()[0]['target']
|
|
||||||
print("Channel 0 target temperature: {:.3f}".format(target_temperature))
|
|
||||||
|
|
||||||
class Series:
|
|
||||||
def __init__(self, conv=lambda x: x):
|
|
||||||
self.conv = conv
|
|
||||||
self.x_data = []
|
|
||||||
self.y_data = []
|
|
||||||
|
|
||||||
def append(self, x, y):
|
|
||||||
self.x_data.append(x)
|
|
||||||
self.y_data.append(self.conv(y))
|
|
||||||
|
|
||||||
def clip(self, min_x):
|
|
||||||
drop = 0
|
|
||||||
while drop < len(self.x_data) and self.x_data[drop] < min_x:
|
|
||||||
drop += 1
|
|
||||||
self.x_data = self.x_data[drop:]
|
|
||||||
self.y_data = self.y_data[drop:]
|
|
||||||
|
|
||||||
series = {
|
|
||||||
# 'adc': Series(),
|
|
||||||
# 'sens': Series(lambda x: x * 0.0001),
|
|
||||||
'temperature': Series(),
|
|
||||||
# 'i_set': Series(),
|
|
||||||
'pid_output': Series(),
|
|
||||||
# 'vref': Series(),
|
|
||||||
# 'dac_value': Series(),
|
|
||||||
# 'dac_feedback': Series(),
|
|
||||||
# 'i_tec': Series(),
|
|
||||||
'tec_i': Series(),
|
|
||||||
'tec_u_meas': Series(),
|
|
||||||
# 'interval': Series(),
|
|
||||||
}
|
|
||||||
series_lock = Lock()
|
|
||||||
|
|
||||||
quit = False
|
|
||||||
|
|
||||||
def recv_data(tec):
|
|
||||||
global last_packet_time
|
|
||||||
while True:
|
|
||||||
data = tec.get_report()
|
|
||||||
ch0 = data[0]
|
|
||||||
series_lock.acquire()
|
|
||||||
try:
|
|
||||||
for k, s in series.items():
|
|
||||||
if k in ch0:
|
|
||||||
v = ch0[k]
|
|
||||||
if type(v) is float:
|
|
||||||
s.append(ch0['time'], v)
|
|
||||||
finally:
|
|
||||||
series_lock.release()
|
|
||||||
|
|
||||||
if quit:
|
|
||||||
break
|
|
||||||
time.sleep(0.05)
|
|
||||||
|
|
||||||
thread = Thread(target=recv_data, args=(tec,))
|
|
||||||
thread.start()
|
|
||||||
|
|
||||||
fig, ax = plt.subplots()
|
|
||||||
|
|
||||||
for k, s in series.items():
|
|
||||||
s.plot, = ax.plot([], [], label=k)
|
|
||||||
legend = ax.legend()
|
|
||||||
|
|
||||||
def animate(i):
|
|
||||||
min_x, max_x, min_y, max_y = None, None, None, None
|
|
||||||
|
|
||||||
series_lock.acquire()
|
|
||||||
try:
|
|
||||||
for k, s in series.items():
|
|
||||||
s.plot.set_data(s.x_data, s.y_data)
|
|
||||||
if len(s.y_data) > 0:
|
|
||||||
s.plot.set_label("{}: {:.3f}".format(k, s.y_data[-1]))
|
|
||||||
|
|
||||||
if len(s.x_data) > 0:
|
|
||||||
min_x_ = min(s.x_data)
|
|
||||||
if min_x is None:
|
|
||||||
min_x = min_x_
|
|
||||||
else:
|
|
||||||
min_x = min(min_x, min_x_)
|
|
||||||
max_x_ = max(s.x_data)
|
|
||||||
if max_x is None:
|
|
||||||
max_x = max_x_
|
|
||||||
else:
|
|
||||||
max_x = max(max_x, max_x_)
|
|
||||||
if len(s.y_data) > 0:
|
|
||||||
min_y_ = min(s.y_data)
|
|
||||||
if min_y is None:
|
|
||||||
min_y = min_y_
|
|
||||||
else:
|
|
||||||
min_y = min(min_y, min_y_)
|
|
||||||
max_y_ = max(s.y_data)
|
|
||||||
if max_y is None:
|
|
||||||
max_y = max_y_
|
|
||||||
else:
|
|
||||||
max_y = max(max_y, max_y_)
|
|
||||||
|
|
||||||
if min_x and max_x - TIME_WINDOW > min_x:
|
|
||||||
for s in series.values():
|
|
||||||
s.clip(max_x - TIME_WINDOW)
|
|
||||||
finally:
|
|
||||||
series_lock.release()
|
|
||||||
|
|
||||||
if min_x != max_x:
|
|
||||||
ax.set_xlim(min_x, max_x)
|
|
||||||
if min_y != max_y:
|
|
||||||
margin_y = 0.01 * (max_y - min_y)
|
|
||||||
ax.set_ylim(min_y - margin_y, max_y + margin_y)
|
|
||||||
|
|
||||||
nonlocal legend
|
|
||||||
legend.remove()
|
|
||||||
legend = ax.legend()
|
|
||||||
|
|
||||||
ani = animation.FuncAnimation(
|
|
||||||
fig, animate, interval=1, blit=False, save_count=50)
|
|
||||||
|
|
||||||
plt.show()
|
|
||||||
quit = True
|
|
||||||
thread.join()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
@ -1,81 +0,0 @@
|
|||||||
import argparse
|
|
||||||
from contextlib import contextmanager
|
|
||||||
from pythermostat.client import Client
|
|
||||||
|
|
||||||
|
|
||||||
CHANNELS = 2
|
|
||||||
|
|
||||||
|
|
||||||
def get_argparser():
|
|
||||||
parser = argparse.ArgumentParser(description="Thermostat hardware testing script")
|
|
||||||
|
|
||||||
parser.add_argument("host", metavar="HOST", default="192.168.1.26", nargs="?")
|
|
||||||
parser.add_argument("port", metavar="PORT", default=23, nargs="?")
|
|
||||||
parser.add_argument(
|
|
||||||
"-r",
|
|
||||||
"--testing_resistance",
|
|
||||||
default=10_000,
|
|
||||||
help="Testing resistance value through SENS pin in Ohms",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-d",
|
|
||||||
"--deviation",
|
|
||||||
default=1,
|
|
||||||
help="Allowed deviation of resistance in percentage",
|
|
||||||
)
|
|
||||||
|
|
||||||
return parser
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
args = get_argparser().parse_args()
|
|
||||||
|
|
||||||
min_allowed_resistance = args.testing_resistance * (1 - args.deviation / 100)
|
|
||||||
max_allowed_resistance = args.testing_resistance * (1 + args.deviation / 100)
|
|
||||||
|
|
||||||
print(min_allowed_resistance, max_allowed_resistance)
|
|
||||||
|
|
||||||
thermostat = Client(args.host, args.port)
|
|
||||||
for channel in range(CHANNELS):
|
|
||||||
print(f"Channel {channel} is active")
|
|
||||||
|
|
||||||
print("Checking resistance through SENS input ....", end=" ")
|
|
||||||
sens_resistance = thermostat.get_report()[channel]["sens"]
|
|
||||||
if sens_resistance is not None:
|
|
||||||
print(sens_resistance, "Ω")
|
|
||||||
if min_allowed_resistance <= sens_resistance <= max_allowed_resistance:
|
|
||||||
print("PASSED")
|
|
||||||
else:
|
|
||||||
print("FAILED")
|
|
||||||
else:
|
|
||||||
print("Floating SENS input! Is the channel connected?")
|
|
||||||
|
|
||||||
with preserve_thermostat_output_settings(thermostat, channel):
|
|
||||||
test_output_settings = {
|
|
||||||
"max_i_pos": 2,
|
|
||||||
"max_i_neg": 2,
|
|
||||||
"max_v": 4,
|
|
||||||
"i_set": 0.1,
|
|
||||||
"polarity": "normal",
|
|
||||||
}
|
|
||||||
for field, value in test_output_settings.items():
|
|
||||||
thermostat.set_param("output", channel, field, value)
|
|
||||||
|
|
||||||
input(f"Check if channel {channel} current = 0.1 A, and press ENTER...")
|
|
||||||
|
|
||||||
input(f"Channel {channel} testing done, press ENTER to continue.")
|
|
||||||
print()
|
|
||||||
|
|
||||||
print("Testing complete.")
|
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def preserve_thermostat_output_settings(client, channel):
|
|
||||||
original_output_settings = client.get_output()[channel]
|
|
||||||
yield original_output_settings
|
|
||||||
for setting in "max_i_pos", "max_i_neg", "max_v", "i_set", "polarity":
|
|
||||||
client.set_param("output", channel, setting, original_output_settings[setting])
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
@ -1,8 +1,8 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ad7172, b_parameter as bp,
|
ad7172,
|
||||||
command_parser::{CenterPoint, Polarity},
|
command_parser::{CenterPoint, Polarity},
|
||||||
config::PwmLimits,
|
config::PwmLimits,
|
||||||
pid,
|
pid, steinhart_hart as sh,
|
||||||
};
|
};
|
||||||
use num_traits::Zero;
|
use num_traits::Zero;
|
||||||
use smoltcp::time::{Duration, Instant};
|
use smoltcp::time::{Duration, Instant};
|
||||||
@ -32,7 +32,7 @@ pub struct ChannelState {
|
|||||||
pub pwm_limits: PwmLimits,
|
pub pwm_limits: PwmLimits,
|
||||||
pub pid_engaged: bool,
|
pub pid_engaged: bool,
|
||||||
pub pid: pid::Controller,
|
pub pid: pid::Controller,
|
||||||
pub bp: bp::Parameters,
|
pub sh: sh::Parameters,
|
||||||
pub polarity: Polarity,
|
pub polarity: Polarity,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ impl ChannelState {
|
|||||||
},
|
},
|
||||||
pid_engaged: false,
|
pid_engaged: false,
|
||||||
pid: pid::Controller::new(pid::Parameters::default()),
|
pid: pid::Controller::new(pid::Parameters::default()),
|
||||||
bp: bp::Parameters::default(),
|
sh: sh::Parameters::default(),
|
||||||
polarity: Polarity::Normal,
|
polarity: Polarity::Normal,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,7 +100,7 @@ impl ChannelState {
|
|||||||
|
|
||||||
pub fn get_temperature(&self) -> Option<ThermodynamicTemperature> {
|
pub fn get_temperature(&self) -> Option<ThermodynamicTemperature> {
|
||||||
let r = self.get_sens()?;
|
let r = self.get_sens()?;
|
||||||
let temperature = self.bp.get_temperature(r);
|
let temperature = self.sh.get_temperature(r);
|
||||||
Some(temperature)
|
Some(temperature)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
use crate::timer::sleep;
|
use crate::timer::sleep;
|
||||||
use crate::{
|
use crate::{
|
||||||
ad5680, ad7172, b_parameter,
|
ad5680, ad7172,
|
||||||
channel::{Channel, Channel0, Channel1},
|
channel::{Channel, Channel0, Channel1},
|
||||||
channel_state::ChannelState,
|
channel_state::ChannelState,
|
||||||
command_handler::JsonBuffer,
|
command_handler::JsonBuffer,
|
||||||
command_parser::{CenterPoint, Polarity, PwmPin},
|
command_parser::{CenterPoint, Polarity, PwmPin},
|
||||||
pins::{self, Channel0VRef, Channel1VRef},
|
pins::{self, Channel0VRef, Channel1VRef},
|
||||||
|
steinhart_hart,
|
||||||
};
|
};
|
||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
use heapless::{consts::U2, Vec};
|
use heapless::{consts::U2, Vec};
|
||||||
@ -557,17 +558,17 @@ impl Channels {
|
|||||||
serde_json_core::to_vec(&summaries)
|
serde_json_core::to_vec(&summaries)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn b_parameter_summary(&mut self, channel: usize) -> BParameterSummary {
|
fn steinhart_hart_summary(&mut self, channel: usize) -> SteinhartHartSummary {
|
||||||
let params = self.channel_state(channel).bp.clone();
|
let params = self.channel_state(channel).sh.clone();
|
||||||
BParameterSummary { channel, params }
|
SteinhartHartSummary { channel, params }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn b_parameter_summaries_json(
|
pub fn steinhart_hart_summaries_json(
|
||||||
&mut self,
|
&mut self,
|
||||||
) -> Result<JsonBuffer, serde_json_core::ser::Error> {
|
) -> Result<JsonBuffer, serde_json_core::ser::Error> {
|
||||||
let mut summaries = Vec::<_, U2>::new();
|
let mut summaries = Vec::<_, U2>::new();
|
||||||
for channel in 0..CHANNELS {
|
for channel in 0..CHANNELS {
|
||||||
let _ = summaries.push(self.b_parameter_summary(channel));
|
let _ = summaries.push(self.steinhart_hart_summary(channel));
|
||||||
}
|
}
|
||||||
serde_json_core::to_vec(&summaries)
|
serde_json_core::to_vec(&summaries)
|
||||||
}
|
}
|
||||||
@ -646,7 +647,7 @@ pub struct PostFilterSummary {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct BParameterSummary {
|
pub struct SteinhartHartSummary {
|
||||||
channel: usize,
|
channel: usize,
|
||||||
params: b_parameter::Parameters,
|
params: steinhart_hart::Parameters,
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ use super::{
|
|||||||
ad7172,
|
ad7172,
|
||||||
channels::{Channels, CHANNELS},
|
channels::{Channels, CHANNELS},
|
||||||
command_parser::{
|
command_parser::{
|
||||||
BpParameter, CenterPoint, Command, Ipv4Config, PidParameter, Polarity, PwmPin, ShowCommand,
|
CenterPoint, Command, Ipv4Config, PidParameter, Polarity, PwmPin, ShParameter, ShowCommand,
|
||||||
},
|
},
|
||||||
config::ChannelConfig,
|
config::ChannelConfig,
|
||||||
dfu,
|
dfu,
|
||||||
@ -113,13 +113,16 @@ impl Handler {
|
|||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_b_parameter(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
fn show_steinhart_hart(
|
||||||
match channels.b_parameter_summaries_json() {
|
socket: &mut TcpSocket,
|
||||||
|
channels: &mut Channels,
|
||||||
|
) -> Result<Handler, Error> {
|
||||||
|
match channels.steinhart_hart_summaries_json() {
|
||||||
Ok(buf) => {
|
Ok(buf) => {
|
||||||
send_line(socket, &buf);
|
send_line(socket, &buf);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("unable to serialize b parameter summaries: {:?}", e);
|
error!("unable to serialize steinhart-hart summaries: {:?}", e);
|
||||||
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
return Err(Error::Report);
|
return Err(Error::Report);
|
||||||
}
|
}
|
||||||
@ -238,19 +241,19 @@ impl Handler {
|
|||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_b_parameter(
|
fn set_steinhart_hart(
|
||||||
socket: &mut TcpSocket,
|
socket: &mut TcpSocket,
|
||||||
channels: &mut Channels,
|
channels: &mut Channels,
|
||||||
channel: usize,
|
channel: usize,
|
||||||
parameter: BpParameter,
|
parameter: ShParameter,
|
||||||
value: f64,
|
value: f64,
|
||||||
) -> Result<Handler, Error> {
|
) -> Result<Handler, Error> {
|
||||||
let bp = &mut channels.channel_state(channel).bp;
|
let sh = &mut channels.channel_state(channel).sh;
|
||||||
use super::command_parser::BpParameter::*;
|
use super::command_parser::ShParameter::*;
|
||||||
match parameter {
|
match parameter {
|
||||||
T0 => bp.t0 = ThermodynamicTemperature::new::<degree_celsius>(value),
|
T0 => sh.t0 = ThermodynamicTemperature::new::<degree_celsius>(value),
|
||||||
B => bp.b = TemperatureInterval::new::<kelvin>(value),
|
B => sh.b = TemperatureInterval::new::<kelvin>(value),
|
||||||
R0 => bp.r0 = ElectricalResistance::new::<ohm>(value),
|
R0 => sh.r0 = ElectricalResistance::new::<ohm>(value),
|
||||||
}
|
}
|
||||||
send_line(socket, b"{}");
|
send_line(socket, b"{}");
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
@ -477,7 +480,9 @@ impl Handler {
|
|||||||
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::Output) => Handler::show_pwm(socket, channels),
|
Command::Show(ShowCommand::Output) => Handler::show_pwm(socket, channels),
|
||||||
Command::Show(ShowCommand::BParameter) => Handler::show_b_parameter(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::OutputPid { channel } => Handler::engage_pid(socket, channels, channel),
|
Command::OutputPid { channel } => Handler::engage_pid(socket, channels, channel),
|
||||||
@ -497,11 +502,11 @@ impl Handler {
|
|||||||
parameter,
|
parameter,
|
||||||
value,
|
value,
|
||||||
} => Handler::set_pid(socket, channels, channel, parameter, value),
|
} => Handler::set_pid(socket, channels, channel, parameter, value),
|
||||||
Command::BParameter {
|
Command::SteinhartHart {
|
||||||
channel,
|
channel,
|
||||||
parameter,
|
parameter,
|
||||||
value,
|
value,
|
||||||
} => Handler::set_b_parameter(socket, channels, channel, parameter, value),
|
} => Handler::set_steinhart_hart(socket, channels, channel, parameter, value),
|
||||||
Command::PostFilter {
|
Command::PostFilter {
|
||||||
channel,
|
channel,
|
||||||
rate: None,
|
rate: None,
|
||||||
|
@ -93,7 +93,7 @@ pub enum ShowCommand {
|
|||||||
Input,
|
Input,
|
||||||
Output,
|
Output,
|
||||||
Pid,
|
Pid,
|
||||||
BParameter,
|
SteinhartHart,
|
||||||
PostFilter,
|
PostFilter,
|
||||||
Ipv4,
|
Ipv4,
|
||||||
}
|
}
|
||||||
@ -108,9 +108,9 @@ pub enum PidParameter {
|
|||||||
OutputMax,
|
OutputMax,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// B-Parameter equation parameter
|
/// Steinhart-Hart equation parameter
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum BpParameter {
|
pub enum ShParameter {
|
||||||
T0,
|
T0,
|
||||||
B,
|
B,
|
||||||
R0,
|
R0,
|
||||||
@ -172,9 +172,9 @@ pub enum Command {
|
|||||||
parameter: PidParameter,
|
parameter: PidParameter,
|
||||||
value: f64,
|
value: f64,
|
||||||
},
|
},
|
||||||
BParameter {
|
SteinhartHart {
|
||||||
channel: usize,
|
channel: usize,
|
||||||
parameter: BpParameter,
|
parameter: ShParameter,
|
||||||
value: f64,
|
value: f64,
|
||||||
},
|
},
|
||||||
PostFilter {
|
PostFilter {
|
||||||
@ -366,18 +366,18 @@ fn pid(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|||||||
))(input)
|
))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `b-p <0-1> <parameter> <value>`
|
/// `s-h <0-1> <parameter> <value>`
|
||||||
fn b_parameter_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
fn steinhart_hart_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
||||||
let (input, channel) = channel(input)?;
|
let (input, channel) = channel(input)?;
|
||||||
let (input, _) = whitespace(input)?;
|
let (input, _) = whitespace(input)?;
|
||||||
let (input, parameter) = alt((
|
let (input, parameter) = alt((
|
||||||
value(BpParameter::T0, tag("t0")),
|
value(ShParameter::T0, tag("t0")),
|
||||||
value(BpParameter::B, tag("b")),
|
value(ShParameter::B, tag("b")),
|
||||||
value(BpParameter::R0, tag("r0")),
|
value(ShParameter::R0, tag("r0")),
|
||||||
))(input)?;
|
))(input)?;
|
||||||
let (input, _) = whitespace(input)?;
|
let (input, _) = whitespace(input)?;
|
||||||
let (input, value) = float(input)?;
|
let (input, value) = float(input)?;
|
||||||
let result = value.map(|value| Command::BParameter {
|
let result = value.map(|value| Command::SteinhartHart {
|
||||||
channel,
|
channel,
|
||||||
parameter,
|
parameter,
|
||||||
value,
|
value,
|
||||||
@ -385,12 +385,12 @@ fn b_parameter_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>>
|
|||||||
Ok((input, result))
|
Ok((input, result))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `b-p` | `b-p <b_parameter_parameter>`
|
/// `s-h` | `s-h <steinhart_hart_parameter>`
|
||||||
fn b_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
fn steinhart_hart(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
||||||
let (input, _) = tag("b-p")(input)?;
|
let (input, _) = tag("s-h")(input)?;
|
||||||
alt((
|
alt((
|
||||||
preceded(whitespace, b_parameter_parameter),
|
preceded(whitespace, steinhart_hart_parameter),
|
||||||
value(Ok(Command::Show(ShowCommand::BParameter)), end),
|
value(Ok(Command::Show(ShowCommand::SteinhartHart)), end),
|
||||||
))(input)
|
))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -572,7 +572,7 @@ fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|||||||
output,
|
output,
|
||||||
center_point,
|
center_point,
|
||||||
pid,
|
pid,
|
||||||
b_parameter,
|
steinhart_hart,
|
||||||
postfilter,
|
postfilter,
|
||||||
value(Ok(Command::Dfu), tag("dfu")),
|
value(Ok(Command::Dfu), tag("dfu")),
|
||||||
fan,
|
fan,
|
||||||
@ -678,7 +678,7 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_output_polarity() {
|
fn parse_output_polarity() {
|
||||||
let command = Command::parse(b"output 0 polarity reversed");
|
let command = Command::parse(b"pwm 0 polarity reversed");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
command,
|
command,
|
||||||
Ok(Command::OutputPolarity {
|
Ok(Command::OutputPolarity {
|
||||||
@ -753,19 +753,19 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_b_parameter() {
|
fn parse_steinhart_hart() {
|
||||||
let command = Command::parse(b"b-p");
|
let command = Command::parse(b"s-h");
|
||||||
assert_eq!(command, Ok(Command::Show(ShowCommand::BParameter)));
|
assert_eq!(command, Ok(Command::Show(ShowCommand::SteinhartHart)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_b_parameter_set() {
|
fn parse_steinhart_hart_set() {
|
||||||
let command = Command::parse(b"b-p 1 t0 23.05");
|
let command = Command::parse(b"s-h 1 t0 23.05");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
command,
|
command,
|
||||||
Ok(Command::BParameter {
|
Ok(Command::SteinhartHart {
|
||||||
channel: 1,
|
channel: 1,
|
||||||
parameter: BpParameter::T0,
|
parameter: ShParameter::T0,
|
||||||
value: 23.05,
|
value: 23.05,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ad7172::PostFilter,
|
ad7172::PostFilter,
|
||||||
b_parameter,
|
|
||||||
channels::Channels,
|
channels::Channels,
|
||||||
command_parser::{CenterPoint, Polarity},
|
command_parser::{CenterPoint, Polarity},
|
||||||
pid,
|
pid, steinhart_hart,
|
||||||
};
|
};
|
||||||
use num_traits::Zero;
|
use num_traits::Zero;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@ -17,7 +16,7 @@ pub struct ChannelConfig {
|
|||||||
pid_engaged: bool,
|
pid_engaged: bool,
|
||||||
i_set: ElectricCurrent,
|
i_set: ElectricCurrent,
|
||||||
polarity: Polarity,
|
polarity: Polarity,
|
||||||
bp: b_parameter::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
|
||||||
adc_postfilter: PostFilter,
|
adc_postfilter: PostFilter,
|
||||||
@ -46,7 +45,7 @@ impl ChannelConfig {
|
|||||||
pid_engaged: state.pid_engaged,
|
pid_engaged: state.pid_engaged,
|
||||||
i_set,
|
i_set,
|
||||||
polarity: state.polarity.clone(),
|
polarity: state.polarity.clone(),
|
||||||
bp: state.bp.clone(),
|
sh: state.sh.clone(),
|
||||||
pwm,
|
pwm,
|
||||||
adc_postfilter,
|
adc_postfilter,
|
||||||
}
|
}
|
||||||
@ -58,7 +57,7 @@ impl ChannelConfig {
|
|||||||
state.pid.parameters = self.pid.clone();
|
state.pid.parameters = self.pid.clone();
|
||||||
state.pid.target = self.pid_target.into();
|
state.pid.target = self.pid_target.into();
|
||||||
state.pid_engaged = self.pid_engaged;
|
state.pid_engaged = self.pid_engaged;
|
||||||
state.bp = self.bp.clone();
|
state.sh = self.sh.clone();
|
||||||
|
|
||||||
self.pwm.apply(channels, channel);
|
self.pwm.apply(channels, channel);
|
||||||
|
|
||||||
|
@ -35,9 +35,9 @@ mod session;
|
|||||||
use session::{Session, SessionInput};
|
use session::{Session, SessionInput};
|
||||||
mod command_parser;
|
mod command_parser;
|
||||||
use command_parser::Ipv4Config;
|
use command_parser::Ipv4Config;
|
||||||
mod b_parameter;
|
|
||||||
mod channels;
|
mod channels;
|
||||||
mod pid;
|
mod pid;
|
||||||
|
mod steinhart_hart;
|
||||||
mod timer;
|
mod timer;
|
||||||
use channels::{Channels, CHANNELS};
|
use channels::{Channels, CHANNELS};
|
||||||
mod channel;
|
mod channel;
|
||||||
@ -107,8 +107,8 @@ fn main() -> ! {
|
|||||||
.use_hse(HSE)
|
.use_hse(HSE)
|
||||||
.sysclk(168.mhz())
|
.sysclk(168.mhz())
|
||||||
.hclk(168.mhz())
|
.hclk(168.mhz())
|
||||||
.pclk1(32.mhz())
|
.pclk1(42.mhz())
|
||||||
.pclk2(64.mhz())
|
.pclk2(84.mhz())
|
||||||
.freeze();
|
.freeze();
|
||||||
|
|
||||||
let mut wd = IndependentWatchdog::new(dp.IWDG);
|
let mut wd = IndependentWatchdog::new(dp.IWDG);
|
||||||
|
@ -8,14 +8,14 @@ use uom::si::{
|
|||||||
thermodynamic_temperature::{degree_celsius, kelvin},
|
thermodynamic_temperature::{degree_celsius, kelvin},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// B-Parameter equation parameters
|
/// Steinhart-Hart equation parameters
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Parameters {
|
pub struct Parameters {
|
||||||
/// Base temperature
|
/// Base temperature
|
||||||
pub t0: ThermodynamicTemperature,
|
pub t0: ThermodynamicTemperature,
|
||||||
/// Thermistor resistance at base temperature
|
/// Resistance at base temperature
|
||||||
pub r0: ElectricalResistance,
|
pub r0: ElectricalResistance,
|
||||||
/// Beta (average slope of the function ln R vs. 1/T)
|
/// Beta
|
||||||
pub b: TemperatureInterval,
|
pub b: TemperatureInterval,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user