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 "drivers/camera_control_impl.h"
40 #include "config/config.h"
43 #include "fc/runtime_config.h"
45 #include "flight/pid.h"
46 #include "flight/failsafe.h"
48 #include "io/beeper.h"
49 #include "io/usb_cdc_hid.h"
50 #include "io/dashboard.h"
51 #include "io/gimbal_control.h"
53 #include "io/vtx_control.h"
56 #include "pg/pg_ids.h"
61 #include "scheduler/scheduler.h"
63 #include "sensors/acceleration.h"
64 #include "sensors/barometer.h"
65 #include "sensors/battery.h"
66 #include "sensors/compass.h"
67 #include "sensors/gyro.h"
69 #include "rc_controls.h"
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
, 1);
78 PG_RESET_TEMPLATE(rcControlsConfig_t
, rcControlsConfig
,
81 .yaw_control_reversed
= false,
84 PG_REGISTER_WITH_RESET_TEMPLATE(armingConfig_t
, armingConfig
, PG_ARMING_CONFIG
, 2);
86 PG_RESET_TEMPLATE(armingConfig_t
, armingConfig
,
87 .gyro_cal_on_first_arm
= 0,
88 .auto_disarm_delay
= 5,
89 .prearm_allow_rearm
= 0,
92 PG_REGISTER_WITH_RESET_TEMPLATE(flight3DConfig_t
, flight3DConfig
, PG_MOTOR_3D_CONFIG
, 0);
93 PG_RESET_TEMPLATE(flight3DConfig_t
, flight3DConfig
,
94 .deadband3d_low
= 1406,
95 .deadband3d_high
= 1514,
97 .deadband3d_throttle
= 50,
100 .switched_mode3d
= false
103 bool isUsingSticksForArming(void)
105 return isUsingSticksToArm
;
108 throttleStatus_e
calculateThrottleStatus(void)
110 if (featureIsEnabled(FEATURE_3D
)) {
111 if (IS_RC_MODE_ACTIVE(BOX3D
) || flight3DConfig()->switched_mode3d
) {
112 if (rcData
[THROTTLE
] < rxConfig()->mincheck
) {
115 } else if ((rcData
[THROTTLE
] > (rxConfig()->midrc
- flight3DConfig()->deadband3d_throttle
) && rcData
[THROTTLE
] < (rxConfig()->midrc
+ flight3DConfig()->deadband3d_throttle
))) {
118 } else if (rcData
[THROTTLE
] < rxConfig()->mincheck
) {
122 return THROTTLE_HIGH
;
125 #define ARM_DELAY_MS 500
126 #define STICK_DELAY_MS 50
127 #define STICK_AUTOREPEAT_MS 250
128 #define repeatAfter(t) { \
130 doNotRepeat = false; \
133 void processRcStickPositions(void)
135 // time the sticks are maintained
136 static int16_t rcDelayMs
;
137 // hold sticks position for command combos
138 static uint8_t rcSticks
;
139 // an extra guard for disarming through switch to prevent that one frame can disarm it
140 static uint8_t rcDisarmTicks
;
141 static bool doNotRepeat
;
142 static bool pendingApplyRollAndPitchTrimDeltaSave
= false;
144 // checking sticks positions
146 for (int i
= 0; i
< 4; i
++) {
148 if (rcData
[i
] > rxConfig()->mincheck
) {
149 stTmp
|= 0x80; // check for MIN
151 if (rcData
[i
] < rxConfig()->maxcheck
) {
152 stTmp
|= 0x40; // check for MAX
155 if (stTmp
== rcSticks
) {
156 if (rcDelayMs
<= INT16_MAX
- (getTaskDeltaTimeUs(TASK_SELF
) / 1000)) {
157 rcDelayMs
+= getTaskDeltaTimeUs(TASK_SELF
) / 1000;
166 if (!isUsingSticksToArm
) {
167 if (IS_RC_MODE_ACTIVE(BOXARM
)) {
169 // Arming via ARM BOX
173 // Disarming via ARM BOX
174 resetArmingDisabled();
175 const bool boxFailsafeSwitchIsOn
= IS_RC_MODE_ACTIVE(BOXFAILSAFE
);
176 if (ARMING_FLAG(ARMED
) && (failsafeIsReceivingRxData() || boxFailsafeSwitchIsOn
)) {
177 // in a true signal loss situation, allow disarm only once we regain validated RxData (failsafeIsReceivingRxData = true),
178 // to avoid potentially false disarm signals soon after link recover
179 // Note that BOXFAILSAFE will also drive failsafeIsReceivingRxData false (immediately at start or end)
180 // That's why we explicitly allow disarm here if BOXFAILSAFE switch is active
181 // Note that BOXGPSRESCUE mode does not trigger failsafe - we can always disarm in that mode
183 if (rcDisarmTicks
> 3) {
184 // require three duplicate disarm values in a row before we disarm
185 disarm(DISARM_REASON_SWITCH
);
189 } else if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_CE
+ ROL_CE
) {
190 if (rcDelayMs
>= ARM_DELAY_MS
&& !doNotRepeat
) {
192 // Disarm on throttle down + yaw
194 if (ARMING_FLAG(ARMED
))
195 disarm(DISARM_REASON_STICKS
);
197 beeper(BEEPER_DISARM_REPEAT
); // sound tone while stick held
198 repeatAfter(STICK_AUTOREPEAT_MS
); // disarm tone will repeat
200 #ifdef USE_RUNAWAY_TAKEOFF
201 // Unset the ARMING_DISABLED_RUNAWAY_TAKEOFF arming disabled flag that might have been set
202 // by a runaway pidSum detection auto-disarm.
203 // This forces the pilot to explicitly perform a disarm sequence (even though we're implicitly disarmed)
204 // before they're able to rearm
205 unsetArmingDisabled(ARMING_DISABLED_RUNAWAY_TAKEOFF
);
207 unsetArmingDisabled(ARMING_DISABLED_CRASH_DETECTED
);
211 } else if (rcSticks
== THR_LO
+ YAW_HI
+ PIT_CE
+ ROL_CE
&& !IS_RC_MODE_ACTIVE(BOXSTICKCOMMANDDISABLE
)) { // disable stick arming if STICK COMMAND DISABLE SW is active
212 if (rcDelayMs
>= ARM_DELAY_MS
&& !doNotRepeat
) {
214 if (!ARMING_FLAG(ARMED
)) {
217 if (isTryingToArm() ||
218 ((getArmingDisableFlags() == ARMING_DISABLED_CALIBRATING
) && armingConfig()->gyro_cal_on_first_arm
)) {
222 resetArmingDisabled();
230 if (ARMING_FLAG(ARMED
) || doNotRepeat
|| rcDelayMs
<= STICK_DELAY_MS
|| (getArmingDisableFlags() & (ARMING_DISABLED_RUNAWAY_TAKEOFF
| ARMING_DISABLED_CRASH_DETECTED
))) {
235 #ifdef USE_USB_CDC_HID
236 // If this target is used as a joystick, we should leave here.
237 if (cdcDeviceIsMayBeActive() || IS_RC_MODE_ACTIVE(BOXSTICKCOMMANDDISABLE
)) {
242 // actions during not armed
244 if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_LO
+ ROL_CE
) {
246 gyroStartCalibration(false);
249 if (featureIsEnabled(FEATURE_GPS
)) {
250 GPS_reset_home_position();
255 if (sensors(SENSOR_BARO
)) {
256 baroSetGroundLevel();
263 if (featureIsEnabled(FEATURE_INFLIGHT_ACC_CAL
) && (rcSticks
== THR_LO
+ YAW_LO
+ PIT_HI
+ ROL_HI
)) {
264 // Inflight ACC Calibration
265 handleInflightCalibrationStickPosition();
269 // Change PID profile
271 case THR_LO
+ YAW_LO
+ PIT_CE
+ ROL_LO
:
272 // ROLL left -> PID profile 1
275 case THR_LO
+ YAW_LO
+ PIT_HI
+ ROL_CE
:
276 // PITCH up -> PID profile 2
279 case THR_LO
+ YAW_LO
+ PIT_CE
+ ROL_HI
:
280 // ROLL right -> PID profile 3
285 if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_LO
+ ROL_HI
) {
286 saveConfigAndNotify();
290 if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_LO
+ ROL_CE
) {
292 accStartCalibration();
298 if (rcSticks
== THR_HI
+ YAW_HI
+ PIT_LO
+ ROL_CE
) {
300 compassStartCalibration();
306 if (FLIGHT_MODE(ANGLE_MODE
| HORIZON_MODE
)) {
307 // in ANGLE or HORIZON mode, so use sticks to apply accelerometer trims
308 rollAndPitchTrims_t accelerometerTrimsDelta
;
309 memset(&accelerometerTrimsDelta
, 0, sizeof(accelerometerTrimsDelta
));
311 if (pendingApplyRollAndPitchTrimDeltaSave
&& ((rcSticks
& THR_MASK
) != THR_HI
)) {
312 saveConfigAndNotify();
313 pendingApplyRollAndPitchTrimDeltaSave
= false;
317 bool shouldApplyRollAndPitchTrimDelta
= false;
319 case THR_HI
+ YAW_CE
+ PIT_HI
+ ROL_CE
:
320 accelerometerTrimsDelta
.values
.pitch
= 1;
321 shouldApplyRollAndPitchTrimDelta
= true;
323 case THR_HI
+ YAW_CE
+ PIT_LO
+ ROL_CE
:
324 accelerometerTrimsDelta
.values
.pitch
= -1;
325 shouldApplyRollAndPitchTrimDelta
= true;
327 case THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_HI
:
328 accelerometerTrimsDelta
.values
.roll
= 1;
329 shouldApplyRollAndPitchTrimDelta
= true;
331 case THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_LO
:
332 accelerometerTrimsDelta
.values
.roll
= -1;
333 shouldApplyRollAndPitchTrimDelta
= true;
336 if (shouldApplyRollAndPitchTrimDelta
) {
338 applyAccelerometerTrimsDelta(&accelerometerTrimsDelta
);
340 pendingApplyRollAndPitchTrimDeltaSave
= true;
342 beeperConfirmationBeeps(1);
344 repeatAfter(STICK_AUTOREPEAT_MS
);
349 // in ACRO mode, so use sticks to change RATE profile
351 case THR_HI
+ YAW_CE
+ PIT_HI
+ ROL_CE
:
352 changeControlRateProfile(0);
354 case THR_HI
+ YAW_CE
+ PIT_LO
+ ROL_CE
:
355 changeControlRateProfile(1);
357 case THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_HI
:
358 changeControlRateProfile(2);
360 case THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_LO
:
361 changeControlRateProfile(3);
367 if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_HI
+ ROL_LO
) {
368 dashboardDisablePageCycling();
371 if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_HI
+ ROL_HI
) {
372 dashboardEnablePageCycling();
376 #ifdef USE_VTX_CONTROL
377 if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_CE
+ ROL_HI
) {
380 if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_CE
+ ROL_LO
) {
383 if (rcSticks
== THR_HI
+ YAW_HI
+ PIT_CE
+ ROL_HI
) {
384 vtxIncrementChannel();
386 if (rcSticks
== THR_HI
+ YAW_HI
+ PIT_CE
+ ROL_LO
) {
387 vtxDecrementChannel();
391 #ifdef USE_CAMERA_CONTROL
392 if (rcSticks
== THR_CE
+ YAW_HI
+ PIT_CE
+ ROL_CE
) {
393 cameraControlKeyPress(CAMERA_CONTROL_KEY_ENTER
, 0);
394 repeatAfter(3 * STICK_DELAY_MS
);
395 } else if (rcSticks
== THR_CE
+ YAW_CE
+ PIT_CE
+ ROL_LO
) {
396 cameraControlKeyPress(CAMERA_CONTROL_KEY_LEFT
, 0);
397 repeatAfter(3 * STICK_DELAY_MS
);
398 } else if (rcSticks
== THR_CE
+ YAW_CE
+ PIT_HI
+ ROL_CE
) {
399 cameraControlKeyPress(CAMERA_CONTROL_KEY_UP
, 0);
400 repeatAfter(3 * STICK_DELAY_MS
);
401 } else if (rcSticks
== THR_CE
+ YAW_CE
+ PIT_CE
+ ROL_HI
) {
402 cameraControlKeyPress(CAMERA_CONTROL_KEY_RIGHT
, 0);
403 repeatAfter(3 * STICK_DELAY_MS
);
404 } else if (rcSticks
== THR_CE
+ YAW_CE
+ PIT_LO
+ ROL_CE
) {
405 cameraControlKeyPress(CAMERA_CONTROL_KEY_DOWN
, 0);
406 repeatAfter(3 * STICK_DELAY_MS
);
407 } else if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_HI
+ ROL_CE
) {
408 cameraControlKeyPress(CAMERA_CONTROL_KEY_UP
, 2000);
413 void rcControlsInit(void)
415 analyzeModeActivationConditions();
416 isUsingSticksToArm
= !isModeActivationConditionPresent(BOXARM
) && systemConfig()->enableStickArming
;