Skip to content

Commit d3a7997

Browse files
authored
Merge pull request #90 from openppg/7.4-Tweaks
Quality of life tweaks: Enhance motor temp handling in ESC, chill range
2 parents 8fd9e8f + 8c9ae75 commit d3a7997

19 files changed

Lines changed: 226 additions & 235 deletions

inc/sp140/altimeter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#define VARIO_BUFFER_SIZE 25 // Number of samples to average for vertical speed
1111
#define MAX_VERTICAL_SPEED 250.0f // Maximum vertical speed to display (m/s)
1212

13-
// Set up the barometer
14-
bool setupAltimeter(bool alt_wire = false);
13+
// Set up the barometer
14+
bool setupAltimeter();
1515

1616
// Get the altitude (in meters)
1717
float getAltitude(const STR_DEVICE_DATA_140_V1& deviceData);

inc/sp140/esc.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@
22
#define INC_SP140_ESC_H_
33

44
#include <Arduino.h>
5+
6+
// Motor temp validity range (disconnected/invalid readings are represented as NaN)
7+
constexpr float MOTOR_TEMP_VALID_MIN_C = -20.0f;
8+
constexpr float MOTOR_TEMP_VALID_MAX_C = 140.0f;
9+
10+
inline bool isMotorTempValidC(float tempC) {
11+
return tempC > MOTOR_TEMP_VALID_MIN_C && tempC <= MOTOR_TEMP_VALID_MAX_C;
12+
}
513
#include "sp140/structs.h"
614
#include "../../inc/sp140/esp32s3-config.h"
715
#include <SineEsc.h>
816
#include <CanardAdapter.h>
917

10-
void initESC();
18+
void initESC();
1119
void setESCThrottle(int throttlePWM);
1220
void readESCTelemetry();
1321
bool setupTWAI();
1422

15-
// Get the highest temperature from all ESC sensors
16-
float getHighestTemp(const STR_ESC_TELEMETRY_140& telemetry);
17-
1823
// ESC Error Decoding Functions
19-
String decodeRunningError(uint16_t errorCode);
24+
String decodeRunningError(uint16_t errorCode);
2025
String decodeSelfCheckError(uint16_t errorCode);
2126
bool hasRunningError(uint16_t errorCode);
2227
bool hasSelfCheckError(uint16_t errorCode);

inc/sp140/simple_monitor.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <vector>
55
#include <functional>
6+
#include <math.h>
67
#include <Arduino.h>
78
#include "sp140/structs.h"
89

@@ -148,6 +149,14 @@ struct SensorMonitor : public IMonitor {
148149

149150
void check() override {
150151
float v = read();
152+
if (isnan(v)) {
153+
if (last != AlertLevel::OK) {
154+
logger->log(id, AlertLevel::OK, v);
155+
last = AlertLevel::OK;
156+
}
157+
return;
158+
}
159+
151160
AlertLevel now = AlertLevel::OK;
152161

153162
if (v <= thr.critLow) now = AlertLevel::CRIT_LOW;
@@ -182,6 +191,14 @@ struct HysteresisSensorMonitor : public SensorMonitor {
182191

183192
void check() override {
184193
float v = read();
194+
if (isnan(v)) {
195+
if (last != AlertLevel::OK) {
196+
logger->log(id, AlertLevel::OK, v);
197+
last = AlertLevel::OK;
198+
}
199+
return;
200+
}
201+
185202
AlertLevel now = last; // Start with current state
186203

187204
// Hysteresis logic to prevent bouncing

inc/sp140/structs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ typedef struct {
147147
float mos_temp; // MOSFET temperature (°C)
148148
float cap_temp; // Capacitor temperature (°C)
149149
float mcu_temp; // MCU temperature (°C)
150-
float motor_temp; // Motor temperature (°C)
150+
float motor_temp; // Motor temperature (°C), NaN if sensor invalid/disconnected
151151
int32_t eRPM; // Electrical RPM
152152
uint16_t inPWM; // Input PWM value
153153
uint16_t running_error; // Runtime error bitmask

inc/sp140/throttle.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,22 @@ uint16_t readThrottleRaw();
4444
/** Get the most recently sampled raw throttle value (0..4095). */
4545
uint16_t getLastThrottleRaw();
4646

47-
/** Convert raw pot reading (0..4095) to PWM microseconds. */
47+
/** Convert raw pot reading (0..4095) to PWM microseconds (full range). */
4848
int potRawToPwm(uint16_t raw);
4949

50+
/**
51+
* Convert raw pot reading (0..4095) to PWM microseconds using the
52+
* mode-specific maximum. In chill mode the full physical range maps to
53+
* ESC_MIN_PWM..CHILL_MODE_MAX_PWM; in sport mode it maps to the full
54+
* ESC_MIN_PWM..ESC_MAX_PWM range. This gives chill mode full granular
55+
* control over its limited output range instead of a hard clamp.
56+
*
57+
* @param raw Raw ADC value (0..4095)
58+
* @param performance_mode 0 = CHILL, 1 = SPORT
59+
* @return PWM microseconds in the mode's range
60+
*/
61+
int potRawToModePwm(uint16_t raw, uint8_t performance_mode);
62+
5063
/**
5164
* Apply mode-based ramp (us/tick) and clamp to the mode's max PWM.
5265
* Updates `prevPwm` with the final value.
@@ -86,10 +99,13 @@ bool throttleEngaged();
8699
/**
87100
* Read throttle input and return smoothed PWM value.
88101
* This is the core throttle processing pipeline without any state logic.
102+
* Uses mode-aware mapping so the full physical range covers the mode's
103+
* PWM output range.
89104
*
105+
* @param performance_mode 0 = CHILL, 1 = SPORT
90106
* @return Smoothed PWM value from throttle input
91107
*/
92-
int getSmoothedThrottlePwm();
108+
int getSmoothedThrottlePwm(uint8_t performance_mode);
93109

94110
/**
95111
* Reset throttle state for clean startup/disarm.
@@ -107,8 +123,8 @@ void handleThrottle();
107123

108124
/**
109125
* Calculate the cruise control PWM value from a raw pot reading.
110-
* Uses the same mapping as normal throttle (full range then clamp to mode max).
111-
* Also applies the absolute cruise max cap.
126+
* Uses the same mode-aware mapping as normal throttle, then applies
127+
* the absolute cruise max cap.
112128
*
113129
* @param potVal Raw potentiometer value (0..4095)
114130
* @param performance_mode 0 = CHILL, 1 = SPORT

inc/sp140/utilities.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
#ifndef INC_SP140_UTILITIES_H_
2-
#define INC_SP140_UTILITIES_H_
3-
4-
#include <Arduino.h>
5-
6-
double mapd(double x, double in_min, double in_max, double out_min, double out_max);
7-
8-
// Function for digital time display - adds leading zeros
9-
String convertToDigits(byte digits);
10-
11-
// Function to get unique chip ID
12-
String chipId();
1+
#ifndef INC_SP140_UTILITIES_H_
2+
#define INC_SP140_UTILITIES_H_
3+
4+
#include <Arduino.h>
5+
6+
// Function to get unique chip ID
7+
String chipId();
138

149
// Definitions for main rainbow colors in WRGB format for NeoPixel.
1510
// The 32-bit color value is WRGB. W (White) is ignored for RGB pixels.

inc/sp140/vibration_pwm.h

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,4 @@ void pulseVibration(uint16_t duration_ms, uint8_t intensity);
7474
*/
7575
void stopVibration();
7676

77-
/**
78-
* @brief Initializes the critical alert service.
79-
*
80-
* This function sets up the necessary resources for handling synchronized critical alerts.
81-
* It should be called once during system initialization.
82-
*/
83-
void initCriticalAlertService();
84-
85-
/**
86-
* @brief Starts the critical alert notifications.
87-
*
88-
* Activates the synchronized vibration and border flashing. If alerts are
89-
* already running, this function has no effect.
90-
*/
91-
void startCriticalAlerts();
92-
93-
/**
94-
* @brief Stops the critical alert notifications.
95-
*
96-
* Deactivates the vibration and border flashing. If alerts are not currently
97-
* running, this function has no effect.
98-
*/
99-
void stopCriticalAlerts();
100-
101-
/**
102-
* @brief Checks if the critical alert system is currently active.
103-
*
104-
* @return true if critical alerts are active, false otherwise.
105-
*/
106-
bool isCriticalAlertActive();
107-
10877
#endif // INC_SP140_VIBRATION_PWM_H_

src/sp140/alert_display.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ void initAlertDisplay() {
4343
return;
4444
}
4545

46-
// Init the new alert service
47-
initCriticalAlertService();
48-
4946
// Create aggregation task (low priority)
5047
xTaskCreate(alertAggregationTask, "AlertAgg", 4096, NULL, 1, &alertAggregationTaskHandle);
5148
USBSerial.println("[AlertDisplay] Init complete");

src/sp140/altimeter.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ float getBaroPressure() {
7575
return __FLT_MIN__; // Return a very small number if BMP is not present
7676
}
7777

78-
// Start the bmp3XX sensor
79-
bool setupAltimeter(bool altWire) {
80-
TwoWire* wire = &Wire;
81-
82-
// pull down pin 40 to high to set the address
83-
pinMode(40, OUTPUT);
84-
digitalWrite(40, HIGH);
78+
// Start the bmp3XX sensor
79+
bool setupAltimeter() {
80+
// pull down pin 40 to high to set the address
81+
pinMode(40, OUTPUT);
82+
digitalWrite(40, HIGH);
8583
if (!bmp.begin_I2C(BMP3XX_DEFAULT_ADDRESS, &Wire)) return false;
8684

8785
bmp.setOutputDataRate(BMP3_ODR_25_HZ);

src/sp140/ble/esc_service.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void updateESCPackedTelemetry(const STR_ESC_TELEMETRY_140& telemetry) {
9292
packet.mos_temp = telemetry.mos_temp;
9393
packet.cap_temp = telemetry.cap_temp;
9494
packet.mcu_temp = telemetry.mcu_temp;
95+
// motor_temp is NaN when sensor is disconnected/invalid.
9596
packet.motor_temp = telemetry.motor_temp;
9697
packet.eRPM = static_cast<int32_t>(telemetry.eRPM);
9798
packet.inPWM = static_cast<uint16_t>(telemetry.inPWM);
@@ -118,6 +119,7 @@ void updateESCTelemetryBLE(const STR_ESC_TELEMETRY_140& telemetry) {
118119
float voltage = telemetry.volts;
119120
float current = telemetry.amps;
120121
int32_t rpm = static_cast<int32_t>(telemetry.eRPM);
122+
// motor_temp is NaN when sensor is disconnected/invalid.
121123
EscTempsPacket temps = {
122124
telemetry.mos_temp,
123125
telemetry.cap_temp,

0 commit comments

Comments
 (0)