Skip to content

Commit 06c0787

Browse files
committed
Add unit tests for throttle mapping and filtering
Introduces comprehensive unit tests for potRawToPwm mapping, applyModeRampClamp, throttle filter functions, and resetThrottleState. Updates test setup to use native Arduino stubs and the real CircularBuffer library. Also updates platformio.ini to include CircularBuffer as a dependency.
1 parent ddd6f75 commit 06c0787

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

platformio.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ build_type = debug
7474
build_flags =
7575
-I inc
7676
-I test/native_stubs
77+
-I .pio/libdeps/native-test/CircularBuffer
7778
-D ARDUINO=1
7879
build_src_filter = -<*>
7980
test_ignore = neopixel*
81+
lib_deps =
82+
rlogiacco/CircularBuffer@^1.4.0

test/test_throttle/test_throttle.cpp

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
// Use the native Arduino stubs provided by the repo and the real CircularBuffer
12
#include <gtest/gtest.h>
3+
#include "../native_stubs/Arduino.h"
24

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
5+
// Include the real implementations under test
6+
#include "../../inc/sp140/shared-config.h"
7+
#include "../../inc/sp140/throttle.h"
78
#include "../../src/sp140/throttle.cpp"
89

910
TEST(ThrottleTest, LimitedThrottleNoLimiting) {
@@ -121,6 +122,83 @@ TEST(ThrottleTest, LimitedThrottleSequentialCalls) {
121122
EXPECT_EQ(current, 1035); // 1100 - 65 (reaches target)
122123
}
123124

125+
// Test potRawToPwm mapping function
126+
TEST(ThrottleTest, PotRawToPwmMapping) {
127+
// Test basic mapping from ADC range (0-4095) to PWM range (1035-1950)
128+
EXPECT_EQ(potRawToPwm(0), 1035); // Min ADC -> Min PWM
129+
EXPECT_EQ(potRawToPwm(4095), 1950); // Max ADC -> Max PWM
130+
EXPECT_EQ(potRawToPwm(2047), 1492); // Mid ADC -> Mid PWM (approx)
131+
132+
// Test quarter points
133+
EXPECT_EQ(potRawToPwm(1024), 1263); // 25% -> 25% of range
134+
EXPECT_EQ(potRawToPwm(3072), 1721); // 75% -> 75% of range
135+
}
136+
137+
// Test applyModeRampClamp function
138+
TEST(ThrottleTest, ApplyModeRampClamp) {
139+
int prevPwm;
140+
141+
// Test CHILL mode (mode 0) - slower ramp, lower max
142+
prevPwm = 1035;
143+
int result = applyModeRampClamp(1500, prevPwm, 0);
144+
EXPECT_EQ(prevPwm, result); // prevPwm should be updated
145+
EXPECT_EQ(result, 1045); // Should ramp by CHILL_MODE_RAMP_RATE (10)
146+
147+
// Test SPORT mode (mode 1) - faster ramp, higher max
148+
prevPwm = 1035;
149+
result = applyModeRampClamp(1500, prevPwm, 1);
150+
EXPECT_EQ(result, 1062); // Should ramp by SPORT_MODE_RAMP_RATE (27)
151+
152+
// Test CHILL mode max PWM clamping
153+
prevPwm = 1840;
154+
result = applyModeRampClamp(1900, prevPwm, 0);
155+
EXPECT_EQ(result, 1850); // Should clamp to CHILL_MODE_MAX_PWM
156+
157+
// Test SPORT mode allows higher PWM
158+
prevPwm = 1840;
159+
result = applyModeRampClamp(1900, prevPwm, 1);
160+
EXPECT_EQ(result, 1867); // Should ramp by 27, not clamp yet
161+
}
162+
163+
// Test throttle filter functions
164+
TEST(ThrottleTest, ThrottleFiltering) {
165+
// Clear buffer first
166+
throttleFilterClear();
167+
168+
// Empty buffer should return 0
169+
EXPECT_EQ(throttleFilterAverage(), 0);
170+
171+
// Add some values and test averaging
172+
throttleFilterPush(1000);
173+
EXPECT_EQ(throttleFilterAverage(), 1000);
174+
175+
throttleFilterPush(1200);
176+
EXPECT_EQ(throttleFilterAverage(), 1100); // (1000+1200)/2
177+
178+
throttleFilterPush(1400);
179+
EXPECT_EQ(throttleFilterAverage(), 1200); // (1000+1200+1400)/3
180+
181+
// Test reset functionality
182+
throttleFilterReset(1500);
183+
EXPECT_EQ(throttleFilterAverage(), 1500); // Should be filled with 1500
184+
}
185+
186+
// Test resetThrottleState function
187+
TEST(ThrottleTest, ResetThrottleState) {
188+
int prevPwm = 1500;
189+
190+
// Add some values to buffer first
191+
throttleFilterPush(1200);
192+
throttleFilterPush(1300);
193+
throttleFilterPush(1400);
194+
195+
// Reset should clear buffer and reset prevPwm
196+
resetThrottleState(prevPwm);
197+
198+
EXPECT_EQ(prevPwm, 1035); // Should reset to ESC_MIN_PWM
199+
EXPECT_EQ(throttleFilterAverage(), 0); // Buffer should be empty
200+
}
201+
124202
int main(int argc, char **argv) {
125203
::testing::InitGoogleTest(&argc, argv);
126204
RUN_ALL_TESTS();

0 commit comments

Comments
 (0)