3 * Cleanflight is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * Cleanflight is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
23 #include "build/debug.h"
25 #include "blackbox/blackbox.h"
26 #include "common/utils.h"
29 #include "pg/pg_ids.h"
33 #include "drivers/accgyro/accgyro.h"
34 #include "drivers/accgyro/gyro_sync.h"
35 #include "drivers/serial.h"
37 #include "flight/failsafe.h"
38 #include "flight/mixer.h"
39 #include "flight/pid.h"
41 #include "fc/rc_controls.h"
42 #include "fc/rc_modes.h"
45 #include "io/serial.h"
49 #include "sensors/battery.h"
50 #include "sensors/gyro.h"
52 extern int16_t blackboxIInterval
;
53 extern int16_t blackboxPInterval
;
56 #include "unittest_macros.h"
57 #include "gtest/gtest.h"
61 TEST(BlackboxTest
, TestInitIntervals
)
63 blackboxConfigMutable()->sample_rate
= 4; // sample_rate = PID loop frequency / 16
65 targetPidLooptime
= 4000;
67 EXPECT_EQ(8, blackboxIInterval
);
68 EXPECT_EQ(0, blackboxPInterval
);
69 EXPECT_EQ(2048, blackboxSInterval
);
72 targetPidLooptime
= 2000;
74 EXPECT_EQ(16, blackboxIInterval
);
75 EXPECT_EQ(16, blackboxPInterval
);
76 EXPECT_EQ(4096, blackboxSInterval
);
78 blackboxConfigMutable()->sample_rate
= 1; // sample_rate = PID loop frequency / 2
80 targetPidLooptime
= 500;
82 EXPECT_EQ(64, blackboxIInterval
);
83 EXPECT_EQ(2, blackboxPInterval
);
84 EXPECT_EQ(16384, blackboxSInterval
);
86 blackboxConfigMutable()->sample_rate
= 2;
88 targetPidLooptime
= 250;
90 EXPECT_EQ(128, blackboxIInterval
);
91 EXPECT_EQ(4, blackboxPInterval
);
92 EXPECT_EQ(32768, blackboxSInterval
);
94 blackboxConfigMutable()->sample_rate
= 3;
96 targetPidLooptime
= 125;
98 EXPECT_EQ(256, blackboxIInterval
);
99 EXPECT_EQ(8, blackboxPInterval
);
100 EXPECT_EQ(65536, blackboxSInterval
);
103 TEST(BlackboxTest
, Test_500Hz
)
105 blackboxConfigMutable()->sample_rate
= 0;
107 targetPidLooptime
= 2000;
109 EXPECT_TRUE(blackboxShouldLogIFrame());
110 EXPECT_TRUE(blackboxShouldLogPFrame());
111 for (int ii
= 0; ii
< 15; ++ii
) {
112 blackboxAdvanceIterationTimers();
113 EXPECT_FALSE(blackboxShouldLogIFrame());
114 EXPECT_TRUE(blackboxShouldLogPFrame());
116 blackboxAdvanceIterationTimers();
117 EXPECT_TRUE(blackboxShouldLogIFrame());
118 EXPECT_TRUE(blackboxShouldLogPFrame());
121 TEST(BlackboxTest
, Test_1kHz
)
123 blackboxConfigMutable()->sample_rate
= 0;
125 targetPidLooptime
= 1000;
127 EXPECT_TRUE(blackboxShouldLogIFrame());
128 EXPECT_TRUE(blackboxShouldLogPFrame());
129 EXPECT_EQ(0, blackboxSlowFrameIterationTimer
);
130 EXPECT_FALSE(blackboxSlowFrameIterationTimer
>= blackboxSInterval
);
131 blackboxSlowFrameIterationTimer
= blackboxSInterval
;
132 EXPECT_TRUE(writeSlowFrameIfNeeded());
133 EXPECT_EQ(0, blackboxSlowFrameIterationTimer
);
135 for (int ii
= 0; ii
< 31; ++ii
) {
136 blackboxAdvanceIterationTimers();
137 EXPECT_FALSE(blackboxShouldLogIFrame());
138 EXPECT_TRUE(blackboxShouldLogPFrame());
139 EXPECT_EQ(ii
+ 1, blackboxSlowFrameIterationTimer
);
140 EXPECT_FALSE(writeSlowFrameIfNeeded());
142 blackboxAdvanceIterationTimers();
143 EXPECT_TRUE(blackboxShouldLogIFrame());
144 EXPECT_TRUE(blackboxShouldLogPFrame());
147 TEST(BlackboxTest
, Test_2kHz
)
149 blackboxConfigMutable()->sample_rate
= 1;
151 targetPidLooptime
= 500;
153 EXPECT_EQ(64, blackboxIInterval
);
154 EXPECT_EQ(2, blackboxPInterval
);
155 EXPECT_TRUE(blackboxShouldLogIFrame());
156 EXPECT_TRUE(blackboxShouldLogPFrame());
157 blackboxAdvanceIterationTimers();
158 EXPECT_FALSE(blackboxShouldLogIFrame());
159 EXPECT_FALSE(blackboxShouldLogPFrame());
161 for (int ii
= 0; ii
< 31; ++ii
) {
162 blackboxAdvanceIterationTimers();
163 EXPECT_FALSE(blackboxShouldLogIFrame());
164 EXPECT_TRUE(blackboxShouldLogPFrame());
166 blackboxAdvanceIterationTimers();
167 EXPECT_FALSE(blackboxShouldLogIFrame());
168 EXPECT_FALSE(blackboxShouldLogPFrame());
171 blackboxAdvanceIterationTimers();
172 EXPECT_TRUE(blackboxShouldLogIFrame());
173 EXPECT_TRUE(blackboxShouldLogPFrame());
175 blackboxAdvanceIterationTimers();
176 EXPECT_FALSE(blackboxShouldLogIFrame());
177 EXPECT_FALSE(blackboxShouldLogPFrame());
180 TEST(BlackboxTest
, Test_8kHz
)
182 blackboxConfigMutable()->sample_rate
= 3;
184 targetPidLooptime
= 125;
186 EXPECT_TRUE(blackboxShouldLogIFrame());
187 EXPECT_TRUE(blackboxShouldLogPFrame());
188 EXPECT_EQ(0, blackboxSlowFrameIterationTimer
);
189 EXPECT_FALSE(blackboxSlowFrameIterationTimer
>= blackboxSInterval
);
190 blackboxSlowFrameIterationTimer
= blackboxSInterval
;
191 EXPECT_TRUE(writeSlowFrameIfNeeded());
192 EXPECT_EQ(0, blackboxSlowFrameIterationTimer
);
194 for (int ii
= 0; ii
< 255; ++ii
) {
195 blackboxAdvanceIterationTimers();
196 EXPECT_FALSE(blackboxShouldLogIFrame());
197 EXPECT_EQ(ii
+ 1, blackboxSlowFrameIterationTimer
);
198 EXPECT_FALSE(writeSlowFrameIfNeeded());
200 blackboxAdvanceIterationTimers();
201 EXPECT_TRUE(blackboxShouldLogIFrame());
202 EXPECT_TRUE(blackboxShouldLogPFrame());
205 TEST(BlackboxTest
, Test_zero_p_interval
)
207 blackboxConfigMutable()->sample_rate
= 4;
209 targetPidLooptime
= 4000;
211 EXPECT_EQ(8, blackboxIInterval
);
212 EXPECT_EQ(0, blackboxPInterval
);
213 EXPECT_TRUE(blackboxShouldLogIFrame());
214 EXPECT_FALSE(blackboxShouldLogPFrame());
216 for (int ii
= 0; ii
< 7; ++ii
) {
217 blackboxAdvanceIterationTimers();
218 EXPECT_FALSE(blackboxShouldLogIFrame());
219 EXPECT_FALSE(blackboxShouldLogPFrame());
221 blackboxAdvanceIterationTimers();
222 EXPECT_TRUE(blackboxShouldLogIFrame());
223 EXPECT_FALSE(blackboxShouldLogPFrame());
226 TEST(BlackboxTest
, Test_CalculatePDenom
)
228 blackboxConfigMutable()->sample_rate
= 0;
229 // note I-frame is logged every 32ms regardless of PIDloop rate
232 targetPidLooptime
= 1000;
234 EXPECT_EQ(32, blackboxIInterval
);
235 EXPECT_EQ(32, blackboxCalculatePDenom(1, 1)); // 1kHz logging
236 EXPECT_EQ(16, blackboxCalculatePDenom(1, 2));
237 EXPECT_EQ(8, blackboxCalculatePDenom(1, 4));
240 targetPidLooptime
= 500;
242 EXPECT_EQ(64, blackboxIInterval
);
243 EXPECT_EQ(64, blackboxCalculatePDenom(1, 1));
244 EXPECT_EQ(32, blackboxCalculatePDenom(1, 2)); // 1kHz logging
245 EXPECT_EQ(16, blackboxCalculatePDenom(1, 4));
248 targetPidLooptime
= 250;
250 EXPECT_EQ(128, blackboxIInterval
);
251 EXPECT_EQ(128, blackboxCalculatePDenom(1, 1));
252 EXPECT_EQ(64, blackboxCalculatePDenom(1, 2));
253 EXPECT_EQ(32, blackboxCalculatePDenom(1, 4)); // 1kHz logging
254 EXPECT_EQ(16, blackboxCalculatePDenom(1, 8));
257 targetPidLooptime
= 125;
259 EXPECT_EQ(256, blackboxIInterval
);
260 EXPECT_EQ(256, blackboxCalculatePDenom(1, 1));
261 EXPECT_EQ(128, blackboxCalculatePDenom(1, 2));
262 EXPECT_EQ(64, blackboxCalculatePDenom(1, 4));
263 EXPECT_EQ(32, blackboxCalculatePDenom(1, 8)); // 1kHz logging
264 EXPECT_EQ(16, blackboxCalculatePDenom(1, 16));
267 TEST(BlackboxTest
, Test_CalculateRates
)
270 targetPidLooptime
= 1000;
271 blackboxConfigMutable()->sample_rate
= 0;
273 EXPECT_EQ(32, blackboxIInterval
);
274 EXPECT_EQ(1, blackboxPInterval
);
275 EXPECT_EQ(1, blackboxGetRateDenom());
277 blackboxConfigMutable()->sample_rate
= 1;
279 EXPECT_EQ(32, blackboxIInterval
);
280 EXPECT_EQ(2, blackboxPInterval
);
281 EXPECT_EQ(2, blackboxGetRateDenom());
283 blackboxConfigMutable()->sample_rate
= 2;
285 EXPECT_EQ(32, blackboxIInterval
);
286 EXPECT_EQ(4, blackboxPInterval
);
287 EXPECT_EQ(4, blackboxGetRateDenom());
291 targetPidLooptime
= 125;
292 blackboxConfigMutable()->sample_rate
= 3; // 1kHz logging
294 EXPECT_EQ(256, blackboxIInterval
);
295 EXPECT_EQ(8, blackboxPInterval
);
296 EXPECT_EQ(8, blackboxGetRateDenom());
298 blackboxConfigMutable()->sample_rate
= 4; // 500Hz logging
300 EXPECT_EQ(256, blackboxIInterval
);
301 EXPECT_EQ(16, blackboxPInterval
);
302 EXPECT_EQ(16, blackboxGetRateDenom());
304 blackboxConfigMutable()->sample_rate
= 2; // 2kHz logging
306 EXPECT_EQ(256, blackboxIInterval
);
307 EXPECT_EQ(4, blackboxPInterval
);
308 EXPECT_EQ(4, blackboxGetRateDenom());
310 blackboxConfigMutable()->sample_rate
= 1; // 4kHz logging
312 EXPECT_EQ(256, blackboxIInterval
);
313 EXPECT_EQ(2, blackboxPInterval
);
314 EXPECT_EQ(2, blackboxGetRateDenom());
316 blackboxConfigMutable()->sample_rate
= 0; // 8kHz logging
318 EXPECT_EQ(256, blackboxIInterval
);
319 EXPECT_EQ(1, blackboxPInterval
);
320 EXPECT_EQ(1, blackboxGetRateDenom());
322 // 0.126 PIDloop = 7.94kHz
323 targetPidLooptime
= 126;
324 blackboxConfigMutable()->sample_rate
= 3; // 0.992kHz logging
326 EXPECT_EQ(253, blackboxIInterval
);
327 EXPECT_EQ(8, blackboxPInterval
);
328 EXPECT_EQ(8, blackboxGetRateDenom());
336 PG_REGISTER(flight3DConfig_t
, flight3DConfig
, PG_MOTOR_3D_CONFIG
, 0);
337 PG_REGISTER(mixerConfig_t
, mixerConfig
, PG_MIXER_CONFIG
, 0);
338 PG_REGISTER(motorConfig_t
, motorConfig
, PG_MOTOR_CONFIG
, 0);
339 PG_REGISTER(batteryConfig_t
, batteryConfig
, PG_BATTERY_CONFIG
, 0);
340 PG_REGISTER(rxConfig_t
, rxConfig
, PG_RX_CONFIG
, 0);
341 PG_REGISTER_ARRAY(modeActivationCondition_t
, MAX_MODE_ACTIVATION_CONDITION_COUNT
, modeActivationConditions
, PG_MODE_ACTIVATION_PROFILE
, 0);
345 const uint32_t baudRates
[] = {0, 9600, 19200, 38400, 57600, 115200, 230400, 250000,
346 400000, 460800, 500000, 921600, 1000000, 1500000, 2000000, 2470000}; // see baudRate_e
347 uint8_t debugMode
= 0;
348 int16_t debug
[DEBUG16_VALUE_COUNT
];
349 int32_t blackboxHeaderBudget
;
350 gpsSolutionData_t gpsSol
;
355 float motor_disarmed
[MAX_SUPPORTED_MOTORS
];
357 struct pidProfile_s
*currentPidProfile
;
358 uint32_t targetPidLooptime
;
360 boxBitmask_t rcModeActivationMask
;
362 void mspSerialAllocatePorts(void) {}
363 uint32_t getArmingBeepTimeMicros(void) {return 0;}
364 uint16_t getBatteryVoltageLatest(void) {return 0;}
365 uint8_t getMotorCount(void) {return 4;}
366 bool areMotorsRunning(void) { return false; }
367 bool IS_RC_MODE_ACTIVE(boxId_e
) {return false;}
368 bool isModeActivationConditionPresent(boxId_e
) {return false;}
369 uint32_t millis(void) {return 0;}
370 bool sensors(uint32_t) {return false;}
371 void serialWrite(serialPort_t
*, uint8_t) {}
372 uint32_t serialTxBytesFree(const serialPort_t
*) {return 0;}
373 bool isSerialTransmitBufferEmpty(const serialPort_t
*) {return false;}
374 bool featureIsEnabled(uint32_t) {return false;}
375 void mspSerialReleasePortIfAllocated(serialPort_t
*) {}
376 const serialPortConfig_t
*findSerialPortConfig(serialPortFunction_e
) {return NULL
;}
377 serialPort_t
*findSharedSerialPort(uint16_t , serialPortFunction_e
) {return NULL
;}
378 serialPort_t
*openSerialPort(serialPortIdentifier_e
, serialPortFunction_e
, serialReceiveCallbackPtr
, void *, uint32_t, portMode_e
, portOptions_e
) {return NULL
;}
379 void closeSerialPort(serialPort_t
*) {}
380 portSharing_e
determinePortSharing(const serialPortConfig_t
*, serialPortFunction_e
) {return PORTSHARING_UNUSED
;}
381 failsafePhase_e
failsafePhase(void) {return FAILSAFE_IDLE
;}
382 bool rxAreFlightChannelsValid(void) {return false;}
383 bool rxIsReceivingSignal(void) {return false;}
384 bool isRssiConfigured(void) {return false;}
385 float getMotorOutputLow(void) {return 0.0;}
386 float getMotorOutputHigh(void) {return 0.0;}