2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
29 #include "blackbox/blackbox.h"
31 #include "build/build_config.h"
33 #include "common/axis.h"
34 #include "common/maths.h"
36 #include "config/feature.h"
38 #include "pg/pg_ids.h"
43 #include "drivers/camera_control.h"
45 #include "fc/config.h"
46 #include "fc/fc_core.h"
47 #include "fc/rc_controls.h"
49 #include "fc/runtime_config.h"
52 #include "io/beeper.h"
53 #include "io/motors.h"
54 #include "io/vtx_control.h"
55 #include "io/dashboard.h"
57 #include "sensors/barometer.h"
58 #include "sensors/battery.h"
59 #include "sensors/sensors.h"
60 #include "sensors/gyro.h"
61 #include "sensors/acceleration.h"
64 #include "scheduler/scheduler.h"
66 #include "flight/pid.h"
67 #include "flight/failsafe.h"
69 static pidProfile_t
*pidProfile
;
71 // true if arming is done via the sticks (as opposed to a switch)
72 static bool isUsingSticksToArm
= true;
74 float rcCommand
[4]; // interval [1000;2000] for THROTTLE and [-500;+500] for ROLL/PITCH/YAW
76 PG_REGISTER_WITH_RESET_TEMPLATE(rcControlsConfig_t
, rcControlsConfig
, PG_RC_CONTROLS_CONFIG
, 0);
78 PG_RESET_TEMPLATE(rcControlsConfig_t
, rcControlsConfig
,
81 .alt_hold_deadband
= 40,
82 .alt_hold_fast_change
= 1,
83 .yaw_control_reversed
= false,
86 PG_REGISTER_WITH_RESET_TEMPLATE(armingConfig_t
, armingConfig
, PG_ARMING_CONFIG
, 1);
88 PG_RESET_TEMPLATE(armingConfig_t
, armingConfig
,
89 .gyro_cal_on_first_arm
= 0, // TODO - Cleanup retarded arm support
90 .auto_disarm_delay
= 5
93 PG_REGISTER_WITH_RESET_TEMPLATE(flight3DConfig_t
, flight3DConfig
, PG_MOTOR_3D_CONFIG
, 0);
94 PG_RESET_TEMPLATE(flight3DConfig_t
, flight3DConfig
,
95 .deadband3d_low
= 1406,
96 .deadband3d_high
= 1514,
98 .deadband3d_throttle
= 50,
100 .limit3d_high
= 2000,
101 .switched_mode3d
= false
104 bool isUsingSticksForArming(void)
106 return isUsingSticksToArm
;
109 bool areSticksInApModePosition(uint16_t ap_mode
)
111 return ABS(rcCommand
[ROLL
]) < ap_mode
&& ABS(rcCommand
[PITCH
]) < ap_mode
;
114 throttleStatus_e
calculateThrottleStatus(void)
116 if (feature(FEATURE_3D
)) {
117 if (IS_RC_MODE_ACTIVE(BOX3D
) || flight3DConfig()->switched_mode3d
) {
118 if (rcData
[THROTTLE
] < rxConfig()->mincheck
) {
121 } else if ((rcData
[THROTTLE
] > (rxConfig()->midrc
- flight3DConfig()->deadband3d_throttle
) && rcData
[THROTTLE
] < (rxConfig()->midrc
+ flight3DConfig()->deadband3d_throttle
))) {
124 } else if (rcData
[THROTTLE
] < rxConfig()->mincheck
) {
128 return THROTTLE_HIGH
;
131 #define ARM_DELAY_MS 500
132 #define STICK_DELAY_MS 50
133 #define STICK_AUTOREPEAT_MS 250
134 #define repeatAfter(t) { \
136 doNotRepeat = false; \
138 void processRcStickPositions()
140 // time the sticks are maintained
141 static int16_t rcDelayMs
;
142 // hold sticks position for command combos
143 static uint8_t rcSticks
;
144 // an extra guard for disarming through switch to prevent that one frame can disarm it
145 static uint8_t rcDisarmTicks
;
146 static bool doNotRepeat
;
154 // checking sticks positions
156 for (int i
= 0; i
< 4; i
++) {
158 if (rcData
[i
] > rxConfig()->mincheck
) {
159 stTmp
|= 0x80; // check for MIN
161 if (rcData
[i
] < rxConfig()->maxcheck
) {
162 stTmp
|= 0x40; // check for MAX
165 if (stTmp
== rcSticks
) {
166 if (rcDelayMs
<= INT16_MAX
- (getTaskDeltaTime(TASK_SELF
) / 1000)) {
167 rcDelayMs
+= getTaskDeltaTime(TASK_SELF
) / 1000;
176 if (!isUsingSticksToArm
) {
177 if (IS_RC_MODE_ACTIVE(BOXARM
)) {
179 // Arming via ARM BOX
183 // Disarming via ARM BOX
184 resetArmingDisabled();
185 if (ARMING_FLAG(ARMED
) && rxIsReceivingSignal() && !failsafeIsActive() ) {
187 if (rcDisarmTicks
> 3) {
192 } else if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_CE
+ ROL_CE
) {
193 if (rcDelayMs
>= ARM_DELAY_MS
&& !doNotRepeat
) {
195 // Disarm on throttle down + yaw
197 if (ARMING_FLAG(ARMED
))
200 beeper(BEEPER_DISARM_REPEAT
); // sound tone while stick held
201 repeatAfter(STICK_AUTOREPEAT_MS
); // disarm tone will repeat
203 #ifdef USE_RUNAWAY_TAKEOFF
204 // Unset the ARMING_DISABLED_RUNAWAY_TAKEOFF arming disabled flag that might have been set
205 // by a runaway pidSum detection auto-disarm.
206 // This forces the pilot to explicitly perform a disarm sequence (even though we're implicitly disarmed)
207 // before they're able to rearm
208 unsetArmingDisabled(ARMING_DISABLED_RUNAWAY_TAKEOFF
);
213 } else if (rcSticks
== THR_LO
+ YAW_HI
+ PIT_CE
+ ROL_CE
) {
214 if (rcDelayMs
>= ARM_DELAY_MS
&& !doNotRepeat
) {
216 if (!ARMING_FLAG(ARMED
)) {
219 if (isTryingToArm()) {
223 resetArmingDisabled();
231 if (ARMING_FLAG(ARMED
) || doNotRepeat
|| rcDelayMs
<= STICK_DELAY_MS
|| (getArmingDisableFlags() & ARMING_DISABLED_RUNAWAY_TAKEOFF
)) {
236 // actions during not armed
238 if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_LO
+ ROL_CE
) {
240 gyroStartCalibration(false);
243 if (feature(FEATURE_GPS
)) {
244 GPS_reset_home_position();
249 if (sensors(SENSOR_BARO
))
250 baroSetCalibrationCycles(10); // calibrate baro to new ground level (10 * 25 ms = ~250 ms non blocking)
256 if (feature(FEATURE_INFLIGHT_ACC_CAL
) && (rcSticks
== THR_LO
+ YAW_LO
+ PIT_HI
+ ROL_HI
)) {
257 // Inflight ACC Calibration
258 handleInflightCalibrationStickPosition();
262 // Change PID profile
264 case THR_LO
+ YAW_LO
+ PIT_CE
+ ROL_LO
:
265 // ROLL left -> PID profile 1
268 case THR_LO
+ YAW_LO
+ PIT_HI
+ ROL_CE
:
269 // PITCH up -> PID profile 2
272 case THR_LO
+ YAW_LO
+ PIT_CE
+ ROL_HI
:
273 // ROLL right -> PID profile 3
278 if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_LO
+ ROL_HI
) {
279 saveConfigAndNotify();
282 if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_LO
+ ROL_CE
) {
284 accSetCalibrationCycles(CALIBRATING_ACC_CYCLES
);
289 if (rcSticks
== THR_HI
+ YAW_HI
+ PIT_LO
+ ROL_CE
) {
291 ENABLE_STATE(CALIBRATE_MAG
);
296 if (FLIGHT_MODE(ANGLE_MODE
|HORIZON_MODE
)) {
297 // in ANGLE or HORIZON mode, so use sticks to apply accelerometer trims
298 rollAndPitchTrims_t accelerometerTrimsDelta
;
299 memset(&accelerometerTrimsDelta
, 0, sizeof(accelerometerTrimsDelta
));
301 bool shouldApplyRollAndPitchTrimDelta
= false;
303 case THR_HI
+ YAW_CE
+ PIT_HI
+ ROL_CE
:
304 accelerometerTrimsDelta
.values
.pitch
= 2;
305 shouldApplyRollAndPitchTrimDelta
= true;
307 case THR_HI
+ YAW_CE
+ PIT_LO
+ ROL_CE
:
308 accelerometerTrimsDelta
.values
.pitch
= -2;
309 shouldApplyRollAndPitchTrimDelta
= true;
311 case THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_HI
:
312 accelerometerTrimsDelta
.values
.roll
= 2;
313 shouldApplyRollAndPitchTrimDelta
= true;
315 case THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_LO
:
316 accelerometerTrimsDelta
.values
.roll
= -2;
317 shouldApplyRollAndPitchTrimDelta
= true;
320 if (shouldApplyRollAndPitchTrimDelta
) {
321 applyAndSaveAccelerometerTrimsDelta(&accelerometerTrimsDelta
);
322 repeatAfter(STICK_AUTOREPEAT_MS
);
326 // in ACRO mode, so use sticks to change RATE profile
328 case THR_HI
+ YAW_CE
+ PIT_HI
+ ROL_CE
:
329 changeControlRateProfile(0);
331 case THR_HI
+ YAW_CE
+ PIT_LO
+ ROL_CE
:
332 changeControlRateProfile(1);
334 case THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_HI
:
335 changeControlRateProfile(2);
337 case THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_LO
:
338 changeControlRateProfile(3);
344 if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_HI
+ ROL_LO
) {
345 dashboardDisablePageCycling();
348 if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_HI
+ ROL_HI
) {
349 dashboardEnablePageCycling();
353 #ifdef USE_VTX_CONTROL
354 if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_CE
+ ROL_HI
) {
357 if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_CE
+ ROL_LO
) {
360 if (rcSticks
== THR_HI
+ YAW_HI
+ PIT_CE
+ ROL_HI
) {
361 vtxIncrementChannel();
363 if (rcSticks
== THR_HI
+ YAW_HI
+ PIT_CE
+ ROL_LO
) {
364 vtxDecrementChannel();
368 #ifdef USE_CAMERA_CONTROL
369 if (rcSticks
== THR_CE
+ YAW_HI
+ PIT_CE
+ ROL_CE
) {
370 cameraControlKeyPress(CAMERA_CONTROL_KEY_ENTER
, 0);
371 repeatAfter(3 * STICK_DELAY_MS
);
372 } else if (rcSticks
== THR_CE
+ YAW_CE
+ PIT_CE
+ ROL_LO
) {
373 cameraControlKeyPress(CAMERA_CONTROL_KEY_LEFT
, 0);
374 repeatAfter(3 * STICK_DELAY_MS
);
375 } else if (rcSticks
== THR_CE
+ YAW_CE
+ PIT_HI
+ ROL_CE
) {
376 cameraControlKeyPress(CAMERA_CONTROL_KEY_UP
, 0);
377 repeatAfter(3 * STICK_DELAY_MS
);
378 } else if (rcSticks
== THR_CE
+ YAW_CE
+ PIT_CE
+ ROL_HI
) {
379 cameraControlKeyPress(CAMERA_CONTROL_KEY_RIGHT
, 0);
380 repeatAfter(3 * STICK_DELAY_MS
);
381 } else if (rcSticks
== THR_CE
+ YAW_CE
+ PIT_LO
+ ROL_CE
) {
382 cameraControlKeyPress(CAMERA_CONTROL_KEY_DOWN
, 0);
383 repeatAfter(3 * STICK_DELAY_MS
);
384 } else if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_HI
+ ROL_CE
) {
385 cameraControlKeyPress(CAMERA_CONTROL_KEY_UP
, 2000);
390 int32_t getRcStickDeflection(int32_t axis
, uint16_t midrc
) {
391 return MIN(ABS(rcData
[axis
] - midrc
), 500);
394 void useRcControlsConfig(pidProfile_t
*pidProfileToUse
)
396 pidProfile
= pidProfileToUse
;
398 isUsingSticksToArm
= !isModeActivationConditionPresent(BOXARM
);