Subversion Repositories FlightCtrl

Rev

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

Rev 1960 Rev 1964
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 (dynamicParams.motorSmoothing) {
90
  switch (dynamicParams.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], PDPartYaw, PPart[2];
156
  int16_t PDPart[2], PDPartYaw, PPart[2];
157
  static int32_t IPart[2] = { 0, 0 };
157
  static int32_t IPart[2] = { 0, 0 };
158
  //  static int32_t yawControlRate = 0;
158
  //  static int32_t yawControlRate = 0;
159
 
159
 
160
  // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway.
160
  // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway.
161
  // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0;
161
  // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0;
162
  // static int32_t CorrectionPitch, CorrectionRoll;
162
  // static int32_t CorrectionPitch, CorrectionRoll;
163
 
163
 
164
  static uint16_t emergencyFlightTime;
164
  static uint16_t emergencyFlightTime;
165
  static int8_t debugDataTimer = 1;
165
  static int8_t debugDataTimer = 1;
166
 
166
 
167
  // High resolution motor values for smoothing of PID motor outputs
167
  // High resolution motor values for smoothing of PID motor outputs
168
  static int16_t motorFilters[MAX_MOTORS];
168
  static int16_t motorFilters[MAX_MOTORS];
169
 
169
 
170
  uint8_t i, axis;
170
  uint8_t i, axis;
171
 
171
 
172
  // Fire the main flight attitude calculation, including integration of angles.
172
  // Fire the main flight attitude calculation, including integration of angles.
173
  // We want that to kick as early as possible, not to delay new AD sampling further.
173
  // We want that to kick as early as possible, not to delay new AD sampling further.
174
  calculateFlightAttitude();
174
  calculateFlightAttitude();
175
  controlMixer_update();
175
  controlMixer_update();
176
  throttleTerm = controls[CONTROL_THROTTLE];
176
  throttleTerm = controls[CONTROL_THROTTLE];
177
 
177
 
178
  // This check removed. Is done on a per-motor basis, after output matrix multiplication.
178
  // This check removed. Is done on a per-motor basis, after output matrix multiplication.
179
  if (throttleTerm < staticParams.minThrottle + 10)
179
  if (throttleTerm < staticParams.minThrottle + 10)
180
    throttleTerm = staticParams.minThrottle + 10;
180
    throttleTerm = staticParams.minThrottle + 10;
181
  else if (throttleTerm > staticParams.maxThrottle - 20)
181
  else if (throttleTerm > staticParams.maxThrottle - 20)
182
    throttleTerm = (staticParams.maxThrottle - 20);
182
    throttleTerm = (staticParams.maxThrottle - 20);
183
 
183
 
184
  /************************************************************************/
184
  /************************************************************************/
185
  /* RC-signal is bad                                                     */
185
  /* RC-signal is bad                                                     */
186
  /* This part could be abstracted, as having yet another control input   */
186
  /* This part could be abstracted, as having yet another control input   */
187
  /* to the control mixer: An emergency autopilot control.                */
187
  /* to the control mixer: An emergency autopilot control.                */
188
  /************************************************************************/
188
  /************************************************************************/
189
 
189
 
190
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
190
  if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy
191
    RED_ON;
-
 
192
    beepRCAlarm();
191
    beepRCAlarm();
193
 
192
 
194
    if (emergencyFlightTime) {
193
    if (emergencyFlightTime) {
195
      // continue emergency flight
194
      // continue emergency flight
196
      emergencyFlightTime--;
195
      emergencyFlightTime--;
197
      if (isFlying > 256) {
196
      if (isFlying > 256) {
198
        // We're probably still flying. Descend slowly.
197
        // We're probably still flying. Descend slowly.
199
        throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle
198
        throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle
200
        MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing
199
        MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing
201
        setStableFlightParameters();
200
        setStableFlightParameters();
202
      } else {
201
      } else {
203
        MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors.
202
        MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors.
204
      }
203
      }
205
    } else {
204
    } else {
206
      // end emergency flight (just cut the motors???)
205
      // end emergency flight (just cut the motors???)
207
      MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT);
206
      MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT);
208
    }
207
    }
209
  } else {
208
  } else {
210
    // signal is acceptable
209
    // signal is acceptable
211
    if (controlMixer_getSignalQuality() > SIGNAL_BAD) {
210
    if (controlMixer_getSignalQuality() > SIGNAL_BAD) {
212
      // Reset emergency landing control variables.
211
      // Reset emergency landing control variables.
213
      MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing
212
      MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing
214
      // The time is in whole seconds.
213
      // The time is in whole seconds.
215
      emergencyFlightTime = (uint16_t) staticParams.emergencyFlightDuration * 488;
214
      emergencyFlightTime = (uint16_t) staticParams.emergencyFlightDuration * 488;
216
    }
215
    }
217
 
216
 
218
    // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying.
217
    // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying.
219
    if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) {
218
    if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) {
220
      // increment flight-time counter until overflow.
219
      // increment flight-time counter until overflow.
221
      if (isFlying != 0xFFFF)
220
      if (isFlying != 0xFFFF)
222
        isFlying++;
221
        isFlying++;
223
    } else
222
    } else
224
    /*
223
    /*
225
     * When standing on the ground, do not apply I controls and zero the yaw stick.
224
     * When standing on the ground, do not apply I controls and zero the yaw stick.
226
     * Probably to avoid integration effects that will cause the copter to spin
225
     * Probably to avoid integration effects that will cause the copter to spin
227
     * or flip when taking off.
226
     * or flip when taking off.
228
     */
227
     */
229
      if (isFlying < 256) {
228
      if (isFlying < 256) {
230
        IPart[PITCH] = IPart[ROLL] = 0;
229
        IPart[PITCH] = IPart[ROLL] = 0;
231
        // TODO: Don't stomp on other modules' variables!!!
230
        // TODO: Don't stomp on other modules' variables!!!
232
        // controlYaw = 0;
231
        // controlYaw = 0;
233
        PDPartYaw = 0; // instead.
232
        PDPartYaw = 0; // instead.
234
        if (isFlying == 250) {
233
        if (isFlying == 250) {
235
          // HC_setGround();
234
          // HC_setGround();
236
          updateCompassCourse = 1;
235
          updateCompassCourse = 1;
237
          yawAngleDiff = 0;
236
          yawAngleDiff = 0;
238
        }
237
        }
239
      } else {
238
      } else {
240
        // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag?
239
        // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag?
241
        // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe.
240
        // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe.
242
        MKFlags |= (MKFLAG_FLY);
241
        MKFlags |= (MKFLAG_FLY);
243
      }
242
      }
244
   
243
   
245
    commands_handleCommands();
244
    commands_handleCommands();
246
    setNormalFlightParameters();
245
    setNormalFlightParameters();
247
  } // end else (not bad signal case)
246
  } // end else (not bad signal case)
248
 
247
 
249
  /************************************************************************/
248
  /************************************************************************/
250
  /*  Yawing                                                              */
249
  /*  Yawing                                                              */
251
  /************************************************************************/
250
  /************************************************************************/
252
  if (abs(controls[CONTROL_YAW]) > 4 * staticParams.stickYawP) { // yaw stick is activated
251
  if (abs(controls[CONTROL_YAW]) > 4 * staticParams.stickYawP) { // yaw stick is activated
253
    ignoreCompassTimer = 1000;
252
    ignoreCompassTimer = 1000;
254
    if (!(staticParams.bitConfig & CFG_COMPASS_FIX)) {
253
    if (!(staticParams.bitConfig & CFG_COMPASS_FIX)) {
255
      updateCompassCourse = 1;
254
      updateCompassCourse = 1;
256
    }
255
    }
257
  }
256
  }
258
 
257
 
259
  // yawControlRate = controlYaw;
258
  // yawControlRate = controlYaw;
260
  // Trim drift of yawAngleDiff with controlYaw.
259
  // Trim drift of yawAngleDiff with controlYaw.
261
  // TODO: We want NO feedback of control related stuff to the attitude related stuff.
260
  // TODO: We want NO feedback of control related stuff to the attitude related stuff.
262
  // This seems to be used as: Difference desired <--> real heading.
261
  // This seems to be used as: Difference desired <--> real heading.
263
  yawAngleDiff -= controls[CONTROL_YAW];
262
  yawAngleDiff -= controls[CONTROL_YAW];
264
 
263
 
265
  // limit the effect
264
  // limit the effect
266
  CHECK_MIN_MAX(yawAngleDiff, -50000, 50000);
265
  CHECK_MIN_MAX(yawAngleDiff, -50000, 50000);
267
 
266
 
268
  /************************************************************************/
267
  /************************************************************************/
269
  /* Compass is currently not supported.                                  */
268
  /* Compass is currently not supported.                                  */
270
  /************************************************************************/
269
  /************************************************************************/
271
  if (staticParams.bitConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) {
270
  if (staticParams.bitConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) {
272
    updateCompass();
271
    updateCompass();
273
  }
272
  }
274
 
273
 
275
#if defined (USE_NAVICTRL)
274
#if defined (USE_NAVICTRL)
276
  /************************************************************************/
275
  /************************************************************************/
277
  /* GPS is currently not supported.                                      */
276
  /* GPS is currently not supported.                                      */
278
  /************************************************************************/
277
  /************************************************************************/
279
  if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) {
278
  if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) {
280
    GPS_Main();
279
    GPS_Main();
281
    MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START);
280
    MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START);
282
  } else {
281
  } else {
283
  }
282
  }
284
#endif
283
#endif
285
  // end part 1: 750-800 usec.
284
  // end part 1: 750-800 usec.
286
  // start part 3: 350 - 400 usec.
285
  // start part 3: 350 - 400 usec.
287
#define SENSOR_LIMIT  (4096 * 4)
286
#define SENSOR_LIMIT  (4096 * 4)
288
  /************************************************************************/
287
  /************************************************************************/
289
 
288
 
290
  /* Calculate control feedback from angle (gyro integral)                */
289
  /* Calculate control feedback from angle (gyro integral)                */
291
  /* and angular velocity (gyro signal)                                   */
290
  /* and angular velocity (gyro signal)                                   */
292
  /************************************************************************/
291
  /************************************************************************/
293
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
292
  // The P-part is the P of the PID controller. That's the angle integrals (not rates).
294
 
293
 
295
  for (axis = PITCH; axis <= ROLL; axis++) {
294
  for (axis = PITCH; axis <= ROLL; axis++) {
296
    PPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral
295
    PPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral
297
   
296
   
298
    /*
297
    /*
299
     * Now blend in the D-part - proportional to the Differential of the integral = the rate.
298
     * Now blend in the D-part - proportional to the Differential of the integral = the rate.
300
     * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING
299
     * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING
301
     * where pfactor is in [0..1].
300
     * where pfactor is in [0..1].
302
     */
301
     */
303
    PDPart[axis] = PPart[axis] + (int32_t) ((int32_t) rate_PID[axis] * gyroPFactor / (256L / CONTROL_SCALING)) + (differential[axis]
302
    PDPart[axis] = PPart[axis] + (int32_t) ((int32_t) rate_PID[axis] * gyroPFactor / (256L / CONTROL_SCALING)) + (differential[axis]
304
                                                                                         * (int16_t) dynamicParams.gyroD) / 16;
303
                                                                                         * (int16_t) dynamicParams.gyroD) / 16;
305
   
304
   
306
    CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT);
305
    CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT);
307
  }
306
  }
308
 
307
 
309
  PDPartYaw = (int32_t) (yawRate * 2 * (int32_t) yawPFactor) / (256L
308
  PDPartYaw = (int32_t) (yawRate * 2 * (int32_t) yawPFactor) / (256L
310
      / CONTROL_SCALING) + (int32_t) (yawAngleDiff * yawIFactor) / (2 * (44000
309
      / CONTROL_SCALING) + (int32_t) (yawAngleDiff * yawIFactor) / (2 * (44000
311
      / CONTROL_SCALING));
310
      / CONTROL_SCALING));
312
 
311
 
313
  // limit control feedback
312
  // limit control feedback
314
  CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT);
313
  CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT);
315
 
314
 
316
  /*
315
  /*
317
   * Compose throttle term.
316
   * Compose throttle term.
318
   * If a Bl-Ctrl is missing, prevent takeoff.
317
   * If a Bl-Ctrl is missing, prevent takeoff.
319
   */
318
   */
320
  if (missingMotor) {
319
  if (missingMotor) {
321
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
320
    // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway???
322
    if (isFlying > 1 && isFlying < 50 && throttleTerm > 0)
321
    if (isFlying > 1 && isFlying < 50 && throttleTerm > 0)
323
      isFlying = 1; // keep within lift off condition
322
      isFlying = 1; // keep within lift off condition
324
    throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of
323
    throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of
325
  }
324
  }
326
 
325
 
327
  // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already?
326
  // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already?
328
  throttleTerm *= CONTROL_SCALING;
327
  throttleTerm *= CONTROL_SCALING;
329
 
328
 
330
  /*
329
  /*
331
   * Compose yaw term.
330
   * Compose yaw term.
332
   * The yaw term is limited: Absolute value is max. = the throttle term / 2.
331
   * The yaw term is limited: Absolute value is max. = the throttle term / 2.
333
   * However, at low throttle the yaw term is limited to a fixed value,
332
   * However, at low throttle the yaw term is limited to a fixed value,
334
   * and at high throttle it is limited by the throttle reserve (the difference
333
   * and at high throttle it is limited by the throttle reserve (the difference
335
   * between current throttle and maximum throttle).
334
   * between current throttle and maximum throttle).
336
   */
335
   */
337
#define MIN_YAWGAS (40 * CONTROL_SCALING)  // yaw also below this gas value
336
#define MIN_YAWGAS (40 * CONTROL_SCALING)  // yaw also below this gas value
338
  yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING;
337
  yawTerm = PDPartYaw - controls[CONTROL_YAW] * CONTROL_SCALING;
339
  // Limit yawTerm
338
  // Limit yawTerm
340
  debugOut.digital[0] &= ~DEBUG_CLIP;
339
  debugOut.digital[0] &= ~DEBUG_CLIP;
341
  if (throttleTerm > MIN_YAWGAS) {
340
  if (throttleTerm > MIN_YAWGAS) {
342
    if (yawTerm < -throttleTerm / 2) {
341
    if (yawTerm < -throttleTerm / 2) {
343
      debugOut.digital[0] |= DEBUG_CLIP;
342
      debugOut.digital[0] |= DEBUG_CLIP;
344
      yawTerm = -throttleTerm / 2;
343
      yawTerm = -throttleTerm / 2;
345
    } else if (yawTerm > throttleTerm / 2) {
344
    } else if (yawTerm > throttleTerm / 2) {
346
      debugOut.digital[0] |= DEBUG_CLIP;
345
      debugOut.digital[0] |= DEBUG_CLIP;
347
      yawTerm = throttleTerm / 2;
346
      yawTerm = throttleTerm / 2;
348
    }
347
    }
349
    //CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2));
348
    //CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2));
350
  } else {
349
  } else {
351
    if (yawTerm < -MIN_YAWGAS / 2) {
350
    if (yawTerm < -MIN_YAWGAS / 2) {
352
      debugOut.digital[0] |= DEBUG_CLIP;
351
      debugOut.digital[0] |= DEBUG_CLIP;
353
      yawTerm = -MIN_YAWGAS / 2;
352
      yawTerm = -MIN_YAWGAS / 2;
354
    } else if (yawTerm > MIN_YAWGAS / 2) {
353
    } else if (yawTerm > MIN_YAWGAS / 2) {
355
      debugOut.digital[0] |= DEBUG_CLIP;
354
      debugOut.digital[0] |= DEBUG_CLIP;
356
      yawTerm = MIN_YAWGAS / 2;
355
      yawTerm = MIN_YAWGAS / 2;
357
    }
356
    }
358
    //CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2));
357
    //CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2));
359
  }
358
  }
360
 
359
 
361
  // FIXME: Throttle may exceed maxThrottle (there is no check no more).
360
  // FIXME: Throttle may exceed maxThrottle (there is no check no more).
362
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
361
  tmp_int = staticParams.maxThrottle * CONTROL_SCALING;
363
  if (yawTerm < -(tmp_int - throttleTerm)) {
362
  if (yawTerm < -(tmp_int - throttleTerm)) {
364
    yawTerm = -(tmp_int - throttleTerm);
363
    yawTerm = -(tmp_int - throttleTerm);
365
    debugOut.digital[0] |= DEBUG_CLIP;
364
    debugOut.digital[0] |= DEBUG_CLIP;
366
  } else if (yawTerm > (tmp_int - throttleTerm)) {
365
  } else if (yawTerm > (tmp_int - throttleTerm)) {
367
    yawTerm = (tmp_int - throttleTerm);
366
    yawTerm = (tmp_int - throttleTerm);
368
    debugOut.digital[0] |= DEBUG_CLIP;
367
    debugOut.digital[0] |= DEBUG_CLIP;
369
  }
368
  }
370
 
369
 
371
  // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm));
370
  // CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm));
372
  debugOut.digital[1] &= ~DEBUG_CLIP;
371
  debugOut.digital[1] &= ~DEBUG_CLIP;
373
  for (axis = PITCH; axis <= ROLL; axis++) {
372
  for (axis = PITCH; axis <= ROLL; axis++) {
374
    /*
373
    /*
375
     * Compose pitch and roll terms. This is finally where the sticks come into play.
374
     * Compose pitch and roll terms. This is finally where the sticks come into play.
376
     */
375
     */
377
    if (gyroIFactor) {
376
    if (gyroIFactor) {
378
      // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos.
377
      // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos.
379
      // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time.
378
      // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time.
380
      // TODO: Find out why this seems to be proportional to stick position - not integrating it at all.
379
      // TODO: Find out why this seems to be proportional to stick position - not integrating it at all.
381
      IPart[axis] += PPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos.
380
      IPart[axis] += PPart[axis] - controls[axis]; // Integrate difference between P part (the angle) and the stick pos.
382
    } else {
381
    } else {
383
      // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos.
382
      // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos.
384
      // To keep up with a full stick PDPart should be about 156...
383
      // To keep up with a full stick PDPart should be about 156...
385
      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.
384
      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.
386
    }
385
    }
387
 
386
 
388
    tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability
387
    tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability
389
        * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64;
388
        * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64;
390
 
389
 
391
    // TODO: From which planet comes the 16000?
390
    // TODO: From which planet comes the 16000?
392
    CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L));
391
    CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L));
393
    // Add (P, D) parts minus stick pos. to the scaled-down I part.
392
    // Add (P, D) parts minus stick pos. to the scaled-down I part.
394
    term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch
393
    term[axis] = PDPart[axis] - controls[axis] + IPart[axis] / Ki; // PID-controller for pitch
395
 
394
 
396
    /*
395
    /*
397
     * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!).
396
     * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!).
398
     * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity
397
     * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity
399
     * (max. pitch or roll term is the throttle value).
398
     * (max. pitch or roll term is the throttle value).
400
     * TODO: Why a growing function of yaw?
399
     * TODO: Why a growing function of yaw?
401
     */
400
     */
402
    if (term[axis] < -tmp_int) {
401
    if (term[axis] < -tmp_int) {
403
      debugOut.digital[1] |= DEBUG_CLIP;
402
      debugOut.digital[1] |= DEBUG_CLIP;
404
    } else if (term[axis] > tmp_int) {
403
    } else if (term[axis] > tmp_int) {
405
      debugOut.digital[1] |= DEBUG_CLIP;
404
      debugOut.digital[1] |= DEBUG_CLIP;
406
    }
405
    }
407
    CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int);
406
    CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int);
408
  }
407
  }
409
 
408
 
410
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
409
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
411
  // Universal Mixer
410
  // Universal Mixer
412
  // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING].
411
  // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING].
413
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
412
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
414
 
413
 
415
  debugOut.analog[12] = term[PITCH];
414
  debugOut.analog[12] = term[PITCH];
416
  debugOut.analog[13] = term[ROLL];
415
  debugOut.analog[13] = term[ROLL];
417
  debugOut.analog[14] = yawTerm;
416
  debugOut.analog[14] = yawTerm;
418
  debugOut.analog[15] = throttleTerm;
417
  debugOut.analog[15] = throttleTerm;
419
 
418
 
420
  for (i = 0; i < MAX_MOTORS; i++) {
419
  for (i = 0; i < MAX_MOTORS; i++) {
421
    int32_t tmp;
420
    int32_t tmp;
422
    uint8_t throttle;
421
    uint8_t throttle;
423
 
422
 
424
    tmp = (int32_t)throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE];
423
    tmp = (int32_t)throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE];
425
    tmp += (int32_t)term[PITCH] * mixerMatrix.motor[i][MIX_PITCH];
424
    tmp += (int32_t)term[PITCH] * mixerMatrix.motor[i][MIX_PITCH];
426
    tmp += (int32_t)term[ROLL] * mixerMatrix.motor[i][MIX_ROLL];
425
    tmp += (int32_t)term[ROLL] * mixerMatrix.motor[i][MIX_ROLL];
427
    tmp += (int32_t)yawTerm * mixerMatrix.motor[i][MIX_YAW];
426
    tmp += (int32_t)yawTerm * mixerMatrix.motor[i][MIX_YAW];
428
    tmp = tmp >> 6;
427
    tmp = tmp >> 6;
429
    motorFilters[i] = motorFilter(tmp, motorFilters[i]);
428
    motorFilters[i] = motorFilter(tmp, motorFilters[i]);
430
    // Now we scale back down to a 0..255 range.
429
    // Now we scale back down to a 0..255 range.
431
    tmp = motorFilters[i] / MOTOR_SCALING;
430
    tmp = motorFilters[i] / MOTOR_SCALING;
432
 
431
 
433
    // So this was the THIRD time a throttle was limited. But should the limitation
432
    // So this was the THIRD time a throttle was limited. But should the limitation
434
    // apply to the common throttle signal (the one used for setting the "power" of
433
    // apply to the common throttle signal (the one used for setting the "power" of
435
    // all motors together) or should it limit the throttle set for each motor,
434
    // all motors together) or should it limit the throttle set for each motor,
436
    // including mix components of pitch, roll and yaw? I think only the common
435
    // including mix components of pitch, roll and yaw? I think only the common
437
    // throttle should be limited.
436
    // throttle should be limited.
438
    // --> WRONG. This caused motors to stall completely in tight maneuvers.
437
    // --> WRONG. This caused motors to stall completely in tight maneuvers.
439
    // Apply to individual signals instead.
438
    // Apply to individual signals instead.
440
    CHECK_MIN_MAX(tmp, 1, 255);
439
    CHECK_MIN_MAX(tmp, 1, 255);
441
    throttle = tmp;
440
    throttle = tmp;
442
 
441
 
443
    if (i < 4) debugOut.analog[22 + i] = throttle;
442
    if (i < 4) debugOut.analog[22 + i] = throttle;
444
 
443
 
445
    if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) {
444
    if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) {
446
      motor[i].SetPoint = throttle;
445
      motor[i].SetPoint = throttle;
447
    } else if (motorTestActive) {
446
    } else if (motorTestActive) {
448
      motor[i].SetPoint = motorTest[i];
447
      motor[i].SetPoint = motorTest[i];
449
    } else {
448
    } else {
450
      motor[i].SetPoint = 0;
449
      motor[i].SetPoint = 0;
451
    }
450
    }
452
  }
451
  }
453
 
452
 
454
  I2C_Start(TWI_STATE_MOTOR_TX);
453
  I2C_Start(TWI_STATE_MOTOR_TX);
455
 
454
 
456
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
455
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
457
  // Debugging
456
  // Debugging
458
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
457
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
459
  if (!(--debugDataTimer)) {
458
  if (!(--debugDataTimer)) {
460
    debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz.
459
    debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz.
461
    debugOut.analog[0] = (10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg
460
    debugOut.analog[0] = (10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg
462
    debugOut.analog[1] = (10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg
461
    debugOut.analog[1] = (10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg
463
    debugOut.analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW;
462
    debugOut.analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW;
464
 
463
 
465
    debugOut.analog[16] = gyroPFactor;
464
    debugOut.analog[16] = gyroPFactor;
466
    debugOut.analog[17] = gyroIFactor;
465
    debugOut.analog[17] = gyroIFactor;
467
    debugOut.analog[18] = dynamicParams.gyroD;
466
    debugOut.analog[18] = dynamicParams.gyroD;
468
  }
467
  }
469
}
468
}
470
 
469