Rework Fast PWM protocol configuration and timing
[betaflight.git] / src / main / flight / mixer.h
blob50cb3eb1cdf2b6cbeb6e3f664252f2d16e52fb98
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 #define MAX_SUPPORTED_MOTORS 12
21 #define MAX_SUPPORTED_SERVOS 8
22 #define YAW_JUMP_PREVENTION_LIMIT_LOW 80
23 #define YAW_JUMP_PREVENTION_LIMIT_HIGH 500
26 // Note: this is called MultiType/MULTITYPE_* in baseflight.
27 typedef enum mixerMode
29 MIXER_TRI = 1,
30 MIXER_QUADP = 2,
31 MIXER_QUADX = 3,
32 MIXER_BICOPTER = 4,
33 MIXER_GIMBAL = 5,
34 MIXER_Y6 = 6,
35 MIXER_HEX6 = 7,
36 MIXER_FLYING_WING = 8,
37 MIXER_Y4 = 9,
38 MIXER_HEX6X = 10,
39 MIXER_OCTOX8 = 11,
40 MIXER_OCTOFLATP = 12,
41 MIXER_OCTOFLATX = 13,
42 MIXER_AIRPLANE = 14, // airplane / singlecopter / dualcopter (not yet properly supported)
43 MIXER_HELI_120_CCPM = 15,
44 MIXER_HELI_90_DEG = 16,
45 MIXER_VTAIL4 = 17,
46 MIXER_HEX6H = 18,
47 MIXER_PPM_TO_SERVO = 19, // PPM -> servo relay
48 MIXER_DUALCOPTER = 20,
49 MIXER_SINGLECOPTER = 21,
50 MIXER_ATAIL4 = 22,
51 MIXER_CUSTOM = 23,
52 MIXER_CUSTOM_AIRPLANE = 24,
53 MIXER_CUSTOM_TRI = 25,
54 MIXER_QUADX_1234 = 26
55 } mixerMode_e;
57 // Custom mixer data per motor
58 typedef struct motorMixer_s {
59 float throttle;
60 float roll;
61 float pitch;
62 float yaw;
63 } motorMixer_t;
65 // Custom mixer configuration
66 typedef struct mixer_s {
67 uint8_t motorCount;
68 uint8_t useServo;
69 const motorMixer_t *motor;
70 } mixer_t;
72 typedef struct mixerConfig_s {
73 int8_t yaw_motor_direction;
74 uint16_t yaw_jump_prevention_limit; // make limit configurable (original fixed value was 100)
75 #ifdef USE_SERVOS
76 uint8_t tri_unarmed_servo; // send tail servo correction pulses even when unarmed
77 uint16_t servo_lowpass_freq; // lowpass servo filter frequency selection; 1/1000ths of loop freq
78 int8_t servo_lowpass_enable; // enable/disable lowpass filter
79 #endif
80 } mixerConfig_t;
82 typedef struct flight3DConfig_s {
83 uint16_t deadband3d_low; // min 3d value
84 uint16_t deadband3d_high; // max 3d value
85 uint16_t neutral3d; // center 3d value
86 uint16_t deadband3d_throttle; // default throttle deadband from MIDRC
87 } flight3DConfig_t;
89 typedef struct airplaneConfig_s {
90 int8_t fixedwing_althold_dir; // +1 or -1 for pitch/althold gain. later check if need more than just sign
91 } airplaneConfig_t;
93 #define CHANNEL_FORWARDING_DISABLED (uint8_t)0xFF
95 #ifdef USE_SERVOS
97 // These must be consecutive, see 'reversedSources'
98 enum {
99 INPUT_STABILIZED_ROLL = 0,
100 INPUT_STABILIZED_PITCH,
101 INPUT_STABILIZED_YAW,
102 INPUT_STABILIZED_THROTTLE,
103 INPUT_RC_ROLL,
104 INPUT_RC_PITCH,
105 INPUT_RC_YAW,
106 INPUT_RC_THROTTLE,
107 INPUT_RC_AUX1,
108 INPUT_RC_AUX2,
109 INPUT_RC_AUX3,
110 INPUT_RC_AUX4,
111 INPUT_GIMBAL_PITCH,
112 INPUT_GIMBAL_ROLL,
114 INPUT_SOURCE_COUNT
115 } inputSource_e;
117 // target servo channels
118 typedef enum {
119 SERVO_GIMBAL_PITCH = 0,
120 SERVO_GIMBAL_ROLL = 1,
121 SERVO_FLAPS = 2,
122 SERVO_FLAPPERON_1 = 3,
123 SERVO_FLAPPERON_2 = 4,
124 SERVO_RUDDER = 5,
125 SERVO_ELEVATOR = 6,
126 SERVO_THROTTLE = 7, // for internal combustion (IC) planes
128 SERVO_BICOPTER_LEFT = 4,
129 SERVO_BICOPTER_RIGHT = 5,
131 SERVO_DUALCOPTER_LEFT = 4,
132 SERVO_DUALCOPTER_RIGHT = 5,
134 SERVO_SINGLECOPTER_1 = 3,
135 SERVO_SINGLECOPTER_2 = 4,
136 SERVO_SINGLECOPTER_3 = 5,
137 SERVO_SINGLECOPTER_4 = 6,
139 } servoIndex_e; // FIXME rename to servoChannel_e
141 #define SERVO_PLANE_INDEX_MIN SERVO_FLAPS
142 #define SERVO_PLANE_INDEX_MAX SERVO_THROTTLE
144 #define SERVO_DUALCOPTER_INDEX_MIN SERVO_DUALCOPTER_LEFT
145 #define SERVO_DUALCOPTER_INDEX_MAX SERVO_DUALCOPTER_RIGHT
147 #define SERVO_SINGLECOPTER_INDEX_MIN SERVO_SINGLECOPTER_1
148 #define SERVO_SINGLECOPTER_INDEX_MAX SERVO_SINGLECOPTER_4
150 #define SERVO_FLAPPERONS_MIN SERVO_FLAPPERON_1
151 #define SERVO_FLAPPERONS_MAX SERVO_FLAPPERON_2
153 typedef struct servoMixer_s {
154 uint8_t targetChannel; // servo that receives the output of the rule
155 uint8_t inputSource; // input channel for this rule
156 int8_t rate; // range [-125;+125] ; can be used to adjust a rate 0-125% and a direction
157 uint8_t speed; // reduces the speed of the rule, 0=unlimited speed
158 int8_t min; // lower bound of rule range [0;100]% of servo max-min
159 int8_t max; // lower bound of rule range [0;100]% of servo max-min
160 uint8_t box; // active rule if box is enabled, range [0;3], 0=no box, 1=BOXSERVO1, 2=BOXSERVO2, 3=BOXSERVO3
161 } servoMixer_t;
163 #define MAX_SERVO_RULES (2 * MAX_SUPPORTED_SERVOS)
164 #define MAX_SERVO_SPEED UINT8_MAX
165 #define MAX_SERVO_BOXES 3
167 // Custom mixer configuration
168 typedef struct mixerRules_s {
169 uint8_t servoRuleCount;
170 const servoMixer_t *rule;
171 } mixerRules_t;
173 typedef struct servoParam_s {
174 int16_t min; // servo min
175 int16_t max; // servo max
176 int16_t middle; // servo middle
177 int8_t rate; // range [-125;+125] ; can be used to adjust a rate 0-125% and a direction
178 uint8_t angleAtMin; // range [0;180] the measured angle in degrees from the middle when the servo is at the 'min' value.
179 uint8_t angleAtMax; // range [0;180] the measured angle in degrees from the middle when the servo is at the 'max' value.
180 int8_t forwardFromChannel; // RX channel index, 0 based. See CHANNEL_FORWARDING_DISABLED
181 uint32_t reversedSources; // the direction of servo movement for each input source of the servo mixer, bit set=inverted
182 } __attribute__ ((__packed__)) servoParam_t;
184 struct gimbalConfig_s;
185 struct escAndServoConfig_s;
186 struct rxConfig_s;
188 extern int16_t servo[MAX_SUPPORTED_SERVOS];
189 bool isMixerUsingServos(void);
190 void writeServos(void);
191 void filterServos(void);
192 bool motorLimitReached;
193 #endif
195 extern int16_t motor[MAX_SUPPORTED_MOTORS];
196 extern int16_t motor_disarmed[MAX_SUPPORTED_MOTORS];
198 struct escAndServoConfig_s;
199 struct rxConfig_s;
201 void mixerUseConfigs(
202 #ifdef USE_SERVOS
203 servoParam_t *servoConfToUse,
204 struct gimbalConfig_s *gimbalConfigToUse,
205 #endif
206 flight3DConfig_t *flight3DConfigToUse,
207 struct escAndServoConfig_s *escAndServoConfigToUse,
208 mixerConfig_t *mixerConfigToUse,
209 airplaneConfig_t *airplaneConfigToUse,
210 struct rxConfig_s *rxConfigToUse);
212 void writeAllMotors(int16_t mc);
213 void mixerLoadMix(int index, motorMixer_t *customMixers);
214 #ifdef USE_SERVOS
215 void servoMixerLoadMix(int index, servoMixer_t *customServoMixers);
216 void loadCustomServoMixer(void);
217 int servoDirection(int servoIndex, int fromChannel);
218 #endif
219 void mixerResetDisarmedMotors(void);
220 void mixTable(void);
221 void writeMotors(uint8_t unsyncedPwm);
222 void stopMotors(void);
223 void StopPwmAllMotors(void);