Skip to content

Commit fcdfca3

Browse files
committed
Add throttle smoothing tests
Introduces test_throttle.cpp with comprehensive unit tests for the limitedThrottle function, covering acceleration, deceleration, edge cases, and sequential calls.
1 parent 6de3b5b commit fcdfca3

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

src/sp140/esc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ void initESC() {
4848
* Set the ESC throttle value
4949
* @param throttlePWM Throttle value in microseconds (1000-2000)
5050
* 1000 = minimum throttle, 2000 = maximum throttle
51-
* Note: The ESC requires messages at least every 300ms or it will reset
51+
*
52+
* Important: The ESC requires messages at least every 300ms or it will reset
5253
*/
5354
void setESCThrottle(int throttlePWM) {
5455
// Input validation

test/test_simplemonitor/test_simplemonitor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,7 @@ TEST(SimpleMonitor, ESCTemperatureAlerts) {
355355

356356
int main(int argc, char **argv) {
357357
::testing::InitGoogleTest(&argc, argv);
358-
if (RUN_ALL_TESTS())
359-
;
358+
RUN_ALL_TESTS();
360359
// Always return zero-code and allow PlatformIO to parse results
361360
return 0;
362361
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <gtest/gtest.h>
2+
3+
// Create minimal stubs for ESP32 dependencies to avoid linking issues
4+
#define ARDUINO_H // Prevent Arduino.h from being included
5+
6+
// Include the actual source file
7+
#include "../../src/sp140/throttle.cpp"
8+
9+
TEST(ThrottleTest, LimitedThrottleNoLimiting) {
10+
// Test cases where no limiting should occur
11+
12+
// No change - should return current value
13+
EXPECT_EQ(limitedThrottle(1500, 1500, 50), 1500);
14+
15+
// Small acceleration within threshold
16+
EXPECT_EQ(limitedThrottle(1540, 1500, 50), 1540); // +40, threshold 50
17+
EXPECT_EQ(limitedThrottle(1549, 1500, 50), 1549); // +49, threshold 50
18+
19+
// Small deceleration within threshold (considering DECEL_MULTIPLIER = 2.0)
20+
EXPECT_EQ(limitedThrottle(1400, 1500, 50), 1400); // -100, threshold*2 = 100
21+
EXPECT_EQ(limitedThrottle(1401, 1500, 50), 1401); // -99, threshold*2 = 100
22+
}
23+
24+
TEST(ThrottleTest, LimitedThrottleAccelerationLimiting) {
25+
// Test acceleration limiting (current > last + threshold)
26+
27+
// Accelerating too fast - should limit to last + threshold
28+
EXPECT_EQ(limitedThrottle(1600, 1500, 50), 1550); // +100 -> +50
29+
EXPECT_EQ(limitedThrottle(1580, 1500, 50), 1550); // +80 -> +50
30+
EXPECT_EQ(limitedThrottle(1700, 1500, 50), 1550); // +200 -> +50
31+
32+
// Edge case: exactly at threshold (should still limit)
33+
EXPECT_EQ(limitedThrottle(1550, 1500, 50), 1550); // +50 -> +50
34+
35+
// Different threshold values
36+
EXPECT_EQ(limitedThrottle(1650, 1500, 100), 1600); // +150 -> +100
37+
EXPECT_EQ(limitedThrottle(1520, 1500, 10), 1510); // +20 -> +10
38+
}
39+
40+
TEST(ThrottleTest, LimitedThrottleDecelerationLimiting) {
41+
// Test deceleration limiting (last - current >= threshold * DECEL_MULTIPLIER)
42+
// DECEL_MULTIPLIER = 2.0
43+
44+
// Decelerating too fast - should limit to last - (threshold * 2)
45+
EXPECT_EQ(limitedThrottle(1300, 1500, 50), 1400); // -200 -> -100 (50*2)
46+
EXPECT_EQ(limitedThrottle(1350, 1500, 50), 1400); // -150 -> -100 (50*2)
47+
EXPECT_EQ(limitedThrottle(1200, 1500, 50), 1400); // -300 -> -100 (50*2)
48+
49+
// Edge case: exactly at decel threshold (should still limit)
50+
EXPECT_EQ(limitedThrottle(1400, 1500, 50), 1400); // -100 -> -100 (50*2)
51+
52+
// Different threshold values
53+
EXPECT_EQ(limitedThrottle(1300, 1500, 75), 1350); // -200 -> -150 (75*2)
54+
EXPECT_EQ(limitedThrottle(1480, 1500, 10), 1480); // -20 -> -20 (10*2=20, so -20 is OK)
55+
EXPECT_EQ(limitedThrottle(1470, 1500, 10), 1480); // -30 -> -20 (10*2)
56+
}
57+
58+
TEST(ThrottleTest, LimitedThrottleRealWorldScenarios) {
59+
// Test realistic PWM values (ESC_MIN_PWM = 1035, ESC_MAX_PWM = 1950)
60+
61+
// Gradual acceleration from idle
62+
EXPECT_EQ(limitedThrottle(1100, 1035, 30), 1065); // +65 -> +30
63+
EXPECT_EQ(limitedThrottle(1065, 1035, 30), 1065); // +30 -> +30 (no limit)
64+
65+
// Emergency deceleration from high throttle
66+
EXPECT_EQ(limitedThrottle(1035, 1500, 100), 1300); // -465 -> -200 (100*2)
67+
EXPECT_EQ(limitedThrottle(1035, 1800, 50), 1700); // -765 -> -100 (50*2)
68+
69+
// Performance mode differences (different thresholds)
70+
// Chill mode (lower threshold)
71+
EXPECT_EQ(limitedThrottle(1600, 1500, 50), 1550); // +100 -> +50
72+
// Sport mode (higher threshold)
73+
EXPECT_EQ(limitedThrottle(1650, 1500, 120), 1620); // +150 -> +120
74+
}
75+
76+
TEST(ThrottleTest, LimitedThrottleEdgeCases) {
77+
// Test edge cases and boundary conditions
78+
79+
// Zero values
80+
EXPECT_EQ(limitedThrottle(0, 0, 10), 0);
81+
EXPECT_EQ(limitedThrottle(20, 0, 10), 10); // +20 -> +10
82+
EXPECT_EQ(limitedThrottle(0, 50, 10), 30); // -50 -> -20 (10*2)
83+
84+
// Large values
85+
EXPECT_EQ(limitedThrottle(2000, 1000, 100), 1100); // +1000 -> +100
86+
EXPECT_EQ(limitedThrottle(500, 1000, 100), 800); // -500 -> -200 (100*2)
87+
88+
// Negative values (shouldn't occur in practice but test robustness)
89+
EXPECT_EQ(limitedThrottle(-10, 0, 5), -10); // -10 -> -10 (exactly at limit 5*2=10)
90+
EXPECT_EQ(limitedThrottle(-20, 0, 5), -10); // -20 -> -10 (5*2)
91+
92+
// Very small threshold
93+
EXPECT_EQ(limitedThrottle(1002, 1000, 1), 1001); // +2 -> +1
94+
EXPECT_EQ(limitedThrottle(997, 1000, 1), 998); // -3 -> -2 (1*2)
95+
}
96+
97+
TEST(ThrottleTest, LimitedThrottleSequentialCalls) {
98+
// Test multiple sequential calls to simulate real usage
99+
100+
int current = 1035; // Start at ESC_MIN_PWM
101+
int threshold = 50;
102+
103+
// Gradual acceleration
104+
current = limitedThrottle(1200, current, threshold);
105+
EXPECT_EQ(current, 1085); // 1035 + 50
106+
107+
current = limitedThrottle(1200, current, threshold);
108+
EXPECT_EQ(current, 1135); // 1085 + 50
109+
110+
current = limitedThrottle(1200, current, threshold);
111+
EXPECT_EQ(current, 1185); // 1135 + 50
112+
113+
current = limitedThrottle(1200, current, threshold);
114+
EXPECT_EQ(current, 1200); // 1185 + 15 (finally reaches target)
115+
116+
// Emergency stop
117+
current = limitedThrottle(1035, current, threshold);
118+
EXPECT_EQ(current, 1100); // 1200 - 100 (50*2)
119+
120+
current = limitedThrottle(1035, current, threshold);
121+
EXPECT_EQ(current, 1035); // 1100 - 65 (reaches target)
122+
}
123+
124+
int main(int argc, char **argv) {
125+
::testing::InitGoogleTest(&argc, argv);
126+
RUN_ALL_TESTS();
127+
// Always return zero-code and allow PlatformIO to parse results
128+
return 0;
129+
}

0 commit comments

Comments
 (0)