Subversion Repositories FlightCtrl

Rev

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

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