Skip to content

Commit 17a84cf

Browse files
cursoragentpaulwq1234
andcommitted
Add centralized monitoring configuration and monitoring framework
Co-authored-by: paulwq1234 <paulwq1234@gmail.com>
1 parent c8dfb76 commit 17a84cf

File tree

2 files changed

+242
-0
lines changed

2 files changed

+242
-0
lines changed

inc/sp140/monitor_config.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#ifndef INC_SP140_MONITOR_CONFIG_H_
2+
#define INC_SP140_MONITOR_CONFIG_H_
3+
4+
//=============================================================================
5+
// CENTRALIZED MONITORING CONFIGURATION
6+
// This file consolidates all thresholds and constants previously scattered
7+
// across multiple files (esc.h, lvgl_display.cpp, etc.)
8+
//=============================================================================
9+
10+
// ESC Temperature Thresholds (°C)
11+
#define ESC_MOS_TEMP_WARN 90.0f
12+
#define ESC_MOS_TEMP_CRIT 110.0f
13+
#define ESC_MCU_TEMP_WARN 80.0f
14+
#define ESC_MCU_TEMP_CRIT 95.0f
15+
#define ESC_CAP_TEMP_WARN 85.0f
16+
#define ESC_CAP_TEMP_CRIT 100.0f
17+
#define MOTOR_TEMP_WARN 90.0f
18+
#define MOTOR_TEMP_CRIT 110.0f
19+
20+
// BMS Temperature Thresholds (°C)
21+
#define BMS_TEMP_WARN 45.0f
22+
#define BMS_TEMP_CRIT 55.0f
23+
24+
// BMS Voltage Thresholds (V)
25+
#define BMS_CELL_VOLT_WARN 3.2f
26+
#define BMS_CELL_VOLT_CRIT 2.95f
27+
#define BMS_TOTAL_VOLT_MIN 75.0f // 25S * 3.0V
28+
#define BMS_TOTAL_VOLT_MAX 105.0f // 25S * 4.2V
29+
30+
// BMS State of Charge Thresholds (%)
31+
#define BMS_SOC_WARN 20.0f
32+
#define BMS_SOC_CRIT 5.0f
33+
34+
// BMS Voltage Differential (V)
35+
#define BMS_VOLT_DIFF_WARN 0.1f // 100mV difference
36+
#define BMS_VOLT_DIFF_CRIT 0.2f // 200mV difference
37+
38+
// Altimeter/Barometric Temperature (°C)
39+
#define BARO_TEMP_WARN 60.0f // Operating limit
40+
#define BARO_TEMP_CRIT 70.0f // Absolute limit
41+
42+
// Internal System Temperature (°C)
43+
#define CPU_TEMP_WARN 70.0f // ESP32 operating temp
44+
#define CPU_TEMP_CRIT 80.0f // ESP32 critical temp
45+
46+
// Temperature Sensor Validation Limits (°C)
47+
#define TEMP_SENSOR_MIN -50.0f // Below this = sensor error
48+
#define TEMP_SENSOR_MAX 200.0f // Above this = sensor error
49+
50+
// Monitoring System Configuration
51+
#define MONITOR_UPDATE_RATE_MS 100 // How often to check sensors
52+
#define MONITOR_ALERT_DEBOUNCE_MS 1000 // Debounce time for alerts
53+
#define MONITOR_CLEAR_DEBOUNCE_MS 2000 // Time to clear alerts
54+
55+
// Alert System Configuration
56+
#define ALERT_MAX_ACTIVE 10 // Maximum active alerts to track
57+
#define ALERT_DISPLAY_TIME_MS 3000 // How long to show each alert
58+
#define ALERT_CYCLE_TIME_MS 5000 // Time between cycling alerts
59+
60+
// ESC Error Bit Masks (from ESC documentation)
61+
#define ESC_RUNNING_ERROR_OVERCURRENT (1 << 0)
62+
#define ESC_RUNNING_ERROR_LOCKED_ROTOR (1 << 1)
63+
#define ESC_RUNNING_ERROR_OVERTEMP (1 << 2)
64+
#define ESC_RUNNING_ERROR_OVERVOLT (1 << 6)
65+
#define ESC_RUNNING_ERROR_VOLTAGE_DROP (1 << 7)
66+
#define ESC_RUNNING_WARNING_THROTTLE_SAT (1 << 5)
67+
68+
#define ESC_SELFCHECK_ERROR_MOTOR_CURRENT_OUT (1 << 0)
69+
#define ESC_SELFCHECK_ERROR_TOTAL_CURRENT_OUT (1 << 1)
70+
#define ESC_SELFCHECK_ERROR_MOTOR_VOLTAGE_OUT (1 << 2)
71+
#define ESC_SELFCHECK_ERROR_CAP_NTC (1 << 3)
72+
#define ESC_SELFCHECK_ERROR_MOS_NTC (1 << 4)
73+
#define ESC_SELFCHECK_ERROR_BUS_VOLT_RANGE (1 << 5)
74+
#define ESC_SELFCHECK_ERROR_BUS_VOLT_SAMPLE (1 << 6)
75+
#define ESC_SELFCHECK_ERROR_MOTOR_Z_LOW (1 << 7)
76+
#define ESC_SELFCHECK_ERROR_MOTOR_Z_HIGH (1 << 8)
77+
#define ESC_SELFCHECK_ERROR_MOTOR_V_DET1 (1 << 9)
78+
#define ESC_SELFCHECK_ERROR_MOTOR_V_DET2 (1 << 10)
79+
#define ESC_SELFCHECK_ERROR_MOTOR_I_DET2 (1 << 11)
80+
#define ESC_SELFCHECK_ERROR_SW_HW_INCOMPAT (1 << 13)
81+
#define ESC_SELFCHECK_ERROR_BOOTLOADER_BAD (1 << 14)
82+
83+
#endif // INC_SP140_MONITOR_CONFIG_H_

inc/sp140/simple_monitor.h

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#ifndef INC_SP140_SIMPLE_MONITOR_H_
2+
#define INC_SP140_SIMPLE_MONITOR_H_
3+
4+
#include <Arduino.h>
5+
#include <vector>
6+
#include "sp140/structs.h"
7+
#include "sp140/globals.h"
8+
9+
// Forward declarations
10+
class ILogger;
11+
class IMonitor;
12+
13+
// Sensor categories for organization
14+
enum class SensorCategory {
15+
ESC,
16+
BMS,
17+
ALTIMETER,
18+
INTERNAL
19+
};
20+
21+
// Alert levels
22+
enum class AlertLevel {
23+
OK = 0,
24+
WARN_LOW = 1,
25+
WARN_HIGH = 2,
26+
CRIT_LOW = 3,
27+
CRIT_HIGH = 4,
28+
INFO = 5
29+
};
30+
31+
// Sensor ID enumeration
32+
enum class SensorID {
33+
// ESC Temperature sensors
34+
ESC_MOS_Temp,
35+
ESC_MCU_Temp,
36+
ESC_CAP_Temp,
37+
Motor_Temp,
38+
39+
// ESC Running Errors (Critical)
40+
ESC_OverCurrent_Error,
41+
ESC_LockedRotor_Error,
42+
ESC_OverTemp_Error,
43+
ESC_OverVolt_Error,
44+
ESC_VoltageDrop_Error,
45+
46+
// ESC Running Warnings
47+
ESC_ThrottleSat_Warning,
48+
49+
// ESC Self-Check Errors
50+
ESC_MotorCurrentOut_Error,
51+
ESC_TotalCurrentOut_Error,
52+
ESC_MotorVoltageOut_Error,
53+
ESC_CapNTC_Error,
54+
ESC_MosNTC_Error,
55+
ESC_BusVoltRange_Error,
56+
ESC_BusVoltSample_Error,
57+
ESC_MotorZLow_Error,
58+
ESC_MotorZHigh_Error,
59+
ESC_MotorVDet1_Error,
60+
ESC_MotorVDet2_Error,
61+
ESC_MotorIDet2_Error,
62+
ESC_SwHwIncompat_Error,
63+
ESC_BootloaderBad_Error,
64+
65+
// BMS sensors
66+
BMS_MOS_Temp,
67+
BMS_Balance_Temp,
68+
BMS_T1_Temp,
69+
BMS_T2_Temp,
70+
BMS_T3_Temp,
71+
BMS_T4_Temp,
72+
BMS_High_Cell_Voltage,
73+
BMS_Low_Cell_Voltage,
74+
BMS_SOC,
75+
BMS_Total_Voltage,
76+
BMS_Voltage_Differential,
77+
BMS_Charge_MOS,
78+
BMS_Discharge_MOS,
79+
80+
// Altimeter
81+
Baro_Temp,
82+
83+
// Internal
84+
CPU_Temp
85+
};
86+
87+
// Logger interface
88+
class ILogger {
89+
public:
90+
virtual ~ILogger() = default;
91+
virtual void log(SensorID id, AlertLevel level, float value) = 0;
92+
virtual void log(SensorID id, AlertLevel level, bool value) = 0;
93+
};
94+
95+
// Multi-logger that dispatches to multiple sinks
96+
class MultiLogger : public ILogger {
97+
private:
98+
std::vector<ILogger*> sinks;
99+
100+
public:
101+
void addSink(ILogger* sink);
102+
void log(SensorID id, AlertLevel level, float value) override;
103+
void log(SensorID id, AlertLevel level, bool value) override;
104+
};
105+
106+
// Serial logger implementation
107+
class SerialLogger : public ILogger {
108+
public:
109+
void log(SensorID id, AlertLevel level, float value) override;
110+
void log(SensorID id, AlertLevel level, bool value) override;
111+
};
112+
113+
// UI logger for alert events
114+
class AlertUILogger : public ILogger {
115+
public:
116+
void log(SensorID id, AlertLevel level, float value) override;
117+
void log(SensorID id, AlertLevel level, bool value) override;
118+
};
119+
120+
// Monitor interface
121+
class IMonitor {
122+
public:
123+
virtual ~IMonitor() = default;
124+
virtual void check() = 0;
125+
virtual SensorID getSensorID() const = 0;
126+
virtual SensorCategory getCategory() const = 0;
127+
virtual void resetState() = 0;
128+
};
129+
130+
// Global instances
131+
extern MultiLogger multiLogger;
132+
extern std::vector<IMonitor*> monitors;
133+
extern SerialLogger serialLogger;
134+
extern bool monitoringEnabled;
135+
136+
// Thread-safe copies for monitoring
137+
extern STR_ESC_TELEMETRY_140 monitoringEscData;
138+
extern STR_BMS_TELEMETRY_140 monitoringBmsData;
139+
140+
// Core functions
141+
void initSimpleMonitor();
142+
void checkAllSensors();
143+
void checkAllSensorsWithData(const STR_ESC_TELEMETRY_140& escData,
144+
const STR_BMS_TELEMETRY_140& bmsData);
145+
void enableMonitoring();
146+
void sendAlertEvent(SensorID id, AlertLevel level);
147+
148+
// Utility functions
149+
const char* sensorIDToString(SensorID id);
150+
const char* sensorIDToAbbreviation(SensorID id);
151+
const char* sensorIDToAbbreviationWithLevel(SensorID id, AlertLevel level);
152+
153+
// Monitor registration functions (to be implemented)
154+
void addESCMonitors();
155+
void addBMSMonitors();
156+
void addAltimeterMonitors();
157+
void addInternalMonitors();
158+
159+
#endif // INC_SP140_SIMPLE_MONITOR_H_

0 commit comments

Comments
 (0)