forked from jihlein/BaseFlightPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixer.c
393 lines (302 loc) · 15.7 KB
/
mixer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
June 2012
BaseFlightPlus Rev -
An Open Source STM32 Based Multicopter
Includes code and/or ideas from:
1)AeroQuad
2)BaseFlight
3)CH Robotics
4)MultiWii
5)S.O.H. Madgwick
Designed to run on Naze32 Flight Control Board
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "board.h"
///////////////////////////////////////////////////////////////////////////////
uint8_t numberMotor;
float motor[6] = { 2000.0f, 2000.0f, 2000.0f, 2000.0f, 2000.0f, 2000.0f };
float servo[2] = { 3000.0f, 3000.0f };
///////////////////////////////////////////////////////////////////////////////
// Initialize Mixer
///////////////////////////////////////////////////////////////////////////////
void initMixer(void)
{
// enable servos for mixes that require them. note, this shifts motor counts.
if ( eepromConfig.mixerConfiguration == MIXERTYPE_GIMBAL ||
eepromConfig.mixerConfiguration == MIXERTYPE_BI ||
eepromConfig.mixerConfiguration == MIXERTYPE_TRI ||
eepromConfig.mixerConfiguration == MIXERTYPE_QUADP ||
eepromConfig.mixerConfiguration == MIXERTYPE_QUADX ||
eepromConfig.mixerConfiguration == MIXERTYPE_Y4 ||
eepromConfig.mixerConfiguration == MIXERTYPE_VTAIL4_NO_COMP ||
(eepromConfig.mixerConfiguration == MIXERTYPE_FREEMIX &&
eepromConfig.freeMixMotors < 5) ||
eepromConfig.mixerConfiguration == MIXERTYPE_FLYING_WING ) pwmOutputConfig.useServos = true;
else pwmOutputConfig.useServos = false;
switch (eepromConfig.mixerConfiguration)
{
case MIXERTYPE_GIMBAL:
numberMotor = 0;
break;
case MIXERTYPE_FLYING_WING:
numberMotor = 1;
break;
case MIXERTYPE_BI:
numberMotor = 2;
break;
case MIXERTYPE_TRI:
numberMotor = 3;
break;
case MIXERTYPE_QUADP:
case MIXERTYPE_QUADX:
case MIXERTYPE_VTAIL4_NO_COMP:
case MIXERTYPE_VTAIL4_Y_COMP:
case MIXERTYPE_VTAIL4_RY_COMP:
case MIXERTYPE_VTAIL4_PY_COMP:
case MIXERTYPE_VTAIL4_RP_COMP:
case MIXERTYPE_VTAIL4_RPY_COMP:
case MIXERTYPE_Y4:
numberMotor = 4;
break;
case MIXERTYPE_HEX6P:
case MIXERTYPE_HEX6X:
case MIXERTYPE_Y6:
numberMotor = 6;
break;
case MIXERTYPE_FREEMIX:
numberMotor = eepromConfig.freeMixMotors;
break;
}
}
///////////////////////////////////////////////////////////////////////////////
// Write to Servos
///////////////////////////////////////////////////////////////////////////////
void writeServos(void)
{
if (!pwmOutputConfig.useServos)
return;
pwmWrite(0, (uint16_t)servo[0]);
pwmWrite(1, (uint16_t)servo[1]);
}
///////////////////////////////////////////////////////////////////////////////
// Write to Motors
///////////////////////////////////////////////////////////////////////////////
void writeMotors(void)
{
uint8_t i;
uint8_t offset = 0;
// when servos are enabled, pwm outputs 1 and 2 are for servos only
if (pwmOutputConfig.useServos)
offset = 2;
for (i = 0; i < numberMotor; i++)
pwmWrite(i + offset, (uint16_t)motor[i]);
}
///////////////////////////////////////////////////////////////////////////////
// Write to All Motors
///////////////////////////////////////////////////////////////////////////////
void writeAllMotors(float mc)
{
uint8_t i;
// Sends commands to all motors
for (i = 0; i < numberMotor; i++)
motor[i] = mc;
writeMotors();
}
///////////////////////////////////////////////////////////////////////////////
// Pulse Motors
///////////////////////////////////////////////////////////////////////////////
void pulseMotors(uint8_t quantity)
{
uint8_t i;
for ( i = 0; i < quantity; i++ )
{
writeAllMotors( eepromConfig.minThrottle );
delay(250);
writeAllMotors( (float)MINCOMMAND );
delay(250);
}
}
///////////////////////////////////////////////////////////////////////////////
// Mixer
///////////////////////////////////////////////////////////////////////////////
#define PIDMIX(X,Y,Z) rxCommand[THROTTLE] + axisPID[ROLL] * X + axisPID[PITCH] * Y + eepromConfig.yawDirection * axisPID[YAW] * Z
void mixTable(void)
{
int16_t maxMotor;
uint8_t i;
switch ( eepromConfig.mixerConfiguration )
{
case MIXERTYPE_GIMBAL:
servo[0] = constrain( eepromConfig.gimbalRollServoMid + eepromConfig.gimbalRollServoGain * sensors.attitude200Hz[ROLL] + rxCommand[ROLL],
eepromConfig.gimbalRollServoMin, eepromConfig.gimbalRollServoMax );
servo[1] = constrain( eepromConfig.gimbalPitchServoMid + eepromConfig.gimbalPitchServoGain * sensors.attitude200Hz[PITCH] + rxCommand[PITCH],
eepromConfig.gimbalPitchServoMin, eepromConfig.gimbalPitchServoMax );
break;
///////////////////////////////
case MIXERTYPE_FLYING_WING:
motor[0] = rxCommand[THROTTLE];
if (flightMode != ATTITUDE)
{ // do not use sensors for correction, simple 2 channel mixing
servo[0] = eepromConfig.pitchDirectionLeft * (rxCommand[PITCH] - eepromConfig.midCommand) +
eepromConfig.rollDirectionLeft * (rxCommand[ROLL ] - eepromConfig.midCommand);
servo[1] = eepromConfig.pitchDirectionRight * (rxCommand[PITCH] - eepromConfig.midCommand) +
eepromConfig.rollDirectionRight * (rxCommand[ROLL] - eepromConfig.midCommand);
}
else
{ // use sensors to correct (attitude only)
servo[0] = eepromConfig.pitchDirectionLeft * axisPID[PITCH] +
eepromConfig.rollDirectionLeft * axisPID[ROLL];
servo[1] = eepromConfig.pitchDirectionRight * axisPID[PITCH] +
eepromConfig.rollDirectionRight * axisPID[ROLL];
}
servo[0] = constrain(servo[0] + eepromConfig.midCommand, eepromConfig.wingLeftMinimum,
eepromConfig.wingLeftMaximum);
servo[1] = constrain(servo[1] + eepromConfig.midCommand, eepromConfig.wingRightMinimum,
eepromConfig.wingRightMaximum);
break;
///////////////////////////////
case MIXERTYPE_BI:
motor[0] = PIDMIX( 1.0f, 0.0f, 0.0f ); // Left Motor
motor[1] = PIDMIX( -1.0f, 0.0f, 0.0f ); // Right Motor
servo[0] = constrain( eepromConfig.biLeftServoMid + (eepromConfig.yawDirection * axisPID[YAW]) + axisPID[PITCH],
eepromConfig.biLeftServoMin, eepromConfig.biLeftServoMax ); // Left Servo
servo[1] = constrain( eepromConfig.biRightServoMid + (eepromConfig.yawDirection * axisPID[YAW]) - axisPID[PITCH],
eepromConfig.biRightServoMin, eepromConfig.biRightServoMax ); // Right Servo
break;
///////////////////////////////
case MIXERTYPE_TRI:
motor[0] = PIDMIX( 1.0f, -0.666667f, 0.0f ); // Left CW
motor[1] = PIDMIX( -1.0f, -0.666667f, 0.0f ); // Right CCW
motor[2] = PIDMIX( 0.0f, 1.333333f, 0.0f ); // Rear CW or CCW
servo[0] = constrain( eepromConfig.triYawServoMid + eepromConfig.yawDirection * axisPID[YAW],
eepromConfig.triYawServoMin, eepromConfig.triYawServoMax ); // Tail Servo
break;
///////////////////////////////
case MIXERTYPE_QUADP:
motor[0] = PIDMIX( 0.0f, -1.0f, -1.0f ); // Front CW
motor[1] = PIDMIX( -1.0f, 0.0f, 1.0f ); // Right CCW
motor[2] = PIDMIX( 0.0f, 1.0f, -1.0f ); // Rear CW
motor[3] = PIDMIX( 1.0f, 0.0f, 1.0f ); // Left CCW
break;
///////////////////////////////
case MIXERTYPE_QUADX:
motor[0] = PIDMIX( 1.0f, -1.0f, -1.0f ); // Front Left CW
motor[1] = PIDMIX( -1.0f, -1.0f, 1.0f ); // Front Right CCW
motor[2] = PIDMIX( -1.0f, 1.0f, -1.0f ); // Rear Right CW
motor[3] = PIDMIX( 1.0f, 1.0f, 1.0f ); // Rear Left CCW
break;
///////////////////////////////
case MIXERTYPE_VTAIL4_NO_COMP:
motor[0] = PIDMIX( 1.0f, -1.0f, 0.0f ); // Front Left CCW - NOTE rotation difference for vtail configurations
motor[1] = PIDMIX( -1.0f, -1.0f, 0.0f ); // Front Right CW - NOTE rotation difference for vtail configurations
motor[2] = PIDMIX( 0.0f, 1.0f, 1.0f ); // Rear Right CCW - NOTE rotation difference for vtail configurations
motor[3] = PIDMIX( 0.0f, 1.0f, -1.0f ); // Rear Left CW - NOTE rotation difference for vtail configurations
break;
///////////////////////////////
case MIXERTYPE_VTAIL4_Y_COMP:
motor[0] = PIDMIX( 1.0f, -1.0f, vTailThrust ); // Front Left CCW - NOTE rotation difference for vtail configurations
motor[1] = PIDMIX( -1.0f, -1.0f, -vTailThrust ); // Front Right CW - NOTE rotation difference for vtail configurations
motor[2] = PIDMIX( 0.0f, 1.0f, 1.0f ); // Rear Right CCW - NOTE rotation difference for vtail configurations
motor[3] = PIDMIX( 0.0f, 1.0f, -1.0f ); // Rear Left CW - NOTE rotation difference for vtail configurations
break;
///////////////////////////////
case MIXERTYPE_VTAIL4_RY_COMP:
motor[0] = PIDMIX( 1.0f, -vTailThrust, vTailThrust ); // Front Left CCW - NOTE rotation difference for vtail configurations
motor[1] = PIDMIX( -1.0f, -vTailThrust, -vTailThrust ); // Front Right CW - NOTE rotation difference for vtail configurations
motor[2] = PIDMIX( 0.0f, 1.0f, 1.0f ); // Rear Right CCW - NOTE rotation difference for vtail configurations
motor[3] = PIDMIX( 0.0f, 1.0f, -1.0f ); // Rear Left CW - NOTE rotation difference for vtail configurations
break;
///////////////////////////////
case MIXERTYPE_VTAIL4_PY_COMP:
motor[0] = PIDMIX( vTailThrust, -1.0f, vTailThrust ); // Front Left CCW - NOTE rotation difference for vtail configurations
motor[1] = PIDMIX( -vTailThrust, -1.0f, -vTailThrust ); // Front Right CW - NOTE rotation difference for vtail configurations
motor[2] = PIDMIX( -1.0f, 1.0f, 1.0f ); // Rear Right CCW - NOTE rotation difference for vtail configurations
motor[3] = PIDMIX( 1.0f, 1.0f, -1.0f ); // Rear Left CW - NOTE rotation difference for vtail configurations
break;
///////////////////////////////
case MIXERTYPE_VTAIL4_RP_COMP:
motor[0] = PIDMIX( vTailThrust, -vTailThrust, 0.0f ); // Front Left CCW - NOTE rotation difference for vtail configurations
motor[1] = PIDMIX( -vTailThrust, -vTailThrust, -0.0f ); // Front Right CW - NOTE rotation difference for vtail configurations
motor[2] = PIDMIX( -1.0f, 1.0f, 1.0f ); // Rear Right CCW - NOTE rotation difference for vtail configurations
motor[3] = PIDMIX( 1.0f, 1.0f, -1.0f ); // Rear Left CW - NOTE rotation difference for vtail configurations
break;
///////////////////////////////
case MIXERTYPE_VTAIL4_RPY_COMP:
motor[0] = PIDMIX( vTailThrust, -vTailThrust, vTailThrust ); // Front Left CCW - NOTE rotation difference for vtail configurations
motor[1] = PIDMIX( -vTailThrust, -vTailThrust, -vTailThrust ); // Front Right CW - NOTE rotation difference for vtail configurations
motor[2] = PIDMIX( -1.0f, 1.0f, 1.0f ); // Rear Right CCW - NOTE rotation difference for vtail configurations
motor[3] = PIDMIX( 1.0f, 1.0f, -1.0f ); // Rear Left CW - NOTE rotation difference for vtail configurations
break;
///////////////////////////////
case MIXERTYPE_Y4:
motor[0] = PIDMIX( 1.0f, -1.0f, 0.0f ); // Front Left CW
motor[1] = PIDMIX( -1.0f, -1.0f, 0.0f ); // Front Right CCW
motor[2] = PIDMIX( 0.0f, 1.0f, -1.0f ); // Top Rear CW
motor[3] = PIDMIX( 0.0f, 1.0f, 1.0f ); // Bottom Rear CCW
break;
///////////////////////////////
case MIXERTYPE_HEX6P:
motor[0] = PIDMIX( 0.0f, -0.866025f, -1.0f ); // Front CW
motor[1] = PIDMIX( -1.0f, -0.866025f, 1.0f ); // Front Right CCW
motor[2] = PIDMIX( -1.0f, 0.866025f, -1.0f ); // Rear Right CW
motor[3] = PIDMIX( 0.0f, 0.866025f, 1.0f ); // Rear CCW
motor[4] = PIDMIX( 1.0f, 0.866025f, -1.0f ); // Rear Left CW
motor[5] = PIDMIX( 1.0f, -0.866025f, 1.0f ); // Front Left CCW
break;
///////////////////////////////
case MIXERTYPE_HEX6X:
motor[0] = PIDMIX( 0.866025f, -1.0f, -1.0f ); // Front Left CW
motor[1] = PIDMIX( -0.866025f, -1.0f, 1.0f ); // Front Right CCW
motor[2] = PIDMIX( -0.866025f, 0.0f, -1.0f ); // Right CW
motor[3] = PIDMIX( -0.866025f, 1.0f, 1.0f ); // Rear Right CCW
motor[4] = PIDMIX( 0.866025f, 1.0f, -1.0f ); // Rear Left CW
motor[5] = PIDMIX( 0.866025f, 0.0f, 1.0f ); // Left CCW
break;
///////////////////////////////
case MIXERTYPE_Y6:
motor[0] = PIDMIX( 1.0f, -0.666667, -1.0f ); // Top Left CW
motor[1] = PIDMIX( -1.0f, -0.666667, -1.0f ); // Top Right CW
motor[2] = PIDMIX( 0.0f, 1.333333, 1.0f ); // Top Rear CCW
motor[3] = PIDMIX( 1.0f, -0.666667, 1.0f ); // Bottom Left CCW
motor[4] = PIDMIX( -1.0f, -0.666667, 1.0f ); // Bottom Right CCW
motor[5] = PIDMIX( 0.0f, 1.333333, -1.0f ); // Bottom Rear CW
break;
///////////////////////////////
case MIXERTYPE_FREEMIX:
for ( i = 0; i < numberMotor; i++ )
{
motor[i] = PIDMIX ( eepromConfig.freeMix[i][ROLL ],
eepromConfig.freeMix[i][PITCH],
eepromConfig.freeMix[i][YAW ]);
}
break;
}
///////////////////////////////////
// this is a way to still have good gyro corrections if any motor reaches its max.
maxMotor = motor[0];
for (i = 1; i < numberMotor; i++)
if (motor[i] > maxMotor)
maxMotor = motor[i];
for (i = 0; i < numberMotor; i++)
{
if (maxMotor > eepromConfig.maxThrottle)
motor[i] -= maxMotor - eepromConfig.maxThrottle;
motor[i] = constrain(motor[i], eepromConfig.minThrottle, eepromConfig.maxThrottle);
if ((rxCommand[THROTTLE]) < eepromConfig.minCheck)
{
motor[i] = eepromConfig.minThrottle;
}
if ( armed == false )
motor[i] = (float)MINCOMMAND;
}
}
///////////////////////////////////////////////////////////////////////////////