Subversion Repositories FlightCtrl

Rev

Rev 1775 | Rev 1805 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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