before merging master
[inav.git] / src / main / flight / servos.h
blob23ac3fda49512603b17ee20c8540101ed5a110e2
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 "config/parameter_group.h"
21 #include "programming/logic_condition.h"
23 #define MAX_SUPPORTED_SERVOS 18
25 // These must be consecutive
26 typedef enum {
27 INPUT_STABILIZED_ROLL = 0,
28 INPUT_STABILIZED_PITCH = 1,
29 INPUT_STABILIZED_YAW = 2,
30 INPUT_STABILIZED_THROTTLE = 3,
31 INPUT_RC_ROLL = 4,
32 INPUT_RC_PITCH = 5,
33 INPUT_RC_YAW = 6,
34 INPUT_RC_THROTTLE = 7,
35 INPUT_RC_CH5 = 8,
36 INPUT_RC_CH6 = 9,
37 INPUT_RC_CH7 = 10,
38 INPUT_RC_CH8 = 11,
39 INPUT_GIMBAL_PITCH = 12,
40 INPUT_GIMBAL_ROLL = 13,
41 INPUT_FEATURE_FLAPS = 14,
42 INPUT_RC_CH9 = 15,
43 INPUT_RC_CH10 = 16,
44 INPUT_RC_CH11 = 17,
45 INPUT_RC_CH12 = 18,
46 INPUT_RC_CH13 = 19,
47 INPUT_RC_CH14 = 20,
48 INPUT_RC_CH15 = 21,
49 INPUT_RC_CH16 = 22,
50 INPUT_STABILIZED_ROLL_PLUS = 23,
51 INPUT_STABILIZED_ROLL_MINUS = 24,
52 INPUT_STABILIZED_PITCH_PLUS = 25,
53 INPUT_STABILIZED_PITCH_MINUS = 26,
54 INPUT_STABILIZED_YAW_PLUS = 27,
55 INPUT_STABILIZED_YAW_MINUS = 28,
56 INPUT_MAX = 29,
57 INPUT_GVAR_0 = 30,
58 INPUT_GVAR_1 = 31,
59 INPUT_GVAR_2 = 32,
60 INPUT_GVAR_3 = 33,
61 INPUT_GVAR_4 = 34,
62 INPUT_GVAR_5 = 35,
63 INPUT_GVAR_6 = 36,
64 INPUT_GVAR_7 = 37,
65 INPUT_MIXER_TRANSITION = 38,
66 INPUT_HEADTRACKER_PAN = 39,
67 INPUT_HEADTRACKER_TILT = 40,
68 INPUT_HEADTRACKER_ROLL = 41,
69 INPUT_RC_CH17 = 42,
70 INPUT_RC_CH18 = 43,
71 INPUT_RC_CH19 = 44,
72 INPUT_RC_CH20 = 45,
73 INPUT_RC_CH21 = 46,
74 INPUT_RC_CH22 = 47,
75 INPUT_RC_CH23 = 48,
76 INPUT_RC_CH24 = 49,
77 INPUT_RC_CH25 = 50,
78 INPUT_RC_CH26 = 51,
79 INPUT_RC_CH27 = 52,
80 INPUT_RC_CH28 = 53,
81 INPUT_RC_CH29 = 54,
82 INPUT_RC_CH30 = 55,
83 INPUT_RC_CH31 = 56,
84 INPUT_RC_CH32 = 57,
85 INPUT_RC_CH33 = 58,
86 INPUT_RC_CH34 = 59,
87 INPUT_SOURCE_COUNT
88 } inputSource_e;
90 // target servo channels
91 typedef enum {
92 SERVO_GIMBAL_PITCH = 0,
93 SERVO_GIMBAL_ROLL = 1,
94 SERVO_ELEVATOR = 2,
95 SERVO_FLAPPERON_1 = 3,
96 SERVO_FLAPPERON_2 = 4,
97 SERVO_RUDDER = 5,
99 SERVO_BICOPTER_LEFT = 4,
100 SERVO_BICOPTER_RIGHT = 5,
102 SERVO_DUALCOPTER_LEFT = 4,
103 SERVO_DUALCOPTER_RIGHT = 5,
105 SERVO_SINGLECOPTER_1 = 3,
106 SERVO_SINGLECOPTER_2 = 4,
107 SERVO_SINGLECOPTER_3 = 5,
108 SERVO_SINGLECOPTER_4 = 6,
110 } servoIndex_e; // FIXME rename to servoChannel_e
112 #define SERVO_PLANE_INDEX_MIN SERVO_ELEVATOR
113 #define SERVO_PLANE_INDEX_MAX SERVO_RUDDER
115 #define SERVO_DUALCOPTER_INDEX_MIN SERVO_DUALCOPTER_LEFT
116 #define SERVO_DUALCOPTER_INDEX_MAX SERVO_DUALCOPTER_RIGHT
118 #define SERVO_SINGLECOPTER_INDEX_MIN SERVO_SINGLECOPTER_1
119 #define SERVO_SINGLECOPTER_INDEX_MAX SERVO_SINGLECOPTER_4
121 #define SERVO_FLAPPERONS_MIN SERVO_FLAPPERON_1
122 #define SERVO_FLAPPERONS_MAX SERVO_FLAPPERON_2
124 #define FLAPERON_THROW_DEFAULT 200
125 #define FLAPERON_THROW_MIN 50
126 #define FLAPERON_THROW_MAX 450
128 typedef struct servoMixer_s {
129 uint8_t targetChannel; // servo that receives the output of the rule
130 uint8_t inputSource; // input channel for this rule
131 int16_t rate; // range [-1000;+1000] ; can be used to adjust a rate 0-1000% and a direction
132 uint8_t speed; // reduces the speed of the rule, 0=unlimited speed
133 #ifdef USE_PROGRAMMING_FRAMEWORK
134 int8_t conditionId;
135 #endif
136 } servoMixer_t;
138 #define MAX_SERVO_RULES (2 * MAX_SUPPORTED_SERVOS)
139 #define MAX_SERVO_SPEED UINT8_MAX
140 #define SERVO_OUTPUT_MAX 2500
141 #define SERVO_OUTPUT_MIN 500
143 PG_DECLARE_ARRAY(servoMixer_t, MAX_SERVO_RULES, customServoMixers);
145 typedef struct servoParam_s {
146 int16_t min; // servo min
147 int16_t max; // servo max
148 int16_t middle; // servo middle
149 int8_t rate; // range [-125;+125] ; can be used to adjust a rate 0-125% and a direction
150 } servoParam_t;
152 PG_DECLARE_ARRAY(servoParam_t, MAX_SUPPORTED_SERVOS, servoParams);
154 typedef struct servoConfig_s {
155 // PWM values, in milliseconds, common range is 1000-2000 (1ms to 2ms)
156 uint16_t servoCenterPulse; // This is the value for servos when they should be in the middle. e.g. 1500.
157 uint16_t servoPwmRate; // The update rate of servo outputs (50-498Hz)
158 int16_t servo_lowpass_freq; // lowpass servo filter frequency selection; 1/1000ths of loop freq
159 uint16_t flaperon_throw_offset;
160 uint8_t servo_protocol; // See servoProtocolType_e
161 uint8_t tri_unarmed_servo; // send tail servo correction pulses even when unarmed
162 uint8_t servo_autotrim_rotation_limit; // Max rotation for servo midpoints to be updated
163 uint8_t servo_autotrim_iterm_threshold; // How much of the Iterm is carried over to the servo midpoints on each update
164 } servoConfig_t;
166 PG_DECLARE(servoConfig_t, servoConfig);
168 typedef struct servoMetadata_s {
169 float scaleMax;
170 float scaleMin;
171 } servoMetadata_t;
173 extern int16_t servo[MAX_SUPPORTED_SERVOS];
175 void Reset_servoMixers(servoMixer_t* instance);
176 bool isServoOutputEnabled(void);
177 void setServoOutputEnabled(bool flag);
178 bool isMixerUsingServos(void);
179 void writeServos(void);
180 void loadCustomServoMixer(void);
181 void servoMixer(float dT);
182 void servoComputeScalingFactors(uint8_t servoIndex);
183 void servosInit(void);
184 int getServoCount(void);