Subversion Repositories FlightCtrl

Rev

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