|
| 1 | +// Use the native Arduino stubs provided by the repo and the real CircularBuffer |
1 | 2 | #include <gtest/gtest.h> |
| 3 | +#include "../native_stubs/Arduino.h" |
2 | 4 |
|
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" |
7 | 8 | #include "../../src/sp140/throttle.cpp" |
8 | 9 |
|
9 | 10 | TEST(ThrottleTest, LimitedThrottleNoLimiting) { |
@@ -121,6 +122,83 @@ TEST(ThrottleTest, LimitedThrottleSequentialCalls) { |
121 | 122 | EXPECT_EQ(current, 1035); // 1100 - 65 (reaches target) |
122 | 123 | } |
123 | 124 |
|
| 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 | + |
124 | 202 | int main(int argc, char **argv) { |
125 | 203 | ::testing::InitGoogleTest(&argc, argv); |
126 | 204 | RUN_ALL_TESTS(); |
|
0 commit comments