Subversion Repositories FlightCtrl

Rev

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

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