Subversion Repositories FlightCtrl

Rev

Rev 2051 | Rev 2053 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1868 - 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1612 dongfang 2
// + Copyright (c) 04.2007 Holger Buss
1870 - 3
// + Nur für den privaten Gebrauch
1612 dongfang 4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1870 - 6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
1612 dongfang 8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1870 - 10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
1612 dongfang 11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1870 - 13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
1612 dongfang 15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
1870 - 17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt werden
1612 dongfang 19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1870 - 20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
1612 dongfang 21
// + Benutzung auf eigene Gefahr
1870 - 22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
1612 dongfang 23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1870 - 25
// + mit unserer Zustimmung zulässig
1612 dongfang 26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
1868 - 35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1612 dongfang 36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1870 - 47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1612 dongfang 48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
 
52
#include <stdlib.h>
53
#include <avr/io.h>
54
#include "eeprom.h"
55
#include "flight.h"
1845 - 56
#include "output.h"
2052 - 57
#include "uart0.h"
1612 dongfang 58
 
59
// Necessary for external control and motor test
60
#include "twimaster.h"
61
#include "attitude.h"
62
#include "controlMixer.h"
1775 - 63
#include "commands.h"
2052 - 64
#include "heightControl.h"
1612 dongfang 65
 
2052 - 66
#ifdef USE_MK3MAG
67
#include "mk3mag.h"
68
#include "compassControl.h"
69
#endif
70
 
1612 dongfang 71
#define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;}
72
 
73
/*
74
 * These are no longer maintained, just left at 0. The original implementation just summed the acc.
75
 * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey???
76
 */
1645 - 77
// int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0;
1612 dongfang 78
 
1872 - 79
uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control
1612 dongfang 80
uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control
81
 
82
// Some integral weight constant...
83
uint16_t Ki = 10300 / 33;
84
 
85
/************************************************************************/
86
/*  Filter for motor value smoothing (necessary???)                     */
87
/************************************************************************/
88
int16_t motorFilter(int16_t newvalue, int16_t oldvalue) {
1988 - 89
  switch (staticParams.motorSmoothing) {
1841 - 90
  case 0:
91
    return newvalue;
92
  case 1:
1872 - 93
    return (oldvalue + newvalue) / 2;
1841 - 94
  case 2:
1872 - 95
    if (newvalue > oldvalue)
96
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
97
    else
1841 - 98
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
99
  case 3:
1872 - 100
    if (newvalue < oldvalue)
101
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
102
    else
1841 - 103
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
1872 - 104
  default:
105
    return newvalue;
1841 - 106
  }
1612 dongfang 107
}
108
 
109
/************************************************************************/
110
/*  Neutral Readings                                                    */
111
/************************************************************************/
112
void flight_setNeutral() {
1841 - 113
  MKFlags |= MKFLAG_CALIBRATE;
114
  // not really used here any more.
1960 - 115
  /*
1841 - 116
  dynamicParams.KalmanK = -1;
117
  dynamicParams.KalmanMaxDrift = 0;
118
  dynamicParams.KalmanMaxFusion = 32;
1960 - 119
  */
1841 - 120
  controlMixer_initVariables();
1612 dongfang 121
}
122
 
1872 - 123
void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor,
124
    uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) {
1841 - 125
  Ki = 10300 / _Ki;
126
  gyroPFactor = _gyroPFactor;
127
  gyroIFactor = _gyroIFactor;
128
  yawPFactor = _yawPFactor;
129
  yawIFactor = _yawIFactor;
1612 dongfang 130
}
131
 
132
void setNormalFlightParameters(void) {
1956 - 133
  setFlightParameters(
2032 - 134
                      staticParams.IFactor,
1960 - 135
                      dynamicParams.gyroP,
136
                      staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI,
137
                      dynamicParams.gyroP,
2032 - 138
                      staticParams.yawIFactor
1956 - 139
                      );
1612 dongfang 140
}
141
 
142
void setStableFlightParameters(void) {
1841 - 143
  setFlightParameters(33, 90, 120, 90, 120);
1612 dongfang 144
}
145
 
146
/************************************************************************/
147
/*  Main Flight Control                                                 */
148
/************************************************************************/
149
void flight_control(void) {
1841 - 150
  int16_t tmp_int;
1872 - 151
  // Mixer Fractions that are combined for Motor Control
1841 - 152
  int16_t yawTerm, throttleTerm, term[2];
1612 dongfang 153
 
1841 - 154
  // PID controller variables
2015 - 155
  int16_t PDPart[2],/* DPart[2],*/ PDPartYaw /*, DPartYaw */;
2052 - 156
  static int32_t IPart[2] = {0, 0};
1841 - 157
  static uint16_t emergencyFlightTime;
158
  static int8_t debugDataTimer = 1;
1612 dongfang 159
 
1841 - 160
  // High resolution motor values for smoothing of PID motor outputs
161
  static int16_t motorFilters[MAX_MOTORS];
1612 dongfang 162
 
1841 - 163
  uint8_t i, axis;
1612 dongfang 164
 
1908 - 165
  throttleTerm = controls[CONTROL_THROTTLE];
1870 - 166
 
1841 - 167
  // This check removed. Is done on a per-motor basis, after output matrix multiplication.
1960 - 168
  if (throttleTerm < staticParams.minThrottle + 10)
169
    throttleTerm = staticParams.minThrottle + 10;
170
  else if (throttleTerm > staticParams.maxThrottle - 20)
171
    throttleTerm = (staticParams.maxThrottle - 20);
1612 dongfang 172
 
1841 - 173
  /************************************************************************/
174
  /* RC-signal is bad                                                     */
175
  /* This part could be abstracted, as having yet another control input   */
176
  /* to the control mixer: An emergency autopilot control.                */
177
  /************************************************************************/
1775 - 178
 
1872 - 179
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
2052 - 180
    if (controlMixer_didReceiveSignal) beepRCAlarm();  // Only make alarm if a control signal was received before the signal loss.
1872 - 181
    if (emergencyFlightTime) {
1841 - 182
      // continue emergency flight
1872 - 183
      emergencyFlightTime--;
184
      if (isFlying > 256) {
185
        // We're probably still flying. Descend slowly.
1960 - 186
        throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle
187
        MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing
1872 - 188
        setStableFlightParameters();
1841 - 189
      } else {
1872 - 190
        MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors.
1841 - 191
      }
192
    } else {
193
      // end emergency flight (just cut the motors???)
1960 - 194
      MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT);
1841 - 195
    }
1872 - 196
  } else {
1841 - 197
    // signal is acceptable
1872 - 198
    if (controlMixer_getSignalQuality() > SIGNAL_BAD) {
1841 - 199
      // Reset emergency landing control variables.
1960 - 200
      MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing
1841 - 201
      // The time is in whole seconds.
2052 - 202
      if (staticParams.emergencyFlightDuration > (65535-F_MAINLOOP)/F_MAINLOOP)
203
        emergencyFlightTime = 0xffff;
204
      else
205
        emergencyFlightTime = (uint16_t)staticParams.emergencyFlightDuration * F_MAINLOOP;
1841 - 206
    }
1612 dongfang 207
 
1841 - 208
    // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying.
1872 - 209
    if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) {
1841 - 210
      // increment flight-time counter until overflow.
1872 - 211
      if (isFlying != 0xFFFF)
212
        isFlying++;
213
    } else
214
    /*
215
     * When standing on the ground, do not apply I controls and zero the yaw stick.
216
     * Probably to avoid integration effects that will cause the copter to spin
217
     * or flip when taking off.
218
     */
1960 - 219
      if (isFlying < 256) {
220
        IPart[PITCH] = IPart[ROLL] = 0;
2026 - 221
        PDPartYaw = 0;
1960 - 222
        if (isFlying == 250) {
2052 - 223
          HC_setGround();
224
#ifdef USE_MK3MAG
2048 - 225
          attitude_resetHeadingToMagnetic();
2052 - 226
          compass_setTakeoffHeading(heading);
227
#endif
2048 - 228
          // Set target heading to the one just gotten off compass.
2051 - 229
          // targetHeading = heading;
1960 - 230
        }
231
      } else {
232
        // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag?
233
        // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe.
234
        MKFlags |= (MKFLAG_FLY);
1841 - 235
      }
1960 - 236
 
1872 - 237
    commands_handleCommands();
1841 - 238
    setNormalFlightParameters();
239
  } // end else (not bad signal case)
1960 - 240
 
1805 - 241
#if defined (USE_NAVICTRL)
1841 - 242
  /************************************************************************/
243
  /* GPS is currently not supported.                                      */
244
  /************************************************************************/
2052 - 245
  if(staticParams.GlobalConfig & CFG_GPS_ENABLED) {
1841 - 246
    GPS_Main();
247
    MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START);
248
  } else {
249
  }
1612 dongfang 250
#endif
1841 - 251
  // end part 1: 750-800 usec.
252
  // start part 3: 350 - 400 usec.
1872 - 253
  /************************************************************************/
1775 - 254
 
1872 - 255
  /* Calculate control feedback from angle (gyro integral)                */
256
  /* and angular velocity (gyro signal)                                   */
257
  /************************************************************************/
258
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
259
  for (axis = PITCH; axis <= ROLL; axis++) {
2049 - 260
    PDPart[axis] = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2); // P-Part - Proportional to Integral
261
    PDPart[axis] += (int32_t)rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 5);
2026 - 262
    PDPart[axis] += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16;
2052 - 263
 
264
    //CHECK_MIN_MAX(PDPart[axis], -6L*GYRO_DEG_FACTOR_PITCHROLL, 6L*GYRO_DEG_FACTOR_PITCHROLL);
265
    if (PDPart[axis] < -6L*GYRO_DEG_FACTOR_PITCHROLL) {
266
      PDPart[axis] =- 6L*GYRO_DEG_FACTOR_PITCHROLL;
267
      debugOut.digital[0] |= DEBUG_FLIGHTCLIP;
268
    } else if (PDPart[axis] > 6L*GYRO_DEG_FACTOR_PITCHROLL) {
269
      PDPart[axis] = 6L*GYRO_DEG_FACTOR_PITCHROLL;
270
      debugOut.digital[0] |= DEBUG_FLIGHTCLIP;
271
    }
1841 - 272
  }
1775 - 273
 
2051 - 274
#define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW)
275
  // This is where control affects the target heading. It also (later) directly controls yaw.
276
  headingError -= controls[CONTROL_YAW];
277
  debugOut.analog[28] = headingError / 100;
278
  if (headingError < -YAW_I_LIMIT) headingError = -YAW_I_LIMIT;
279
  if (headingError > YAW_I_LIMIT) headingError = YAW_I_LIMIT;
2048 - 280
 
2051 - 281
  PDPartYaw =  (int32_t)(headingError * yawIFactor) / (GYRO_DEG_FACTOR_PITCHROLL << 3);
282
  // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on.
2049 - 283
  PDPartYaw += (int32_t)(yawRate * yawPFactor) /  (GYRO_DEG_FACTOR_PITCHROLL >> 6);
1872 - 284
 
1841 - 285
  // limit control feedback
1992 - 286
  // CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT);
1872 - 287
 
1841 - 288
  /*
289
   * Compose throttle term.
290
   * If a Bl-Ctrl is missing, prevent takeoff.
291
   */
1872 - 292
  if (missingMotor) {
1841 - 293
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
1872 - 294
    if (isFlying > 1 && isFlying < 50 && throttleTerm > 0)
1841 - 295
      isFlying = 1; // keep within lift off condition
1960 - 296
    throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of
1841 - 297
  }
1612 dongfang 298
 
1841 - 299
  // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already?
300
  throttleTerm *= CONTROL_SCALING;
1612 dongfang 301
 
1841 - 302
  /*
303
   * Compose yaw term.
304
   * The yaw term is limited: Absolute value is max. = the throttle term / 2.
305
   * However, at low throttle the yaw term is limited to a fixed value,
306
   * and at high throttle it is limited by the throttle reserve (the difference
307
   * between current throttle and maximum throttle).
308
   */
1645 - 309
#define MIN_YAWGAS (40 * CONTROL_SCALING)  // yaw also below this gas value
1908 - 310
  yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING;
1841 - 311
  // Limit yawTerm
1955 - 312
  debugOut.digital[0] &= ~DEBUG_CLIP;
1872 - 313
  if (throttleTerm > MIN_YAWGAS) {
314
    if (yawTerm < -throttleTerm / 2) {
1955 - 315
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 316
      yawTerm = -throttleTerm / 2;
317
    } else if (yawTerm > throttleTerm / 2) {
1955 - 318
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 319
      yawTerm = throttleTerm / 2;
1841 - 320
    }
321
  } else {
1872 - 322
    if (yawTerm < -MIN_YAWGAS / 2) {
1955 - 323
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 324
      yawTerm = -MIN_YAWGAS / 2;
325
    } else if (yawTerm > MIN_YAWGAS / 2) {
1955 - 326
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 327
      yawTerm = MIN_YAWGAS / 2;
1841 - 328
    }
329
  }
1775 - 330
 
1960 - 331
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
1845 - 332
  if (yawTerm < -(tmp_int - throttleTerm)) {
333
    yawTerm = -(tmp_int - throttleTerm);
1955 - 334
    debugOut.digital[0] |= DEBUG_CLIP;
1845 - 335
  } else if (yawTerm > (tmp_int - throttleTerm)) {
336
    yawTerm = (tmp_int - throttleTerm);
1955 - 337
    debugOut.digital[0] |= DEBUG_CLIP;
1841 - 338
  }
1867 - 339
 
1841 - 340
  // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm));
1955 - 341
  debugOut.digital[1] &= ~DEBUG_CLIP;
1872 - 342
  for (axis = PITCH; axis <= ROLL; axis++) {
1841 - 343
    /*
344
     * Compose pitch and roll terms. This is finally where the sticks come into play.
345
     */
1872 - 346
    if (gyroIFactor) {
1841 - 347
      // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos.
348
      // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time.
349
      // TODO: Find out why this seems to be proportional to stick position - not integrating it at all.
2015 - 350
      IPart[axis] += PDPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos.
1841 - 351
    } else {
352
      // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos.
353
      // To keep up with a full stick PDPart should be about 156...
1908 - 354
      IPart[axis] += PDPart[axis] - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos.
1841 - 355
    }
1612 dongfang 356
 
1960 - 357
    tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability
1872 - 358
        * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64;
1612 dongfang 359
 
2052 - 360
    //CHECK_MIN_MAX(IPart[axis], -25L*GYRO_DEG_FACTOR_PITCHROLL, 25L*GYRO_DEG_FACTOR_PITCHROLL);
361
    if (IPart[axis] < -25L*GYRO_DEG_FACTOR_PITCHROLL) {
362
      IPart[axis] =- 25L*GYRO_DEG_FACTOR_PITCHROLL;
363
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
364
    } else if (PDPart[axis] > 25L*GYRO_DEG_FACTOR_PITCHROLL) {
365
      PDPart[axis] = 25L*GYRO_DEG_FACTOR_PITCHROLL;
366
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
367
    }
368
 
1841 - 369
    // Add (P, D) parts minus stick pos. to the scaled-down I part.
1908 - 370
    term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch
1991 - 371
        term[axis] += (dynamicParams.levelCorrection[axis] - 128);
2051 - 372
        /*
1841 - 373
     * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!).
374
     * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity
375
     * (max. pitch or roll term is the throttle value).
376
     * TODO: Why a growing function of yaw?
377
     */
378
    if (term[axis] < -tmp_int) {
1955 - 379
      debugOut.digital[1] |= DEBUG_CLIP;
1841 - 380
    } else if (term[axis] > tmp_int) {
1955 - 381
      debugOut.digital[1] |= DEBUG_CLIP;
1841 - 382
    }
383
  }
1775 - 384
 
1841 - 385
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
386
  // Universal Mixer
387
  // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING].
388
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1612 dongfang 389
 
1976 - 390
  debugOut.analog[3]  = rate_ATT[PITCH];
391
  debugOut.analog[4]  = rate_ATT[ROLL];
392
  debugOut.analog[5]  = yawRate;
393
 
394
  debugOut.analog[6]  = filteredAcc[PITCH];
395
  debugOut.analog[7]  = filteredAcc[ROLL];
396
  debugOut.analog[8]  = filteredAcc[Z];
397
 
2044 - 398
  debugOut.analog[13] = term[PITCH];
399
  debugOut.analog[14] = term[ROLL];
400
  debugOut.analog[15] = yawTerm;
401
  debugOut.analog[16] = throttleTerm;
1775 - 402
 
1872 - 403
  for (i = 0; i < MAX_MOTORS; i++) {
1874 - 404
    int32_t tmp;
1908 - 405
    uint8_t throttle;
406
 
1960 - 407
    tmp = (int32_t)throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE];
408
    tmp += (int32_t)term[PITCH] * mixerMatrix.motor[i][MIX_PITCH];
409
    tmp += (int32_t)term[ROLL] * mixerMatrix.motor[i][MIX_ROLL];
410
    tmp += (int32_t)yawTerm * mixerMatrix.motor[i][MIX_YAW];
1908 - 411
    tmp = tmp >> 6;
412
    motorFilters[i] = motorFilter(tmp, motorFilters[i]);
413
    // Now we scale back down to a 0..255 range.
414
    tmp = motorFilters[i] / MOTOR_SCALING;
415
 
416
    // So this was the THIRD time a throttle was limited. But should the limitation
417
    // apply to the common throttle signal (the one used for setting the "power" of
418
    // all motors together) or should it limit the throttle set for each motor,
419
    // including mix components of pitch, roll and yaw? I think only the common
420
    // throttle should be limited.
421
    // --> WRONG. This caused motors to stall completely in tight maneuvers.
422
    // Apply to individual signals instead.
423
    CHECK_MIN_MAX(tmp, 1, 255);
424
    throttle = tmp;
425
 
2017 - 426
    // if (i < 4) debugOut.analog[22 + i] = throttle;
1908 - 427
 
1960 - 428
    if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) {
2035 - 429
      motor[i].throttle = throttle;
1872 - 430
    } else if (motorTestActive) {
2035 - 431
      motor[i].throttle = motorTest[i];
1841 - 432
    } else {
2035 - 433
      motor[i].throttle = 0;
1841 - 434
    }
435
  }
1872 - 436
 
1841 - 437
  I2C_Start(TWI_STATE_MOTOR_TX);
1872 - 438
 
1841 - 439
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
440
  // Debugging
441
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1872 - 442
  if (!(--debugDataTimer)) {
1841 - 443
    debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz.
2048 - 444
    debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg
445
    debugOut.analog[1] = attitude[ROLL]  / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg
446
    debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW;
1841 - 447
  }
1612 dongfang 448
}