Subversion Repositories FlightCtrl

Rev

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

Rev 1646 Rev 1775
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 excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  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
/************************************************************************/
52
/************************************************************************/
53
/* Flight Attitude                                                      */
53
/* Flight Attitude                                                      */
54
/************************************************************************/
54
/************************************************************************/
55
 
55
 
56
#include <stdlib.h>
56
#include <stdlib.h>
57
#include <avr/io.h>
57
#include <avr/io.h>
58
 
58
 
59
#include "attitude.h"
59
#include "attitude.h"
60
#include "dongfangMath.h"
60
#include "dongfangMath.h"
-
 
61
 
-
 
62
// For scope debugging only!
-
 
63
#include "rc.h"
61
 
64
 
62
// where our main data flow comes from.
65
// where our main data flow comes from.
63
#include "analog.h"
66
#include "analog.h"
64
 
67
 
65
#include "configuration.h"
68
#include "configuration.h"
-
 
69
#include "output.h"
66
 
70
 
67
// Some calculations are performed depending on some stick related things.
71
// Some calculations are performed depending on some stick related things.
68
#include "controlMixer.h"
72
#include "controlMixer.h"
69
 
73
 
70
// For Servo_On / Off
74
// For Servo_On / Off
71
// #include "timer2.h"
75
// #include "timer2.h"
72
 
76
 
73
#ifdef USE_MK3MAG
77
#ifdef USE_MK3MAG
74
#include "mk3mag.h"
78
#include "mk3mag.h"
75
#include "gps.h"
79
#include "gps.h"
76
#endif
80
#endif
77
#define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;}
81
#define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;}
78
 
82
 
79
/*
83
/*
80
 * Gyro readings, as read from the analog module. It would have been nice to flow
84
 * Gyro readings, as read from the analog module. It would have been nice to flow
81
 * them around between the different calculations as a struct or array (doing
85
 * them around between the different calculations as a struct or array (doing
82
 * things functionally without side effects) but this is shorter and probably
86
 * things functionally without side effects) but this is shorter and probably
83
 * faster too.
87
 * faster too.
84
 * The variables are overwritten at each attitude calculation invocation - the values
88
 * The variables are overwritten at each attitude calculation invocation - the values
85
 * are not preserved or reused.
89
 * are not preserved or reused.
86
 */
90
 */
87
int16_t rate[2], yawRate;
91
int16_t rate_ATT[2], yawRate;
88
 
92
 
89
// With different (less) filtering
93
// With different (less) filtering
90
int16_t rate_PID[2];
94
int16_t rate_PID[2];
91
int16_t differential[2];
95
int16_t differential[2];
92
 
96
 
93
/*
97
/*
94
 * Gyro readings, after performing "axis coupling" - that is, the transfomation
98
 * Gyro readings, after performing "axis coupling" - that is, the transfomation
95
 * of rotation rates from the airframe-local coordinate system to a ground-fixed
99
 * of rotation rates from the airframe-local coordinate system to a ground-fixed
96
 * coordinate system. If axis copling is disabled, the gyro readings will be
100
 * coordinate system. If axis copling is disabled, the gyro readings will be
97
 * copied into these directly.
101
 * copied into these directly.
98
 * These are global for the same pragmatic reason as with the gyro readings.
102
 * These are global for the same pragmatic reason as with the gyro readings.
99
 * The variables are overwritten at each attitude calculation invocation - the values
103
 * The variables are overwritten at each attitude calculation invocation - the values
100
 * are not preserved or reused.
104
 * are not preserved or reused.
101
 */
105
 */
102
int16_t ACRate[2], ACYawRate;
106
int16_t ACRate[2], ACYawRate;
103
 
107
 
104
/*
108
/*
105
 * Gyro integrals. These are the rotation angles of the airframe compared to the
109
 * Gyro integrals. These are the rotation angles of the airframe compared to the
106
 * horizontal plane, yaw relative to yaw at start.
110
 * horizontal plane, yaw relative to yaw at start.
107
 */
111
 */
108
int32_t angle[2], yawAngle;
112
int32_t angle[2], yawAngleDiff;
109
 
113
 
110
int readingHeight = 0;
114
int readingHeight = 0;
111
 
115
 
112
// compass course
116
// compass course
113
int16_t compassHeading = -1; // negative angle indicates invalid data.
117
int16_t compassHeading       = -1; // negative angle indicates invalid data.
114
int16_t compassCourse = -1;
118
int16_t compassCourse        = -1;
115
int16_t compassOffCourse = 0;
119
int16_t compassOffCourse     = 0;
116
uint16_t updateCompassCourse = 0;
120
uint16_t updateCompassCourse = 0;
117
uint8_t compassCalState = 0;
121
uint8_t compassCalState      = 0;
118
 
-
 
119
// uint8_t FunnelCourse = 0;
-
 
120
uint16_t badCompassHeading = 500;
122
uint16_t badCompassHeading = 500;
121
int32_t yawGyroHeading; // Yaw Gyro Integral supported by compass
123
int32_t yawGyroHeading; // Yaw Gyro Integral supported by compass
-
 
124
int16_t yawGyroDrift;
122
 
125
 
123
#define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L)
126
#define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L)
124
#define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L)
127
#define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L)
125
#define YAWOVER360       (GYRO_DEG_FACTOR_YAW * 360L)
128
#define YAWOVER360       (GYRO_DEG_FACTOR_YAW * 360L)
126
 
129
 
127
int16_t correctionSum[2] = {0,0};
130
int16_t correctionSum[2] = {0,0};
-
 
131
 
-
 
132
// For NaviCTRL use.
-
 
133
int16_t averageAcc[2] = {0,0}, averageAccCount = 0;
128
 
134
 
129
/*
135
/*
130
 * Experiment: Compensating for dynamic-induced gyro biasing.
136
 * Experiment: Compensating for dynamic-induced gyro biasing.
131
 */
137
 */
132
int16_t dynamicOffset[2] = {0,0}, dynamicOffsetYaw = 0;
138
int16_t driftComp[2] = {0,0}, driftCompYaw = 0;
133
// int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0;
139
// int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0;
134
// int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw;
140
// int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw;
135
// int16_t dynamicCalCount;
141
// int16_t dynamicCalCount;
136
 
142
 
137
/************************************************************************
143
/************************************************************************
138
 * Set inclination angles from the acc. sensor data.                    
144
 * Set inclination angles from the acc. sensor data.                    
139
 * If acc. sensors are not used, set to zero.                          
145
 * If acc. sensors are not used, set to zero.                          
140
 * TODO: One could use inverse sine to calculate the angles more        
146
 * TODO: One could use inverse sine to calculate the angles more        
141
 * accurately, but since: 1) the angles are rather small at times when
147
 * accurately, but since: 1) the angles are rather small at times when
142
 * it makes sense to set the integrals (standing on ground, or flying at  
148
 * it makes sense to set the integrals (standing on ground, or flying at  
143
 * constant speed, and 2) at small angles a, sin(a) ~= constant * a,    
149
 * constant speed, and 2) at small angles a, sin(a) ~= constant * a,    
144
 * it is hardly worth the trouble.                                      
150
 * it is hardly worth the trouble.                                      
145
 ************************************************************************/
151
 ************************************************************************/
146
 
152
 
147
int32_t getAngleEstimateFromAcc(uint8_t axis) {
153
int32_t getAngleEstimateFromAcc(uint8_t axis) {
148
  return GYRO_ACC_FACTOR * (int32_t)filteredAcc[axis];
154
  return GYRO_ACC_FACTOR * (int32_t)filteredAcc[axis];
149
}
155
}
150
 
156
 
151
void setStaticAttitudeAngles(void) {
157
void setStaticAttitudeAngles(void) {
152
#ifdef ATTITUDE_USE_ACC_SENSORS
158
#ifdef ATTITUDE_USE_ACC_SENSORS
153
  angle[PITCH] = getAngleEstimateFromAcc(PITCH);
159
  angle[PITCH] = getAngleEstimateFromAcc(PITCH);
154
  angle[ROLL] = getAngleEstimateFromAcc(ROLL);
160
  angle[ROLL] = getAngleEstimateFromAcc(ROLL);
155
#else
161
#else
156
  angle[PITCH] = angle[ROLL] = 0;
162
  angle[PITCH] = angle[ROLL] = 0;
157
#endif
163
#endif
158
}
164
}
159
 
165
 
160
/************************************************************************
166
/************************************************************************
161
 * Neutral Readings                                                    
167
 * Neutral Readings                                                    
162
 ************************************************************************/
168
 ************************************************************************/
163
void attitude_setNeutral(void) {
169
void attitude_setNeutral(void) {
164
  // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway.
170
  // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway.
165
  dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0;
171
  dynamicParams.AxisCoupling1 = dynamicParams.AxisCoupling2 = 0;
166
 
172
 
167
  dynamicOffset[PITCH] = dynamicOffset[ROLL] = 0;
173
  driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0;
168
  correctionSum[PITCH] = correctionSum[ROLL] = 0;
174
  correctionSum[PITCH] = correctionSum[ROLL] = 0;
169
 
175
 
170
  // Calibrate hardware.
176
  // Calibrate hardware.
171
  analog_calibrate();
177
  analog_calibrate();
172
 
178
 
173
  // reset gyro readings
179
  // reset gyro readings
174
  rate[PITCH] = rate[ROLL] = yawRate = 0;
180
  // rate_ATT[PITCH] = rate_ATT[ROLL] = yawRate = 0;
175
 
181
 
176
  // reset gyro integrals to acc guessing
182
  // reset gyro integrals to acc guessing
177
  setStaticAttitudeAngles();
183
  setStaticAttitudeAngles();
178
  yawAngle = 0;
184
  yawAngleDiff = 0;
179
 
185
 
180
  // update compass course to current heading
186
  // update compass course to current heading
181
  compassCourse = compassHeading;
187
  compassCourse = compassHeading;
182
 
188
 
183
  // Inititialize YawGyroIntegral value with current compass heading
189
  // Inititialize YawGyroIntegral value with current compass heading
184
  yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW;
190
  yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW;
185
 
191
 
186
  // Servo_On(); //enable servo output
192
  // Servo_On(); //enable servo output
187
}
193
}
188
 
194
 
189
/************************************************************************
195
/************************************************************************
190
 * Get sensor data from the analog module, and release the ADC          
196
 * Get sensor data from the analog module, and release the ADC          
191
 * TODO: Ultimately, the analog module could do this (instead of dumping
197
 * TODO: Ultimately, the analog module could do this (instead of dumping
192
 * the values into variables).
198
 * the values into variables).
193
 * The rate variable end up in a range of about [-1024, 1023].
199
 * The rate variable end up in a range of about [-1024, 1023].
194
 * When scaled down by CONTROL_SCALING, the interval is about [-256, 256].
-
 
195
 *************************************************************************/
200
 *************************************************************************/
196
void getAnalogData(void) {
201
void getAnalogData(void) {
197
  uint8_t axis;
202
  uint8_t axis;
198
 
203
 
199
  for (axis=PITCH; axis <=ROLL; axis++) {
204
  for (axis=PITCH; axis <=ROLL; axis++) {
200
    rate_PID[axis]     = (gyro_PID[axis] + dynamicOffset[axis]) / HIRES_GYRO_INTEGRATION_FACTOR;
205
    rate_PID[axis]     = (gyro_PID[axis] + driftComp[axis]) / HIRES_GYRO_INTEGRATION_FACTOR;
201
    rate[axis]         = (gyro_ATT[axis] + dynamicOffset[axis]) / HIRES_GYRO_INTEGRATION_FACTOR;
206
    rate_ATT[axis]     = (gyro_ATT[axis] + driftComp[axis]) / HIRES_GYRO_INTEGRATION_FACTOR;
202
    differential[axis] = gyroD[axis];
207
    differential[axis] = gyroD[axis];
-
 
208
    averageAcc[axis]  += acc[axis];
203
  }
209
  }
204
 
210
 
-
 
211
  averageAccCount++;
205
  yawRate = yawGyro + dynamicOffsetYaw;
212
  yawRate = yawGyro + driftCompYaw;
206
 
213
 
207
  // We are done reading variables from the analog module.
214
  // We are done reading variables from the analog module.
208
  // Interrupt-driven sensor reading may restart.
215
  // Interrupt-driven sensor reading may restart.
209
  analogDataReady = 0;
216
  analogDataReady = 0;
210
  analog_start();
217
  analog_start();
211
}
218
}
212
 
219
 
213
/*
220
/*
214
 * This is the standard flight-style coordinate system transformation
221
 * This is the standard flight-style coordinate system transformation
215
 * (from airframe-local axes to a ground-based system). For some reason
222
 * (from airframe-local axes to a ground-based system). For some reason
216
 * the MK uses a left-hand coordinate system. The tranformation has been
223
 * the MK uses a left-hand coordinate system. The tranformation has been
217
 * changed accordingly.
224
 * changed accordingly.
218
 */
225
 */
219
void trigAxisCoupling(void) {
226
void trigAxisCoupling(void) {
220
  int16_t cospitch = int_cos(angle[PITCH]);
227
  int16_t cospitch = int_cos(angle[PITCH]);
221
  int16_t cosroll =  int_cos(angle[ROLL]);
228
  int16_t cosroll =  int_cos(angle[ROLL]);
222
  int16_t sinroll =  int_sin(angle[ROLL]);
229
  int16_t sinroll =  int_sin(angle[ROLL]);
223
  int16_t tanpitch = int_tan(angle[PITCH]);
230
  int16_t tanpitch = int_tan(angle[PITCH]);
224
#define ANTIOVF 1024
231
#define ANTIOVF 512
225
  ACRate[PITCH] =             ((int32_t) rate[PITCH] * cosroll - (int32_t)yawRate * sinroll) / (int32_t)MATH_UNIT_FACTOR;
232
  ACRate[PITCH] =                 ((int32_t) rate_ATT[PITCH] * cosroll - (int32_t)yawRate * sinroll) / (int32_t)MATH_UNIT_FACTOR;
226
  ACRate[ROLL] = rate[ROLL] + (((int32_t)rate[PITCH] * sinroll / ANTIOVF * tanpitch + (int32_t)yawRate * int_cos(angle[ROLL]) / ANTIOVF * tanpitch) / ((int32_t)MATH_UNIT_FACTOR / ANTIOVF * MATH_UNIT_FACTOR));
233
  ACRate[ROLL] = rate_ATT[ROLL] + (((int32_t)rate_ATT[PITCH] * sinroll / ANTIOVF * tanpitch + (int32_t)yawRate * int_cos(angle[ROLL]) / ANTIOVF * tanpitch) / ((int32_t)MATH_UNIT_FACTOR / ANTIOVF * MATH_UNIT_FACTOR));
227
  ACYawRate =                 ((int32_t) rate[PITCH] * sinroll) / cospitch + ((int32_t)yawRate * cosroll) / cospitch;
234
  ACYawRate =                     ((int32_t) rate_ATT[PITCH] * sinroll) / cospitch + ((int32_t)yawRate * cosroll) / cospitch;
228
}
235
}
-
 
236
 
229
 
237
// 480 usec with axis coupling - almost no time without.
230
void integrate(void) {
238
void integrate(void) {
231
  // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate.
239
  // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate.
232
  uint8_t axis;
240
  uint8_t axis;
233
 
-
 
234
  if(!looping && (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) {
241
  if(!looping && (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) {
235
    // The rotary rate limiter bit is abused for selecting axis coupling algorithm instead.
242
    // The rotary rate limiter bit is abused for selecting axis coupling algorithm instead.
236
    trigAxisCoupling();    
243
    trigAxisCoupling();
237
  } else {
244
  } else {
238
    ACRate[PITCH] = rate[PITCH];
245
    ACRate[PITCH] = rate_ATT[PITCH];
239
    ACRate[ROLL]  = rate[ROLL];
246
    ACRate[ROLL]  = rate_ATT[ROLL];
240
    ACYawRate = yawRate;
247
    ACYawRate     = yawRate;
241
  }
248
  }
242
 
249
 
243
  /*
250
  /*
244
   * Yaw
251
   * Yaw
245
   * Calculate yaw gyro integral (~ to rotation angle)
252
   * Calculate yaw gyro integral (~ to rotation angle)
246
   * Limit yawGyroHeading proportional to 0 deg to 360 deg
253
   * Limit yawGyroHeading proportional to 0 deg to 360 deg
247
   */
254
   */
248
  yawGyroHeading += ACYawRate;
255
  yawGyroHeading += ACYawRate;
249
 
-
 
250
  // Why is yawAngle not wrapped 'round?
-
 
251
  yawAngle += ACYawRate;
256
  yawAngleDiff += yawRate;
252
 
257
 
253
  if(yawGyroHeading >= YAWOVER360) {
258
  if(yawGyroHeading >= YAWOVER360) {
254
    yawGyroHeading -= YAWOVER360;  // 360 deg. wrap
259
    yawGyroHeading -= YAWOVER360;  // 360 deg. wrap
255
  } else if(yawGyroHeading < 0) {
260
  } else if(yawGyroHeading < 0) {
256
    yawGyroHeading += YAWOVER360;
261
    yawGyroHeading += YAWOVER360;
257
  }
262
  }
258
 
263
 
259
  /*
264
  /*
260
   * Pitch axis integration and range boundary wrap.
265
   * Pitch axis integration and range boundary wrap.
261
   */
266
   */
262
  for (axis=PITCH; axis<=ROLL; axis++) {
267
  for (axis=PITCH; axis<=ROLL; axis++) {
263
    angle[axis] += ACRate[axis];
268
    angle[axis] += ACRate[axis];
264
    if(angle[axis] > PITCHROLLOVER180) {
269
    if(angle[axis] > PITCHROLLOVER180) {
265
      angle[axis] -= PITCHROLLOVER360;
270
      angle[axis] -= PITCHROLLOVER360;
266
    } else if (angle[axis] <= -PITCHROLLOVER180) {
271
    } else if (angle[axis] <= -PITCHROLLOVER180) {
267
      angle[axis] += PITCHROLLOVER360;
272
      angle[axis] += PITCHROLLOVER360;
268
    }
273
    }
269
  }
274
  }
270
}
275
}
271
 
276
 
272
/************************************************************************
277
/************************************************************************
273
 * A kind of 0'th order integral correction, that corrects the integrals
278
 * A kind of 0'th order integral correction, that corrects the integrals
274
 * directly. This is the "gyroAccFactor" stuff in the original code.
279
 * directly. This is the "gyroAccFactor" stuff in the original code.
275
 * There is (there) also a drift compensation
280
 * There is (there) also a drift compensation
276
 * - it corrects the differential of the integral = the gyro offsets.
281
 * - it corrects the differential of the integral = the gyro offsets.
277
 * That should only be necessary with drifty gyros like ENC-03.
282
 * That should only be necessary with drifty gyros like ENC-03.
278
 ************************************************************************/
283
 ************************************************************************/
279
void correctIntegralsByAcc0thOrder(void) {
284
void correctIntegralsByAcc0thOrder(void) {
280
  // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities
285
  // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities
281
  // are less than ....., or reintroduce Kalman.
286
  // are less than ....., or reintroduce Kalman.
282
  // Well actually the Z axis acc. check is not so silly.
287
  // Well actually the Z axis acc. check is not so silly.
283
  uint8_t axis;
288
  uint8_t axis;
284
  int32_t correction;
289
  int32_t correction;
285
  if(!looping && acc[Z] >= -dynamicParams.UserParams[7] && acc[Z] <= dynamicParams.UserParams[7]) {
290
  if(!looping && acc[Z] >= -dynamicParams.UserParams[7] && acc[Z] <= dynamicParams.UserParams[7]) {
286
    DebugOut.Digital[0] = 1;
291
    DebugOut.Digital[0] |= DEBUG_ACC0THORDER;
287
   
292
   
288
    uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!!
293
    uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!!
289
    uint8_t debugFullWeight = 1;
294
    uint8_t debugFullWeight = 1;
290
    int32_t accDerived;
295
    int32_t accDerived;
291
   
296
   
292
    if((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands
297
    if((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active
293
      permilleAcc /= 2;
298
      permilleAcc /= 2;
294
      debugFullWeight = 0;
299
      debugFullWeight = 0;
295
    }
300
    }
296
   
301
 
297
    if(abs(controlYaw) > 25) { // reduce further if yaw stick is active
302
    if((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands
298
      permilleAcc /= 2;
303
      permilleAcc /= 2;
299
      debugFullWeight = 0;
304
      debugFullWeight = 0;
300
    }
305
    }
-
 
306
   
-
 
307
    if (debugFullWeight)
-
 
308
      DebugOut.Digital[1] |= DEBUG_ACC0THORDER;
-
 
309
    else
-
 
310
      DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER;
301
 
311
 
302
    /*
312
    /*
303
     * Add to each sum: The amount by which the angle is changed just below.
313
     * Add to each sum: The amount by which the angle is changed just below.
304
     */
314
     */
305
    for (axis=PITCH; axis<=ROLL; axis++) {
315
    for (axis=PITCH; axis<=ROLL; axis++) {
306
      accDerived = getAngleEstimateFromAcc(axis);
316
      accDerived = getAngleEstimateFromAcc(axis);
307
      DebugOut.Analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL;
317
      DebugOut.Analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL;
308
     
318
 
309
      // 1000 * the correction amount that will be added to the gyro angle in next line.
319
      // 1000 * the correction amount that will be added to the gyro angle in next line.
310
      correction = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000;
320
      correction = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000;
311
      angle[axis] = ((int32_t)(1000 - permilleAcc) * angle[axis] + (int32_t)permilleAcc * accDerived) / 1000L;
321
      angle[axis] = ((int32_t)(1000L - permilleAcc) * angle[axis] + (int32_t)permilleAcc * accDerived) / 1000L;
312
 
-
 
313
      correctionSum[axis] += angle[axis] - correction;
322
      correctionSum[axis] += angle[axis] - correction;
314
   
-
 
315
      // There should not be a risk of overflow here, since the integrals do not exceed a few 100000.
-
 
316
      // change = ((1000 - permilleAcc) * angle[axis] + permilleAcc * accDerived) / 1000 - angle[axis]
-
 
317
      // = (1000 * angle[axis] - permilleAcc * angle[axis] + permilleAcc * accDerived) / 1000 - angle[axis]
-
 
318
      // = (- permilleAcc * angle[axis] + permilleAcc * accDerived) / 1000
-
 
319
      // = permilleAcc * (accDerived - angle[axis]) / 1000
-
 
320
     
-
 
321
      // Experiment: Do not acutally apply the correction. See if drift compensation does that.
-
 
322
      // angle[axis] += correction / 1000;
323
      DebugOut.Analog[16+axis] = angle[axis] - correction;
323
    }
324
    }
324
       
-
 
325
    DebugOut.Digital[1] = debugFullWeight;
-
 
326
  } else {
325
  } else {
-
 
326
    DebugOut.Digital[0] &= ~DEBUG_ACC0THORDER;
-
 
327
    DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER;
-
 
328
    DebugOut.Analog[9] = 0;
327
    DebugOut.Digital[0] = 0;
329
    DebugOut.Analog[10] = 0;
-
 
330
 
-
 
331
    DebugOut.Analog[16] = 0;
-
 
332
    DebugOut.Analog[17] = 0;
-
 
333
    // experiment: Kill drift compensation updates when not flying smooth.
-
 
334
    correctionSum[PITCH] = correctionSum[ROLL] = 0;
328
  }
335
  }
329
}
336
}
330
 
337
 
331
/************************************************************************
338
/************************************************************************
332
 * This is an attempt to correct not the error in the angle integrals
339
 * This is an attempt to correct not the error in the angle integrals
333
 * (that happens in correctIntegralsByAcc0thOrder above) but rather the
340
 * (that happens in correctIntegralsByAcc0thOrder above) but rather the
334
 * cause of it: Gyro drift, vibration and rounding errors.
341
 * cause of it: Gyro drift, vibration and rounding errors.
335
 * All the corrections made in correctIntegralsByAcc0thOrder over
342
 * All the corrections made in correctIntegralsByAcc0thOrder over
336
 * DRIFTCORRECTION_TIME cycles are summed up. This number is
343
 * DRIFTCORRECTION_TIME cycles are summed up. This number is
337
 * then divided by DRIFTCORRECTION_TIME to get the approx.
344
 * then divided by DRIFTCORRECTION_TIME to get the approx.
338
 * correction that should have been applied to each iteration to fix
345
 * correction that should have been applied to each iteration to fix
339
 * the error. This is then added to the dynamic offsets.
346
 * the error. This is then added to the dynamic offsets.
340
 ************************************************************************/
347
 ************************************************************************/
341
// 2 times / sec. = 488/2
348
// 2 times / sec. = 488/2
342
#define DRIFTCORRECTION_TIME 256L
349
#define DRIFTCORRECTION_TIME 256L
343
void driftCorrection(void) {
350
void driftCorrection(void) {
344
  static int16_t timer = DRIFTCORRECTION_TIME;
351
  static int16_t timer = DRIFTCORRECTION_TIME;
345
  int16_t deltaCorrection;
352
  int16_t deltaCorrection;
346
  uint8_t axis;
353
  uint8_t axis;
347
  if (! --timer) {
354
  if (! --timer) {
348
    timer = DRIFTCORRECTION_TIME;
355
    timer = DRIFTCORRECTION_TIME;
349
    for (axis=PITCH; axis<=ROLL; axis++) {
356
    for (axis=PITCH; axis<=ROLL; axis++) {
350
      // Take the sum of corrections applied, add it to delta
357
      // Take the sum of corrections applied, add it to delta
351
      deltaCorrection = ((correctionSum[axis] + DRIFTCORRECTION_TIME / 2) * HIRES_GYRO_INTEGRATION_FACTOR) / DRIFTCORRECTION_TIME;
358
      deltaCorrection = (correctionSum[axis] * HIRES_GYRO_INTEGRATION_FACTOR + DRIFTCORRECTION_TIME / 2) / DRIFTCORRECTION_TIME;
352
      // Add the delta to the compensation. So positive delta means, gyro should have higher value.
359
      // Add the delta to the compensation. So positive delta means, gyro should have higher value.
353
      dynamicOffset[axis] += deltaCorrection / staticParams.GyroAccTrim;
360
      driftComp[axis] += deltaCorrection / staticParams.GyroAccTrim;
354
      CHECK_MIN_MAX(dynamicOffset[axis], -staticParams.DriftComp, staticParams.DriftComp);
361
      CHECK_MIN_MAX(driftComp[axis], -staticParams.DriftComp, staticParams.DriftComp);
355
      DebugOut.Analog[11 + axis] = correctionSum[axis];
362
      // DebugOut.Analog[11 + axis] = correctionSum[axis];
-
 
363
 
-
 
364
      DebugOut.Analog[18+axis] = deltaCorrection / staticParams.GyroAccTrim;
356
      DebugOut.Analog[28 + axis] = dynamicOffset[axis];
365
      DebugOut.Analog[28+axis] = driftComp[axis];
-
 
366
 
357
      correctionSum[axis] = 0;
367
      correctionSum[axis] = 0;
358
    }
368
    }
359
  }
369
  }
360
}
370
}
361
 
371
 
362
/************************************************************************
372
/************************************************************************
363
 * Main procedure.
373
 * Main procedure.
364
 ************************************************************************/
374
 ************************************************************************/
365
void calculateFlightAttitude(void) {  
375
void calculateFlightAttitude(void) {  
-
 
376
  // part1: 550 usec.
-
 
377
  // part1a: 550 usec.
-
 
378
  // part1b: 60 usec.
366
  getAnalogData();
379
  getAnalogData();
-
 
380
  // end part1b
367
  integrate();
381
  integrate();
-
 
382
  // end part1a
-
 
383
 
368
 
384
 
369
  DebugOut.Analog[6] = ACRate[PITCH];
385
  DebugOut.Analog[6] = ACRate[PITCH];
370
  DebugOut.Analog[7] = ACRate[ROLL];
386
  DebugOut.Analog[7] = ACRate[ROLL];
371
  DebugOut.Analog[8] = ACYawRate;
387
  DebugOut.Analog[8] = ACYawRate;
372
 
388
 
373
  DebugOut.Analog[3] = rate_PID[PITCH];
389
  DebugOut.Analog[3] = rate_PID[PITCH];
374
  DebugOut.Analog[4] = rate_PID[ROLL];
390
  DebugOut.Analog[4] = rate_PID[ROLL];
375
  DebugOut.Analog[5] = yawRate;
391
  DebugOut.Analog[5] = yawRate;
376
 
392
 
377
#ifdef ATTITUDE_USE_ACC_SENSORS
393
#ifdef ATTITUDE_USE_ACC_SENSORS
378
  correctIntegralsByAcc0thOrder();
394
  correctIntegralsByAcc0thOrder();
379
  driftCorrection();
395
  driftCorrection();
380
#endif
396
#endif
-
 
397
  // end part1
381
}
398
}
382
 
-
 
383
/*
399
 
384
  void updateCompass(void) {
400
void updateCompass(void) {
385
  int16_t w, v, r,correction, error;
401
  int16_t w, v, r,correction, error;
386
 
402
 
387
  if(compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) {
403
  if(compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) {
-
 
404
    if (controlMixer_testCompassCalState()) {
388
  setCompassCalState();
405
      compassCalState++;
-
 
406
      if(compassCalState < 5) beepNumber(compassCalState);
-
 
407
      else beep(1000);
-
 
408
    }
389
  } else {
409
  } else {
390
  // get maximum attitude angle
410
    // get maximum attitude angle
391
  w = abs(pitchAngle / 512);
411
    w = abs(angle[PITCH] / 512);
392
  v = abs(rollAngle / 512);
412
    v = abs(angle[ROLL]  / 512);
393
  if(v > w) w = v;
413
    if(v > w) w = v;
394
  correction = w / 8 + 1;
414
    correction = w / 8 + 1;
395
  // calculate the deviation of the yaw gyro heading and the compass heading
415
    // calculate the deviation of the yaw gyro heading and the compass heading
396
  if (compassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined
416
    if (compassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined
397
  else error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) % 360) - 180;
417
    else error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) % 360) - 180;
398
  if(abs(yawRate) > 128) { // spinning fast
418
    if(abs(yawRate) > 128) { // spinning fast
399
  error = 0;
419
      error = 0;
400
  }
420
    }
401
  if(!badCompassHeading && w < 25) {
421
    if(!badCompassHeading && w < 25) {
-
 
422
      yawGyroDrift += error;
402
  if(updateCompassCourse) {
423
      if(updateCompassCourse) {
403
  beep(200);
424
        beep(200);
404
  yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW;
425
        yawGyroHeading = (int32_t)compassHeading * GYRO_DEG_FACTOR_YAW;
405
  compassCourse = (int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW);
426
        compassCourse = compassHeading; //(int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW);
406
  updateCompassCourse = 0;
427
        updateCompassCourse = 0;
407
  }
428
      }
408
  }
429
    }
409
  yawGyroHeading += (error * 8) / correction;
430
    yawGyroHeading += (error * 8) / correction;
410
  w = (w * dynamicParams.CompassYawEffect) / 32;
431
    w = (w * dynamicParams.CompassYawEffect) / 32;
411
  w = dynamicParams.CompassYawEffect - w;
432
    w = dynamicParams.CompassYawEffect - w;
412
  if(w >= 0) {
433
    if(w >= 0) {
413
  if(!badCompassHeading) {
434
      if(!badCompassHeading) {
414
  v = 64 + (maxControlPitch + maxControlRoll) / 8;
435
        v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 8;
415
  // calc course deviation
436
        // calc course deviation
416
  r = ((540 + (yawGyroHeading / GYRO_DEG_FACTOR_YAW) - compassCourse) % 360) - 180;
437
        r = ((540 + (yawGyroHeading / GYRO_DEG_FACTOR_YAW) - compassCourse) % 360) - 180;
417
  v = (r * w) / v; // align to compass course
438
        v = (r * w) / v; // align to compass course
418
  // limit yaw rate
439
        // limit yaw rate
419
  w = 3 * dynamicParams.CompassYawEffect;
440
        w = 3 * dynamicParams.CompassYawEffect;
420
  if (v > w) v = w;
441
        if (v > w) v = w;
421
  else if (v < -w) v = -w;
442
        else if (v < -w) v = -w;
422
  yawAngle += v;
443
        yawAngleDiff += v;
423
  }
444
      }
424
  else
445
      else
425
  { // wait a while
446
        { // wait a while
426
  badCompassHeading--;
447
          badCompassHeading--;
427
  }
-
 
428
  }
448
        }
429
  else {  // ignore compass at extreme attitudes for a while
449
    } else {  // ignore compass at extreme attitudes for a while
430
  badCompassHeading = 500;
450
      badCompassHeading = 500;
431
  }
-
 
432
  }
451
    }
433
  }
452
  }
434
*/
453
}
435
 
454
 
436
/*
455
/*
437
 * This is part of an experiment to measure average sensor offsets caused by motor vibration,
456
 * This is part of an experiment to measure average sensor offsets caused by motor vibration,
438
 * and to compensate them away. It brings about some improvement, but no miracles.
457
 * and to compensate them away. It brings about some improvement, but no miracles.
439
 * As long as the left stick is kept in the start-motors position, the dynamic compensation
458
 * As long as the left stick is kept in the start-motors position, the dynamic compensation
440
 * will measure the effect of vibration, to use for later compensation. So, one should keep
459
 * will measure the effect of vibration, to use for later compensation. So, one should keep
441
 * the stick in the start-motors position for a few seconds, till all motors run (at the wrong
460
 * the stick in the start-motors position for a few seconds, till all motors run (at the wrong
442
 * speed unfortunately... must find a better way)
461
 * speed unfortunately... must find a better way)
443
 */
462
 */
444
/*
463
/*
445
  void attitude_startDynamicCalibration(void) {
464
  void attitude_startDynamicCalibration(void) {
446
  dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0;
465
  dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0;
447
  savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000;
466
  savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000;
448
  }
467
  }
449
 
468
 
450
  void attitude_continueDynamicCalibration(void) {
469
  void attitude_continueDynamicCalibration(void) {
451
  // measure dynamic offset now...
470
  // measure dynamic offset now...
452
  dynamicCalPitch += hiResPitchGyro;
471
  dynamicCalPitch += hiResPitchGyro;
453
  dynamicCalRoll += hiResRollGyro;
472
  dynamicCalRoll += hiResRollGyro;
454
  dynamicCalYaw += rawYawGyroSum;
473
  dynamicCalYaw += rawYawGyroSum;
455
  dynamicCalCount++;
474
  dynamicCalCount++;
456
 
475
 
457
  // Param6: Manual mode. The offsets are taken from Param7 and Param8.
476
  // Param6: Manual mode. The offsets are taken from Param7 and Param8.
458
  if (dynamicParams.UserParam6 || 1) { // currently always enabled.
477
  if (dynamicParams.UserParam6 || 1) { // currently always enabled.
459
  // manual mode
478
  // manual mode
460
  dynamicOffsetPitch = dynamicParams.UserParam7 - 128;
479
  driftCompPitch = dynamicParams.UserParam7 - 128;
461
  dynamicOffsetRoll = dynamicParams.UserParam8 - 128;
480
  driftCompRoll = dynamicParams.UserParam8 - 128;
462
  } else {
481
  } else {
463
  // use the sampled value (does not seem to work so well....)
482
  // use the sampled value (does not seem to work so well....)
464
  dynamicOffsetPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount;
483
  driftCompPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount;
465
  dynamicOffsetRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount;
484
  driftCompRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount;
466
  dynamicOffsetYaw = -dynamicCalYaw / dynamicCalCount;
485
  driftCompYaw = -dynamicCalYaw / dynamicCalCount;
467
  }
486
  }
468
 
487
 
469
  // keep resetting these meanwhile, to avoid accumulating errors.
488
  // keep resetting these meanwhile, to avoid accumulating errors.
470
  setStaticAttitudeIntegrals();
489
  setStaticAttitudeIntegrals();
471
  yawAngle = 0;
490
  yawAngle = 0;
472
  }
491
  }
473
*/
492
*/
474
 
493