Skip to content

Commit d13e900

Browse files
committed
Accumulate ESC runtime; tidy BLE UUIDs
Unwrap and accumulate the ESC 16-bit time_10ms counter into a 32-bit runtime (ms) to handle the ~10.9min rollover and avoid stale updates. Adds sEscLastTime10ms, sEscAccumulatedRuntimeMs and sEscFirstUpdate, applies stale-data guard, updates escTelemetryData.lastUpdateMs/esc_runtime_ms to use the accumulator, and resets the accumulator on disconnect. Also removes several obsolete packed-telemetry and controller BLE UUID defines from ble_ids.h and normalizes spacing for FAST_LINK UUID defines.
1 parent 0bc925a commit d13e900

2 files changed

Lines changed: 26 additions & 38 deletions

File tree

inc/sp140/ble/ble_ids.h

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,17 @@
2424
#define MANUFACTURER_NAME_UUID "2A29"
2525
#define DEVICE_UNIQUE_ID_UUID "B1571560-345F-4974-A14D-66E98740232F"
2626

27-
// BMS service
28-
#define BMS_TELEMETRY_SERVICE_UUID "9E0F2FA3-3F2B-49C0-A6A3-3D8923062133"
29-
30-
// ESC service
31-
#define ESC_TELEMETRY_SERVICE_UUID "C154DAE9-1984-40EA-B20F-5B23F9CBA0A9"
32-
3327
// Log sync service (historical backfill)
3428
#define LOG_SYNC_SERVICE_UUID "F21F9A40-7A3D-4E08-9A8E-2014C5207A10"
3529
#define LOG_SYNC_CONTROL_UUID "F21F9A41-7A3D-4E08-9A8E-2014C5207A10"
3630
#define LOG_SYNC_DATA_UUID "F21F9A42-7A3D-4E08-9A8E-2014C5207A10"
3731
#define LOG_SYNC_META_UUID "F21F9A43-7A3D-4E08-9A8E-2014C5207A10"
3832
#define LOG_SYNC_STATUS_UUID "F21F9A44-7A3D-4E08-9A8E-2014C5207A10"
3933

40-
// ============================================================================
41-
// Binary Packed Telemetry Characteristics (V1)
42-
// High-efficiency binary telemetry for reduced BLE overhead
43-
// ============================================================================
44-
45-
// Packed BMS telemetry characteristic (single binary packet per BMS)
46-
// For multi-BMS, multiple notifications sent with different bms_id values
47-
#define BMS_PACKED_TELEMETRY_UUID "ABAB6342-D068-45A7-9D45-FBCE8E4D4DF1"
48-
49-
// Extended BMS data (cell voltages + temps array) - separate packet for
50-
// detailed data
51-
#define BMS_EXTENDED_TELEMETRY_UUID "93431570-64A3-448D-AC97-4655B2D1458F"
52-
53-
// Packed ESC telemetry characteristic
54-
#define ESC_PACKED_TELEMETRY_UUID "D3C2B470-007F-494E-95C5-8E275A181A3A"
55-
56-
// Controller telemetry service and characteristic (ESP32 sensors: altitude,
57-
// baro, vario, temps)
58-
#define CONTROLLER_SERVICE_UUID "01C63B60-0891-4655-BBCA-8E745C48A175"
59-
#define CONTROLLER_TELEMETRY_UUID "01C63B61-0891-4655-BBCA-8E745C48A176"
60-
6134
// Fast-Link Unified Telemetry (V1)
6235
#define FAST_LINK_TELEMETRY_SERVICE_UUID "45A17001-B73B-49E1-8B39-5E9ED5E1B930"
63-
#define FAST_LINK_TELEMETRY_UUID "45A17002-B73B-49E1-8B39-5E9ED5E1B930"
36+
#define FAST_LINK_TELEMETRY_UUID "45A17002-B73B-49E1-8B39-5E9ED5E1B930"
6437
// App writes 0x01 here to trigger a GetHwInfo request to the ESC
65-
#define FAST_LINK_COMMAND_UUID "45A17003-B73B-49E1-8B39-5E9ED5E1B930"
38+
#define FAST_LINK_COMMAND_UUID "45A17003-B73B-49E1-8B39-5E9ED5E1B930"
6639

6740
#endif // INC_SP140_BLE_BLE_IDS_H_

src/sp140/esc.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ static unsigned long lastSuccessfulCommTimeMs = 0; // Store millis() time of la
1919
// consumed safely inside readESCTelemetry() on the throttle task.
2020
static volatile bool s_hwInfoRequested = false;
2121

22+
// ESC runtime accumulation — unwraps the uint16 time_10ms counter (~10.9 min period)
23+
// Unsigned subtraction naturally handles wrap: (uint16_t)(current - last) is correct even across rollover.
24+
static uint16_t sEscLastTime10ms = 0;
25+
static uint32_t sEscAccumulatedRuntimeMs = 0;
26+
static bool sEscFirstUpdate = true;
27+
2228

2329
STR_ESC_TELEMETRY_140 escTelemetryData = {
2430
.escState = TelemetryState::NOT_CONNECTED,
@@ -77,21 +83,27 @@ void readESCTelemetry() {
7783
// Only proceed if TWAI is initialized
7884
if (!escTwaiInitialized) { return; } // NOLINT(whitespace/newline)
7985

80-
// Store the last known ESC timestamp before checking for new data
81-
unsigned long previousEscReportedTimeMs = escTelemetryData.lastUpdateMs;
82-
8386
const SineEscModel &model = esc.getModel();
8487

8588
if (model.hasSetThrottleSettings2Response) {
8689
const sine_esc_SetThrottleSettings2Response *res = &model.setThrottleSettings2Response;
8790

88-
unsigned long newEscReportedTimeMs = res->time_10ms * 10;
91+
uint16_t rawTime10ms = res->time_10ms;
92+
93+
// Check if the timestamp from the ESC has actually changed (stale-data guard)
94+
if (sEscFirstUpdate || rawTime10ms != sEscLastTime10ms) {
95+
// Unwrap the uint16 counter using unsigned subtraction — naturally correct across rollover.
96+
// e.g. last=65000, current=100: (uint16_t)(100-65000) = 636 ticks = 6360ms elapsed.
97+
if (!sEscFirstUpdate) {
98+
uint16_t deltaTicks = rawTime10ms - sEscLastTime10ms;
99+
sEscAccumulatedRuntimeMs += (uint32_t)deltaTicks * 10UL;
100+
}
101+
sEscFirstUpdate = false;
102+
sEscLastTime10ms = rawTime10ms;
89103

90-
// Check if the timestamp from the ESC has actually changed
91-
if (newEscReportedTimeMs != previousEscReportedTimeMs) {
92104
// Timestamp is new, process the telemetry data
93-
escTelemetryData.lastUpdateMs = newEscReportedTimeMs;
94-
escTelemetryData.esc_runtime_ms = newEscReportedTimeMs;
105+
escTelemetryData.lastUpdateMs = sEscAccumulatedRuntimeMs;
106+
escTelemetryData.esc_runtime_ms = sEscAccumulatedRuntimeMs;
95107

96108
// Update telemetry data
97109
escTelemetryData.volts = res->voltage / 10.0f;
@@ -135,7 +147,10 @@ void readESCTelemetry() {
135147
// Log state change only if it actually changed
136148
USBSerial.printf("ESC State: %d -> NOT_CONNECTED (Timeout)\n", escTelemetryData.escState);
137149
escTelemetryData.escState = TelemetryState::NOT_CONNECTED;
138-
// Optional: Consider resetting telemetry values here if needed when disconnected
150+
// Reset runtime accumulator so it restarts from zero on reconnect
151+
sEscAccumulatedRuntimeMs = 0;
152+
sEscLastTime10ms = 0;
153+
sEscFirstUpdate = true;
139154
}
140155
} else {
141156
if (escTelemetryData.escState != TelemetryState::CONNECTED) {

0 commit comments

Comments
 (0)