Subversion Repositories FlightCtrl

Rev

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

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