Version 1.0 bump
[inav/snaewe.git] / src / main / io / rc_controls.h
blob286e4979eac15ccb0db9120a6a7194cc81ced291
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 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 ROL_LO (1 << (2 * ROLL))
81 #define ROL_CE (3 << (2 * ROLL))
82 #define ROL_HI (2 << (2 * ROLL))
83 #define PIT_LO (1 << (2 * PITCH))
84 #define PIT_CE (3 << (2 * PITCH))
85 #define PIT_HI (2 << (2 * PITCH))
86 #define YAW_LO (1 << (2 * YAW))
87 #define YAW_CE (3 << (2 * YAW))
88 #define YAW_HI (2 << (2 * YAW))
89 #define THR_LO (1 << (2 * THROTTLE))
90 #define THR_CE (3 << (2 * THROTTLE))
91 #define THR_HI (2 << (2 * THROTTLE))
93 #define MAX_MODE_ACTIVATION_CONDITION_COUNT 20
95 #define CHANNEL_RANGE_MIN 900
96 #define CHANNEL_RANGE_MAX 2100
98 #define MODE_STEP_TO_CHANNEL_VALUE(step) (CHANNEL_RANGE_MIN + 25 * step)
99 #define CHANNEL_VALUE_TO_STEP(channelValue) ((constrain(channelValue, CHANNEL_RANGE_MIN, CHANNEL_RANGE_MAX) - CHANNEL_RANGE_MIN) / 25)
101 #define MIN_MODE_RANGE_STEP 0
102 #define MAX_MODE_RANGE_STEP ((CHANNEL_RANGE_MAX - CHANNEL_RANGE_MIN) / 25)
104 // Roll/pitch rates are a proportion used for mixing, so it tops out at 1.0:
105 #define CONTROL_RATE_CONFIG_ROLL_PITCH_RATE_MAX 100
107 /* Meaningful yaw rates are effectively unbounded because they are treated as a rotation rate multiplier: */
108 #define CONTROL_RATE_CONFIG_YAW_RATE_MAX 255
110 #define CONTROL_RATE_CONFIG_TPA_MAX 100
112 // steps are 25 apart
113 // a value of 0 corresponds to a channel value of 900 or less
114 // a value of 48 corresponds to a channel value of 2100 or more
115 // 48 steps between 900 and 1200
116 typedef struct channelRange_s {
117 uint8_t startStep;
118 uint8_t endStep;
119 } channelRange_t;
121 typedef struct modeActivationCondition_s {
122 boxId_e modeId;
123 uint8_t auxChannelIndex;
124 channelRange_t range;
125 } modeActivationCondition_t;
127 #define IS_RANGE_USABLE(range) ((range)->startStep < (range)->endStep)
129 #define SHOULD_RESET_ERRORS ((throttleStatus == THROTTLE_LOW && !(IS_RC_MODE_ACTIVE(BOXAIRMODE))) || !(ARMING_FLAG(ARMED)) || ((throttleStatus == THROTTLE_LOW && feature(FEATURE_MOTOR_STOP))))
131 typedef struct controlRateConfig_s {
132 uint8_t rcRate8;
133 uint8_t rcExpo8;
134 uint8_t thrMid8;
135 uint8_t thrExpo8;
136 uint8_t rates[3];
137 uint8_t dynThrPID;
138 uint8_t rcYawExpo8;
139 uint16_t tpa_breakpoint; // Breakpoint where TPA is activated
140 } controlRateConfig_t;
142 extern int16_t rcCommand[4];
144 typedef struct rcControlsConfig_s {
145 uint8_t deadband; // introduce a deadband around the stick center for pitch and roll axis. Must be greater than zero.
146 uint8_t yaw_deadband; // introduce a deadband around the stick center for yaw axis. Must be greater than zero.
147 uint8_t pos_hold_deadband; // Adds ability to adjust the Hold-position when moving the sticks (assisted mode)
148 uint8_t alt_hold_deadband; // Defines the neutral zone of throttle stick during altitude hold
149 } rcControlsConfig_t;
151 bool areUsingSticksToArm(void);
153 bool areSticksInApModePosition(uint16_t ap_mode);
154 throttleStatus_e calculateThrottleStatus(rxConfig_t *rxConfig, uint16_t deadband3d_throttle);
155 void processRcStickPositions(rxConfig_t *rxConfig, throttleStatus_e throttleStatus, bool disarm_kill_switch);
157 void updateActivatedModes(modeActivationCondition_t *modeActivationConditions);
160 typedef enum {
161 ADJUSTMENT_NONE = 0,
162 ADJUSTMENT_RC_RATE,
163 ADJUSTMENT_RC_EXPO,
164 ADJUSTMENT_THROTTLE_EXPO,
165 ADJUSTMENT_PITCH_ROLL_RATE,
166 ADJUSTMENT_YAW_RATE,
167 ADJUSTMENT_PITCH_ROLL_P,
168 ADJUSTMENT_PITCH_ROLL_I,
169 ADJUSTMENT_PITCH_ROLL_D,
170 ADJUSTMENT_YAW_P,
171 ADJUSTMENT_YAW_I,
172 ADJUSTMENT_YAW_D,
173 ADJUSTMENT_RATE_PROFILE,
174 ADJUSTMENT_PITCH_RATE,
175 ADJUSTMENT_ROLL_RATE,
176 ADJUSTMENT_PITCH_P,
177 ADJUSTMENT_PITCH_I,
178 ADJUSTMENT_PITCH_D,
179 ADJUSTMENT_ROLL_P,
180 ADJUSTMENT_ROLL_I,
181 ADJUSTMENT_ROLL_D,
183 } adjustmentFunction_e;
185 #define ADJUSTMENT_FUNCTION_COUNT 21
187 typedef enum {
188 ADJUSTMENT_MODE_STEP,
189 ADJUSTMENT_MODE_SELECT
190 } adjustmentMode_e;
192 typedef struct adjustmentStepConfig_s {
193 uint8_t step;
194 } adjustmentStepConfig_t;
196 typedef struct adjustmentSelectConfig_s {
197 uint8_t switchPositions;
198 } adjustmentSelectConfig_t;
200 typedef union adjustmentConfig_u {
201 adjustmentStepConfig_t stepConfig;
202 adjustmentSelectConfig_t selectConfig;
203 } adjustmentData_t;
205 typedef struct adjustmentConfig_s {
206 uint8_t adjustmentFunction;
207 uint8_t mode;
208 adjustmentData_t data;
209 } adjustmentConfig_t;
211 typedef struct adjustmentRange_s {
212 // when aux channel is in range...
213 uint8_t auxChannelIndex;
214 channelRange_t range;
216 // ..then apply the adjustment function to the auxSwitchChannel ...
217 uint8_t adjustmentFunction;
218 uint8_t auxSwitchChannelIndex;
220 // ... via slot
221 uint8_t adjustmentIndex;
222 } adjustmentRange_t;
224 #define ADJUSTMENT_INDEX_OFFSET 1
226 typedef struct adjustmentState_s {
227 uint8_t auxChannelIndex;
228 const adjustmentConfig_t *config;
229 uint32_t timeoutAt;
230 } adjustmentState_t;
233 #ifndef MAX_SIMULTANEOUS_ADJUSTMENT_COUNT
234 #define MAX_SIMULTANEOUS_ADJUSTMENT_COUNT 4 // enough for 4 x 3position switches / 4 aux channel
235 #endif
237 #define MAX_ADJUSTMENT_RANGE_COUNT 12 // enough for 2 * 6pos switches.
239 void resetAdjustmentStates(void);
240 void configureAdjustment(uint8_t index, uint8_t auxChannelIndex, const adjustmentConfig_t *adjustmentConfig);
241 void updateAdjustmentStates(adjustmentRange_t *adjustmentRanges);
242 void processRcAdjustments(controlRateConfig_t *controlRateConfig, rxConfig_t *rxConfig);
244 bool isUsingSticksForArming(void);
245 bool isUsingNavigationModes(void);
247 int32_t getRcStickDeflection(int32_t axis, uint16_t midrc);
248 bool isModeActivationConditionPresent(modeActivationCondition_t *modeActivationConditions, boxId_e modeId);