Rework Iterm Shrink for AIR Mode
[betaflight.git] / src / main / io / rc_controls.h
blob64ec5af1fd6b2682633ba5537de0fbf19fefdb6c
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 #pragma once
20 #include "rx/rx.h"
22 typedef enum {
23 BOXARM = 0,
24 BOXANGLE,
25 BOXHORIZON,
26 BOXBARO,
27 // BOXVARIO,
28 BOXMAG,
29 BOXHEADFREE,
30 BOXHEADADJ,
31 BOXCAMSTAB,
32 BOXCAMTRIG,
33 BOXGPSHOME,
34 BOXGPSHOLD,
35 BOXPASSTHRU,
36 BOXBEEPERON,
37 BOXLEDMAX,
38 BOXLEDLOW,
39 BOXLLIGHTS,
40 BOXCALIB,
41 BOXGOV,
42 BOXOSD,
43 BOXTELEMETRY,
44 BOXGTUNE,
45 BOXSONAR,
46 BOXSERVO1,
47 BOXSERVO2,
48 BOXSERVO3,
49 BOXBLACKBOX,
50 BOXFAILSAFE,
51 BOXAIRMODE,
52 CHECKBOX_ITEM_COUNT
53 } boxId_e;
55 extern uint32_t rcModeActivationMask;
57 #define IS_RC_MODE_ACTIVE(modeId) ((1 << (modeId)) & rcModeActivationMask)
58 #define ACTIVATE_RC_MODE(modeId) (rcModeActivationMask |= (1 << modeId))
60 typedef enum rc_alias {
61 ROLL = 0,
62 PITCH,
63 YAW,
64 THROTTLE,
65 AUX1,
66 AUX2,
67 AUX3,
68 AUX4,
69 AUX5,
70 AUX6,
71 AUX7,
72 AUX8
73 } rc_alias_e;
75 typedef enum {
76 THROTTLE_LOW = 0,
77 THROTTLE_HIGH
78 } throttleStatus_e;
80 #define AIRMODEDEADBAND 10
82 typedef enum {
83 NOT_CENTERED = 0,
84 CENTERED
85 } rollPitchStatus_e;
87 #define ROL_LO (1 << (2 * ROLL))
88 #define ROL_CE (3 << (2 * ROLL))
89 #define ROL_HI (2 << (2 * ROLL))
90 #define PIT_LO (1 << (2 * PITCH))
91 #define PIT_CE (3 << (2 * PITCH))
92 #define PIT_HI (2 << (2 * PITCH))
93 #define YAW_LO (1 << (2 * YAW))
94 #define YAW_CE (3 << (2 * YAW))
95 #define YAW_HI (2 << (2 * YAW))
96 #define THR_LO (1 << (2 * THROTTLE))
97 #define THR_CE (3 << (2 * THROTTLE))
98 #define THR_HI (2 << (2 * THROTTLE))
100 #define MAX_MODE_ACTIVATION_CONDITION_COUNT 20
102 #define CHANNEL_RANGE_MIN 900
103 #define CHANNEL_RANGE_MAX 2100
105 #define MODE_STEP_TO_CHANNEL_VALUE(step) (CHANNEL_RANGE_MIN + 25 * step)
106 #define CHANNEL_VALUE_TO_STEP(channelValue) ((constrain(channelValue, CHANNEL_RANGE_MIN, CHANNEL_RANGE_MAX) - CHANNEL_RANGE_MIN) / 25)
108 #define MIN_MODE_RANGE_STEP 0
109 #define MAX_MODE_RANGE_STEP ((CHANNEL_RANGE_MAX - CHANNEL_RANGE_MIN) / 25)
111 // Roll/pitch rates are a proportion used for mixing, so it tops out at 1.0:
112 #define CONTROL_RATE_CONFIG_ROLL_PITCH_RATE_MAX 100
114 /* Meaningful yaw rates are effectively unbounded because they are treated as a rotation rate multiplier: */
115 #define CONTROL_RATE_CONFIG_YAW_RATE_MAX 255
117 #define CONTROL_RATE_CONFIG_TPA_MAX 100
119 // steps are 25 apart
120 // a value of 0 corresponds to a channel value of 900 or less
121 // a value of 48 corresponds to a channel value of 2100 or more
122 // 48 steps between 900 and 1200
123 typedef struct channelRange_s {
124 uint8_t startStep;
125 uint8_t endStep;
126 } channelRange_t;
128 typedef struct modeActivationCondition_s {
129 boxId_e modeId;
130 uint8_t auxChannelIndex;
131 channelRange_t range;
132 } modeActivationCondition_t;
134 #define IS_RANGE_USABLE(range) ((range)->startStep < (range)->endStep)
136 typedef struct controlRateConfig_s {
137 uint8_t rcRate8;
138 uint8_t rcExpo8;
139 uint8_t thrMid8;
140 uint8_t thrExpo8;
141 uint8_t rates[3];
142 uint8_t dynThrPID;
143 uint8_t rcYawExpo8;
144 uint16_t tpa_breakpoint; // Breakpoint where TPA is activated
145 } controlRateConfig_t;
147 extern int16_t rcCommand[4];
149 typedef struct rcControlsConfig_s {
150 uint8_t deadband; // introduce a deadband around the stick center for pitch and roll axis. Must be greater than zero.
151 uint8_t yaw_deadband; // introduce a deadband around the stick center for yaw axis. Must be greater than zero.
152 uint8_t alt_hold_deadband; // defines the neutral zone of throttle stick during altitude hold, default setting is +/-40
153 uint8_t alt_hold_fast_change; // when disabled, turn off the althold when throttle stick is out of deadband defined with alt_hold_deadband; when enabled, altitude changes slowly proportional to stick movement
154 } rcControlsConfig_t;
156 bool areUsingSticksToArm(void);
158 bool areSticksInApModePosition(uint16_t ap_mode);
159 throttleStatus_e calculateThrottleStatus(rxConfig_t *rxConfig, uint16_t deadband3d_throttle);
160 void processRcStickPositions(rxConfig_t *rxConfig, throttleStatus_e throttleStatus, bool retarded_arm, bool disarm_kill_switch);
162 void updateActivatedModes(modeActivationCondition_t *modeActivationConditions);
165 typedef enum {
166 ADJUSTMENT_NONE = 0,
167 ADJUSTMENT_RC_RATE,
168 ADJUSTMENT_RC_EXPO,
169 ADJUSTMENT_THROTTLE_EXPO,
170 ADJUSTMENT_PITCH_ROLL_RATE,
171 ADJUSTMENT_YAW_RATE,
172 ADJUSTMENT_PITCH_ROLL_P,
173 ADJUSTMENT_PITCH_ROLL_I,
174 ADJUSTMENT_PITCH_ROLL_D,
175 ADJUSTMENT_YAW_P,
176 ADJUSTMENT_YAW_I,
177 ADJUSTMENT_YAW_D,
178 ADJUSTMENT_RATE_PROFILE,
179 ADJUSTMENT_PITCH_RATE,
180 ADJUSTMENT_ROLL_RATE,
181 ADJUSTMENT_PITCH_P,
182 ADJUSTMENT_PITCH_I,
183 ADJUSTMENT_PITCH_D,
184 ADJUSTMENT_ROLL_P,
185 ADJUSTMENT_ROLL_I,
186 ADJUSTMENT_ROLL_D,
188 } adjustmentFunction_e;
190 #define ADJUSTMENT_FUNCTION_COUNT 21
192 typedef enum {
193 ADJUSTMENT_MODE_STEP,
194 ADJUSTMENT_MODE_SELECT
195 } adjustmentMode_e;
197 typedef struct adjustmentStepConfig_s {
198 uint8_t step;
199 } adjustmentStepConfig_t;
201 typedef struct adjustmentSelectConfig_s {
202 uint8_t switchPositions;
203 } adjustmentSelectConfig_t;
205 typedef union adjustmentConfig_u {
206 adjustmentStepConfig_t stepConfig;
207 adjustmentSelectConfig_t selectConfig;
208 } adjustmentData_t;
210 typedef struct adjustmentConfig_s {
211 uint8_t adjustmentFunction;
212 uint8_t mode;
213 adjustmentData_t data;
214 } adjustmentConfig_t;
216 typedef struct adjustmentRange_s {
217 // when aux channel is in range...
218 uint8_t auxChannelIndex;
219 channelRange_t range;
221 // ..then apply the adjustment function to the auxSwitchChannel ...
222 uint8_t adjustmentFunction;
223 uint8_t auxSwitchChannelIndex;
225 // ... via slot
226 uint8_t adjustmentIndex;
227 } adjustmentRange_t;
229 #define ADJUSTMENT_INDEX_OFFSET 1
231 typedef struct adjustmentState_s {
232 uint8_t auxChannelIndex;
233 const adjustmentConfig_t *config;
234 uint32_t timeoutAt;
235 } adjustmentState_t;
238 #ifndef MAX_SIMULTANEOUS_ADJUSTMENT_COUNT
239 #define MAX_SIMULTANEOUS_ADJUSTMENT_COUNT 4 // enough for 4 x 3position switches / 4 aux channel
240 #endif
242 #define MAX_ADJUSTMENT_RANGE_COUNT 12 // enough for 2 * 6pos switches.
244 void resetAdjustmentStates(void);
245 void configureAdjustment(uint8_t index, uint8_t auxChannelIndex, const adjustmentConfig_t *adjustmentConfig);
246 void updateAdjustmentStates(adjustmentRange_t *adjustmentRanges);
247 void processRcAdjustments(controlRateConfig_t *controlRateConfig, rxConfig_t *rxConfig);
249 bool isUsingSticksForArming(void);
251 int32_t getRcStickDeflection(int32_t axis, uint16_t midrc);
252 bool isModeActivationConditionPresent(modeActivationCondition_t *modeActivationConditions, boxId_e modeId);
253 rollPitchStatus_e calculateRollPitchCenterStatus(rxConfig_t *rxConfig);