Uart debug (#64)

* heater: export state and description

* pump_dac: save and export Ipump

* float interpolation helpers

* Show heater state, heater duty, temperature and fault over uart

* uart: more stack for thread
This commit is contained in:
Andrey G 2022-05-11 11:41:07 +03:00 committed by GitHub
parent 2167016c16
commit d3e460a9a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 86 additions and 41 deletions

View File

@ -15,14 +15,6 @@ using namespace wbo;
// 400khz / 1024 = 390hz PWM // 400khz / 1024 = 390hz PWM
static Pwm heaterPwm(HEATER_PWM_DEVICE, HEATER_PWM_CHANNEL, 400'000, 1024); static Pwm heaterPwm(HEATER_PWM_DEVICE, HEATER_PWM_CHANNEL, 400'000, 1024);
enum class HeaterState
{
Preheat,
WarmupRamp,
ClosedLoop,
Stopped,
};
static constexpr int preheatTimeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_PERIOD; static constexpr int preheatTimeCounter = HEATER_PREHEAT_TIME / HEATER_CONTROL_PERIOD;
static constexpr int batteryStabTimeCounter = HEATER_BATTERY_STAB_TIME / HEATER_CONTROL_PERIOD; static constexpr int batteryStabTimeCounter = HEATER_BATTERY_STAB_TIME / HEATER_CONTROL_PERIOD;
static int timeCounter = preheatTimeCounter; static int timeCounter = preheatTimeCounter;
@ -217,3 +209,24 @@ float GetHeaterDuty()
{ {
return heaterPwm.GetLastDuty(); return heaterPwm.GetLastDuty();
} }
HeaterState GetHeaterState()
{
return state;
}
const char* describeHeaterState(HeaterState state)
{
switch (state) {
case HeaterState::Preheat:
return "Preheat";
case HeaterState::WarmupRamp:
return "WarmupRamp";
case HeaterState::ClosedLoop:
return "ClosedLoop";
case HeaterState::Stopped:
return "Stopped";
}
return "Unknown";
}

View File

@ -2,6 +2,16 @@
#include <cstdint> #include <cstdint>
enum class HeaterState
{
Preheat,
WarmupRamp,
ClosedLoop,
Stopped,
};
void StartHeaterControl(); void StartHeaterControl();
bool IsRunningClosedLoop(); bool IsRunningClosedLoop();
float GetHeaterDuty(); float GetHeaterDuty();
HeaterState GetHeaterState();
const char* describeHeaterState(HeaterState state);

View File

@ -1,37 +1,37 @@
#include "interpolation.h" #include "interpolation.h"
int interpolateInt(int x1, int y1, int x2, int y2, int x) float interpolateFloat(float x1, float y1, float x2, float y2, float x)
{ {
if (x1 == x2) if (x1 == x2)
return y1; return y1;
return (y1 + (y2 - y1) * (x - x1) / (x2 - x1)); return (y1 + (y2 - y1) * (x - x1) / (x2 - x1));
} }
int interpolateIntClamped(int x1, int y1, int x2, int y2, int x) float interpolateFloatClamped(float x1, float y1, float x2, float y2, float x)
{ {
if (x <= x1) if (x <= x1)
return y1; return y1;
if (x >= x2) if (x >= x2)
return y2; return y2;
return interpolateInt(x1, y1, x2, y2, x); return interpolateFloat(x1, y1, x2, y2, x);
} }
int interpolate_1d_int(const struct inter_point *p, int size, int x) float interpolate_1d_float(const struct inter_point *p, int size, float x)
{ {
int i; int i;
/* no exterpolation */ /* no exterpolation */
if (x < p[0].x) if (x < p[0].x)
return p[0].y; return p[0].y;
for (i = 0; i < size - 1; i++) { for (i = 0; i < size - 1; i++) {
if ((x >= p[i].x) && (x < p[i + 1].x)) { if ((x >= p[i].x) && (x < p[i + 1].x)) {
return interpolateInt(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y, x); return interpolateFloat(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y, x);
} }
} }
/* no exterpolation */ /* no exterpolation */
return p[size - 1].y; return p[size - 1].y;
} }

View File

@ -1,11 +1,10 @@
#pragma once #pragma once
struct inter_point struct inter_point {
{ float x;
int x; float y;
int y;
}; };
int interpolateInt(int x1, int y1, int x2, int y2, int x); float interpolateFloat(float x1, float y1, float x2, float y2, float x);
int interpolateIntClamped(int x1, int y1, int x2, int y2, int x); float interpolateFloatClamped(float x1, float y1, float x2, float y2, float x);
int interpolate_1d_int(const struct inter_point *p, int size, int x); float interpolate_1d_float(const struct inter_point *p, int size, float x);

View File

@ -9,6 +9,8 @@
// 48MHz / 1024 = 46.8khz PWM // 48MHz / 1024 = 46.8khz PWM
static Pwm pumpDac(PUMP_DAC_PWM_DEVICE, PUMP_DAC_PWM_CHANNEL, 48'000'000, 1024); static Pwm pumpDac(PUMP_DAC_PWM_DEVICE, PUMP_DAC_PWM_CHANNEL, 48'000'000, 1024);
static int32_t curIpump;
void InitPumpDac() void InitPumpDac()
{ {
pumpDac.Start(); pumpDac.Start();
@ -26,6 +28,8 @@ void SetPumpCurrentTarget(int32_t microampere)
microampere = 0; microampere = 0;
} }
curIpump = microampere;
// 47 ohm resistor // 47 ohm resistor
// 0.147 gain // 0.147 gain
// effective resistance of 317 ohms // effective resistance of 317 ohms
@ -41,3 +45,8 @@ float GetPumpOutputDuty()
{ {
return pumpDac.GetLastDuty(); return pumpDac.GetLastDuty();
} }
int32_t GetPumpCurrent()
{
return curIpump;
}

View File

@ -5,3 +5,4 @@
void InitPumpDac(); void InitPumpDac();
void SetPumpCurrentTarget(int32_t microamperes); void SetPumpCurrentTarget(int32_t microamperes);
float GetPumpOutputDuty(); float GetPumpOutputDuty();
int32_t GetPumpCurrent();

View File

@ -110,7 +110,7 @@ float GetSensorInternalResistance()
float GetSensorTemperature() float GetSensorTemperature()
{ {
return interpolate_1d_int(lsu49_r_to_temp, ARRAY_SIZE(lsu49_r_to_temp), GetSensorInternalResistance()); return interpolate_1d_float(lsu49_r_to_temp, ARRAY_SIZE(lsu49_r_to_temp), GetSensorInternalResistance());
} }
float GetNernstDc() float GetNernstDc()

View File

@ -4,6 +4,8 @@
#include "lambda_conversion.h" #include "lambda_conversion.h"
#include "sampling.h" #include "sampling.h"
#include "heater_control.h"
#include "fault.h"
#include "uart.h" #include "uart.h"
static const UARTConfig uartCfg = static const UARTConfig uartCfg =
@ -27,7 +29,7 @@ static const UARTConfig uartCfg =
static char printBuffer[200]; static char printBuffer[200];
static THD_WORKING_AREA(waUartThread, 256); static THD_WORKING_AREA(waUartThread, 512);
static void UartThread(void*) static void UartThread(void*)
{ {
while(true) while(true)
@ -35,11 +37,22 @@ static void UartThread(void*)
float lambda = GetLambda(); float lambda = GetLambda();
int lambdaIntPart = lambda; int lambdaIntPart = lambda;
int lambdaThousandths = (lambda - lambdaIntPart) * 1000; int lambdaThousandths = (lambda - lambdaIntPart) * 1000;
int batteryVoltageMv = GetInternalBatteryVoltage() * 1000;
int duty = GetHeaterDuty() * 100;
size_t writeCount = chsnprintf(printBuffer, 200, "%d.%03d\t%d\t%d\r\n", lambdaIntPart, lambdaThousandths, (int)GetSensorInternalResistance(), (int)(GetPumpNominalCurrent() * 1000)); size_t writeCount = chsnprintf(printBuffer, 200,
"%d.%03d\tAC %d mV\tR: %d\tT: %d\tIpump: %d\tVbat: %d\theater: %s (%d)\tfault: %s\r\n",
lambdaIntPart, lambdaThousandths,
(int)(GetNernstAc() * 1000.0),
(int)GetSensorInternalResistance(),
(int)GetSensorTemperature(),
(int)(GetPumpNominalCurrent() * 1000),
batteryVoltageMv,
describeHeaterState(GetHeaterState()), duty,
describeFault(GetCurrentFault()));
uartStartSend(&UARTD1, writeCount, printBuffer); uartStartSend(&UARTD1, writeCount, printBuffer);
chThdSleepMilliseconds(20); chThdSleepMilliseconds(50);
} }
} }