Update cloud build defines (#14080)
[betaflight.git] / src / main / flight / servos.h
blob1c4b6b3bd6fdac91ec1bd954c2e8e2fb63ed0242
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 #include "pg/pg.h"
24 #include "drivers/io_types.h"
25 #include "drivers/pwm_output.h"
27 #define PWM_SERVO_MIN 500 // minimum servo PWM pulse width which we can set from cli
28 #define PWM_SERVO_MAX 2500 // maximum servo PWM pulse width which we can set from cli
30 #define DEFAULT_SERVO_MIN 1000
31 #define DEFAULT_SERVO_MIDDLE 1500
32 #define DEFAULT_SERVO_MAX 2000
34 // These must be consecutive, see 'reversedSources'
35 enum {
36 INPUT_STABILIZED_ROLL = 0,
37 INPUT_STABILIZED_PITCH,
38 INPUT_STABILIZED_YAW,
39 INPUT_STABILIZED_THROTTLE,
40 INPUT_RC_ROLL,
41 INPUT_RC_PITCH,
42 INPUT_RC_YAW,
43 INPUT_RC_THROTTLE,
44 INPUT_RC_AUX1,
45 INPUT_RC_AUX2,
46 INPUT_RC_AUX3,
47 INPUT_RC_AUX4,
48 INPUT_GIMBAL_PITCH,
49 INPUT_GIMBAL_ROLL,
50 INPUT_SOURCE_COUNT
53 // target servo channels
54 typedef enum {
55 SERVO_GIMBAL_PITCH = 0,
56 SERVO_GIMBAL_ROLL = 1,
57 SERVO_FLAPS = 2,
58 SERVO_FLAPPERON_1 = 3,
59 SERVO_FLAPPERON_2 = 4,
60 SERVO_RUDDER = 5,
61 SERVO_ELEVATOR = 6,
62 SERVO_THROTTLE = 7, // for internal combustion (IC) planes
64 SERVO_BICOPTER_LEFT = 4,
65 SERVO_BICOPTER_RIGHT = 5,
67 SERVO_DUALCOPTER_LEFT = 4,
68 SERVO_DUALCOPTER_RIGHT = 5,
70 SERVO_SINGLECOPTER_1 = 3,
71 SERVO_SINGLECOPTER_2 = 4,
72 SERVO_SINGLECOPTER_3 = 5,
73 SERVO_SINGLECOPTER_4 = 6,
75 SERVO_HELI_LEFT = 0,
76 SERVO_HELI_RIGHT = 1,
77 SERVO_HELI_TOP = 2,
78 SERVO_HELI_RUD = 3
80 } servoIndex_e; // FIXME rename to servoChannel_e
82 #define SERVO_PLANE_INDEX_MIN SERVO_FLAPS
83 #define SERVO_PLANE_INDEX_MAX SERVO_THROTTLE
85 #define SERVO_DUALCOPTER_INDEX_MIN SERVO_DUALCOPTER_LEFT
86 #define SERVO_DUALCOPTER_INDEX_MAX SERVO_DUALCOPTER_RIGHT
88 #define SERVO_SINGLECOPTER_INDEX_MIN SERVO_SINGLECOPTER_1
89 #define SERVO_SINGLECOPTER_INDEX_MAX SERVO_SINGLECOPTER_4
91 #define SERVO_FLAPPERONS_MIN SERVO_FLAPPERON_1
92 #define SERVO_FLAPPERONS_MAX SERVO_FLAPPERON_2
94 #define MAX_SERVO_RULES (2 * MAX_SUPPORTED_SERVOS)
96 typedef struct servoMixer_s {
97 uint8_t targetChannel; // servo that receives the output of the rule
98 uint8_t inputSource; // input channel for this rule
99 int8_t rate; // range [-125;+125] ; can be used to adjust a rate 0-125% and a direction
100 uint8_t speed; // reduces the speed of the rule, 0=unlimited speed
101 int8_t min; // lower bound of rule range [0;100]% of servo max-min
102 int8_t max; // lower bound of rule range [0;100]% of servo max-min
103 uint8_t box; // active rule if box is enabled, range [0;3], 0=no box, 1=BOXSERVO1, 2=BOXSERVO2, 3=BOXSERVO3
104 } servoMixer_t;
106 PG_DECLARE_ARRAY(servoMixer_t, MAX_SERVO_RULES, customServoMixers);
108 #define MAX_SERVO_SPEED UINT8_MAX
109 #define MAX_SERVO_BOXES 3
111 // Custom mixer configuration
112 typedef struct mixerRules_s {
113 uint8_t servoRuleCount;
114 const servoMixer_t *rule;
115 } mixerRules_t;
117 extern const mixerRules_t servoMixers[];
119 typedef struct servoParam_s {
120 uint32_t reversedSources; // the direction of servo movement for each input source of the servo mixer, bit set=inverted
121 int16_t min; // servo min
122 int16_t max; // servo max
123 int16_t middle; // servo middle
124 int8_t rate; // range [-125;+125] ; can be used to adjust a rate 0-125% and a direction
125 int8_t forwardFromChannel; // RX channel index, 0 based. See CHANNEL_FORWARDING_DISABLED
126 } servoParam_t;
128 PG_DECLARE_ARRAY(servoParam_t, MAX_SUPPORTED_SERVOS, servoParams);
130 typedef struct servoConfig_s {
131 servoDevConfig_t dev;
132 uint16_t servo_lowpass_freq; // lowpass servo filter frequency selection; 1/1000ths of loop freq
133 uint8_t tri_unarmed_servo; // send tail servo correction pulses even when unarmed
134 uint8_t channelForwardingStartChannel;
135 } servoConfig_t;
137 PG_DECLARE(servoConfig_t, servoConfig);
139 typedef struct servoProfile_s {
140 servoParam_t servoConf[MAX_SUPPORTED_SERVOS];
141 } servoProfile_t;
143 extern int16_t servo[MAX_SUPPORTED_SERVOS];
145 bool isMixerUsingServos(void);
146 void writeServos(void);
147 void servoMixerLoadMix(int index);
148 void loadCustomServoMixer(void);
149 int servoDirection(int servoIndex, int fromChannel);
150 void servosInit(void);
151 void servosFilterInit(void);
152 void servoMixer(void);
153 // tricopter specific
154 void servosTricopterInit(void);
155 void servosTricopterMixer(void);
156 bool servosTricopterIsEnabledServoUnarmed(void);