Subversion Repositories FlightCtrl

Rev

Rev 2051 | Rev 2053 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2051 Rev 2052
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
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.
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
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
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
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
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"
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt werden
18
// + eindeutig als Ursprung verlinkt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
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
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
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
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.
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
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
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"
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
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
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)
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
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
 
51
 
52
#include <stdlib.h>
52
#include <stdlib.h>
53
#include <avr/io.h>
53
#include <avr/io.h>
54
#include "eeprom.h"
54
#include "eeprom.h"
55
#include "flight.h"
55
#include "flight.h"
56
#include "output.h"
56
#include "output.h"
-
 
57
#include "uart0.h"
57
 
58
 
58
// Necessary for external control and motor test
-
 
59
#include "uart0.h"
59
// Necessary for external control and motor test
60
#include "twimaster.h"
60
#include "twimaster.h"
61
#include "attitude.h"
61
#include "attitude.h"
62
#include "controlMixer.h"
62
#include "controlMixer.h"
63
#include "commands.h"
63
#include "commands.h"
-
 
64
#include "heightControl.h"
-
 
65
 
-
 
66
#ifdef USE_MK3MAG
-
 
67
#include "mk3mag.h"
-
 
68
#include "compassControl.h"
-
 
69
#endif
64
 
70
 
65
#define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;}
71
#define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;}
66
 
72
 
67
/*
73
/*
68
 * These are no longer maintained, just left at 0. The original implementation just summed the acc.
74
 * These are no longer maintained, just left at 0. The original implementation just summed the acc.
69
 * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey???
75
 * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey???
70
 */
76
 */
71
// int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0;
77
// int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0;
72
 
78
 
73
uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control
79
uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control
74
uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control
80
uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control
75
 
81
 
76
// Some integral weight constant...
82
// Some integral weight constant...
77
uint16_t Ki = 10300 / 33;
83
uint16_t Ki = 10300 / 33;
78
 
84
 
79
/************************************************************************/
85
/************************************************************************/
80
/*  Filter for motor value smoothing (necessary???)                     */
86
/*  Filter for motor value smoothing (necessary???)                     */
81
/************************************************************************/
87
/************************************************************************/
82
int16_t motorFilter(int16_t newvalue, int16_t oldvalue) {
88
int16_t motorFilter(int16_t newvalue, int16_t oldvalue) {
83
  switch (staticParams.motorSmoothing) {
89
  switch (staticParams.motorSmoothing) {
84
  case 0:
90
  case 0:
85
    return newvalue;
91
    return newvalue;
86
  case 1:
92
  case 1:
87
    return (oldvalue + newvalue) / 2;
93
    return (oldvalue + newvalue) / 2;
88
  case 2:
94
  case 2:
89
    if (newvalue > oldvalue)
95
    if (newvalue > oldvalue)
90
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
96
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
91
    else
97
    else
92
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
98
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
93
  case 3:
99
  case 3:
94
    if (newvalue < oldvalue)
100
    if (newvalue < oldvalue)
95
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
101
      return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new
96
    else
102
    else
97
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
103
      return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old
98
  default:
104
  default:
99
    return newvalue;
105
    return newvalue;
100
  }
106
  }
101
}
107
}
102
 
108
 
103
/************************************************************************/
109
/************************************************************************/
104
/*  Neutral Readings                                                    */
110
/*  Neutral Readings                                                    */
105
/************************************************************************/
111
/************************************************************************/
106
void flight_setNeutral() {
112
void flight_setNeutral() {
107
  MKFlags |= MKFLAG_CALIBRATE;
113
  MKFlags |= MKFLAG_CALIBRATE;
108
  // not really used here any more.
114
  // not really used here any more.
109
  /*
115
  /*
110
  dynamicParams.KalmanK = -1;
116
  dynamicParams.KalmanK = -1;
111
  dynamicParams.KalmanMaxDrift = 0;
117
  dynamicParams.KalmanMaxDrift = 0;
112
  dynamicParams.KalmanMaxFusion = 32;
118
  dynamicParams.KalmanMaxFusion = 32;
113
  */
119
  */
114
  controlMixer_initVariables();
120
  controlMixer_initVariables();
115
}
121
}
116
 
122
 
117
void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor,
123
void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor,
118
    uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) {
124
    uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) {
119
  Ki = 10300 / _Ki;
125
  Ki = 10300 / _Ki;
120
  gyroPFactor = _gyroPFactor;
126
  gyroPFactor = _gyroPFactor;
121
  gyroIFactor = _gyroIFactor;
127
  gyroIFactor = _gyroIFactor;
122
  yawPFactor = _yawPFactor;
128
  yawPFactor = _yawPFactor;
123
  yawIFactor = _yawIFactor;
129
  yawIFactor = _yawIFactor;
124
}
130
}
125
 
131
 
126
void setNormalFlightParameters(void) {
132
void setNormalFlightParameters(void) {
127
  setFlightParameters(
133
  setFlightParameters(
128
                      staticParams.IFactor,
134
                      staticParams.IFactor,
129
                      dynamicParams.gyroP,
135
                      dynamicParams.gyroP,
130
                      staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI,
136
                      staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI,
131
                      dynamicParams.gyroP,
137
                      dynamicParams.gyroP,
132
                      staticParams.yawIFactor
138
                      staticParams.yawIFactor
133
                      );
139
                      );
134
}
140
}
135
 
141
 
136
void setStableFlightParameters(void) {
142
void setStableFlightParameters(void) {
137
  setFlightParameters(33, 90, 120, 90, 120);
143
  setFlightParameters(33, 90, 120, 90, 120);
138
}
144
}
139
 
145
 
140
/************************************************************************/
146
/************************************************************************/
141
/*  Main Flight Control                                                 */
147
/*  Main Flight Control                                                 */
142
/************************************************************************/
148
/************************************************************************/
143
void flight_control(void) {
149
void flight_control(void) {
144
  int16_t tmp_int;
150
  int16_t tmp_int;
145
  // Mixer Fractions that are combined for Motor Control
151
  // Mixer Fractions that are combined for Motor Control
146
  int16_t yawTerm, throttleTerm, term[2];
152
  int16_t yawTerm, throttleTerm, term[2];
147
 
153
 
148
  // PID controller variables
154
  // PID controller variables
149
  int16_t PDPart[2],/* DPart[2],*/ PDPartYaw /*, DPartYaw */;
155
  int16_t PDPart[2],/* DPart[2],*/ PDPartYaw /*, DPartYaw */;
150
  static int32_t IPart[2] = { 0, 0 };
156
  static int32_t IPart[2] = {0, 0};
151
  static uint16_t emergencyFlightTime;
157
  static uint16_t emergencyFlightTime;
152
  static int8_t debugDataTimer = 1;
158
  static int8_t debugDataTimer = 1;
153
 
159
 
154
  // High resolution motor values for smoothing of PID motor outputs
160
  // High resolution motor values for smoothing of PID motor outputs
155
  static int16_t motorFilters[MAX_MOTORS];
161
  static int16_t motorFilters[MAX_MOTORS];
156
 
162
 
157
  uint8_t i, axis;
163
  uint8_t i, axis;
158
 
164
 
159
  throttleTerm = controls[CONTROL_THROTTLE];
165
  throttleTerm = controls[CONTROL_THROTTLE];
160
 
166
 
161
  // This check removed. Is done on a per-motor basis, after output matrix multiplication.
167
  // This check removed. Is done on a per-motor basis, after output matrix multiplication.
162
  if (throttleTerm < staticParams.minThrottle + 10)
168
  if (throttleTerm < staticParams.minThrottle + 10)
163
    throttleTerm = staticParams.minThrottle + 10;
169
    throttleTerm = staticParams.minThrottle + 10;
164
  else if (throttleTerm > staticParams.maxThrottle - 20)
170
  else if (throttleTerm > staticParams.maxThrottle - 20)
165
    throttleTerm = (staticParams.maxThrottle - 20);
171
    throttleTerm = (staticParams.maxThrottle - 20);
166
 
172
 
167
  /************************************************************************/
173
  /************************************************************************/
168
  /* RC-signal is bad                                                     */
174
  /* RC-signal is bad                                                     */
169
  /* This part could be abstracted, as having yet another control input   */
175
  /* This part could be abstracted, as having yet another control input   */
170
  /* to the control mixer: An emergency autopilot control.                */
176
  /* to the control mixer: An emergency autopilot control.                */
171
  /************************************************************************/
177
  /************************************************************************/
172
 
178
 
173
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
179
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
174
    if (controlMixer_didReceiveSignal) beepRCAlarm();
180
    if (controlMixer_didReceiveSignal) beepRCAlarm();  // Only make alarm if a control signal was received before the signal loss.
175
    if (emergencyFlightTime) {
181
    if (emergencyFlightTime) {
176
      // continue emergency flight
182
      // continue emergency flight
177
      emergencyFlightTime--;
183
      emergencyFlightTime--;
178
      if (isFlying > 256) {
184
      if (isFlying > 256) {
179
        // We're probably still flying. Descend slowly.
185
        // We're probably still flying. Descend slowly.
180
        throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle
186
        throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle
181
        MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing
187
        MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing
182
        setStableFlightParameters();
188
        setStableFlightParameters();
183
      } else {
189
      } else {
184
        MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors.
190
        MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors.
185
      }
191
      }
186
    } else {
192
    } else {
187
      // end emergency flight (just cut the motors???)
193
      // end emergency flight (just cut the motors???)
188
      MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT);
194
      MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT);
189
    }
195
    }
190
  } else {
196
  } else {
191
    // signal is acceptable
197
    // signal is acceptable
192
    if (controlMixer_getSignalQuality() > SIGNAL_BAD) {
198
    if (controlMixer_getSignalQuality() > SIGNAL_BAD) {
193
      // Reset emergency landing control variables.
199
      // Reset emergency landing control variables.
194
      MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing
200
      MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing
195
      // The time is in whole seconds.
201
      // The time is in whole seconds.
-
 
202
      if (staticParams.emergencyFlightDuration > (65535-F_MAINLOOP)/F_MAINLOOP)
-
 
203
        emergencyFlightTime = 0xffff;
-
 
204
      else
196
      emergencyFlightTime = (uint16_t) staticParams.emergencyFlightDuration * 488;
205
        emergencyFlightTime = (uint16_t)staticParams.emergencyFlightDuration * F_MAINLOOP;
197
    }
206
    }
198
 
207
 
199
    // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying.
208
    // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying.
200
    if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) {
209
    if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) {
201
      // increment flight-time counter until overflow.
210
      // increment flight-time counter until overflow.
202
      if (isFlying != 0xFFFF)
211
      if (isFlying != 0xFFFF)
203
        isFlying++;
212
        isFlying++;
204
    } else
213
    } else
205
    /*
214
    /*
206
     * When standing on the ground, do not apply I controls and zero the yaw stick.
215
     * When standing on the ground, do not apply I controls and zero the yaw stick.
207
     * Probably to avoid integration effects that will cause the copter to spin
216
     * Probably to avoid integration effects that will cause the copter to spin
208
     * or flip when taking off.
217
     * or flip when taking off.
209
     */
218
     */
210
      if (isFlying < 256) {
219
      if (isFlying < 256) {
211
        IPart[PITCH] = IPart[ROLL] = 0;
220
        IPart[PITCH] = IPart[ROLL] = 0;
212
        PDPartYaw = 0;
221
        PDPartYaw = 0;
213
        if (isFlying == 250) {
222
        if (isFlying == 250) {
214
          // HC_setGround();
223
          HC_setGround();
-
 
224
#ifdef USE_MK3MAG
215
          attitude_resetHeadingToMagnetic();
225
          attitude_resetHeadingToMagnetic();
-
 
226
          compass_setTakeoffHeading(heading);
-
 
227
#endif
216
          // Set target heading to the one just gotten off compass.
228
          // Set target heading to the one just gotten off compass.
217
          // targetHeading = heading;
229
          // targetHeading = heading;
218
        }
230
        }
219
      } else {
231
      } else {
220
        // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag?
232
        // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag?
221
        // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe.
233
        // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe.
222
        MKFlags |= (MKFLAG_FLY);
234
        MKFlags |= (MKFLAG_FLY);
223
      }
235
      }
224
   
236
   
225
    commands_handleCommands();
237
    commands_handleCommands();
226
    setNormalFlightParameters();
238
    setNormalFlightParameters();
227
  } // end else (not bad signal case)
239
  } // end else (not bad signal case)
228
 
-
 
229
  /************************************************************************/
-
 
230
  /*  Yawing                                                              */
-
 
231
  /************************************************************************/
-
 
232
  /*
-
 
233
  if (abs(controls[CONTROL_YAW]) > 4 * staticParams.stickYawP) { // yaw stick is activated
-
 
234
    ignoreCompassTimer = 1000;
-
 
235
    if (!(staticParams.bitConfig & CFG_COMPASS_FIX)) {
-
 
236
      //targetHeading = heading;
-
 
237
      // YGBSM!!!
-
 
238
    }
-
 
239
  }
-
 
240
  */
-
 
241
 
240
 
242
#if defined (USE_NAVICTRL)
241
#if defined (USE_NAVICTRL)
243
  /************************************************************************/
242
  /************************************************************************/
244
  /* GPS is currently not supported.                                      */
243
  /* GPS is currently not supported.                                      */
245
  /************************************************************************/
244
  /************************************************************************/
246
  if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) {
245
  if(staticParams.GlobalConfig & CFG_GPS_ENABLED) {
247
    GPS_Main();
246
    GPS_Main();
248
    MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START);
247
    MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START);
249
  } else {
248
  } else {
250
  }
249
  }
251
#endif
250
#endif
252
  // end part 1: 750-800 usec.
251
  // end part 1: 750-800 usec.
253
  // start part 3: 350 - 400 usec.
252
  // start part 3: 350 - 400 usec.
254
  /************************************************************************/
253
  /************************************************************************/
255
 
254
 
256
  /* Calculate control feedback from angle (gyro integral)                */
255
  /* Calculate control feedback from angle (gyro integral)                */
257
  /* and angular velocity (gyro signal)                                   */
256
  /* and angular velocity (gyro signal)                                   */
258
  /************************************************************************/
257
  /************************************************************************/
259
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
258
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
260
  for (axis = PITCH; axis <= ROLL; axis++) {
259
  for (axis = PITCH; axis <= ROLL; axis++) {
261
    PDPart[axis] = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2); // P-Part - Proportional to Integral
260
    PDPart[axis] = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2); // P-Part - Proportional to Integral
262
    PDPart[axis] += (int32_t)rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 5);
261
    PDPart[axis] += (int32_t)rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 5);
263
    PDPart[axis] += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16;
262
    PDPart[axis] += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16;
-
 
263
 
264
    CHECK_MIN_MAX(PDPart[axis], -6L*GYRO_DEG_FACTOR_PITCHROLL, 6L*GYRO_DEG_FACTOR_PITCHROLL);
264
    //CHECK_MIN_MAX(PDPart[axis], -6L*GYRO_DEG_FACTOR_PITCHROLL, 6L*GYRO_DEG_FACTOR_PITCHROLL);
-
 
265
    if (PDPart[axis] < -6L*GYRO_DEG_FACTOR_PITCHROLL) {
-
 
266
      PDPart[axis] =- 6L*GYRO_DEG_FACTOR_PITCHROLL;
-
 
267
      debugOut.digital[0] |= DEBUG_FLIGHTCLIP;
-
 
268
    } else if (PDPart[axis] > 6L*GYRO_DEG_FACTOR_PITCHROLL) {
-
 
269
      PDPart[axis] = 6L*GYRO_DEG_FACTOR_PITCHROLL;
-
 
270
      debugOut.digital[0] |= DEBUG_FLIGHTCLIP;
-
 
271
    }
265
  }
272
  }
266
 
273
 
267
#define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW)
274
#define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW)
268
  // This is where control affects the target heading. It also (later) directly controls yaw.
275
  // This is where control affects the target heading. It also (later) directly controls yaw.
269
  headingError -= controls[CONTROL_YAW];
276
  headingError -= controls[CONTROL_YAW];
270
  debugOut.analog[28] = headingError / 100;
277
  debugOut.analog[28] = headingError / 100;
271
  if (headingError < -YAW_I_LIMIT) headingError = -YAW_I_LIMIT;
278
  if (headingError < -YAW_I_LIMIT) headingError = -YAW_I_LIMIT;
272
  if (headingError > YAW_I_LIMIT) headingError = YAW_I_LIMIT;
279
  if (headingError > YAW_I_LIMIT) headingError = YAW_I_LIMIT;
273
 
280
 
274
  PDPartYaw =  (int32_t)(headingError * yawIFactor) / (GYRO_DEG_FACTOR_PITCHROLL << 3);
281
  PDPartYaw =  (int32_t)(headingError * yawIFactor) / (GYRO_DEG_FACTOR_PITCHROLL << 3);
275
  // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on.
282
  // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on.
276
  PDPartYaw += (int32_t)(yawRate * yawPFactor) /  (GYRO_DEG_FACTOR_PITCHROLL >> 6);
283
  PDPartYaw += (int32_t)(yawRate * yawPFactor) /  (GYRO_DEG_FACTOR_PITCHROLL >> 6);
277
 
284
 
278
  // limit control feedback
285
  // limit control feedback
279
  // CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT);
286
  // CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT);
280
 
287
 
281
  /*
288
  /*
282
   * Compose throttle term.
289
   * Compose throttle term.
283
   * If a Bl-Ctrl is missing, prevent takeoff.
290
   * If a Bl-Ctrl is missing, prevent takeoff.
284
   */
291
   */
285
  if (missingMotor) {
292
  if (missingMotor) {
286
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
293
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
287
    if (isFlying > 1 && isFlying < 50 && throttleTerm > 0)
294
    if (isFlying > 1 && isFlying < 50 && throttleTerm > 0)
288
      isFlying = 1; // keep within lift off condition
295
      isFlying = 1; // keep within lift off condition
289
    throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of
296
    throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of
290
  }
297
  }
291
 
298
 
292
  // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already?
299
  // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already?
293
  throttleTerm *= CONTROL_SCALING;
300
  throttleTerm *= CONTROL_SCALING;
294
 
301
 
295
  /*
302
  /*
296
   * Compose yaw term.
303
   * Compose yaw term.
297
   * The yaw term is limited: Absolute value is max. = the throttle term / 2.
304
   * The yaw term is limited: Absolute value is max. = the throttle term / 2.
298
   * However, at low throttle the yaw term is limited to a fixed value,
305
   * However, at low throttle the yaw term is limited to a fixed value,
299
   * and at high throttle it is limited by the throttle reserve (the difference
306
   * and at high throttle it is limited by the throttle reserve (the difference
300
   * between current throttle and maximum throttle).
307
   * between current throttle and maximum throttle).
301
   */
308
   */
302
#define MIN_YAWGAS (40 * CONTROL_SCALING)  // yaw also below this gas value
309
#define MIN_YAWGAS (40 * CONTROL_SCALING)  // yaw also below this gas value
303
  yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING;
310
  yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING;
304
  // Limit yawTerm
311
  // Limit yawTerm
305
  debugOut.digital[0] &= ~DEBUG_CLIP;
312
  debugOut.digital[0] &= ~DEBUG_CLIP;
306
  if (throttleTerm > MIN_YAWGAS) {
313
  if (throttleTerm > MIN_YAWGAS) {
307
    if (yawTerm < -throttleTerm / 2) {
314
    if (yawTerm < -throttleTerm / 2) {
308
      debugOut.digital[0] |= DEBUG_CLIP;
315
      debugOut.digital[0] |= DEBUG_CLIP;
309
      yawTerm = -throttleTerm / 2;
316
      yawTerm = -throttleTerm / 2;
310
    } else if (yawTerm > throttleTerm / 2) {
317
    } else if (yawTerm > throttleTerm / 2) {
311
      debugOut.digital[0] |= DEBUG_CLIP;
318
      debugOut.digital[0] |= DEBUG_CLIP;
312
      yawTerm = throttleTerm / 2;
319
      yawTerm = throttleTerm / 2;
313
    }
320
    }
314
  } else {
321
  } else {
315
    if (yawTerm < -MIN_YAWGAS / 2) {
322
    if (yawTerm < -MIN_YAWGAS / 2) {
316
      debugOut.digital[0] |= DEBUG_CLIP;
323
      debugOut.digital[0] |= DEBUG_CLIP;
317
      yawTerm = -MIN_YAWGAS / 2;
324
      yawTerm = -MIN_YAWGAS / 2;
318
    } else if (yawTerm > MIN_YAWGAS / 2) {
325
    } else if (yawTerm > MIN_YAWGAS / 2) {
319
      debugOut.digital[0] |= DEBUG_CLIP;
326
      debugOut.digital[0] |= DEBUG_CLIP;
320
      yawTerm = MIN_YAWGAS / 2;
327
      yawTerm = MIN_YAWGAS / 2;
321
    }
328
    }
322
  }
329
  }
323
 
330
 
324
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
331
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
325
  if (yawTerm < -(tmp_int - throttleTerm)) {
332
  if (yawTerm < -(tmp_int - throttleTerm)) {
326
    yawTerm = -(tmp_int - throttleTerm);
333
    yawTerm = -(tmp_int - throttleTerm);
327
    debugOut.digital[0] |= DEBUG_CLIP;
334
    debugOut.digital[0] |= DEBUG_CLIP;
328
  } else if (yawTerm > (tmp_int - throttleTerm)) {
335
  } else if (yawTerm > (tmp_int - throttleTerm)) {
329
    yawTerm = (tmp_int - throttleTerm);
336
    yawTerm = (tmp_int - throttleTerm);
330
    debugOut.digital[0] |= DEBUG_CLIP;
337
    debugOut.digital[0] |= DEBUG_CLIP;
331
  }
338
  }
332
 
339
 
333
  // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm));
340
  // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm));
334
  debugOut.digital[1] &= ~DEBUG_CLIP;
341
  debugOut.digital[1] &= ~DEBUG_CLIP;
335
  for (axis = PITCH; axis <= ROLL; axis++) {
342
  for (axis = PITCH; axis <= ROLL; axis++) {
336
    /*
343
    /*
337
     * Compose pitch and roll terms. This is finally where the sticks come into play.
344
     * Compose pitch and roll terms. This is finally where the sticks come into play.
338
     */
345
     */
339
    if (gyroIFactor) {
346
    if (gyroIFactor) {
340
      // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos.
347
      // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos.
341
      // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time.
348
      // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time.
342
      // TODO: Find out why this seems to be proportional to stick position - not integrating it at all.
349
      // TODO: Find out why this seems to be proportional to stick position - not integrating it at all.
343
      IPart[axis] += PDPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos.
350
      IPart[axis] += PDPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos.
344
    } else {
351
    } else {
345
      // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos.
352
      // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos.
346
      // To keep up with a full stick PDPart should be about 156...
353
      // To keep up with a full stick PDPart should be about 156...
347
      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.
354
      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.
348
    }
355
    }
349
 
356
 
350
    tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability
357
    tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability
351
        * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64;
358
        * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64;
-
 
359
 
-
 
360
    //CHECK_MIN_MAX(IPart[axis], -25L*GYRO_DEG_FACTOR_PITCHROLL, 25L*GYRO_DEG_FACTOR_PITCHROLL);
-
 
361
    if (IPart[axis] < -25L*GYRO_DEG_FACTOR_PITCHROLL) {
352
 
362
      IPart[axis] =- 25L*GYRO_DEG_FACTOR_PITCHROLL;
353
    // TODO: From which planet comes the 16000?
363
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
-
 
364
    } else if (PDPart[axis] > 25L*GYRO_DEG_FACTOR_PITCHROLL) {
-
 
365
      PDPart[axis] = 25L*GYRO_DEG_FACTOR_PITCHROLL;
-
 
366
      debugOut.digital[1] |= DEBUG_FLIGHTCLIP;
-
 
367
    }
354
    CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L));
368
 
355
    // Add (P, D) parts minus stick pos. to the scaled-down I part.
369
    // Add (P, D) parts minus stick pos. to the scaled-down I part.
356
    term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch
370
    term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch
357
        term[axis] += (dynamicParams.levelCorrection[axis] - 128);
371
        term[axis] += (dynamicParams.levelCorrection[axis] - 128);
358
        /*
372
        /*
359
     * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!).
373
     * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!).
360
     * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity
374
     * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity
361
     * (max. pitch or roll term is the throttle value).
375
     * (max. pitch or roll term is the throttle value).
362
     * TODO: Why a growing function of yaw?
376
     * TODO: Why a growing function of yaw?
363
     */
377
     */
364
    if (term[axis] < -tmp_int) {
378
    if (term[axis] < -tmp_int) {
365
      debugOut.digital[1] |= DEBUG_CLIP;
379
      debugOut.digital[1] |= DEBUG_CLIP;
366
    } else if (term[axis] > tmp_int) {
380
    } else if (term[axis] > tmp_int) {
367
      debugOut.digital[1] |= DEBUG_CLIP;
381
      debugOut.digital[1] |= DEBUG_CLIP;
368
    }
382
    }
369
  }
383
  }
370
 
384
 
371
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
385
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
372
  // Universal Mixer
386
  // Universal Mixer
373
  // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING].
387
  // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING].
374
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
388
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
375
 
389
 
376
  debugOut.analog[3]  = rate_ATT[PITCH];
390
  debugOut.analog[3]  = rate_ATT[PITCH];
377
  debugOut.analog[4]  = rate_ATT[ROLL];
391
  debugOut.analog[4]  = rate_ATT[ROLL];
378
  debugOut.analog[5]  = yawRate;
392
  debugOut.analog[5]  = yawRate;
379
 
393
 
380
  debugOut.analog[6]  = filteredAcc[PITCH];
394
  debugOut.analog[6]  = filteredAcc[PITCH];
381
  debugOut.analog[7]  = filteredAcc[ROLL];
395
  debugOut.analog[7]  = filteredAcc[ROLL];
382
  debugOut.analog[8]  = filteredAcc[Z];
396
  debugOut.analog[8]  = filteredAcc[Z];
383
 
397
 
384
  debugOut.analog[13] = term[PITCH];
398
  debugOut.analog[13] = term[PITCH];
385
  debugOut.analog[14] = term[ROLL];
399
  debugOut.analog[14] = term[ROLL];
386
  debugOut.analog[15] = yawTerm;
400
  debugOut.analog[15] = yawTerm;
387
  debugOut.analog[16] = throttleTerm;
401
  debugOut.analog[16] = throttleTerm;
388
 
402
 
389
  for (i = 0; i < MAX_MOTORS; i++) {
403
  for (i = 0; i < MAX_MOTORS; i++) {
390
    int32_t tmp;
404
    int32_t tmp;
391
    uint8_t throttle;
405
    uint8_t throttle;
392
 
406
 
393
    tmp = (int32_t)throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE];
407
    tmp = (int32_t)throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE];
394
    tmp += (int32_t)term[PITCH] * mixerMatrix.motor[i][MIX_PITCH];
408
    tmp += (int32_t)term[PITCH] * mixerMatrix.motor[i][MIX_PITCH];
395
    tmp += (int32_t)term[ROLL] * mixerMatrix.motor[i][MIX_ROLL];
409
    tmp += (int32_t)term[ROLL] * mixerMatrix.motor[i][MIX_ROLL];
396
    tmp += (int32_t)yawTerm * mixerMatrix.motor[i][MIX_YAW];
410
    tmp += (int32_t)yawTerm * mixerMatrix.motor[i][MIX_YAW];
397
    tmp = tmp >> 6;
411
    tmp = tmp >> 6;
398
    motorFilters[i] = motorFilter(tmp, motorFilters[i]);
412
    motorFilters[i] = motorFilter(tmp, motorFilters[i]);
399
    // Now we scale back down to a 0..255 range.
413
    // Now we scale back down to a 0..255 range.
400
    tmp = motorFilters[i] / MOTOR_SCALING;
414
    tmp = motorFilters[i] / MOTOR_SCALING;
401
 
415
 
402
    // So this was the THIRD time a throttle was limited. But should the limitation
416
    // So this was the THIRD time a throttle was limited. But should the limitation
403
    // apply to the common throttle signal (the one used for setting the "power" of
417
    // apply to the common throttle signal (the one used for setting the "power" of
404
    // all motors together) or should it limit the throttle set for each motor,
418
    // all motors together) or should it limit the throttle set for each motor,
405
    // including mix components of pitch, roll and yaw? I think only the common
419
    // including mix components of pitch, roll and yaw? I think only the common
406
    // throttle should be limited.
420
    // throttle should be limited.
407
    // --> WRONG. This caused motors to stall completely in tight maneuvers.
421
    // --> WRONG. This caused motors to stall completely in tight maneuvers.
408
    // Apply to individual signals instead.
422
    // Apply to individual signals instead.
409
    CHECK_MIN_MAX(tmp, 1, 255);
423
    CHECK_MIN_MAX(tmp, 1, 255);
410
    throttle = tmp;
424
    throttle = tmp;
411
 
425
 
412
    // if (i < 4) debugOut.analog[22 + i] = throttle;
426
    // if (i < 4) debugOut.analog[22 + i] = throttle;
413
 
427
 
414
    if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) {
428
    if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) {
415
      motor[i].throttle = throttle;
429
      motor[i].throttle = throttle;
416
    } else if (motorTestActive) {
430
    } else if (motorTestActive) {
417
      motor[i].throttle = motorTest[i];
431
      motor[i].throttle = motorTest[i];
418
    } else {
432
    } else {
419
      motor[i].throttle = 0;
433
      motor[i].throttle = 0;
420
    }
434
    }
421
  }
435
  }
422
 
436
 
423
  I2C_Start(TWI_STATE_MOTOR_TX);
437
  I2C_Start(TWI_STATE_MOTOR_TX);
424
 
438
 
425
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
439
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
426
  // Debugging
440
  // Debugging
427
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
441
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
428
  if (!(--debugDataTimer)) {
442
  if (!(--debugDataTimer)) {
429
    debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz.
443
    debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz.
430
    debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg
444
    debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg
431
    debugOut.analog[1] = attitude[ROLL]  / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg
445
    debugOut.analog[1] = attitude[ROLL]  / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg
432
    debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW;
446
    debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW;
433
  }
447
  }
434
}
448
}
435
 
449