Skip to content

Commit 5673e87

Browse files
committed
Filter invalid motor temperature readings from ESC
Removed temperature state enums and thresholds, and updated logic to filter out invalid/disconnected motor temperature sensor readings (values >140°C) in telemetry processing. Adjusted UI and monitor code to only display valid motor temperature values, improving robustness against sensor disconnection or failure.
1 parent 72a4b97 commit 5673e87

5 files changed

Lines changed: 28 additions & 97 deletions

File tree

inc/sp140/esc.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,15 @@
77
#include <SineEsc.h>
88
#include <CanardAdapter.h>
99

10-
// Temperature thresholds
11-
#define ESC_MOS_WARN 90
12-
#define ESC_MOS_CRIT 110
13-
#define ESC_MCU_WARN 80
14-
#define ESC_MCU_CRIT 95
15-
#define ESC_CAP_WARN 85
16-
#define ESC_CAP_CRIT 100
17-
#define MOTOR_WARN 105
18-
#define MOTOR_CRIT 150
19-
20-
void initESC();
10+
void initESC();
2111
void setESCThrottle(int throttlePWM);
2212
void readESCTelemetry();
2313
bool setupTWAI();
2414

25-
// Get the highest temperature from all ESC sensors
26-
float getHighestTemp(const STR_ESC_TELEMETRY_140& telemetry);
27-
28-
// Function declarations
29-
TempState checkTempState(float temp, TempComponent component);
30-
31-
// ESC Error Decoding Functions
15+
// Get the highest temperature from all ESC sensors
16+
float getHighestTemp(const STR_ESC_TELEMETRY_140& telemetry);
17+
18+
// ESC Error Decoding Functions
3219
String decodeRunningError(uint16_t errorCode);
3320
String decodeSelfCheckError(uint16_t errorCode);
3421
bool hasRunningError(uint16_t errorCode);

inc/sp140/structs.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@
66

77
#pragma pack(push, 1)
88

9-
// Temperature component enum
10-
enum TempComponent {
11-
COMP_ESC_MOS = 0,
12-
COMP_ESC_MCU = 1,
13-
COMP_ESC_CAP = 2,
14-
COMP_MOTOR = 3
15-
};
16-
17-
enum TempState {
18-
TEMP_INVALID = -1, // Sensor reading is invalid
19-
TEMP_NORMAL = 0,
20-
TEMP_WARNING = 1,
21-
TEMP_CRITICAL = 2
22-
};
23-
249
// Telemetry connection state
2510
enum class TelemetryState : uint8_t {
2611
NOT_CONNECTED, // Never received data since boot
@@ -42,11 +27,6 @@ typedef struct {
4227
uint8_t statusFlag;
4328
word checksum;
4429
unsigned long lastUpdateMs; // Timestamp of last telemetry update
45-
TempState mos_state; // MOS temperature state
46-
TempState mcu_state; // MCU temperature state
47-
TempState cap_state; // CAP temperature state
48-
TempState motor_state; // Motor temperature state
49-
bool temp_sensor_error; // True if any sensor is invalid
5030
TelemetryState escState; // Current connection state
5131
uint16_t running_error; // Runtime error bitmask
5232
uint16_t selfcheck_error; // Self-check error bitmask

src/sp140/esc.cpp

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,23 @@ void readESCTelemetry() {
9494
escTelemetryData.amps = res->bus_current / 10.0f;
9595
escTelemetryData.mos_temp = res->mos_temp / 10.0f;
9696
escTelemetryData.cap_temp = res->cap_temp / 10.0f;
97-
escTelemetryData.mcu_temp = res->mcu_temp / 10.0f;
98-
escTelemetryData.motor_temp = res->motor_temp / 10.0f;
97+
escTelemetryData.mcu_temp = res->mcu_temp / 10.0f;
98+
// Filter motor temp - only update if sensor is connected (valid range: -20°C to 140°C)
99+
// Disconnected sensor reads ~149°C (thermistor pulled high)
100+
float rawMotorTemp = res->motor_temp / 10.0f;
101+
if (rawMotorTemp > -20.0f && rawMotorTemp <= 140.0f) {
102+
escTelemetryData.motor_temp = rawMotorTemp;
103+
}
104+
// else: keep previous value (sensor disconnected or invalid)
99105
escTelemetryData.eRPM = res->speed;
100106
escTelemetryData.inPWM = res->recv_pwm / 10.0f;
101107
watts = escTelemetryData.amps * escTelemetryData.volts;
102108

103-
// Store error bitmasks
104-
escTelemetryData.running_error = res->running_error;
105-
escTelemetryData.selfcheck_error = res->selfcheck_error;
106-
107-
// Temperature states
108-
escTelemetryData.mos_state = checkTempState(escTelemetryData.mos_temp, COMP_ESC_MOS);
109-
escTelemetryData.mcu_state = checkTempState(escTelemetryData.mcu_temp, COMP_ESC_MCU);
110-
escTelemetryData.cap_state = checkTempState(escTelemetryData.cap_temp, COMP_ESC_CAP);
111-
escTelemetryData.motor_state = checkTempState(escTelemetryData.motor_temp, COMP_MOTOR);
112-
escTelemetryData.temp_sensor_error =
113-
(escTelemetryData.mos_state == TEMP_INVALID) ||
114-
(escTelemetryData.mcu_state == TEMP_INVALID) ||
115-
(escTelemetryData.cap_state == TEMP_INVALID) ||
116-
(escTelemetryData.motor_state == TEMP_INVALID);
117-
118-
// Record the time of this successful communication using the local clock
109+
// Store error bitmasks
110+
escTelemetryData.running_error = res->running_error;
111+
escTelemetryData.selfcheck_error = res->selfcheck_error;
112+
113+
// Record the time of this successful communication using the local clock
119114
lastSuccessfulCommTimeMs = millis();
120115
} // else: Timestamp hasn't changed, treat as stale data, don't update local timer or telemetry
121116

@@ -309,46 +304,12 @@ double mapDouble(double x, double in_min, double in_max, double out_min, double
309304
* @param telemetry ESC telemetry data structure
310305
* @return The highest temperature value among motor, MOSFET, and capacitor temps
311306
*/
312-
float getHighestTemp(const STR_ESC_TELEMETRY_140& telemetry) {
313-
return max(telemetry.motor_temp, max(telemetry.mos_temp, telemetry.cap_temp));
314-
}
315-
316-
/**
317-
* Check temperature state for a specific component
318-
* @param temp Temperature value to check
319-
* @param component Component type (ESC_MOS, ESC_MCU, ESC_CAP, or MOTOR)
320-
* @return Temperature state (NORMAL, WARNING, CRITICAL, or INVALID)
321-
*/
322-
TempState checkTempState(float temp, TempComponent component) {
323-
// Check for invalid temperature readings
324-
if (temp < -50 || temp > 200) {
325-
return TEMP_INVALID;
326-
}
327-
328-
switch (component) {
329-
case COMP_ESC_MOS:
330-
return temp >= ESC_MOS_CRIT ? TEMP_CRITICAL :
331-
temp >= ESC_MOS_WARN ? TEMP_WARNING : TEMP_NORMAL;
332-
333-
case COMP_ESC_MCU:
334-
return temp >= ESC_MCU_CRIT ? TEMP_CRITICAL :
335-
temp >= ESC_MCU_WARN ? TEMP_WARNING : TEMP_NORMAL;
336-
337-
case COMP_ESC_CAP:
338-
return temp >= ESC_CAP_CRIT ? TEMP_CRITICAL :
339-
temp >= ESC_CAP_WARN ? TEMP_WARNING : TEMP_NORMAL;
340-
341-
case COMP_MOTOR:
342-
return temp >= MOTOR_CRIT ? TEMP_CRITICAL :
343-
temp >= MOTOR_WARN ? TEMP_WARNING : TEMP_NORMAL;
344-
345-
default:
346-
return TEMP_INVALID;
347-
}
348-
}
349-
350-
/**
351-
* Decode running error bitmask into human-readable string
307+
float getHighestTemp(const STR_ESC_TELEMETRY_140& telemetry) {
308+
return max(telemetry.motor_temp, max(telemetry.mos_temp, telemetry.cap_temp));
309+
}
310+
311+
/**
312+
* Decode running error bitmask into human-readable string
352313
* @param errorCode 16-bit running error code from ESC
353314
* @return String containing decoded error messages
354315
*/

src/sp140/esc_monitors.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void addESCMonitors() {
5151
monitors.push_back(escCapTemp);
5252

5353
// Motor Temperature Monitor - with hysteresis
54+
// Note: Invalid readings (>140°C = disconnected) are filtered in esc.cpp
5455
static HysteresisSensorMonitor* motorTemp = new HysteresisSensorMonitor(
5556
SensorID::Motor_Temp,
5657
SensorCategory::ESC,

src/sp140/lvgl/lvgl_updates.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,9 @@ void updateLvglMainScreen(
704704
lv_obj_remove_style(motor_temp_bg, &style_warning, 0);
705705
lv_obj_remove_style(motor_temp_bg, &style_critical, 0);
706706

707-
if (escTelemetry.escState == TelemetryState::CONNECTED && motorTemp > -20.0f) {
707+
// Show motor temp if ESC connected and reading is valid (not disconnected)
708+
// Disconnected sensor reads ~149°C, so treat > 140°C as invalid
709+
if (escTelemetry.escState == TelemetryState::CONNECTED && motorTemp > -20.0f && motorTemp <= 140.0f) {
708710
lv_label_set_text_fmt(motor_temp_label, "%d", static_cast<int>(motorTemp));
709711

710712
if (motorTemp >= motorTempThresholds.critHigh) {

0 commit comments

Comments
 (0)