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/>.
26 #include "build/debug.h"
28 #include "blackbox/blackbox.h"
30 #include "common/axis.h"
31 #include "common/maths.h"
32 #include "common/utils.h"
34 #include "config/feature.h"
35 #include "config/parameter_group.h"
36 #include "config/parameter_group_ids.h"
38 #include "drivers/time.h"
41 #include "fc/config.h"
42 #include "fc/controlrate_profile.h"
43 #include "fc/fc_core.h"
44 #include "fc/rc_controls.h"
45 #include "fc/rc_curves.h"
46 #include "fc/rc_modes.h"
47 #include "fc/runtime_config.h"
48 #include "fc/settings.h"
50 #include "flight/pid.h"
51 #include "flight/failsafe.h"
54 #include "io/beeper.h"
56 #include "navigation/navigation.h"
60 #include "sensors/barometer.h"
61 #include "sensors/battery.h"
62 #include "sensors/sensors.h"
63 #include "sensors/gyro.h"
64 #include "sensors/acceleration.h"
66 #define AIRMODE_DEADBAND 25
67 #define MIN_RC_TICK_INTERVAL_MS 20
68 #define DEFAULT_RC_SWITCH_DISARM_DELAY_MS 250 // Wait at least 250ms before disarming via switch
69 #define DEFAULT_PREARM_TIMEOUT 10000 // Prearm is invalidated after 10 seconds
71 stickPositions_e rcStickPositions
;
73 FASTRAM
int16_t rcCommand
[4]; // interval [1000;2000] for THROTTLE and [-500;+500] for ROLL/PITCH/YAW
75 PG_REGISTER_WITH_RESET_TEMPLATE(rcControlsConfig_t
, rcControlsConfig
, PG_RC_CONTROLS_CONFIG
, 3);
77 PG_RESET_TEMPLATE(rcControlsConfig_t
, rcControlsConfig
,
78 .deadband
= SETTING_DEADBAND_DEFAULT
,
79 .yaw_deadband
= SETTING_YAW_DEADBAND_DEFAULT
,
80 .pos_hold_deadband
= SETTING_POS_HOLD_DEADBAND_DEFAULT
,
81 .control_deadband
= SETTING_CONTROL_DEADBAND_DEFAULT
,
82 .alt_hold_deadband
= SETTING_ALT_HOLD_DEADBAND_DEFAULT
,
83 .mid_throttle_deadband
= SETTING_3D_DEADBAND_THROTTLE_DEFAULT
,
84 .airmodeHandlingType
= SETTING_AIRMODE_TYPE_DEFAULT
,
85 .airmodeThrottleThreshold
= SETTING_AIRMODE_THROTTLE_THRESHOLD_DEFAULT
,
88 PG_REGISTER_WITH_RESET_TEMPLATE(armingConfig_t
, armingConfig
, PG_ARMING_CONFIG
, 2);
90 PG_RESET_TEMPLATE(armingConfig_t
, armingConfig
,
91 .fixed_wing_auto_arm
= SETTING_FIXED_WING_AUTO_ARM_DEFAULT
,
92 .disarm_kill_switch
= SETTING_DISARM_KILL_SWITCH_DEFAULT
,
93 .switchDisarmDelayMs
= SETTING_SWITCH_DISARM_DELAY_DEFAULT
,
94 .prearmTimeoutMs
= SETTING_PREARM_TIMEOUT_DEFAULT
,
97 bool areSticksInApModePosition(uint16_t ap_mode
)
99 return ABS(rcCommand
[ROLL
]) < ap_mode
&& ABS(rcCommand
[PITCH
]) < ap_mode
;
102 bool areSticksDeflected(void)
104 return (ABS(rcCommand
[ROLL
]) > rcControlsConfig()->control_deadband
) || (ABS(rcCommand
[PITCH
]) > rcControlsConfig()->control_deadband
) || (ABS(rcCommand
[YAW
]) > rcControlsConfig()->control_deadband
);
107 bool isRollPitchStickDeflected(uint8_t deadband
)
109 return (ABS(rcCommand
[ROLL
]) > deadband
) || (ABS(rcCommand
[PITCH
]) > deadband
);
112 throttleStatus_e FAST_CODE NOINLINE
calculateThrottleStatus(throttleStatusType_e type
)
114 int value
= rxGetChannelValue(THROTTLE
); // THROTTLE_STATUS_TYPE_RC
115 if (type
== THROTTLE_STATUS_TYPE_COMMAND
) {
116 value
= rcCommand
[THROTTLE
];
119 const uint16_t mid_throttle_deadband
= rcControlsConfig()->mid_throttle_deadband
;
120 bool midThrottle
= value
> (PWM_RANGE_MIDDLE
- mid_throttle_deadband
) && value
< (PWM_RANGE_MIDDLE
+ mid_throttle_deadband
);
121 if ((feature(FEATURE_REVERSIBLE_MOTORS
) && midThrottle
) || (!feature(FEATURE_REVERSIBLE_MOTORS
) && (value
< rxConfig()->mincheck
))) {
125 return THROTTLE_HIGH
;
128 bool throttleStickIsLow(void)
130 return calculateThrottleStatus(feature(FEATURE_REVERSIBLE_MOTORS
) ? THROTTLE_STATUS_TYPE_COMMAND
: THROTTLE_STATUS_TYPE_RC
) == THROTTLE_LOW
;
133 int16_t throttleStickMixedValue(void)
135 int16_t throttleValue
;
137 throttleValue
= constrain(rxGetChannelValue(THROTTLE
), rxConfig()->mincheck
, PWM_RANGE_MAX
);
138 throttleValue
= (uint16_t)(throttleValue
- rxConfig()->mincheck
) * PWM_RANGE_MIN
/ (PWM_RANGE_MAX
- rxConfig()->mincheck
); // [MINCHECK;2000] -> [0;1000]
139 return rcLookupThrottle(throttleValue
);
142 rollPitchStatus_e
calculateRollPitchCenterStatus(void)
144 if (((rxGetChannelValue(PITCH
) < (PWM_RANGE_MIDDLE
+ AIRMODE_DEADBAND
)) && (rxGetChannelValue(PITCH
) > (PWM_RANGE_MIDDLE
-AIRMODE_DEADBAND
)))
145 && ((rxGetChannelValue(ROLL
) < (PWM_RANGE_MIDDLE
+ AIRMODE_DEADBAND
)) && (rxGetChannelValue(ROLL
) > (PWM_RANGE_MIDDLE
-AIRMODE_DEADBAND
))))
151 stickPositions_e
getRcStickPositions(void)
153 return rcStickPositions
;
156 bool checkStickPosition(stickPositions_e stickPos
)
158 const uint8_t mask
[4] = { ROL_LO
| ROL_HI
, PIT_LO
| PIT_HI
, YAW_LO
| YAW_HI
, THR_LO
| THR_HI
};
159 for (int i
= 0; i
< 4; i
++) {
160 if (((stickPos
& mask
[i
]) != 0) && ((stickPos
& mask
[i
]) != (rcStickPositions
& mask
[i
]))) {
168 static void updateRcStickPositions(void)
170 stickPositions_e tmp
= 0;
172 tmp
|= ((rxGetChannelValue(ROLL
) > rxConfig()->mincheck
) ? 0x02 : 0x00) << (ROLL
* 2);
173 tmp
|= ((rxGetChannelValue(ROLL
) < rxConfig()->maxcheck
) ? 0x01 : 0x00) << (ROLL
* 2);
175 tmp
|= ((rxGetChannelValue(PITCH
) > rxConfig()->mincheck
) ? 0x02 : 0x00) << (PITCH
* 2);
176 tmp
|= ((rxGetChannelValue(PITCH
) < rxConfig()->maxcheck
) ? 0x01 : 0x00) << (PITCH
* 2);
178 tmp
|= ((rxGetChannelValue(YAW
) > rxConfig()->mincheck
) ? 0x02 : 0x00) << (YAW
* 2);
179 tmp
|= ((rxGetChannelValue(YAW
) < rxConfig()->maxcheck
) ? 0x01 : 0x00) << (YAW
* 2);
181 tmp
|= ((rxGetChannelValue(THROTTLE
) > rxConfig()->mincheck
) ? 0x02 : 0x00) << (THROTTLE
* 2);
182 tmp
|= ((rxGetChannelValue(THROTTLE
) < rxConfig()->maxcheck
) ? 0x01 : 0x00) << (THROTTLE
* 2);
184 rcStickPositions
= tmp
;
187 void processRcStickPositions(bool isThrottleLow
)
189 static timeMs_t lastTickTimeMs
= 0;
190 static uint8_t rcDelayCommand
; // this indicates the number of time (multiple of RC measurement at 50Hz) the sticks must be maintained to run or switch off motors
191 static uint32_t rcSticks
; // this hold sticks position for command combos
192 static timeMs_t rcDisarmTimeMs
; // this is an extra guard for disarming through switch to prevent that one frame can disarm it
193 const timeMs_t currentTimeMs
= millis();
195 updateRcStickPositions();
197 uint32_t stTmp
= getRcStickPositions();
198 if (stTmp
== rcSticks
) {
199 if (rcDelayCommand
< 250) {
200 if ((currentTimeMs
- lastTickTimeMs
) >= MIN_RC_TICK_INTERVAL_MS
) {
201 lastTickTimeMs
= currentTimeMs
;
212 bool armingSwitchIsActive
= IS_RC_MODE_ACTIVE(BOXARM
);
214 if (STATE(AIRPLANE
) && feature(FEATURE_MOTOR_STOP
) && armingConfig()->fixed_wing_auto_arm
) {
215 // Auto arm on throttle when using fixedwing and motorstop
216 if (!isThrottleLow
) {
222 if (armingSwitchIsActive
) {
223 rcDisarmTimeMs
= currentTimeMs
;
226 emergencyArmingUpdate(armingSwitchIsActive
);
227 // Disarming via ARM BOX
228 // Don't disarm via switch if failsafe is active or receiver doesn't receive data - we can't trust receiver
229 // and can't afford to risk disarming in the air
230 if (ARMING_FLAG(ARMED
) && !IS_RC_MODE_ACTIVE(BOXFAILSAFE
) && rxIsReceivingSignal() && !failsafeIsActive()) {
231 const timeMs_t disarmDelay
= currentTimeMs
- rcDisarmTimeMs
;
232 if (disarmDelay
> armingConfig()->switchDisarmDelayMs
) {
233 if (armingConfig()->disarm_kill_switch
|| isThrottleLow
) {
234 disarm(DISARM_SWITCH
);
239 rcDisarmTimeMs
= currentTimeMs
;
243 // KILLSWITCH disarms instantly
244 if (IS_RC_MODE_ACTIVE(BOXKILLSWITCH
)) {
245 disarm(DISARM_KILLSWITCH
);
249 if (rcDelayCommand
!= 20) {
253 if (ARMING_FLAG(ARMED
)) {
254 // actions during armed
258 // Disable stick commands when in CLI mode. Ideally, they should also be disabled when configurator is connected
263 // actions during not armed and not in CLI
266 if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_LO
+ ROL_CE
) {
267 gyroStartCalibration();
272 #if defined(NAV_NON_VOLATILE_WAYPOINT_STORAGE)
273 // Save waypoint list
274 if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_HI
+ ROL_LO
) {
275 const bool success
= saveNonVolatileWaypointList();
276 beeper(success
? BEEPER_ACTION_SUCCESS
: BEEPER_ACTION_FAIL
);
279 // Load waypoint list
280 if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_HI
+ ROL_HI
) {
281 const bool success
= loadNonVolatileWaypointList(false);
282 beeper(success
? BEEPER_ACTION_SUCCESS
: BEEPER_ACTION_FAIL
);
284 #ifdef USE_MULTI_MISSION
285 // Increment multi mission index up
286 if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_CE
+ ROL_HI
) {
287 selectMultiMissionIndex(1);
292 // Decrement multi mission index down
293 if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_CE
+ ROL_LO
) {
294 selectMultiMissionIndex(-1);
299 if (rcSticks
== THR_LO
+ YAW_CE
+ PIT_LO
+ ROL_HI
) {
301 beeper(BEEPER_ACTION_FAIL
); // The above cannot fail, but traditionally, we play FAIL for not-loading
305 // Multiple configuration profiles
306 if (feature(FEATURE_TX_PROF_SEL
)) {
310 if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_CE
+ ROL_LO
) // ROLL left -> Profile 1
312 else if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_HI
+ ROL_CE
) // PITCH up -> Profile 2
314 else if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_CE
+ ROL_HI
) // ROLL right -> Profile 3
318 setConfigProfileAndWriteEEPROM(i
- 1);
324 // Multiple battery configuration profiles
325 if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_CE
+ ROL_LO
) // ROLL left -> Profile 1
327 else if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_HI
+ ROL_CE
) // PITCH up -> Profile 2
329 else if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_CE
+ ROL_HI
) // ROLL right -> Profile 3
333 setConfigBatteryProfileAndWriteEEPROM(i
- 1);
334 batteryDisableProfileAutoswitch();
335 activateBatteryProfile();
342 if (rcSticks
== THR_LO
+ YAW_LO
+ PIT_LO
+ ROL_HI
) {
343 saveConfigAndNotify();
347 if (rcSticks
== THR_HI
+ YAW_LO
+ PIT_LO
+ ROL_CE
) {
348 accStartCalibration();
353 if (rcSticks
== THR_HI
+ YAW_HI
+ PIT_LO
+ ROL_CE
) {
354 ENABLE_STATE(CALIBRATE_MAG
);
358 // Accelerometer Trim
359 if (rcSticks
== THR_HI
+ YAW_CE
+ PIT_HI
+ ROL_CE
) {
360 applyAndSaveBoardAlignmentDelta(0, -2);
363 } else if (rcSticks
== THR_HI
+ YAW_CE
+ PIT_LO
+ ROL_CE
) {
364 applyAndSaveBoardAlignmentDelta(0, 2);
367 } else if (rcSticks
== THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_HI
) {
368 applyAndSaveBoardAlignmentDelta(-2, 0);
371 } else if (rcSticks
== THR_HI
+ YAW_CE
+ PIT_CE
+ ROL_LO
) {
372 applyAndSaveBoardAlignmentDelta(2, 0);
378 int32_t getRcStickDeflection(int32_t axis
) {
379 return MIN(ABS(rxGetChannelValue(axis
) - PWM_RANGE_MIDDLE
), 500);