Position hold depends on GPS (#14101)
[betaflight.git] / src / test / unit / rx_ranges_unittest.cc
blob34cdf94a0cdf255f487c311a70aa545119a2da65
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdint.h>
19 #include <stdbool.h>
21 #include <limits.h>
23 extern "C" {
24 #include "platform.h"
25 #include "build/debug.h"
26 #include "drivers/io.h"
27 #include "common/maths.h"
28 #include "flight/failsafe.h"
29 #include "pg/pg.h"
30 #include "pg/pg_ids.h"
31 #include "pg/rx.h"
32 #include "fc/rc_controls.h"
33 #include "fc/rc_modes.h"
34 #include "fc/runtime_config.h"
35 #include "rx/rx.h"
38 #include "unittest_macros.h"
39 #include "gtest/gtest.h"
41 extern "C" {
43 PG_REGISTER(flight3DConfig_t, flight3DConfig, PG_MOTOR_3D_CONFIG, 0);
44 PG_REGISTER(failsafeConfig_t, failsafeConfig, PG_FAILSAFE_CONFIG, 0);
46 extern boxBitmask_t rcModeActivationMask;
47 int16_t debug[DEBUG16_VALUE_COUNT];
48 uint8_t debugMode = 0;
49 uint8_t armingFlags = 0;
51 extern float applyRxChannelRangeConfiguraton(float sample, const rxChannelRangeConfig_t *range);
54 #define RANGE_CONFIGURATION(min, max) new (rxChannelRangeConfig_t) {min, max}
56 TEST(RxChannelRangeTest, TestRxChannelRanges)
58 memset(&rcModeActivationMask, 0, sizeof(rcModeActivationMask)); // BOXFAILSAFE must be OFF
60 // No signal, special condition
61 EXPECT_EQ(0, applyRxChannelRangeConfiguraton(0, RANGE_CONFIGURATION(1000, 2000)));
62 EXPECT_EQ(0, applyRxChannelRangeConfiguraton(0, RANGE_CONFIGURATION(1300, 1700)));
63 EXPECT_EQ(0, applyRxChannelRangeConfiguraton(0, RANGE_CONFIGURATION(900, 2100)));
65 // Exact mapping
66 EXPECT_EQ(1000, applyRxChannelRangeConfiguraton(1000, RANGE_CONFIGURATION(1000, 2000)));
67 EXPECT_EQ(1500, applyRxChannelRangeConfiguraton(1500, RANGE_CONFIGURATION(1000, 2000)));
68 EXPECT_EQ(2000, applyRxChannelRangeConfiguraton(2000, RANGE_CONFIGURATION(1000, 2000)));
69 EXPECT_EQ(700, applyRxChannelRangeConfiguraton(700, RANGE_CONFIGURATION(1000, 2000)));
70 EXPECT_EQ(2500, applyRxChannelRangeConfiguraton(2500, RANGE_CONFIGURATION(1000, 2000)));
72 // Reversed channel
73 EXPECT_EQ(2000, applyRxChannelRangeConfiguraton(1000, RANGE_CONFIGURATION(2000, 1000)));
74 EXPECT_EQ(1500, applyRxChannelRangeConfiguraton(1500, RANGE_CONFIGURATION(2000, 1000)));
75 EXPECT_EQ(1000, applyRxChannelRangeConfiguraton(2000, RANGE_CONFIGURATION(2000, 1000)));
77 // Shifted range
78 EXPECT_EQ(1000, applyRxChannelRangeConfiguraton(900, RANGE_CONFIGURATION(900, 1900)));
79 EXPECT_EQ(1500, applyRxChannelRangeConfiguraton(1400, RANGE_CONFIGURATION(900, 1900)));
80 EXPECT_EQ(2000, applyRxChannelRangeConfiguraton(1900, RANGE_CONFIGURATION(900, 1900)));
81 EXPECT_EQ(700, applyRxChannelRangeConfiguraton(600, RANGE_CONFIGURATION(900, 1900)));
82 EXPECT_EQ(2600, applyRxChannelRangeConfiguraton(2500, RANGE_CONFIGURATION(900, 1900)));
84 // Narrower range than expected
85 EXPECT_EQ(1000, applyRxChannelRangeConfiguraton(1300, RANGE_CONFIGURATION(1300, 1700)));
86 EXPECT_EQ(1500, applyRxChannelRangeConfiguraton(1500, RANGE_CONFIGURATION(1300, 1700)));
87 EXPECT_EQ(2000, applyRxChannelRangeConfiguraton(1700, RANGE_CONFIGURATION(1300, 1700)));
88 EXPECT_EQ(-500, applyRxChannelRangeConfiguraton(700, RANGE_CONFIGURATION(1300, 1700)));
89 EXPECT_EQ(4000, applyRxChannelRangeConfiguraton(2500, RANGE_CONFIGURATION(1300, 1700)));
91 // Wider range than expected
92 EXPECT_EQ(1000, applyRxChannelRangeConfiguraton(900, RANGE_CONFIGURATION(900, 2100)));
93 EXPECT_EQ(1500, applyRxChannelRangeConfiguraton(1500, RANGE_CONFIGURATION(900, 2100)));
94 EXPECT_EQ(2000, applyRxChannelRangeConfiguraton(2100, RANGE_CONFIGURATION(900, 2100)));
95 EXPECT_EQ(750, applyRxChannelRangeConfiguraton(600, RANGE_CONFIGURATION(900, 2100)));
96 EXPECT_EQ(2500, applyRxChannelRangeConfiguraton(2700, RANGE_CONFIGURATION(900, 2100)));
98 // extreme out of range
99 EXPECT_EQ(1, applyRxChannelRangeConfiguraton(1, RANGE_CONFIGURATION(1000, 2000)));
100 EXPECT_EQ(-2245, applyRxChannelRangeConfiguraton(2, RANGE_CONFIGURATION(1300, 1700)));
101 EXPECT_EQ(252.5, applyRxChannelRangeConfiguraton(3, RANGE_CONFIGURATION(900, 2100)));
103 EXPECT_EQ(10000, applyRxChannelRangeConfiguraton(10000, RANGE_CONFIGURATION(1000, 2000)));
104 EXPECT_EQ(22750, applyRxChannelRangeConfiguraton(10000, RANGE_CONFIGURATION(1300, 1700)));
105 EXPECT_EQ(25250, applyRxChannelRangeConfiguraton(30000, RANGE_CONFIGURATION(900, 2100)));
109 // stubs
110 extern "C" {
112 void failsafeOnRxSuspend(uint32_t ) {}
113 void failsafeOnRxResume(void) {}
114 bool failsafeIsActive(void) { return false; }
115 bool failsafeIsReceivingRxData(void) { return true; }
116 bool taskUpdateRxMainInProgress(void) { return true; }
117 void setArmingDisabled(armingDisableFlags_e flag) { UNUSED(flag); }
118 void unsetArmingDisabled(armingDisableFlags_e flag) { UNUSED(flag); }
119 uint16_t flightModeFlags = 0;
121 uint32_t micros(void) { return 0; }
122 uint32_t millis(void) { return 0; }
124 void rxPwmInit(rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
126 UNUSED(rxRuntimeState);
127 UNUSED(callback);
130 bool sbusInit(rxConfig_t *initialRxConfig, rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
132 UNUSED(initialRxConfig);
133 UNUSED(rxRuntimeState);
134 UNUSED(callback);
135 return true;
138 bool spektrumInit(rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
140 UNUSED(rxConfig);
141 UNUSED(rxRuntimeState);
142 UNUSED(callback);
143 return true;
146 bool sumdInit(rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
148 UNUSED(rxConfig);
149 UNUSED(rxRuntimeState);
150 UNUSED(callback);
151 return true;
154 bool sumhInit(rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
156 UNUSED(rxConfig);
157 UNUSED(rxRuntimeState);
158 UNUSED(callback);
159 return true;
162 bool crsfRxInit(rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
164 UNUSED(rxConfig);
165 UNUSED(rxRuntimeState);
166 UNUSED(callback);
167 return true;
170 bool jetiExBusInit(rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
172 UNUSED(rxConfig);
173 UNUSED(rxRuntimeState);
174 UNUSED(callback);
175 return true;
178 bool ibusInit(rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
180 UNUSED(rxConfig);
181 UNUSED(rxRuntimeState);
182 UNUSED(callback);
183 return true;
186 bool xBusInit(rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
188 UNUSED(rxConfig);
189 UNUSED(rxRuntimeState);
190 UNUSED(callback);
191 return true;
194 bool rxMspInit(rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState, rcReadRawDataFnPtr *callback)
196 UNUSED(rxConfig);
197 UNUSED(rxRuntimeState);
198 UNUSED(callback);
199 return true;
202 bool featureIsEnabled(uint32_t)
204 return false;
207 void featureDisableImmediate(uint32_t)
211 bool rxMspFrameComplete(void)
213 return false;
216 bool isPPMDataBeingReceived(void)
218 return false;
221 bool isPWMDataBeingReceived(void)
223 return false;
226 void resetPPMDataReceivedState(void)
230 void failsafeOnValidDataReceived(void)
234 void failsafeOnValidDataFailed(void)
238 uint32_t failsafeFailurePeriodMs(void)
240 return 400;
243 float pt1FilterGain(float f_cut, float dT)
245 UNUSED(f_cut);
246 UNUSED(dT);
247 return 0.0;
250 void pt1FilterInit(pt1Filter_t *filter, float k)
252 UNUSED(filter);
253 UNUSED(k);
256 void pt1FilterUpdateCutoff(pt1Filter_t *filter, float k)
258 UNUSED(filter);
259 UNUSED(k);
262 float pt1FilterApply(pt1Filter_t *filter, float input)
264 UNUSED(filter);
265 UNUSED(input);
266 return 0.0;
269 void pinioBoxTaskControl(void) {}