Subversion Repositories FlightCtrl

Rev

Rev 2053 | Rev 2057 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2053 Rev 2055
Line 1... Line -...
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
2
// + Copyright (c) 04.2007 Holger Buss
-
 
3
// + Nur für den privaten Gebrauch
-
 
4
// + www.MikroKopter.com
-
 
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
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.
-
 
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
-
 
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
-
 
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
-
 
11
// + Verkauf von Luftbildaufnahmen, usw.
-
 
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
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
-
 
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
-
 
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
-
 
18
// + eindeutig als Ursprung verlinkt werden
-
 
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
-
 
21
// + Benutzung auf eigene Gefahr
-
 
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
-
 
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
-
 
25
// + mit unserer Zustimmung zulässig
-
 
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 example: 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
-
 
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-
 
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>
1
#include <stdlib.h>
53
#include <avr/io.h>
2
#include <avr/io.h>
54
#include "eeprom.h"
3
#include "eeprom.h"
55
#include "flight.h"
4
#include "flight.h"
56
#include "output.h"
5
#include "output.h"
Line 73... Line 22...
73
/*
22
/*
74
 * These are no longer maintained, just left at 0. The original implementation just summed the acc.
23
 * 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???
24
 * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey???
76
 */
25
 */
77
// int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0;
26
// int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0;
78
 
-
 
79
uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control
27
uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control
80
uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control
28
uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control
81
uint8_t invKi = 64;
29
uint8_t invKi;
-
 
30
int32_t IPart[2];
Line 82... Line 31...
82
 
31
 
83
/************************************************************************/
32
/************************************************************************/
84
/*  Filter for motor value smoothing (necessary???)                     */
33
/*  Filter for motor value smoothing (necessary???)                     */
85
/************************************************************************/
34
/************************************************************************/
Line 102... Line 51...
102
  default:
51
  default:
103
    return newvalue;
52
    return newvalue;
104
  }
53
  }
105
}
54
}
Line 106... Line -...
106
 
-
 
107
/************************************************************************/
-
 
108
/*  Neutral Readings                                                    */
-
 
109
/************************************************************************/
-
 
110
void flight_setNeutral() {
-
 
111
  MKFlags |= MKFLAG_CALIBRATE;
-
 
112
  // not really used here any more.
-
 
113
  /*
-
 
114
  dynamicParams.KalmanK = -1;
-
 
115
  dynamicParams.KalmanMaxDrift = 0;
-
 
116
  dynamicParams.KalmanMaxFusion = 32;
-
 
117
  */
-
 
118
  controlMixer_initVariables();
-
 
119
}
-
 
120
 
55
 
121
void setFlightParameters(uint8_t _invKi, uint8_t _gyroPFactor,
56
void flight_setParameters(uint8_t _invKi, uint8_t _gyroPFactor,
122
    uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) {
57
    uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) {
123
  invKi = _invKi;
58
  invKi = _invKi;
124
  gyroPFactor = _gyroPFactor;
59
  gyroPFactor = _gyroPFactor;
125
  gyroIFactor = _gyroIFactor;
60
  gyroIFactor = _gyroIFactor;
126
  yawPFactor = _yawPFactor;
61
  yawPFactor = _yawPFactor;
127
  yawIFactor = _yawIFactor;
62
  yawIFactor = _yawIFactor;
Line 128... Line 63...
128
}
63
}
129
 
64
 
130
void setNormalFlightParameters(void) {
65
void flight_setGround() {
131
  setFlightParameters(
66
  // Just reset all I terms.
132
                      staticParams.IFactor,
-
 
133
                      dynamicParams.gyroP,
-
 
134
                      staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI,
-
 
135
                      dynamicParams.gyroP,
-
 
136
                      staticParams.yawIFactor
67
  IPart[PITCH] = IPart[ROLL] = 0;
Line 137... Line 68...
137
                      );
68
  headingError = 0;
-
 
69
}
-
 
70
 
138
}
71
void flight_takeOff() {
-
 
72
  HC_setGround();
-
 
73
#ifdef USE_MK3MAG
139
 
74
  attitude_resetHeadingToMagnetic();
Line 140... Line 75...
140
void setStableFlightParameters(void) {
75
  compass_setTakeoffHeading(heading);
141
  setFlightParameters(0, 90, 120, 90, 120);
76
#endif
142
}
77
}
143
 
78
 
144
/************************************************************************/
79
/************************************************************************/
145
/*  Main Flight Control                                                 */
80
/*  Main Flight Control                                                 */
146
/************************************************************************/
81
/************************************************************************/
Line 147... Line 82...
147
void flight_control(void) {
82
void flight_control(void) {
148
  uint16_t tmp_int;
83
  int16_t tmp_int;
149
  // Mixer Fractions that are combined for Motor Control
-
 
150
  int16_t yawTerm, throttleTerm, term[2];
-
 
151
 
84
  // Mixer Fractions that are combined for Motor Control
Line 152... Line 85...
152
  // PID controller variables
85
  int16_t yawTerm, throttleTerm, term[2];
153
  int16_t PDPart;
86
 
Line 154... Line 87...
154
  static int32_t IPart[2] = {0, 0};
87
  // PID controller variables
Line 155... Line 88...
155
  static uint16_t emergencyFlightTime;
88
  int16_t PDPart;
Line -... Line 89...
-
 
89
  static int8_t debugDataTimer = 1;
-
 
90
 
-
 
91
  // High resolution motor values for smoothing of PID motor outputs
-
 
92
  static int16_t motorFilters[MAX_MOTORS];
-
 
93
 
-
 
94
  uint8_t i, axis;
-
 
95
 
-
 
96
  throttleTerm = controls[CONTROL_THROTTLE];
-
 
97
 
-
 
98
  if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) {
-
 
99
    // increment flight-time counter until overflow.
-
 
100
    if (isFlying != 0xFFFF)
-
 
101
      isFlying++;
-
 
102
  }
-
 
103
  /*
-
 
104
   * When standing on the ground, do not apply I controls and zero the yaw stick.
156
  static int8_t debugDataTimer = 1;
105
   * Probably to avoid integration effects that will cause the copter to spin
157
 
106
   * or flip when taking off.
158
  // High resolution motor values for smoothing of PID motor outputs
107
   */
159
  static int16_t motorFilters[MAX_MOTORS];
108
  if (isFlying < 256) {
160
 
109
    flight_setGround();
Line 161... Line -...
161
  uint8_t i, axis;
-
 
162
 
-
 
163
  throttleTerm = controls[CONTROL_THROTTLE];
110
    if (isFlying == 250)
164
 
-
 
165
  // This check removed. Is done on a per-motor basis, after output matrix multiplication.
-
 
166
  if (throttleTerm < staticParams.minThrottle + 10)
-
 
167
    throttleTerm = staticParams.minThrottle + 10;
-
 
168
  else if (throttleTerm > staticParams.maxThrottle - 20)
-
 
169
    throttleTerm = (staticParams.maxThrottle - 20);
-
 
170
 
-
 
171
  /************************************************************************/
111
      flight_takeOff();
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
  /************************************************************************/
-
 
176
 
-
 
177
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
-
 
178
    if (controlMixer_didReceiveSignal) beepRCAlarm();  // Only make alarm if a control signal was received before the signal loss.
-
 
179
    if (emergencyFlightTime) {
-
 
180
      // continue emergency flight
-
 
181
      emergencyFlightTime--;
112
  }
182
      if (isFlying > 256) {
-
 
183
        // We're probably still flying. Descend slowly.
-
 
184
        throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle
-
 
185
        MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing
-
 
186
        setStableFlightParameters();
-
 
187
      } else {
-
 
188
        MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors.
-
 
189
      }
-
 
190
    } else {
-
 
191
      // end emergency flight (just cut the motors???)
-
 
192
      MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT);
-
 
193
    }
-
 
194
  } else {
-
 
Line 195... Line -...
195
    // signal is acceptable
-
 
196
    if (controlMixer_getSignalQuality() > SIGNAL_BAD) {
-
 
197
      // Reset emergency landing control variables.
-
 
198
      MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing
-
 
199
      // The time is in whole seconds.
-
 
200
      if (staticParams.emergencyFlightDuration > (65535-F_MAINLOOP)/F_MAINLOOP)
-
 
201
        emergencyFlightTime = 0xffff;
-
 
202
      else
-
 
203
        emergencyFlightTime = (uint16_t)staticParams.emergencyFlightDuration * F_MAINLOOP;
-
 
204
    }
-
 
205
 
-
 
206
    // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying.
-
 
207
    if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) {
-
 
208
      // increment flight-time counter until overflow.
-
 
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
     */
-
 
217
    if (isFlying < 256) {
-
 
218
      IPart[PITCH] = IPart[ROLL] = 0;
-
 
219
        if (isFlying == 250) {
-
 
220
          HC_setGround();
-
 
221
#ifdef USE_MK3MAG
-
 
222
          attitude_resetHeadingToMagnetic();
-
 
223
          compass_setTakeoffHeading(heading);
-
 
224
#endif
-
 
225
          // Set target heading to the one just gotten off compass.
-
 
226
          // targetHeading = heading;
-
 
227
        }
113
 
228
   } else {
114
  // This check removed. Is done on a per-motor basis, after output matrix multiplication.
229
   // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag?
115
  if (throttleTerm < staticParams.minThrottle + 10)
230
   // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe.
116
    throttleTerm = staticParams.minThrottle + 10;
231
     MKFlags |= (MKFLAG_FLY);
117
  else if (throttleTerm > staticParams.maxThrottle - 20)
232
   }
118
    throttleTerm = (staticParams.maxThrottle - 20);
233
   
119
 
234
    commands_handleCommands();
120
  // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already?
-
 
121
  throttleTerm *= CONTROL_SCALING;
235
    setNormalFlightParameters();
122
  // TODO: We dont need to repeat this for every iteration!
236
  } // end else (not bad signal case)
123
 
237
 
124
// end part 1: 750-800 usec.
238
  // end part 1: 750-800 usec.
125
// start part 3: 350 - 400 usec.
Line 239... Line -...
239
  // start part 3: 350 - 400 usec.
-
 
240
#define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW)
126
#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;
-
 
246
 
127
// This is where control affects the target heading. It also (later) directly controls yaw.
247
  PDPart =  (int32_t)(headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4);
-
 
248
  // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on.
-
 
249
  PDPart += (int32_t)(yawRate * yawPFactor) /  (GYRO_DEG_FACTOR_YAW >> 5);
-
 
250
 
-
 
251
  /*
-
 
Line 252... Line 128...
252
   * Compose throttle term.
128
  headingError -= controls[CONTROL_YAW];
253
   * If a Bl-Ctrl is missing, prevent takeoff.
129
  if (headingError < -YAW_I_LIMIT)
254
   */
130
    headingError = -YAW_I_LIMIT;
255
  if (missingMotor) {
131
  if (headingError > YAW_I_LIMIT)
256
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
132
    headingError = YAW_I_LIMIT;
257
    if (isFlying > 1 && isFlying < 50 && throttleTerm > 0)
133
 
258
      isFlying = 1; // keep within lift off condition
134
  PDPart = (int32_t) (headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4);
259
    throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of
135
// Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on.
260
  }
136
  PDPart += (int32_t) (yawRate * yawPFactor) / (GYRO_DEG_FACTOR_YAW >> 5);
261
 
137
 
262
  // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already?
138
  // Lets not limit P and D.
263
  throttleTerm *= CONTROL_SCALING;
139
// CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT);
264
 
140
 
265
  /*
141
  /*
266
   * Compose yaw term.
142
   * Compose yaw term.
Line 290... Line 166...
290
      yawTerm = MIN_YAWGAS / 2;
166
      yawTerm = MIN_YAWGAS / 2;
291
    }
167
    }
292
  }
168
  }
Line 293... Line 169...
293
 
169
 
-
 
170
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
294
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
171
 
295
  if (yawTerm < -(tmp_int - throttleTerm)) {
172
  if (yawTerm < -(tmp_int - throttleTerm)) {
296
    yawTerm = -(tmp_int - throttleTerm);
173
    yawTerm = -(tmp_int - throttleTerm);
297
    debugOut.digital[0] |= DEBUG_CLIP;
174
    debugOut.digital[0] |= DEBUG_CLIP;
298
  } else if (yawTerm > (tmp_int - throttleTerm)) {
175
  } else if (yawTerm > (tmp_int - throttleTerm)) {
299
    yawTerm = (tmp_int - throttleTerm);
176
    yawTerm = (tmp_int - throttleTerm);
300
    debugOut.digital[0] |= DEBUG_CLIP;
177
    debugOut.digital[0] |= DEBUG_CLIP;
Line 301... Line 178...
301
  }
178
  }
Line 302... Line 179...
302
 
179
 
-
 
180
  debugOut.digital[1] &= ~DEBUG_CLIP;
Line 303... Line 181...
303
  debugOut.digital[1] &= ~DEBUG_CLIP;
181
 
304
 
182
  tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + (abs(yawTerm) >> 1)) >> 6);
305
  tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + abs(yawTerm) / 2)) >> 6;
183
  //tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64;
306
 
184
 
307
  /************************************************************************/
185
  /************************************************************************/
308
  /* Calculate control feedback from angle (gyro integral)                */
186
  /* 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).
187
  /* and angular velocity (gyro signal)                                   */
312
  for (axis = PITCH; axis <= ROLL; axis++) {
188
  /************************************************************************/
313
    int16_t iDiff;
189
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
314
    iDiff = PDPart = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 3);
190
  for (axis = PITCH; axis <= ROLL; axis++) {
315
    PDPart += (int32_t)rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4);
191
    PDPart = (int32_t) rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4);
-
 
192
    PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16;
-
 
193
    // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick.
316
    PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16;
194
    // In HH mode, the I part is summed from P and D of gyros minus stick.
317
    // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick.
195
    if (gyroIFactor) {
318
    // In HH mode, the I part is summed from P and D of gyros minus stick.
196
      int16_t iDiff = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 3);
319
    if (gyroIFactor) {
197
      PDPart += iDiff;
Line 320... Line 198...
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.
198
      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.
321
    } else {
199
    } else {
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.
200
      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.
323
    }
201
    }
324
 
202
 
325
    // With normal Ki, limit effect to +/- 205 (of 1024!!!)
203
    // With normal Ki, limit I parts to +/- 205 (of about 1024)
326
    if (IPart[axis] < -64000) {
204
    if (IPart[axis] < -64000) {
327
      IPart[axis] = -64000;
205
      IPart[axis] = -64000;
Line 328... Line 206...
328
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
206
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
329
    } else if (IPart[axis] > 64000) {
207
    } else if (IPart[axis] > 64000) {
-
 
208
      IPart[axis] = 64000;
330
      IPart[axis] = 64000;
209
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
331
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
210
    }
332
    }
211
 
333
 
212
    term[axis] = PDPart - controls[axis] + (((int32_t) IPart[axis] * invKi) >> 14);
334
    term[axis] = PDPart - controls[axis] + ((int32_t)IPart[axis] * invKi) >> 14;
213
    term[axis] += (dynamicParams.levelCorrection[axis] - 128);
335
        term[axis] += (dynamicParams.levelCorrection[axis] - 128);
214
 
336
        /*
215
    /*
337
     * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!).
216
     * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!).
-
 
217
     * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity
338
     * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity
218
     * (max. pitch or roll term is the throttle value).
339
     * (max. pitch or roll term is the throttle value).
219
     * TODO: Why a growing function of yaw?
-
 
220
     */
340
     * TODO: Why a growing function of yaw?
221
    if (term[axis] < -tmp_int) {
341
     */
222
      debugOut.digital[1] |= DEBUG_CLIP;
Line 342... Line 223...
342
    if (term[axis] < -tmp_int) {
223
      term[axis] = -tmp_int;
343
      debugOut.digital[1] |= DEBUG_CLIP;
224
    } else if (term[axis] > tmp_int) {
344
    } else if (term[axis] > tmp_int) {
225
      debugOut.digital[1] |= DEBUG_CLIP;
345
      debugOut.digital[1] |= DEBUG_CLIP;
226
      term[axis] = tmp_int;
Line -... Line 227...
-
 
227
    }
-
 
228
  }
346
    }
229
 
347
  }
230
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
348
 
231
  // Universal Mixer
349
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
232
  // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING].
350
  // Universal Mixer
233
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
351
  // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING].
234
 
352
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
235
  if (!(--debugDataTimer)) {
-
 
236
    debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz.
353
 
237
    debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg
354
  debugOut.analog[3]  = rate_ATT[PITCH];
238
    debugOut.analog[1] = attitude[ROLL]  / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg
355
  debugOut.analog[4]  = rate_ATT[ROLL];
-
 
356
  debugOut.analog[5]  = yawRate;
239
    debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW;
-
 
240
 
357
 
241
    debugOut.analog[3] = rate_ATT[PITCH];
Line 358... Line 242...
358
  debugOut.analog[6]  = filteredAcc[PITCH];
242
    debugOut.analog[4] = rate_ATT[ROLL];
359
  debugOut.analog[7]  = filteredAcc[ROLL];
243
    debugOut.analog[5] = yawRate;
360
  debugOut.analog[8]  = filteredAcc[Z];
244
  }
Line 361... Line 245...
361
 
245
 
362
  debugOut.analog[13] = term[PITCH];
246
  debugOut.analog[8] = yawTerm;
363
  debugOut.analog[14] = term[ROLL];
247
  debugOut.analog[9] = throttleTerm;
364
  debugOut.analog[15] = yawTerm;
248
 
365
  debugOut.analog[16] = throttleTerm;
249
  debugOut.analog[16] = gyroActivity;
366
 
250
 
367
  for (i = 0; i < MAX_MOTORS; i++) {
251
  for (i = 0; i < MAX_MOTORS; i++) {
368
    int32_t tmp;
252
    int32_t tmp;
Line 385... Line 269...
385
    // --> WRONG. This caused motors to stall completely in tight maneuvers.
269
    // --> WRONG. This caused motors to stall completely in tight maneuvers.
386
    // Apply to individual signals instead.
270
    // Apply to individual signals instead.
387
    CHECK_MIN_MAX(tmp, 1, 255);
271
    CHECK_MIN_MAX(tmp, 1, 255);
388
    throttle = tmp;
272
    throttle = tmp;
Line -... Line 273...
-
 
273
 
389
 
274
    if (i < 4)
Line 390... Line 275...
390
    // if (i < 4) debugOut.analog[22 + i] = throttle;
275
      debugOut.analog[10 + i] = throttle;
391
 
276
 
392
    if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) {
277
    if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) {
393
      motor[i].throttle = throttle;
278
      motor[i].throttle = throttle;
Line 397... Line 282...
397
      motor[i].throttle = 0;
282
      motor[i].throttle = 0;
398
    }
283
    }
399
  }
284
  }
Line 400... Line 285...
400
 
285
 
401
  I2C_Start(TWI_STATE_MOTOR_TX);
-
 
402
 
-
 
403
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
404
  // Debugging
-
 
405
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
406
  if (!(--debugDataTimer)) {
-
 
407
    debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz.
-
 
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;
-
 
411
  }
286
  I2C_Start(TWI_STATE_MOTOR_TX);