forked from EmbeddedTeam/app_photomagnetic
Add support for heating state and NTC commands
Extend the UART protocol to support writing and reading the heating state, and reading individual pole NTC sensors and the heating pad NTC. Cache NTC readings for query on demand. Move PID updates to a workqueue to avoid blocking the timer IRQ context.
This commit is contained in:
+64
-4
@@ -1,7 +1,9 @@
|
||||
#ifndef __THER_COM_HPP__
|
||||
#define __THER_COM_HPP__
|
||||
|
||||
#include "heating.hpp"
|
||||
#include "led.hpp"
|
||||
#include "ntc.hpp"
|
||||
#include <led_strip_indicator/led_strip_indicator.hpp>
|
||||
#include <uart_com/simple_protocal.hpp>
|
||||
#include <zephyr/drivers/hwinfo.h>
|
||||
@@ -23,7 +25,17 @@ public:
|
||||
|
||||
private:
|
||||
enum HostStatus : uint8_t { STANDBY, RUNNING, PAUSE, ERROR };
|
||||
enum Addr : uint8_t { TEMP = 0, GET_ID, RUNNING_STATE };
|
||||
enum Addr : uint8_t {
|
||||
TEMP = 0,
|
||||
GET_ID,
|
||||
RUNNING_STATE,
|
||||
W_HEATING_STATE,
|
||||
R_HEATING_STATE
|
||||
};
|
||||
enum DevAddr : uint8_t {
|
||||
R_TEMP_POLE_NTC = 100,
|
||||
R_TEMP_HETING_PAD_NTC = 101,
|
||||
};
|
||||
using CbTableValueType =
|
||||
std::pair<const uint8_t, void (*)(uart_com::DataType)>;
|
||||
struct Cb {
|
||||
@@ -42,17 +54,65 @@ private:
|
||||
} else {
|
||||
InfLed::Off();
|
||||
}
|
||||
s_led_strip_indicator->Status(data[0]);
|
||||
printk("set ledsrtip to %d", data[0]);
|
||||
const uint8_t id = data[0];
|
||||
s_led_strip_indicator->Status(id).on_error([](zpp::error_code err) {
|
||||
printk("err code: %s", zpp::error_str(err));
|
||||
});
|
||||
}
|
||||
static auto WHeatingState(uart_com::DataType data) -> void {
|
||||
if (data.size() != 1) {
|
||||
return;
|
||||
}
|
||||
const bool state = (data[0] > 0);
|
||||
if (state) {
|
||||
HeatingPad::Start(sensor_value{data[0], 0});
|
||||
} else {
|
||||
HeatingPad::Stop();
|
||||
}
|
||||
}
|
||||
static auto RHeatingState(uart_com::DataType data) -> void {
|
||||
if (data.size() != 0) {
|
||||
return;
|
||||
}
|
||||
const sensor_value temp = HeatingPad::CurrentTemp();
|
||||
const uint8_t state[] = {s_heating_state, (uint8_t)temp.val1,
|
||||
(uint8_t)temp.val2};
|
||||
s_proto->Send(R_HEATING_STATE, state);
|
||||
}
|
||||
/// Read one pole NTC by index (0~7).
|
||||
static auto ReadPoleNtcTemperature(uart_com::DataType data) -> void {
|
||||
if (data.size() != 1) {
|
||||
return;
|
||||
}
|
||||
const sensor_value temp = NtcGroup::GetSensorValue(data[0]);
|
||||
const uint8_t payload[] = {(uint8_t)temp.val1, (uint8_t)temp.val2};
|
||||
s_proto->Send(R_TEMP_POLE_NTC, payload);
|
||||
}
|
||||
/// Read heating pad NTC (onboard ADC).
|
||||
static auto ReadHeatingPadNtcTemperature(uart_com::DataType data) -> void {
|
||||
const sensor_value temp = HeatingPad::CurrentTemp();
|
||||
const uint8_t payload[] = {(uint8_t)temp.val1, (uint8_t)temp.val2};
|
||||
s_proto->Send(R_TEMP_HETING_PAD_NTC, payload);
|
||||
}
|
||||
};
|
||||
constexpr static std::pair<const uint8_t,
|
||||
uart_com::SimpleProtocal::CallbackType>
|
||||
kRxCallbackTable[] = {{GET_ID, Cb::GetId},
|
||||
{RUNNING_STATE, Cb::RunningState}};
|
||||
kRxCallbackTable[] = {
|
||||
{GET_ID, Cb::GetId},
|
||||
{RUNNING_STATE, Cb::RunningState},
|
||||
{W_HEATING_STATE, Cb::WHeatingState},
|
||||
{R_HEATING_STATE, Cb::RHeatingState},
|
||||
// Pole NTC (MCP3208)
|
||||
{R_TEMP_POLE_NTC, Cb::ReadPoleNtcTemperature},
|
||||
// Heating pad NTC (ADC1)
|
||||
{R_TEMP_HETING_PAD_NTC, Cb::ReadHeatingPadNtcTemperature},
|
||||
};
|
||||
inline static auto s_led_strip_indicator =
|
||||
ZPP_DRV_GET_P(ledstrip::Indicator, DT_NODELABEL(indicator));
|
||||
inline static auto s_proto =
|
||||
(uart_com::SimpleProtocal *)(DEVICE_DT_GET(DT_NODELABEL(pm_protocal)));
|
||||
inline static auto s_heating_state = false;
|
||||
using InfLed = ther::Led<LED_DT_SPEC_GET(DT_NODELABEL(inf_led))>;
|
||||
};
|
||||
} // namespace ther
|
||||
|
||||
+28
-54
@@ -1,6 +1,7 @@
|
||||
#ifndef __THER_HEATING_HPP__
|
||||
#define __THER_HEATING_HPP__
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/pwm.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
@@ -11,69 +12,41 @@ namespace ther {
|
||||
|
||||
class HeatingPad {
|
||||
public:
|
||||
/// Start PID temperature control toward the given target.
|
||||
static auto Start(sensor_value target_temp) -> void {
|
||||
s_target = sensor_value_to_double(&target_temp);
|
||||
s_integral = 0.0;
|
||||
s_last_error = 0.0;
|
||||
s_last_time = k_uptime_get();
|
||||
|
||||
printk("[heat] start target=%.1f°C\n", s_target);
|
||||
printk("[heat] start target=%.1f C\n", s_target);
|
||||
|
||||
// Check PWM device
|
||||
if (!device_is_ready(s_pwm_spec.dev)) {
|
||||
printk("[heat] ERR: PWM device not ready\n");
|
||||
printk("[heat] ERR: PWM not ready\n");
|
||||
return;
|
||||
}
|
||||
printk("[heat] PWM dev=%s ch=%d period=%u ns\n", s_pwm_spec.dev->name,
|
||||
s_pwm_spec.channel, s_pwm_spec.period);
|
||||
printk("[heat] PWM OK\n");
|
||||
|
||||
// Check sensor
|
||||
if (!device_is_ready(s_temp_sensor)) {
|
||||
printk("[heat] ERR: temp sensor not ready\n");
|
||||
return;
|
||||
}
|
||||
printk("[heat] temp sensor OK\n");
|
||||
|
||||
// Test: set 50% duty immediately to verify PWM works
|
||||
const uint32_t pulse_50 = s_pwm_spec.period / 2;
|
||||
int ret = pwm_set_pulse_dt(&s_pwm_spec, pulse_50);
|
||||
printk("[heat] test 50%% duty pulse=%u ret=%d\n", pulse_50, ret);
|
||||
|
||||
k_timer_init(&s_timer, Tick, nullptr);
|
||||
k_work_init(&s_work, WorkHandler);
|
||||
k_timer_init(&s_timer, TimerTick, nullptr);
|
||||
k_timer_start(&s_timer, K_NO_WAIT, K_MSEC(kUpdatePeriodMs));
|
||||
s_active = true;
|
||||
}
|
||||
static auto Start() {
|
||||
|
||||
const uint32_t pulse_50 = s_pwm_spec.period / 2;
|
||||
int ret = pwm_set_pulse_dt(&s_pwm_spec, pulse_50);
|
||||
return ret;
|
||||
}
|
||||
/// Stop PID control and turn off heating.
|
||||
static auto Stop() -> void {
|
||||
printk("[heat] stop\n");
|
||||
s_active = false;
|
||||
k_timer_stop(&s_timer);
|
||||
pwm_set_pulse_dt(&s_pwm_spec, 0);
|
||||
}
|
||||
|
||||
/// Change the target temperature while keeping PID running.
|
||||
static auto SetTarget(sensor_value target_temp) -> void {
|
||||
s_target = sensor_value_to_double(&target_temp);
|
||||
printk("[heat] new target=%.1f°C\n", s_target);
|
||||
}
|
||||
static auto CurrentTemp() -> sensor_value { return s_current_temp; }
|
||||
|
||||
private:
|
||||
static constexpr uint32_t kPeriodNs = PWM_KHZ(5);
|
||||
static constexpr uint32_t kUpdatePeriodMs = 100;
|
||||
static constexpr double kOutMin = 0.0;
|
||||
static constexpr double kOutMax = 1.0;
|
||||
|
||||
// ── PI gains (tune these) ─────────────────────────
|
||||
static constexpr double kKp = 2.0; // proportional
|
||||
static constexpr double kKi = 0.02; // integral
|
||||
static constexpr double kKd = 0.0; // derivative (not needed for heating-only)
|
||||
static constexpr double kKp = 2.0;
|
||||
static constexpr double kKi = 0.02;
|
||||
|
||||
inline static bool s_active{false};
|
||||
inline static double s_target{0.0};
|
||||
@@ -92,45 +65,44 @@ private:
|
||||
DEVICE_DT_GET(DT_NODELABEL(heating_pad_ntc));
|
||||
|
||||
inline static k_timer s_timer;
|
||||
inline static k_work s_work;
|
||||
inline static bool s_work_pending{false};
|
||||
|
||||
/// Called periodically by the timer.
|
||||
static auto Tick(k_timer * /*timer*/) -> void {
|
||||
/// Timer fires → submit work. Skips if previous work hasn't finished.
|
||||
static auto TimerTick(k_timer * /*timer*/) -> void {
|
||||
if (s_work_pending) {
|
||||
return;
|
||||
}
|
||||
s_work_pending = true;
|
||||
k_work_submit(&s_work);
|
||||
}
|
||||
|
||||
/// PID update running in system workqueue context.
|
||||
static auto WorkHandler(k_work * /*work*/) -> void {
|
||||
double current_temp;
|
||||
if (ReadTemperature(current_temp) != 0) {
|
||||
printk("[heat] ERR: read temp failed\n");
|
||||
s_work_pending = false;
|
||||
return;
|
||||
}
|
||||
|
||||
printk("[heat] tick: cur=%.1f°C target=%.1f°C\n", current_temp, s_target);
|
||||
printk("[heat] cur=%.1f C target=%.1f C\n", current_temp, s_target);
|
||||
|
||||
// PI computation
|
||||
const int64_t now = k_uptime_get();
|
||||
const double dt = static_cast<double>(now - s_last_time) / 1000.0;
|
||||
s_last_time = now;
|
||||
|
||||
const double error = s_target - current_temp;
|
||||
|
||||
// Proportional
|
||||
const double p = kKp * error;
|
||||
|
||||
// Integral with anti-windup
|
||||
s_integral += kKi * error * dt;
|
||||
s_integral = std::clamp(s_integral, kOutMin, kOutMax);
|
||||
|
||||
s_last_error = error;
|
||||
|
||||
// Compute output, clamp to [0, 1]
|
||||
double output = p + s_integral;
|
||||
double output = kKp * error + s_integral;
|
||||
output = std::clamp(output, kOutMin, kOutMax);
|
||||
|
||||
// Apply PWM duty cycle
|
||||
const uint32_t pulse = static_cast<uint32_t>(output * kPeriodNs);
|
||||
printk("[heat] PID out=%.2f%% pulse=%u / %u\n", output * 100.0, pulse,
|
||||
s_pwm_spec.period);
|
||||
pwm_set_pulse_dt(&s_pwm_spec, pulse);
|
||||
s_work_pending = false;
|
||||
}
|
||||
|
||||
/// Read temperature from the NTC sensor into `out_temp` (in °C).
|
||||
static auto ReadTemperature(double &out_temp) -> int {
|
||||
if (0 != sensor_sample_fetch(s_temp_sensor)) {
|
||||
printk("[heat] ERR: sensor fetch failed\n");
|
||||
@@ -145,8 +117,10 @@ private:
|
||||
}
|
||||
|
||||
out_temp = sensor_value_to_double(&val);
|
||||
s_current_temp = val;
|
||||
return 0;
|
||||
}
|
||||
inline static sensor_value s_current_temp{};
|
||||
};
|
||||
|
||||
} // namespace ther
|
||||
|
||||
+12
-2
@@ -1,5 +1,6 @@
|
||||
#ifndef THER_NTC_HPP
|
||||
#define THER_NTC_HPP
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
@@ -30,7 +31,8 @@ public:
|
||||
continue;
|
||||
}
|
||||
|
||||
printk("[ntc]#%zu = %d.%d°C\n", i + 1, val.val1, val.val2);
|
||||
// Always cache the latest reading for individual query
|
||||
s_current_temp[i] = val;
|
||||
|
||||
if (!found || val.val1 > max_val.val1 ||
|
||||
(val.val1 == max_val.val1 && val.val2 > max_val.val2)) {
|
||||
@@ -46,9 +48,17 @@ public:
|
||||
return zpp::error_code::k_io;
|
||||
}
|
||||
|
||||
/// Return the cached value for a single NTC sensor.
|
||||
static auto GetSensorValue(uint8_t id) -> sensor_value {
|
||||
if (id >= NTC_COUNT) {
|
||||
return sensor_value{};
|
||||
}
|
||||
return s_current_temp[id];
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr size_t NTC_COUNT = 8;
|
||||
|
||||
inline static std::array<sensor_value, NTC_COUNT> s_current_temp{};
|
||||
inline static const device *s_devices[] = {
|
||||
DEVICE_DT_GET(DT_NODELABEL(pole_ntc1)),
|
||||
DEVICE_DT_GET(DT_NODELABEL(pole_ntc2)),
|
||||
|
||||
Reference in New Issue
Block a user