Subversion Repositories FlightCtrl

Rev

Rev 1956 | Rev 1964 | 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"
1612 dongfang 57
 
58
// Only for debug. Remove.
1645 - 59
//#include "analog.h"
60
//#include "rc.h"
1612 dongfang 61
 
62
// Necessary for external control and motor test
63
#include "uart0.h"
64
#include "twimaster.h"
65
#include "attitude.h"
66
#include "controlMixer.h"
1775 - 67
#include "commands.h"
1612 dongfang 68
#ifdef USE_MK3MAG
69
#include "gps.h"
70
#endif
71
 
72
#define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;}
73
 
74
/*
75
 * These are no longer maintained, just left at 0. The original implementation just summed the acc.
76
 * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey???
77
 */
1645 - 78
// int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0;
1612 dongfang 79
 
1872 - 80
uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control
1612 dongfang 81
uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control
82
 
83
// Some integral weight constant...
84
uint16_t Ki = 10300 / 33;
85
 
86
/************************************************************************/
87
/*  Filter for motor value smoothing (necessary???)                     */
88
/************************************************************************/
89
int16_t motorFilter(int16_t newvalue, int16_t oldvalue) {
1960 - 90
  switch (dynamicParams.motorSmoothing) {
1841 - 91
  case 0:
92
    return newvalue;
93
  case 1:
1872 - 94
    return (oldvalue + newvalue) / 2;
1841 - 95
  case 2:
1872 - 96
    if (newvalue > oldvalue)
97
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
98
    else
1841 - 99
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
100
  case 3:
1872 - 101
    if (newvalue < oldvalue)
102
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
103
    else
1841 - 104
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
1872 - 105
  default:
106
    return newvalue;
1841 - 107
  }
1612 dongfang 108
}
109
 
110
/************************************************************************/
111
/*  Neutral Readings                                                    */
112
/************************************************************************/
113
void flight_setNeutral() {
1841 - 114
  MKFlags |= MKFLAG_CALIBRATE;
115
  // not really used here any more.
1960 - 116
  /*
1841 - 117
  dynamicParams.KalmanK = -1;
118
  dynamicParams.KalmanMaxDrift = 0;
119
  dynamicParams.KalmanMaxFusion = 32;
1960 - 120
  */
1841 - 121
  controlMixer_initVariables();
1612 dongfang 122
}
123
 
1872 - 124
void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor,
125
    uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) {
1841 - 126
  Ki = 10300 / _Ki;
127
  gyroPFactor = _gyroPFactor;
128
  gyroIFactor = _gyroIFactor;
129
  yawPFactor = _yawPFactor;
130
  yawIFactor = _yawIFactor;
1612 dongfang 131
}
132
 
133
void setNormalFlightParameters(void) {
1956 - 134
  setFlightParameters(
135
                      dynamicParams.IFactor,
1960 - 136
                      dynamicParams.gyroP,
137
                      staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI,
138
                      dynamicParams.gyroP,
139
                      dynamicParams.yawIFactor
1956 - 140
                      );
1612 dongfang 141
}
142
 
143
void setStableFlightParameters(void) {
1841 - 144
  setFlightParameters(33, 90, 120, 90, 120);
1612 dongfang 145
}
146
 
147
/************************************************************************/
148
/*  Main Flight Control                                                 */
149
/************************************************************************/
150
void flight_control(void) {
1841 - 151
  int16_t tmp_int;
1872 - 152
  // Mixer Fractions that are combined for Motor Control
1841 - 153
  int16_t yawTerm, throttleTerm, term[2];
1612 dongfang 154
 
1841 - 155
  // PID controller variables
156
  int16_t PDPart[2], PDPartYaw, PPart[2];
1872 - 157
  static int32_t IPart[2] = { 0, 0 };
1841 - 158
  //  static int32_t yawControlRate = 0;
1612 dongfang 159
 
1841 - 160
  // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway.
161
  // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0;
162
  // static int32_t CorrectionPitch, CorrectionRoll;
1612 dongfang 163
 
1841 - 164
  static uint16_t emergencyFlightTime;
165
  static int8_t debugDataTimer = 1;
1612 dongfang 166
 
1841 - 167
  // High resolution motor values for smoothing of PID motor outputs
168
  static int16_t motorFilters[MAX_MOTORS];
1612 dongfang 169
 
1841 - 170
  uint8_t i, axis;
1612 dongfang 171
 
1841 - 172
  // Fire the main flight attitude calculation, including integration of angles.
1870 - 173
  // We want that to kick as early as possible, not to delay new AD sampling further.
1841 - 174
  calculateFlightAttitude();
1870 - 175
  controlMixer_update();
1908 - 176
  throttleTerm = controls[CONTROL_THROTTLE];
1870 - 177
 
1841 - 178
  // This check removed. Is done on a per-motor basis, after output matrix multiplication.
1960 - 179
  if (throttleTerm < staticParams.minThrottle + 10)
180
    throttleTerm = staticParams.minThrottle + 10;
181
  else if (throttleTerm > staticParams.maxThrottle - 20)
182
    throttleTerm = (staticParams.maxThrottle - 20);
1612 dongfang 183
 
1841 - 184
  /************************************************************************/
185
  /* RC-signal is bad                                                     */
186
  /* This part could be abstracted, as having yet another control input   */
187
  /* to the control mixer: An emergency autopilot control.                */
188
  /************************************************************************/
1775 - 189
 
1872 - 190
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
1841 - 191
    RED_ON;
192
    beepRCAlarm();
1872 - 193
 
194
    if (emergencyFlightTime) {
1841 - 195
      // continue emergency flight
1872 - 196
      emergencyFlightTime--;
197
      if (isFlying > 256) {
198
        // We're probably still flying. Descend slowly.
1960 - 199
        throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle
200
        MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing
1872 - 201
        setStableFlightParameters();
1841 - 202
      } else {
1872 - 203
        MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors.
1841 - 204
      }
205
    } else {
206
      // end emergency flight (just cut the motors???)
1960 - 207
      MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT);
1841 - 208
    }
1872 - 209
  } else {
1841 - 210
    // signal is acceptable
1872 - 211
    if (controlMixer_getSignalQuality() > SIGNAL_BAD) {
1841 - 212
      // Reset emergency landing control variables.
1960 - 213
      MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing
1841 - 214
      // The time is in whole seconds.
1960 - 215
      emergencyFlightTime = (uint16_t) staticParams.emergencyFlightDuration * 488;
1841 - 216
    }
1612 dongfang 217
 
1841 - 218
    // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying.
1872 - 219
    if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) {
1841 - 220
      // increment flight-time counter until overflow.
1872 - 221
      if (isFlying != 0xFFFF)
222
        isFlying++;
223
    } else
224
    /*
225
     * When standing on the ground, do not apply I controls and zero the yaw stick.
226
     * Probably to avoid integration effects that will cause the copter to spin
227
     * or flip when taking off.
228
     */
1960 - 229
      if (isFlying < 256) {
230
        IPart[PITCH] = IPart[ROLL] = 0;
231
        // TODO: Don't stomp on other modules' variables!!!
232
        // controlYaw = 0;
233
        PDPartYaw = 0; // instead.
234
        if (isFlying == 250) {
235
          // HC_setGround();
236
          updateCompassCourse = 1;
237
          yawAngleDiff = 0;
238
        }
239
      } else {
240
        // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag?
241
        // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe.
242
        MKFlags |= (MKFLAG_FLY);
1841 - 243
      }
1960 - 244
 
1872 - 245
    commands_handleCommands();
1841 - 246
    setNormalFlightParameters();
247
  } // end else (not bad signal case)
1960 - 248
 
1841 - 249
  /************************************************************************/
250
  /*  Yawing                                                              */
251
  /************************************************************************/
1960 - 252
  if (abs(controls[CONTROL_YAW]) > 4 * staticParams.stickYawP) { // yaw stick is activated
1841 - 253
    ignoreCompassTimer = 1000;
1960 - 254
    if (!(staticParams.bitConfig & CFG_COMPASS_FIX)) {
1841 - 255
      updateCompassCourse = 1;
256
    }
257
  }
1872 - 258
 
1960 - 259
  // yawControlRate = controlYaw;
1841 - 260
  // Trim drift of yawAngleDiff with controlYaw.
261
  // TODO: We want NO feedback of control related stuff to the attitude related stuff.
262
  // This seems to be used as: Difference desired <--> real heading.
1908 - 263
  yawAngleDiff -= controls[CONTROL_YAW];
1872 - 264
 
1841 - 265
  // limit the effect
266
  CHECK_MIN_MAX(yawAngleDiff, -50000, 50000);
1872 - 267
 
1841 - 268
  /************************************************************************/
269
  /* Compass is currently not supported.                                  */
270
  /************************************************************************/
1960 - 271
  if (staticParams.bitConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) {
1841 - 272
    updateCompass();
273
  }
1872 - 274
 
1805 - 275
#if defined (USE_NAVICTRL)
1841 - 276
  /************************************************************************/
277
  /* GPS is currently not supported.                                      */
278
  /************************************************************************/
279
  if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) {
280
    GPS_Main();
281
    MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START);
282
  } else {
283
  }
1612 dongfang 284
#endif
1841 - 285
  // end part 1: 750-800 usec.
286
  // start part 3: 350 - 400 usec.
1645 - 287
#define SENSOR_LIMIT  (4096 * 4)
1872 - 288
  /************************************************************************/
1775 - 289
 
1872 - 290
  /* Calculate control feedback from angle (gyro integral)                */
291
  /* and angular velocity (gyro signal)                                   */
292
  /************************************************************************/
293
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
1868 - 294
 
1872 - 295
  for (axis = PITCH; axis <= ROLL; axis++) {
1960 - 296
    PPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral
297
 
1841 - 298
    /*
299
     * Now blend in the D-part - proportional to the Differential of the integral = the rate.
300
     * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING
301
     * where pfactor is in [0..1].
302
     */
1960 - 303
    PDPart[axis] = PPart[axis] + (int32_t) ((int32_t) rate_PID[axis] * gyroPFactor / (256L / CONTROL_SCALING)) + (differential[axis]
304
                                                                                         * (int16_t) dynamicParams.gyroD) / 16;
305
 
1841 - 306
    CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT);
307
  }
1775 - 308
 
1872 - 309
  PDPartYaw = (int32_t) (yawRate * 2 * (int32_t) yawPFactor) / (256L
310
      / CONTROL_SCALING) + (int32_t) (yawAngleDiff * yawIFactor) / (2 * (44000
311
      / CONTROL_SCALING));
312
 
1841 - 313
  // limit control feedback
314
  CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT);
1872 - 315
 
1841 - 316
  /*
317
   * Compose throttle term.
318
   * If a Bl-Ctrl is missing, prevent takeoff.
319
   */
1872 - 320
  if (missingMotor) {
1841 - 321
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
1872 - 322
    if (isFlying > 1 && isFlying < 50 && throttleTerm > 0)
1841 - 323
      isFlying = 1; // keep within lift off condition
1960 - 324
    throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of
1841 - 325
  }
1612 dongfang 326
 
1841 - 327
  // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already?
328
  throttleTerm *= CONTROL_SCALING;
1612 dongfang 329
 
1841 - 330
  /*
331
   * Compose yaw term.
332
   * The yaw term is limited: Absolute value is max. = the throttle term / 2.
333
   * However, at low throttle the yaw term is limited to a fixed value,
334
   * and at high throttle it is limited by the throttle reserve (the difference
335
   * between current throttle and maximum throttle).
336
   */
1645 - 337
#define MIN_YAWGAS (40 * CONTROL_SCALING)  // yaw also below this gas value
1908 - 338
  yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING;
1841 - 339
  // Limit yawTerm
1955 - 340
  debugOut.digital[0] &= ~DEBUG_CLIP;
1872 - 341
  if (throttleTerm > MIN_YAWGAS) {
342
    if (yawTerm < -throttleTerm / 2) {
1955 - 343
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 344
      yawTerm = -throttleTerm / 2;
345
    } else if (yawTerm > throttleTerm / 2) {
1955 - 346
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 347
      yawTerm = throttleTerm / 2;
1841 - 348
    }
349
    //CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2));
350
  } else {
1872 - 351
    if (yawTerm < -MIN_YAWGAS / 2) {
1955 - 352
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 353
      yawTerm = -MIN_YAWGAS / 2;
354
    } else if (yawTerm > MIN_YAWGAS / 2) {
1955 - 355
      debugOut.digital[0] |= DEBUG_CLIP;
1872 - 356
      yawTerm = MIN_YAWGAS / 2;
1841 - 357
    }
358
    //CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2));
359
  }
1775 - 360
 
1841 - 361
  // FIXME: Throttle may exceed maxThrottle (there is no check no more).
1960 - 362
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
1845 - 363
  if (yawTerm < -(tmp_int - throttleTerm)) {
364
    yawTerm = -(tmp_int - throttleTerm);
1955 - 365
    debugOut.digital[0] |= DEBUG_CLIP;
1845 - 366
  } else if (yawTerm > (tmp_int - throttleTerm)) {
367
    yawTerm = (tmp_int - throttleTerm);
1955 - 368
    debugOut.digital[0] |= DEBUG_CLIP;
1841 - 369
  }
1867 - 370
 
1841 - 371
  // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm));
1955 - 372
  debugOut.digital[1] &= ~DEBUG_CLIP;
1872 - 373
  for (axis = PITCH; axis <= ROLL; axis++) {
1841 - 374
    /*
375
     * Compose pitch and roll terms. This is finally where the sticks come into play.
376
     */
1872 - 377
    if (gyroIFactor) {
1841 - 378
      // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos.
379
      // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time.
380
      // TODO: Find out why this seems to be proportional to stick position - not integrating it at all.
1908 - 381
      IPart[axis] += PPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos.
1841 - 382
    } else {
383
      // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos.
384
      // To keep up with a full stick PDPart should be about 156...
1908 - 385
      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 - 386
    }
1612 dongfang 387
 
1960 - 388
    tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability
1872 - 389
        * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64;
1612 dongfang 390
 
1841 - 391
    // TODO: From which planet comes the 16000?
392
    CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L));
393
    // Add (P, D) parts minus stick pos. to the scaled-down I part.
1908 - 394
    term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch
1775 - 395
 
1841 - 396
    /*
397
     * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!).
398
     * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity
399
     * (max. pitch or roll term is the throttle value).
400
     * TODO: Why a growing function of yaw?
401
     */
402
    if (term[axis] < -tmp_int) {
1955 - 403
      debugOut.digital[1] |= DEBUG_CLIP;
1841 - 404
    } else if (term[axis] > tmp_int) {
1955 - 405
      debugOut.digital[1] |= DEBUG_CLIP;
1841 - 406
    }
407
    CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int);
408
  }
1775 - 409
 
1841 - 410
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
411
  // Universal Mixer
412
  // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING].
413
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1612 dongfang 414
 
1955 - 415
  debugOut.analog[12] = term[PITCH];
416
  debugOut.analog[13] = term[ROLL];
417
  debugOut.analog[14] = yawTerm;
418
  debugOut.analog[15] = throttleTerm;
1775 - 419
 
1872 - 420
  for (i = 0; i < MAX_MOTORS; i++) {
1874 - 421
    int32_t tmp;
1908 - 422
    uint8_t throttle;
423
 
1960 - 424
    tmp = (int32_t)throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE];
425
    tmp += (int32_t)term[PITCH] * mixerMatrix.motor[i][MIX_PITCH];
426
    tmp += (int32_t)term[ROLL] * mixerMatrix.motor[i][MIX_ROLL];
427
    tmp += (int32_t)yawTerm * mixerMatrix.motor[i][MIX_YAW];
1908 - 428
    tmp = tmp >> 6;
429
    motorFilters[i] = motorFilter(tmp, motorFilters[i]);
430
    // Now we scale back down to a 0..255 range.
431
    tmp = motorFilters[i] / MOTOR_SCALING;
432
 
433
    // So this was the THIRD time a throttle was limited. But should the limitation
434
    // apply to the common throttle signal (the one used for setting the "power" of
435
    // all motors together) or should it limit the throttle set for each motor,
436
    // including mix components of pitch, roll and yaw? I think only the common
437
    // throttle should be limited.
438
    // --> WRONG. This caused motors to stall completely in tight maneuvers.
439
    // Apply to individual signals instead.
440
    CHECK_MIN_MAX(tmp, 1, 255);
441
    throttle = tmp;
442
 
1955 - 443
    if (i < 4) debugOut.analog[22 + i] = throttle;
1908 - 444
 
1960 - 445
    if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) {
1908 - 446
      motor[i].SetPoint = throttle;
1872 - 447
    } else if (motorTestActive) {
1841 - 448
      motor[i].SetPoint = motorTest[i];
449
    } else {
450
      motor[i].SetPoint = 0;
451
    }
452
  }
1872 - 453
 
1841 - 454
  I2C_Start(TWI_STATE_MOTOR_TX);
1872 - 455
 
1841 - 456
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
457
  // Debugging
458
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1872 - 459
  if (!(--debugDataTimer)) {
1841 - 460
    debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz.
1955 - 461
    debugOut.analog[0] = (10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg
462
    debugOut.analog[1] = (10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg
463
    debugOut.analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW;
1612 dongfang 464
 
1955 - 465
    debugOut.analog[16] = gyroPFactor;
466
    debugOut.analog[17] = gyroIFactor;
1960 - 467
    debugOut.analog[18] = dynamicParams.gyroD;
1841 - 468
  }
1612 dongfang 469
}