Subversion Repositories FlightCtrl

Rev

Rev 2052 | Rev 2055 | 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
2053 - 81
uint8_t invKi = 64;
1612 dongfang 82
 
83
/************************************************************************/
84
/*  Filter for motor value smoothing (necessary???)                     */
85
/************************************************************************/
86
int16_t motorFilter(int16_t newvalue, int16_t oldvalue) {
1988 - 87
  switch (staticParams.motorSmoothing) {
1841 - 88
  case 0:
89
    return newvalue;
90
  case 1:
1872 - 91
    return (oldvalue + newvalue) / 2;
1841 - 92
  case 2:
1872 - 93
    if (newvalue > oldvalue)
94
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
95
    else
1841 - 96
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
97
  case 3:
1872 - 98
    if (newvalue < oldvalue)
99
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
100
    else
1841 - 101
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
1872 - 102
  default:
103
    return newvalue;
1841 - 104
  }
1612 dongfang 105
}
106
 
107
/************************************************************************/
108
/*  Neutral Readings                                                    */
109
/************************************************************************/
110
void flight_setNeutral() {
1841 - 111
  MKFlags |= MKFLAG_CALIBRATE;
112
  // not really used here any more.
1960 - 113
  /*
1841 - 114
  dynamicParams.KalmanK = -1;
115
  dynamicParams.KalmanMaxDrift = 0;
116
  dynamicParams.KalmanMaxFusion = 32;
1960 - 117
  */
1841 - 118
  controlMixer_initVariables();
1612 dongfang 119
}
120
 
2053 - 121
void setFlightParameters(uint8_t _invKi, uint8_t _gyroPFactor,
1872 - 122
    uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) {
2053 - 123
  invKi = _invKi;
1841 - 124
  gyroPFactor = _gyroPFactor;
125
  gyroIFactor = _gyroIFactor;
126
  yawPFactor = _yawPFactor;
127
  yawIFactor = _yawIFactor;
1612 dongfang 128
}
129
 
130
void setNormalFlightParameters(void) {
1956 - 131
  setFlightParameters(
2032 - 132
                      staticParams.IFactor,
1960 - 133
                      dynamicParams.gyroP,
134
                      staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI,
135
                      dynamicParams.gyroP,
2032 - 136
                      staticParams.yawIFactor
1956 - 137
                      );
1612 dongfang 138
}
139
 
140
void setStableFlightParameters(void) {
2053 - 141
  setFlightParameters(0, 90, 120, 90, 120);
1612 dongfang 142
}
143
 
144
/************************************************************************/
145
/*  Main Flight Control                                                 */
146
/************************************************************************/
147
void flight_control(void) {
2053 - 148
  uint16_t tmp_int;
1872 - 149
  // Mixer Fractions that are combined for Motor Control
1841 - 150
  int16_t yawTerm, throttleTerm, term[2];
1612 dongfang 151
 
1841 - 152
  // PID controller variables
2053 - 153
  int16_t PDPart;
2052 - 154
  static int32_t IPart[2] = {0, 0};
1841 - 155
  static uint16_t emergencyFlightTime;
156
  static int8_t debugDataTimer = 1;
1612 dongfang 157
 
1841 - 158
  // High resolution motor values for smoothing of PID motor outputs
159
  static int16_t motorFilters[MAX_MOTORS];
1612 dongfang 160
 
1841 - 161
  uint8_t i, axis;
1612 dongfang 162
 
1908 - 163
  throttleTerm = controls[CONTROL_THROTTLE];
1870 - 164
 
1841 - 165
  // This check removed. Is done on a per-motor basis, after output matrix multiplication.
1960 - 166
  if (throttleTerm < staticParams.minThrottle + 10)
167
    throttleTerm = staticParams.minThrottle + 10;
168
  else if (throttleTerm > staticParams.maxThrottle - 20)
169
    throttleTerm = (staticParams.maxThrottle - 20);
1612 dongfang 170
 
1841 - 171
  /************************************************************************/
172
  /* RC-signal is bad                                                     */
173
  /* This part could be abstracted, as having yet another control input   */
174
  /* to the control mixer: An emergency autopilot control.                */
175
  /************************************************************************/
1775 - 176
 
1872 - 177
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
2052 - 178
    if (controlMixer_didReceiveSignal) beepRCAlarm();  // Only make alarm if a control signal was received before the signal loss.
1872 - 179
    if (emergencyFlightTime) {
1841 - 180
      // continue emergency flight
1872 - 181
      emergencyFlightTime--;
182
      if (isFlying > 256) {
183
        // We're probably still flying. Descend slowly.
1960 - 184
        throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle
185
        MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing
1872 - 186
        setStableFlightParameters();
1841 - 187
      } else {
1872 - 188
        MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors.
1841 - 189
      }
190
    } else {
191
      // end emergency flight (just cut the motors???)
1960 - 192
      MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT);
1841 - 193
    }
1872 - 194
  } else {
1841 - 195
    // signal is acceptable
1872 - 196
    if (controlMixer_getSignalQuality() > SIGNAL_BAD) {
1841 - 197
      // Reset emergency landing control variables.
1960 - 198
      MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing
1841 - 199
      // The time is in whole seconds.
2052 - 200
      if (staticParams.emergencyFlightDuration > (65535-F_MAINLOOP)/F_MAINLOOP)
201
        emergencyFlightTime = 0xffff;
202
      else
203
        emergencyFlightTime = (uint16_t)staticParams.emergencyFlightDuration * F_MAINLOOP;
1841 - 204
    }
1612 dongfang 205
 
1841 - 206
    // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying.
1872 - 207
    if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) {
1841 - 208
      // increment flight-time counter until overflow.
1872 - 209
      if (isFlying != 0xFFFF)
210
        isFlying++;
211
    } else
212
    /*
213
     * When standing on the ground, do not apply I controls and zero the yaw stick.
214
     * Probably to avoid integration effects that will cause the copter to spin
215
     * or flip when taking off.
216
     */
2053 - 217
    if (isFlying < 256) {
218
      IPart[PITCH] = IPart[ROLL] = 0;
1960 - 219
        if (isFlying == 250) {
2052 - 220
          HC_setGround();
221
#ifdef USE_MK3MAG
2048 - 222
          attitude_resetHeadingToMagnetic();
2052 - 223
          compass_setTakeoffHeading(heading);
224
#endif
2048 - 225
          // Set target heading to the one just gotten off compass.
2051 - 226
          // targetHeading = heading;
1960 - 227
        }
2053 - 228
   } else {
229
   // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag?
230
   // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe.
231
     MKFlags |= (MKFLAG_FLY);
232
   }
1960 - 233
 
1872 - 234
    commands_handleCommands();
1841 - 235
    setNormalFlightParameters();
236
  } // end else (not bad signal case)
1960 - 237
 
1841 - 238
  // end part 1: 750-800 usec.
239
  // start part 3: 350 - 400 usec.
2051 - 240
#define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW)
241
  // This is where control affects the target heading. It also (later) directly controls yaw.
242
  headingError -= controls[CONTROL_YAW];
243
  debugOut.analog[28] = headingError / 100;
244
  if (headingError < -YAW_I_LIMIT) headingError = -YAW_I_LIMIT;
245
  if (headingError > YAW_I_LIMIT) headingError = YAW_I_LIMIT;
2048 - 246
 
2053 - 247
  PDPart =  (int32_t)(headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4);
2051 - 248
  // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on.
2053 - 249
  PDPart += (int32_t)(yawRate * yawPFactor) /  (GYRO_DEG_FACTOR_YAW >> 5);
1872 - 250
 
1841 - 251
  /*
252
   * Compose throttle term.
253
   * If a Bl-Ctrl is missing, prevent takeoff.
254
   */
1872 - 255
  if (missingMotor) {
1841 - 256
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
1872 - 257
    if (isFlying > 1 && isFlying < 50 && throttleTerm > 0)
1841 - 258
      isFlying = 1; // keep within lift off condition
1960 - 259
    throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of
1841 - 260
  }
1612 dongfang 261
 
1841 - 262
  // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already?
263
  throttleTerm *= CONTROL_SCALING;
1612 dongfang 264
 
1841 - 265
  /*
266
   * Compose yaw term.
267
   * The yaw term is limited: Absolute value is max. = the throttle term / 2.
268
   * However, at low throttle the yaw term is limited to a fixed value,
269
   * and at high throttle it is limited by the throttle reserve (the difference
270
   * between current throttle and maximum throttle).
271
   */
1645 - 272
#define MIN_YAWGAS (40 * CONTROL_SCALING)  // yaw also below this gas value
2053 - 273
  yawTerm = PDPart - controls[CONTROL_YAW] * CONTROL_SCALING;
1841 - 274
  // Limit yawTerm
1955 - 275
  debugOut.digital[0] &= ~DEBUG_CLIP;
1872 - 276
  if (throttleTerm > MIN_YAWGAS) {
277
    if (yawTerm < -throttleTerm / 2) {
1955 - 278
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 279
      yawTerm = -throttleTerm / 2;
280
    } else if (yawTerm > throttleTerm / 2) {
1955 - 281
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 282
      yawTerm = throttleTerm / 2;
1841 - 283
    }
284
  } else {
1872 - 285
    if (yawTerm < -MIN_YAWGAS / 2) {
1955 - 286
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 287
      yawTerm = -MIN_YAWGAS / 2;
288
    } else if (yawTerm > MIN_YAWGAS / 2) {
1955 - 289
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 290
      yawTerm = MIN_YAWGAS / 2;
1841 - 291
    }
292
  }
1775 - 293
 
1960 - 294
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
1845 - 295
  if (yawTerm < -(tmp_int - throttleTerm)) {
296
    yawTerm = -(tmp_int - throttleTerm);
1955 - 297
    debugOut.digital[0] |= DEBUG_CLIP;
1845 - 298
  } else if (yawTerm > (tmp_int - throttleTerm)) {
299
    yawTerm = (tmp_int - throttleTerm);
1955 - 300
    debugOut.digital[0] |= DEBUG_CLIP;
1841 - 301
  }
1867 - 302
 
1955 - 303
  debugOut.digital[1] &= ~DEBUG_CLIP;
2053 - 304
 
305
  tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + abs(yawTerm) / 2)) >> 6;
306
 
307
  /************************************************************************/
308
  /* Calculate control feedback from angle (gyro integral)                */
309
  /* and angular velocity (gyro signal)                                   */
310
  /************************************************************************/
311
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
1872 - 312
  for (axis = PITCH; axis <= ROLL; axis++) {
2053 - 313
    int16_t iDiff;
314
    iDiff = PDPart = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 3);
315
    PDPart += (int32_t)rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4);
316
    PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16;
317
    // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick.
318
    // In HH mode, the I part is summed from P and D of gyros minus stick.
1872 - 319
    if (gyroIFactor) {
2053 - 320
      IPart[axis] += iDiff - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos.
1841 - 321
    } else {
2053 - 322
      IPart[axis] += PDPart - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos.
1841 - 323
    }
1612 dongfang 324
 
2053 - 325
    // With normal Ki, limit effect to +/- 205 (of 1024!!!)
326
    if (IPart[axis] < -64000) {
327
      IPart[axis] = -64000;
2052 - 328
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
2053 - 329
    } else if (IPart[axis] > 64000) {
330
      IPart[axis] = 64000;
2052 - 331
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
332
    }
333
 
2053 - 334
    term[axis] = PDPart - controls[axis] + ((int32_t)IPart[axis] * invKi) >> 14;
1991 - 335
        term[axis] += (dynamicParams.levelCorrection[axis] - 128);
2051 - 336
        /*
1841 - 337
     * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!).
338
     * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity
339
     * (max. pitch or roll term is the throttle value).
340
     * TODO: Why a growing function of yaw?
341
     */
342
    if (term[axis] < -tmp_int) {
1955 - 343
      debugOut.digital[1] |= DEBUG_CLIP;
1841 - 344
    } else if (term[axis] > tmp_int) {
1955 - 345
      debugOut.digital[1] |= DEBUG_CLIP;
1841 - 346
    }
347
  }
1775 - 348
 
1841 - 349
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
350
  // Universal Mixer
351
  // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING].
352
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1612 dongfang 353
 
1976 - 354
  debugOut.analog[3]  = rate_ATT[PITCH];
355
  debugOut.analog[4]  = rate_ATT[ROLL];
356
  debugOut.analog[5]  = yawRate;
357
 
358
  debugOut.analog[6]  = filteredAcc[PITCH];
359
  debugOut.analog[7]  = filteredAcc[ROLL];
360
  debugOut.analog[8]  = filteredAcc[Z];
361
 
2044 - 362
  debugOut.analog[13] = term[PITCH];
363
  debugOut.analog[14] = term[ROLL];
364
  debugOut.analog[15] = yawTerm;
365
  debugOut.analog[16] = throttleTerm;
1775 - 366
 
1872 - 367
  for (i = 0; i < MAX_MOTORS; i++) {
1874 - 368
    int32_t tmp;
1908 - 369
    uint8_t throttle;
370
 
1960 - 371
    tmp = (int32_t)throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE];
372
    tmp += (int32_t)term[PITCH] * mixerMatrix.motor[i][MIX_PITCH];
373
    tmp += (int32_t)term[ROLL] * mixerMatrix.motor[i][MIX_ROLL];
374
    tmp += (int32_t)yawTerm * mixerMatrix.motor[i][MIX_YAW];
1908 - 375
    tmp = tmp >> 6;
376
    motorFilters[i] = motorFilter(tmp, motorFilters[i]);
377
    // Now we scale back down to a 0..255 range.
378
    tmp = motorFilters[i] / MOTOR_SCALING;
379
 
380
    // So this was the THIRD time a throttle was limited. But should the limitation
381
    // apply to the common throttle signal (the one used for setting the "power" of
382
    // all motors together) or should it limit the throttle set for each motor,
383
    // including mix components of pitch, roll and yaw? I think only the common
384
    // throttle should be limited.
385
    // --> WRONG. This caused motors to stall completely in tight maneuvers.
386
    // Apply to individual signals instead.
387
    CHECK_MIN_MAX(tmp, 1, 255);
388
    throttle = tmp;
389
 
2017 - 390
    // if (i < 4) debugOut.analog[22 + i] = throttle;
1908 - 391
 
1960 - 392
    if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) {
2035 - 393
      motor[i].throttle = throttle;
1872 - 394
    } else if (motorTestActive) {
2035 - 395
      motor[i].throttle = motorTest[i];
1841 - 396
    } else {
2035 - 397
      motor[i].throttle = 0;
1841 - 398
    }
399
  }
1872 - 400
 
1841 - 401
  I2C_Start(TWI_STATE_MOTOR_TX);
1872 - 402
 
1841 - 403
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
404
  // Debugging
405
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1872 - 406
  if (!(--debugDataTimer)) {
1841 - 407
    debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz.
2048 - 408
    debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg
409
    debugOut.analog[1] = attitude[ROLL]  / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg
410
    debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW;
1841 - 411
  }
1612 dongfang 412
}