Renaming getRcStickPosition to getRcStickDeflection and moving to
[betaflight.git] / src / main / io / rc_controls.h
blob8441ea90f170f2e100d583b8ca78a108b50cd87e
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 typedef enum {
21 BOXARM = 0,
22 BOXANGLE,
23 BOXHORIZON,
24 BOXBARO,
25 // BOXVARIO,
26 BOXMAG,
27 BOXHEADFREE,
28 BOXHEADADJ,
29 BOXCAMSTAB,
30 BOXCAMTRIG,
31 BOXGPSHOME,
32 BOXGPSHOLD,
33 BOXPASSTHRU,
34 BOXBEEPERON,
35 BOXLEDMAX,
36 BOXLEDLOW,
37 BOXLLIGHTS,
38 BOXCALIB,
39 BOXGOV,
40 BOXOSD,
41 BOXTELEMETRY,
42 BOXAUTOTUNE,
43 BOXSONAR,
44 CHECKBOX_ITEM_COUNT
45 } boxId_e;
47 extern uint32_t rcModeActivationMask;
49 #define IS_RC_MODE_ACTIVE(modeId) ((1 << modeId) & rcModeActivationMask)
50 #define ACTIVATE_RC_MODE(modeId) (rcModeActivationMask |= (1 << modeId))
52 typedef enum rc_alias {
53 ROLL = 0,
54 PITCH,
55 YAW,
56 THROTTLE,
57 AUX1,
58 AUX2,
59 AUX3,
60 AUX4,
61 AUX5,
62 AUX6,
63 AUX7,
64 AUX8
65 } rc_alias_e;
67 typedef enum {
68 THROTTLE_LOW = 0,
69 THROTTLE_HIGH
70 } throttleStatus_e;
72 #define ROL_LO (1 << (2 * ROLL))
73 #define ROL_CE (3 << (2 * ROLL))
74 #define ROL_HI (2 << (2 * ROLL))
75 #define PIT_LO (1 << (2 * PITCH))
76 #define PIT_CE (3 << (2 * PITCH))
77 #define PIT_HI (2 << (2 * PITCH))
78 #define YAW_LO (1 << (2 * YAW))
79 #define YAW_CE (3 << (2 * YAW))
80 #define YAW_HI (2 << (2 * YAW))
81 #define THR_LO (1 << (2 * THROTTLE))
82 #define THR_CE (3 << (2 * THROTTLE))
83 #define THR_HI (2 << (2 * THROTTLE))
85 #define MAX_MODE_ACTIVATION_CONDITION_COUNT 40
86 // 40 is enough for 1 mode for each position of 11 * 3 position switches and a 6 pos switch.
87 // however, that is unlikely because you don't define the 'off' positions, so for a 3 position
88 // switch it's normal that only 2 values would be configured.
89 // this leaves plenty of 'slots' free for cases where you enable multiple modes for a switch
90 // position (like gps rth + horizon + baro + beeper)
92 #define CHANNEL_RANGE_MIN 900
93 #define CHANNEL_RANGE_MAX 2100
95 #define MODE_STEP_TO_CHANNEL_VALUE(step) (CHANNEL_RANGE_MIN + 25 * step)
96 #define CHANNEL_VALUE_TO_STEP(channelValue) ((constrain(channelValue, CHANNEL_RANGE_MIN, CHANNEL_RANGE_MAX) - CHANNEL_RANGE_MIN) / 25)
98 #define MIN_MODE_RANGE_STEP 0
99 #define MAX_MODE_RANGE_STEP ((CHANNEL_RANGE_MAX - CHANNEL_RANGE_MIN) / 25)
101 // steps are 25 apart
102 // a value of 0 corresponds to a channel value of 900 or less
103 // a value of 48 corresponds to a channel value of 2100 or more
104 // 48 steps between 900 and 1200
105 typedef struct channelRange_s {
106 uint8_t startStep;
107 uint8_t endStep;
108 } channelRange_t;
110 typedef struct modeActivationCondition_s {
111 boxId_e modeId;
112 uint8_t auxChannelIndex;
113 channelRange_t range;
114 } modeActivationCondition_t;
116 #define IS_RANGE_USABLE(range) ((range)->startStep < (range)->endStep)
118 typedef struct controlRateConfig_s {
119 uint8_t rcRate8;
120 uint8_t rcExpo8;
121 uint8_t thrMid8;
122 uint8_t thrExpo8;
123 uint8_t rollPitchRate;
124 uint8_t yawRate;
125 uint8_t dynThrPID;
126 uint16_t tpa_breakpoint; // Breakpoint where TPA is activated
127 } controlRateConfig_t;
129 extern int16_t rcCommand[4];
131 typedef struct rcControlsConfig_s {
132 uint8_t deadband; // introduce a deadband around the stick center for pitch and roll axis. Must be greater than zero.
133 uint8_t yaw_deadband; // introduce a deadband around the stick center for yaw axis. Must be greater than zero.
134 uint8_t alt_hold_deadband; // defines the neutral zone of throttle stick during altitude hold, default setting is +/-40
135 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
136 } rcControlsConfig_t;
138 bool areSticksInApModePosition(uint16_t ap_mode);
139 throttleStatus_e calculateThrottleStatus(rxConfig_t *rxConfig, uint16_t deadband3d_throttle);
140 void processRcStickPositions(rxConfig_t *rxConfig, throttleStatus_e throttleStatus, bool retarded_arm, bool disarm_kill_switch);
142 void updateActivatedModes(modeActivationCondition_t *modeActivationConditions);
145 typedef enum {
146 ADJUSTMENT_NONE = 0,
147 ADJUSTMENT_RC_RATE,
148 ADJUSTMENT_RC_EXPO,
149 ADJUSTMENT_THROTTLE_EXPO,
150 ADJUSTMENT_PITCH_ROLL_RATE,
151 ADJUSTMENT_YAW_RATE,
152 ADJUSTMENT_PITCH_ROLL_P,
153 ADJUSTMENT_PITCH_ROLL_I,
154 ADJUSTMENT_PITCH_ROLL_D,
155 ADJUSTMENT_YAW_P,
156 ADJUSTMENT_YAW_I,
157 ADJUSTMENT_YAW_D,
158 ADJUSTMENT_RATE_PROFILE
159 } adjustmentFunction_e;
161 #define ADJUSTMENT_FUNCTION_COUNT 13
163 typedef enum {
164 ADJUSTMENT_MODE_STEP,
165 ADJUSTMENT_MODE_SELECT
166 } adjustmentMode_e;
168 typedef struct adjustmentStepConfig_s {
169 uint8_t step;
170 } adjustmentStepConfig_t;
172 typedef struct adjustmentSelectConfig_s {
173 uint8_t switchPositions;
174 } adjustmentSelectConfig_t;
176 typedef union adjustmentConfig_u {
177 adjustmentStepConfig_t stepConfig;
178 adjustmentSelectConfig_t selectConfig;
179 } adjustmentData_t;
181 typedef struct adjustmentConfig_s {
182 uint8_t adjustmentFunction;
183 uint8_t mode;
184 adjustmentData_t data;
185 } adjustmentConfig_t;
187 typedef struct adjustmentRange_s {
188 // when aux channel is in range...
189 uint8_t auxChannelIndex;
190 channelRange_t range;
192 // ..then apply the adjustment function to the auxSwitchChannel ...
193 uint8_t adjustmentFunction;
194 uint8_t auxSwitchChannelIndex;
196 // ... via slot
197 uint8_t adjustmentIndex;
198 } adjustmentRange_t;
200 #define ADJUSTMENT_INDEX_OFFSET 1
202 typedef struct adjustmentState_s {
203 uint8_t auxChannelIndex;
204 const adjustmentConfig_t *config;
205 uint32_t timeoutAt;
206 } adjustmentState_t;
208 #define MAX_SIMULTANEOUS_ADJUSTMENT_COUNT 4 // enough for 4 x 3position switches / 4 aux channel
210 #define MAX_ADJUSTMENT_RANGE_COUNT 12 // enough for 2 * 6pos switches.
212 void configureAdjustment(uint8_t index, uint8_t auxChannelIndex, const adjustmentConfig_t *adjustmentConfig);
213 void updateAdjustmentStates(adjustmentRange_t *adjustmentRanges);
214 void processRcAdjustments(controlRateConfig_t *controlRateConfig, rxConfig_t *rxConfig);
216 bool isUsingSticksForArming(void);
218 int32_t getRcStickDeflection(int32_t axis, uint16_t midrc);