Skip to content

Commit 1d6635a

Browse files
authored
Merge pull request #74 from openppg/chill-tweaks
Tweak chill mode, more robust preference checking
2 parents 3c0ca5d + ed7879f commit 1d6635a

File tree

7 files changed

+75
-47
lines changed

7 files changed

+75
-47
lines changed

inc/sp140/altimeter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "sp140/shared-config.h"
88

99
// Constants
10-
#define VARIO_BUFFER_SIZE 10 // Number of samples to average for vertical speed
10+
#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

1313
// Set up the barometer
@@ -25,4 +25,7 @@ void setGroundAltitude(const STR_DEVICE_DATA_140_V1& deviceData);
2525
// Get the temperature in degrees Celsius
2626
float getBaroTemperature();
2727

28+
// Get the pressure in hPa
29+
float getBaroPressure();
30+
2831
#endif // INC_SP140_ALTIMETER_H_

inc/sp140/throttle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
// Throttle control (PWM-first) constants
2121
// Ramping in PWM microseconds per tick (~20ms per tick in throttle task)
22-
#define CHILL_MODE_MAX_PWM 1850 // 85% max power in chill mode
23-
#define CHILL_MODE_RAMP_RATE 10 // us/tick in chill mode (~1.6s 1035->1850)
22+
#define CHILL_MODE_MAX_PWM 1600 // 70% max power in chill mode
23+
#define CHILL_MODE_RAMP_RATE 8 // us/tick in chill mode (~1.6s 1035->1600)
2424
#define SPORT_MODE_RAMP_RATE 27 // us/tick in sport mode (~0.68s 1035->1950)
2525

2626
/**

src/sp140/altimeter.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Adafruit_BMP3XX bmp;
77
bool bmpPresent = false;
88
float groundAltitude = 0;
99

10-
// Buffer to store altitude readings with timestamps
1110
struct AltitudeReading {
1211
float altitude;
1312
unsigned long timestamp;
1413
};
1514

15+
// Buffer to store altitude readings with timestamps
1616
CircularBuffer<AltitudeReading, VARIO_BUFFER_SIZE> altitudeBuffer;
1717

1818
float getAltitude(const STR_DEVICE_DATA_140_V1& deviceData) {
@@ -67,6 +67,14 @@ float getBaroTemperature() {
6767
return __FLT_MIN__; // Return a very small number if BMP is not present
6868
}
6969

70+
// Get the pressure in hPa
71+
float getBaroPressure() {
72+
if (bmpPresent) {
73+
return bmp.readPressure() / 100.0f; // Convert Pa to hPa
74+
}
75+
return __FLT_MIN__; // Return a very small number if BMP is not present
76+
}
77+
7078
// Start the bmp3XX sensor
7179
bool setupAltimeter(bool altWire) {
7280
TwoWire* wire = &Wire;

src/sp140/extra-data.ino

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,16 @@ void setupBLE() {
590590

591591
// Read saved data from Preferences
592592
void refreshDeviceData() {
593+
// Try to initialize preferences, with corruption recovery
593594
if (!preferences.begin(PREFS_NAMESPACE, false)) {
594-
USBSerial.println(F("Failed to initialize Preferences"));
595+
USBSerial.println(F("Failed to initialize Preferences - may be corrupted"));
596+
597+
// Try to clear corrupted preferences and start fresh
598+
preferences.begin(PREFS_NAMESPACE, false);
599+
preferences.clear();
600+
preferences.end();
601+
602+
USBSerial.println(F("Cleared potentially corrupted preferences, using defaults"));
595603
resetDeviceData();
596604
return;
597605
}
@@ -604,7 +612,9 @@ void refreshDeviceData() {
604612
return;
605613
}
606614

607-
// Load all values from preferences
615+
// Load all values from preferences with validation
616+
bool dataValid = true;
617+
608618
deviceData.version_major = preferences.getUChar(KEY_VERSION_MAJOR, VERSION_MAJOR);
609619
deviceData.version_minor = preferences.getUChar(KEY_VERSION_MINOR, VERSION_MINOR);
610620
deviceData.screen_rotation = preferences.getUChar(KEY_SCREEN_ROTATION, DEFAULT_SCREEN_ROTATION);
@@ -617,10 +627,24 @@ void refreshDeviceData() {
617627
deviceData.revision = preferences.getUChar(KEY_REVISION, 3); // Default to ESP32-S3
618628
deviceData.timezone_offset = preferences.getInt(KEY_TIMEZONE_OFFSET, 0);
619629

630+
// Validate critical display-related settings
631+
if (deviceData.screen_rotation != 1 && deviceData.screen_rotation != 3) {
632+
USBSerial.println(F("Warning: Invalid screen rotation detected, using default"));
633+
deviceData.screen_rotation = DEFAULT_SCREEN_ROTATION;
634+
dataValid = false;
635+
}
636+
637+
if (deviceData.theme > 1) {
638+
USBSerial.println(F("Warning: Invalid theme detected, using default"));
639+
deviceData.theme = DEFAULT_THEME;
640+
dataValid = false;
641+
}
642+
620643
preferences.end();
621644

622645
// Ensure values are within valid ranges
623-
if (sanitizeDeviceData()) {
646+
if (sanitizeDeviceData() || !dataValid) {
647+
USBSerial.println(F("Sanitized corrupted preference values"));
624648
writeDeviceData(); // Save sanitized values
625649
}
626650

@@ -634,21 +658,27 @@ void writeDeviceData() {
634658
return;
635659
}
636660

637-
// Save all values to preferences
638-
preferences.putUChar(KEY_VERSION_MAJOR, deviceData.version_major);
639-
preferences.putUChar(KEY_VERSION_MINOR, deviceData.version_minor);
640-
preferences.putUChar(KEY_SCREEN_ROTATION, deviceData.screen_rotation);
641-
preferences.putFloat(KEY_SEA_PRESSURE, deviceData.sea_pressure);
642-
preferences.putBool(KEY_METRIC_TEMP, deviceData.metric_temp);
643-
preferences.putBool(KEY_METRIC_ALT, deviceData.metric_alt);
644-
preferences.putUChar(KEY_PERFORMANCE_MODE, deviceData.performance_mode);
645-
preferences.putUChar(KEY_THEME, deviceData.theme);
646-
preferences.putUShort(KEY_ARMED_TIME, deviceData.armed_time);
647-
preferences.putUChar(KEY_REVISION, deviceData.revision);
648-
preferences.putInt(KEY_TIMEZONE_OFFSET, deviceData.timezone_offset);
649-
650-
preferences.end();
651-
USBSerial.println(F("Device data saved to Preferences"));
661+
// Save all values to preferences with error checking
662+
bool success = true;
663+
success &= (preferences.putUChar(KEY_VERSION_MAJOR, deviceData.version_major) > 0);
664+
success &= (preferences.putUChar(KEY_VERSION_MINOR, deviceData.version_minor) > 0);
665+
success &= (preferences.putUChar(KEY_SCREEN_ROTATION, deviceData.screen_rotation) > 0);
666+
success &= (preferences.putFloat(KEY_SEA_PRESSURE, deviceData.sea_pressure) > 0);
667+
success &= (preferences.putBool(KEY_METRIC_TEMP, deviceData.metric_temp) > 0);
668+
success &= (preferences.putBool(KEY_METRIC_ALT, deviceData.metric_alt) > 0);
669+
success &= (preferences.putUChar(KEY_PERFORMANCE_MODE, deviceData.performance_mode) > 0);
670+
success &= (preferences.putUChar(KEY_THEME, deviceData.theme) > 0);
671+
success &= (preferences.putUShort(KEY_ARMED_TIME, deviceData.armed_time) > 0);
672+
success &= (preferences.putUChar(KEY_REVISION, deviceData.revision) > 0);
673+
success &= (preferences.putInt(KEY_TIMEZONE_OFFSET, deviceData.timezone_offset) > 0);
674+
675+
if (success) {
676+
preferences.end();
677+
USBSerial.println(F("Device data saved to Preferences"));
678+
} else {
679+
preferences.end();
680+
USBSerial.println(F("Warning: Some preferences may not have been saved correctly"));
681+
}
652682
}
653683

654684
// Reset Preferences and deviceData to factory defaults
@@ -689,9 +719,9 @@ void parse_serial_commands() {
689719

690720
DeserializationError error = deserializeJson(doc, USBSerial);
691721

722+
// Handle parsing results
692723
if (error) {
693-
USBSerial.print("JSON parse error: ");
694-
USBSerial.println(error.c_str());
724+
// Silently ignore non-JSON input or parsing errors
695725
return;
696726
}
697727

@@ -738,8 +768,8 @@ void parse_serial_commands() {
738768

739769
sanitizeDeviceData();
740770
writeDeviceData();
741-
//resetRotation(deviceData.screen_rotation);
742-
//setTheme(deviceData.theme);
771+
// resetRotation(deviceData.screen_rotation);
772+
// setTheme(deviceData.theme);
743773

744774
send_device_data();
745775
}

src/sp140/lvgl/lvgl_updates.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ void updateClimbRateIndicator(float climbRate) {
293293
lv_color_make(75, 0, 130) // Dark purple (again)
294294
};
295295

296-
if (climbRate >= 0.05f) {
296+
if (climbRate >= 0.15f) {
297297
// Positive climb rate - fill sections above center line
298-
int sectionsToFill = (int)(climbRate / 0.1f);
298+
int sectionsToFill = (int)(climbRate / 0.3f);
299299
if (sectionsToFill > 6) sectionsToFill = 6;
300300

301301
for (int i = 0; i < sectionsToFill; i++) {
@@ -305,9 +305,9 @@ void updateClimbRateIndicator(float climbRate) {
305305
lv_obj_set_style_bg_color(climb_rate_fill_sections[sectionIndex], positive_colors[i], LV_PART_MAIN);
306306
}
307307
}
308-
} else if (climbRate <= -0.05f) {
308+
} else if (climbRate <= -0.15f) {
309309
// Negative climb rate - fill sections below center line
310-
int sectionsToFill = (int)(-climbRate / 0.1f);
310+
int sectionsToFill = (int)(-climbRate / 0.3f);
311311
if (sectionsToFill > 6) sectionsToFill = 6;
312312

313313
for (int i = 0; i < sectionsToFill; i++) {
@@ -318,7 +318,7 @@ void updateClimbRateIndicator(float climbRate) {
318318
}
319319
}
320320
}
321-
// If climb rate is between -0.05 and +0.05, no sections are filled (neutral)
321+
// If climb rate is between -0.15 and +0.15, no sections are filled (neutral)
322322
}
323323

324324
void updateLvglMainScreen(
@@ -758,7 +758,7 @@ void updateLvglMainScreen(
758758
static uint32_t lastAltitudeTime = 0;
759759
uint32_t currentTime = millis();
760760

761-
if (currentTime - lastAltitudeTime > 500) { // Update climb rate every 500ms
761+
if (currentTime - lastAltitudeTime > 200) { // Update climb rate every 200ms
762762
float climbRate = 0.0f;
763763
if (lastAltitudeTime > 0) { // Skip first calculation
764764
float altitudeChange = altitude - lastAltitude;

src/sp140/sp140.ino

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -874,18 +874,6 @@ void buttonHandlerTask(void* parameter) {
874874
buttonPressStartTime = currentTime;
875875
}
876876
}
877-
878-
// Debug current state
879-
if (buttonPressed) {
880-
uint32_t currentHoldTime = currentTime - buttonPressStartTime;
881-
if (currentHoldTime % 500 == 0) { // Print every 500ms
882-
USBSerial.print("Current hold time: ");
883-
USBSerial.print(currentHoldTime);
884-
USBSerial.println("ms");
885-
USBSerial.print("Arm sequence: ");
886-
USBSerial.println(armSequenceStarted ? "ACTIVE" : "INACTIVE");
887-
}
888-
}
889877
}
890878

891879
vTaskDelay(pdMS_TO_TICKS(10));

test/test_throttle/test_throttle.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ TEST(ThrottleTest, LimitedThrottleAccelerationLimiting) {
2828
// Accelerating too fast - should limit to last + threshold
2929
EXPECT_EQ(limitedThrottle(1600, 1500, 50), 1550); // +100 -> +50
3030
EXPECT_EQ(limitedThrottle(1580, 1500, 50), 1550); // +80 -> +50
31-
EXPECT_EQ(limitedThrottle(1700, 1500, 50), 1550); // +200 -> +50
3231

3332
// Edge case: exactly at threshold (should still limit)
3433
EXPECT_EQ(limitedThrottle(1550, 1500, 50), 1550); // +50 -> +50
@@ -142,7 +141,7 @@ TEST(ThrottleTest, ApplyModeRampClamp) {
142141
prevPwm = 1035;
143142
int result = applyModeRampClamp(1500, prevPwm, 0);
144143
EXPECT_EQ(prevPwm, result); // prevPwm should be updated
145-
EXPECT_EQ(result, 1045); // Should ramp by CHILL_MODE_RAMP_RATE (10)
144+
EXPECT_EQ(result, 1043); // Should ramp by CHILL_MODE_RAMP_RATE (8)
146145

147146
// Test SPORT mode (mode 1) - faster ramp, higher max
148147
prevPwm = 1035;
@@ -152,7 +151,7 @@ TEST(ThrottleTest, ApplyModeRampClamp) {
152151
// Test CHILL mode max PWM clamping
153152
prevPwm = 1840;
154153
result = applyModeRampClamp(1900, prevPwm, 0);
155-
EXPECT_EQ(result, 1850); // Should clamp to CHILL_MODE_MAX_PWM
154+
EXPECT_EQ(result, 1600); // Should clamp to CHILL_MODE_MAX_PWM
156155

157156
// Test SPORT mode allows higher PWM
158157
prevPwm = 1840;

0 commit comments

Comments
 (0)