Subversion Repositories FlightCtrl

Rev

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

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