An implenentaion of nav-rewrite (squashed commit, November 17th).
[inav.git] / src / main / io / rc_controls.h
blob33d6e7f429948a67e2369200cea9fd3cc3f7c232
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 BOXNAVALTHOLD, // old BOXBARO
27 // BOXVARIO,
28 BOXMAG,
29 BOXHEADFREE,
30 BOXHEADADJ,
31 BOXCAMSTAB,
32 BOXCAMTRIG,
33 BOXNAVRTH, // old GPSHOME
34 BOXNAVPOSHOLD, // old GPSHOLD
35 BOXPASSTHRU,
36 BOXBEEPERON,
37 BOXLEDMAX,
38 BOXLEDLOW,
39 BOXLLIGHTS,
40 //BOXCALIB,
41 BOXGOV,
42 BOXOSD,
43 BOXTELEMETRY,
44 BOXGTUNE,
45 BOXSERVO1,
46 BOXSERVO2,
47 BOXSERVO3,
48 BOXBLACKBOX,
49 BOXFAILSAFE,
50 BOXNAVWP,
51 CHECKBOX_ITEM_COUNT
52 } boxId_e;
54 extern uint32_t rcModeActivationMask;
56 #define IS_RC_MODE_ACTIVE(modeId) ((1 << (modeId)) & rcModeActivationMask)
57 #define ACTIVATE_RC_MODE(modeId) (rcModeActivationMask |= (1 << modeId))
59 typedef enum rc_alias {
60 ROLL = 0,
61 PITCH,
62 YAW,
63 THROTTLE,
64 AUX1,
65 AUX2,
66 AUX3,
67 AUX4,
68 AUX5,
69 AUX6,
70 AUX7,
71 AUX8
72 } rc_alias_e;
74 typedef enum {
75 THROTTLE_LOW = 0,
76 THROTTLE_HIGH
77 } throttleStatus_e;
79 #define ROL_LO (1 << (2 * ROLL))
80 #define ROL_CE (3 << (2 * ROLL))
81 #define ROL_HI (2 << (2 * ROLL))
82 #define PIT_LO (1 << (2 * PITCH))
83 #define PIT_CE (3 << (2 * PITCH))
84 #define PIT_HI (2 << (2 * PITCH))
85 #define YAW_LO (1 << (2 * YAW))
86 #define YAW_CE (3 << (2 * YAW))
87 #define YAW_HI (2 << (2 * YAW))
88 #define THR_LO (1 << (2 * THROTTLE))
89 #define THR_CE (3 << (2 * THROTTLE))
90 #define THR_HI (2 << (2 * THROTTLE))
92 #define MAX_MODE_ACTIVATION_CONDITION_COUNT 20
94 #define CHANNEL_RANGE_MIN 900
95 #define CHANNEL_RANGE_MAX 2100
97 #define MODE_STEP_TO_CHANNEL_VALUE(step) (CHANNEL_RANGE_MIN + 25 * step)
98 #define CHANNEL_VALUE_TO_STEP(channelValue) ((constrain(channelValue, CHANNEL_RANGE_MIN, CHANNEL_RANGE_MAX) - CHANNEL_RANGE_MIN) / 25)
100 #define MIN_MODE_RANGE_STEP 0
101 #define MAX_MODE_RANGE_STEP ((CHANNEL_RANGE_MAX - CHANNEL_RANGE_MIN) / 25)
103 // Roll/pitch rates are a proportion used for mixing, so it tops out at 1.0:
104 #define CONTROL_RATE_CONFIG_ROLL_PITCH_RATE_MAX 100
106 /* Meaningful yaw rates are effectively unbounded because they are treated as a rotation rate multiplier: */
107 #define CONTROL_RATE_CONFIG_YAW_RATE_MAX 255
109 #define CONTROL_RATE_CONFIG_TPA_MAX 100
111 // steps are 25 apart
112 // a value of 0 corresponds to a channel value of 900 or less
113 // a value of 48 corresponds to a channel value of 2100 or more
114 // 48 steps between 900 and 1200
115 typedef struct channelRange_s {
116 uint8_t startStep;
117 uint8_t endStep;
118 } channelRange_t;
120 typedef struct modeActivationCondition_s {
121 boxId_e modeId;
122 uint8_t auxChannelIndex;
123 channelRange_t range;
124 } modeActivationCondition_t;
126 #define IS_RANGE_USABLE(range) ((range)->startStep < (range)->endStep)
128 typedef struct controlRateConfig_s {
129 uint8_t rcRate8;
130 uint8_t rcExpo8;
131 uint8_t thrMid8;
132 uint8_t thrExpo8;
133 uint8_t rates[3];
134 uint8_t dynThrPID;
135 uint8_t rcYawExpo8;
136 uint16_t tpa_breakpoint; // Breakpoint where TPA is activated
137 } controlRateConfig_t;
139 extern int16_t rcCommand[4];
141 typedef struct rcControlsConfig_s {
142 uint8_t deadband; // introduce a deadband around the stick center for pitch and roll axis. Must be greater than zero.
143 uint8_t yaw_deadband; // introduce a deadband around the stick center for yaw axis. Must be greater than zero.
144 } rcControlsConfig_t;
146 bool areUsingSticksToArm(void);
148 bool areSticksInApModePosition(uint16_t ap_mode);
149 throttleStatus_e calculateThrottleStatus(rxConfig_t *rxConfig, uint16_t deadband3d_throttle);
150 void processRcStickPositions(rxConfig_t *rxConfig, throttleStatus_e throttleStatus, bool retarded_arm, bool disarm_kill_switch);
152 void updateActivatedModes(modeActivationCondition_t *modeActivationConditions);
155 typedef enum {
156 ADJUSTMENT_NONE = 0,
157 ADJUSTMENT_RC_RATE,
158 ADJUSTMENT_RC_EXPO,
159 ADJUSTMENT_THROTTLE_EXPO,
160 ADJUSTMENT_PITCH_ROLL_RATE,
161 ADJUSTMENT_YAW_RATE,
162 ADJUSTMENT_PITCH_ROLL_P,
163 ADJUSTMENT_PITCH_ROLL_I,
164 ADJUSTMENT_PITCH_ROLL_D,
165 ADJUSTMENT_YAW_P,
166 ADJUSTMENT_YAW_I,
167 ADJUSTMENT_YAW_D,
168 ADJUSTMENT_RATE_PROFILE,
169 ADJUSTMENT_PITCH_RATE,
170 ADJUSTMENT_ROLL_RATE,
171 ADJUSTMENT_PITCH_P,
172 ADJUSTMENT_PITCH_I,
173 ADJUSTMENT_PITCH_D,
174 ADJUSTMENT_ROLL_P,
175 ADJUSTMENT_ROLL_I,
176 ADJUSTMENT_ROLL_D,
178 } adjustmentFunction_e;
180 #define ADJUSTMENT_FUNCTION_COUNT 21
182 typedef enum {
183 ADJUSTMENT_MODE_STEP,
184 ADJUSTMENT_MODE_SELECT
185 } adjustmentMode_e;
187 typedef struct adjustmentStepConfig_s {
188 uint8_t step;
189 } adjustmentStepConfig_t;
191 typedef struct adjustmentSelectConfig_s {
192 uint8_t switchPositions;
193 } adjustmentSelectConfig_t;
195 typedef union adjustmentConfig_u {
196 adjustmentStepConfig_t stepConfig;
197 adjustmentSelectConfig_t selectConfig;
198 } adjustmentData_t;
200 typedef struct adjustmentConfig_s {
201 uint8_t adjustmentFunction;
202 uint8_t mode;
203 adjustmentData_t data;
204 } adjustmentConfig_t;
206 typedef struct adjustmentRange_s {
207 // when aux channel is in range...
208 uint8_t auxChannelIndex;
209 channelRange_t range;
211 // ..then apply the adjustment function to the auxSwitchChannel ...
212 uint8_t adjustmentFunction;
213 uint8_t auxSwitchChannelIndex;
215 // ... via slot
216 uint8_t adjustmentIndex;
217 } adjustmentRange_t;
219 #define ADJUSTMENT_INDEX_OFFSET 1
221 typedef struct adjustmentState_s {
222 uint8_t auxChannelIndex;
223 const adjustmentConfig_t *config;
224 uint32_t timeoutAt;
225 } adjustmentState_t;
228 #ifndef MAX_SIMULTANEOUS_ADJUSTMENT_COUNT
229 #define MAX_SIMULTANEOUS_ADJUSTMENT_COUNT 4 // enough for 4 x 3position switches / 4 aux channel
230 #endif
232 #define MAX_ADJUSTMENT_RANGE_COUNT 12 // enough for 2 * 6pos switches.
234 void resetAdjustmentStates(void);
235 void configureAdjustment(uint8_t index, uint8_t auxChannelIndex, const adjustmentConfig_t *adjustmentConfig);
236 void updateAdjustmentStates(adjustmentRange_t *adjustmentRanges);
237 void processRcAdjustments(controlRateConfig_t *controlRateConfig, rxConfig_t *rxConfig);
239 bool isUsingSticksForArming(void);
241 int32_t getRcStickDeflection(int32_t axis, uint16_t midrc);
242 bool isModeActivationConditionPresent(modeActivationCondition_t *modeActivationConditions, boxId_e modeId);