Subversion Repositories FlightCtrl

Rev

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

Rev 1978 Rev 1980
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zulässig ist.
7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
18
// + eindeutig als Ursprung verlinkt und genannt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for 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
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
/************************************************************************/
52
/************************************************************************/
53
/* 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
 
61
 
62
// For scope debugging only!
62
// For scope debugging only!
63
#include "rc.h"
63
#include "rc.h"
64
 
64
 
65
// where our main data flow comes from.
65
// where our main data flow comes from.
66
#include "analog.h"
66
#include "analog.h"
67
 
67
 
68
#include "configuration.h"
68
#include "configuration.h"
69
#include "output.h"
69
#include "output.h"
70
 
70
 
71
// Some calculations are performed depending on some stick related things.
71
// Some calculations are performed depending on some stick related things.
72
#include "controlMixer.h"
72
#include "controlMixer.h"
73
 
73
 
74
#define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;}
74
#define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;}
75
 
75
 
76
/*
76
/*
77
 * Gyro readings, as read from the analog module. It would have been nice to flow
77
 * Gyro readings, as read from the analog module. It would have been nice to flow
78
 * them around between the different calculations as a struct or array (doing
78
 * them around between the different calculations as a struct or array (doing
79
 * things functionally without side effects) but this is shorter and probably
79
 * things functionally without side effects) but this is shorter and probably
80
 * faster too.
80
 * faster too.
81
 * The variables are overwritten at each attitude calculation invocation - the values
81
 * The variables are overwritten at each attitude calculation invocation - the values
82
 * are not preserved or reused.
82
 * are not preserved or reused.
83
 */
83
 */
84
int16_t rate_ATT[2], yawRate;
84
int16_t rate_ATT[2], yawRate;
85
 
85
 
86
// With different (less) filtering
86
// With different (less) filtering
87
int16_t rate_PID[2];
87
int16_t rate_PID[2];
88
int16_t differential[2];
88
int16_t differential[2];
89
 
89
 
90
/*
90
/*
91
 * Gyro readings, after performing "axis coupling" - that is, the transfomation
91
 * Gyro readings, after performing "axis coupling" - that is, the transfomation
92
 * of rotation rates from the airframe-local coordinate system to a ground-fixed
92
 * of rotation rates from the airframe-local coordinate system to a ground-fixed
93
 * coordinate system. If axis copling is disabled, the gyro readings will be
93
 * coordinate system. If axis copling is disabled, the gyro readings will be
94
 * copied into these directly.
94
 * copied into these directly.
95
 * These are global for the same pragmatic reason as with the gyro readings.
95
 * These are global for the same pragmatic reason as with the gyro readings.
96
 * The variables are overwritten at each attitude calculation invocation - the values
96
 * The variables are overwritten at each attitude calculation invocation - the values
97
 * are not preserved or reused.
97
 * are not preserved or reused.
98
 */
98
 */
99
int16_t ACRate[2], ACYawRate;
99
int16_t ACRate[2], ACYawRate;
100
 
100
 
101
/*
101
/*
102
 * Gyro integrals. These are the rotation angles of the airframe compared to the
102
 * Gyro integrals. These are the rotation angles of the airframe compared to the
103
 * horizontal plane, yaw relative to yaw at start.
103
 * horizontal plane, yaw relative to yaw at start.
104
 */
104
 */
105
int32_t angle[2], yawAngleDiff;
105
int32_t angle[2], yawAngleDiff;
106
 
106
 
107
int readingHeight = 0;
107
int readingHeight = 0;
108
 
108
 
109
// Yaw angle and compass stuff.
109
// Yaw angle and compass stuff.
110
 
110
 
111
// This is updated/written from MM3. Negative angle indicates invalid data.
111
// This is updated/written from MM3. Negative angle indicates invalid data.
112
int16_t compassHeading = -1;
112
int16_t compassHeading = -1;
113
 
113
 
114
// This is NOT updated from MM3. Negative angle indicates invalid data.
114
// This is NOT updated from MM3. Negative angle indicates invalid data.
115
int16_t compassCourse = -1;
115
int16_t compassCourse = -1;
116
 
116
 
117
// The difference between the above 2 (heading - course) on a -180..179 degree interval.
117
// The difference between the above 2 (heading - course) on a -180..179 degree interval.
118
// Not necessary. Never read anywhere.
118
// Not necessary. Never read anywhere.
119
// int16_t compassOffCourse = 0;
119
// int16_t compassOffCourse = 0;
120
 
120
 
121
uint8_t updateCompassCourse = 0;
121
uint8_t updateCompassCourse = 0;
122
uint8_t compassCalState = 0;
122
uint8_t compassCalState = 0;
123
uint16_t ignoreCompassTimer = 500;
123
uint16_t ignoreCompassTimer = 500;
124
 
124
 
125
int32_t yawGyroHeading; // Yaw Gyro Integral supported by compass
125
int32_t yawGyroHeading; // Yaw Gyro Integral supported by compass
126
int16_t yawGyroDrift;
126
int16_t yawGyroDrift;
127
 
127
 
128
#define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L)
128
#define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L)
129
#define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L)
129
#define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L)
130
#define YAWOVER360       (GYRO_DEG_FACTOR_YAW * 360L)
130
#define YAWOVER360       (GYRO_DEG_FACTOR_YAW * 360L)
131
 
131
 
132
int16_t correctionSum[2] = { 0, 0 };
132
int16_t correctionSum[2] = { 0, 0 };
133
 
133
 
134
// For NaviCTRL use.
134
// For NaviCTRL use.
135
int16_t averageAcc[2] = { 0, 0 }, averageAccCount = 0;
135
int16_t averageAcc[2] = { 0, 0 }, averageAccCount = 0;
136
 
136
 
137
/*
137
/*
138
 * Experiment: Compensating for dynamic-induced gyro biasing.
138
 * Experiment: Compensating for dynamic-induced gyro biasing.
139
 */
139
 */
140
int16_t driftComp[2] = { 0, 0 }, driftCompYaw = 0;
140
int16_t driftComp[2] = { 0, 0 }, driftCompYaw = 0;
141
// int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0;
141
// int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0;
142
// int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw;
142
// int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw;
143
// int16_t dynamicCalCount;
143
// int16_t dynamicCalCount;
-
 
144
 
-
 
145
uint16_t accVector;
144
 
146
 
145
/************************************************************************
147
/************************************************************************
146
 * Set inclination angles from the acc. sensor data.                    
148
 * Set inclination angles from the acc. sensor data.                    
147
 * If acc. sensors are not used, set to zero.                          
149
 * If acc. sensors are not used, set to zero.                          
148
 * TODO: One could use inverse sine to calculate the angles more        
150
 * TODO: One could use inverse sine to calculate the angles more        
149
 * accurately, but since: 1) the angles are rather small at times when
151
 * accurately, but since: 1) the angles are rather small at times when
150
 * it makes sense to set the integrals (standing on ground, or flying at  
152
 * it makes sense to set the integrals (standing on ground, or flying at  
151
 * constant speed, and 2) at small angles a, sin(a) ~= constant * a,    
153
 * constant speed, and 2) at small angles a, sin(a) ~= constant * a,    
152
 * it is hardly worth the trouble.                                      
154
 * it is hardly worth the trouble.                                      
153
 ************************************************************************/
155
 ************************************************************************/
154
 
156
 
155
int32_t getAngleEstimateFromAcc(uint8_t axis) {
157
int32_t getAngleEstimateFromAcc(uint8_t axis) {
156
  return GYRO_ACC_FACTOR * (int32_t) filteredAcc[axis];
158
  return GYRO_ACC_FACTOR * (int32_t) filteredAcc[axis];
157
}
159
}
158
 
160
 
159
void setStaticAttitudeAngles(void) {
161
void setStaticAttitudeAngles(void) {
160
#ifdef ATTITUDE_USE_ACC_SENSORS
162
#ifdef ATTITUDE_USE_ACC_SENSORS
161
  angle[PITCH] = getAngleEstimateFromAcc(PITCH);
163
  angle[PITCH] = getAngleEstimateFromAcc(PITCH);
162
  angle[ROLL] = getAngleEstimateFromAcc(ROLL);
164
  angle[ROLL] = getAngleEstimateFromAcc(ROLL);
163
#else
165
#else
164
  angle[PITCH] = angle[ROLL] = 0;
166
  angle[PITCH] = angle[ROLL] = 0;
165
#endif
167
#endif
166
}
168
}
167
 
169
 
168
/************************************************************************
170
/************************************************************************
169
 * Neutral Readings                                                    
171
 * Neutral Readings                                                    
170
 ************************************************************************/
172
 ************************************************************************/
171
void attitude_setNeutral(void) {
173
void attitude_setNeutral(void) {
172
  // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway.
174
  // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway.
173
  dynamicParams.axisCoupling1 = dynamicParams.axisCoupling2 = 0;
175
  dynamicParams.axisCoupling1 = dynamicParams.axisCoupling2 = 0;
174
 
176
 
175
  driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0;
177
  driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0;
176
  correctionSum[PITCH] = correctionSum[ROLL] = 0;
178
  correctionSum[PITCH] = correctionSum[ROLL] = 0;
177
 
179
 
178
  // Calibrate hardware.
180
  // Calibrate hardware.
179
  analog_setNeutral();
181
  analog_setNeutral();
180
 
182
 
181
  // reset gyro integrals to acc guessing
183
  // reset gyro integrals to acc guessing
182
  setStaticAttitudeAngles();
184
  setStaticAttitudeAngles();
183
  yawAngleDiff = 0;
185
  yawAngleDiff = 0;
184
 
186
 
185
  // update compass course to current heading
187
  // update compass course to current heading
186
  compassCourse = compassHeading;
188
  compassCourse = compassHeading;
187
 
189
 
188
  // Inititialize YawGyroIntegral value with current compass heading
190
  // Inititialize YawGyroIntegral value with current compass heading
189
  yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW;
191
  yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW;
190
 
192
 
191
  // Servo_On(); //enable servo output
193
  // Servo_On(); //enable servo output
192
}
194
}
193
 
195
 
194
/************************************************************************
196
/************************************************************************
195
 * Get sensor data from the analog module, and release the ADC          
197
 * Get sensor data from the analog module, and release the ADC          
196
 * TODO: Ultimately, the analog module could do this (instead of dumping
198
 * TODO: Ultimately, the analog module could do this (instead of dumping
197
 * the values into variables).
199
 * the values into variables).
198
 * The rate variable end up in a range of about [-1024, 1023].
200
 * The rate variable end up in a range of about [-1024, 1023].
199
 *************************************************************************/
201
 *************************************************************************/
200
void getAnalogData(void) {
202
void getAnalogData(void) {
201
  uint8_t axis;
203
  uint8_t axis;
202
 
204
 
203
  analog_update();
205
  analog_update();
204
 
206
 
205
  for (axis = PITCH; axis <= ROLL; axis++) {
207
  for (axis = PITCH; axis <= ROLL; axis++) {
206
    rate_PID[axis] = gyro_PID[axis] + driftComp[axis];
208
    rate_PID[axis] = gyro_PID[axis] + driftComp[axis];
207
    rate_ATT[axis] = gyro_ATT[axis] + driftComp[axis];
209
    rate_ATT[axis] = gyro_ATT[axis] + driftComp[axis];
208
    differential[axis] = gyroD[axis];
210
    differential[axis] = gyroD[axis];
209
    averageAcc[axis] += acc[axis];
211
    averageAcc[axis] += acc[axis];
210
  }
212
  }
211
 
213
 
212
  averageAccCount++;
214
  averageAccCount++;
213
  yawRate = yawGyro + driftCompYaw;
215
  yawRate = yawGyro + driftCompYaw;
214
 
216
 
215
  // We are done reading variables from the analog module.
217
  // We are done reading variables from the analog module.
216
  // Interrupt-driven sensor reading may restart.
218
  // Interrupt-driven sensor reading may restart.
217
  startAnalogConversionCycle();
219
  startAnalogConversionCycle();
218
}
220
}
219
 
221
 
220
/*
222
/*
221
 * This is the standard flight-style coordinate system transformation
223
 * This is the standard flight-style coordinate system transformation
222
 * (from airframe-local axes to a ground-based system). For some reason
224
 * (from airframe-local axes to a ground-based system). For some reason
223
 * the MK uses a left-hand coordinate system. The tranformation has been
225
 * the MK uses a left-hand coordinate system. The tranformation has been
224
 * changed accordingly.
226
 * changed accordingly.
225
 */
227
 */
226
void trigAxisCoupling(void) {
228
void trigAxisCoupling(void) {
227
  int16_t cospitch = int_cos(angle[PITCH]);
229
  int16_t cospitch = int_cos(angle[PITCH]);
228
  int16_t cosroll = int_cos(angle[ROLL]);
230
  int16_t cosroll = int_cos(angle[ROLL]);
229
  int16_t sinroll = int_sin(angle[ROLL]);
231
  int16_t sinroll = int_sin(angle[ROLL]);
230
 
232
 
231
  ACRate[PITCH] = (((int32_t)rate_ATT[PITCH] * cosroll - (int32_t)yawRate
233
  ACRate[PITCH] = (((int32_t)rate_ATT[PITCH] * cosroll - (int32_t)yawRate
232
      * sinroll) >> MATH_UNIT_FACTOR_LOG);
234
      * sinroll) >> MATH_UNIT_FACTOR_LOG);
233
 
235
 
234
  ACRate[ROLL] = rate_ATT[ROLL] + (((((int32_t)rate_ATT[PITCH] * sinroll
236
  ACRate[ROLL] = rate_ATT[ROLL] + (((((int32_t)rate_ATT[PITCH] * sinroll
235
      + (int32_t)yawRate * cosroll) >> MATH_UNIT_FACTOR_LOG) * int_tan(
237
      + (int32_t)yawRate * cosroll) >> MATH_UNIT_FACTOR_LOG) * int_tan(
236
      angle[PITCH])) >> MATH_UNIT_FACTOR_LOG);
238
      angle[PITCH])) >> MATH_UNIT_FACTOR_LOG);
237
 
239
 
238
  ACYawRate = ((int32_t)rate_ATT[PITCH] * sinroll + (int32_t)yawRate * cosroll) / cospitch;
240
  ACYawRate = ((int32_t)rate_ATT[PITCH] * sinroll + (int32_t)yawRate * cosroll) / cospitch;
239
 
241
 
240
  ACYawRate = ((int32_t)rate_ATT[PITCH] * sinroll + (int32_t)yawRate * cosroll) / cospitch;
242
  ACYawRate = ((int32_t)rate_ATT[PITCH] * sinroll + (int32_t)yawRate * cosroll) / cospitch;
241
}
243
}
242
 
244
 
243
// 480 usec with axis coupling - almost no time without.
245
// 480 usec with axis coupling - almost no time without.
244
void integrate(void) {
246
void integrate(void) {
245
  // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate.
247
  // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate.
246
  uint8_t axis;
248
  uint8_t axis;
247
 
249
 
248
  if (staticParams.bitConfig & CFG_AXIS_COUPLING_ACTIVE) {
250
  if (staticParams.bitConfig & CFG_AXIS_COUPLING_ACTIVE) {
249
    trigAxisCoupling();
251
    trigAxisCoupling();
250
  } else {
252
  } else {
251
    ACRate[PITCH] = rate_ATT[PITCH];
253
    ACRate[PITCH] = rate_ATT[PITCH];
252
    ACRate[ROLL] = rate_ATT[ROLL];
254
    ACRate[ROLL] = rate_ATT[ROLL];
253
    ACYawRate = yawRate;
255
    ACYawRate = yawRate;
254
  }
256
  }
255
 
257
 
256
  /*
258
  /*
257
   * Yaw
259
   * Yaw
258
   * Calculate yaw gyro integral (~ to rotation angle)
260
   * Calculate yaw gyro integral (~ to rotation angle)
259
   * Limit yawGyroHeading proportional to 0 deg to 360 deg
261
   * Limit yawGyroHeading proportional to 0 deg to 360 deg
260
   */
262
   */
261
  yawGyroHeading += ACYawRate;
263
  yawGyroHeading += ACYawRate;
262
  yawAngleDiff += yawRate;
264
  yawAngleDiff += yawRate;
263
 
265
 
264
  if (yawGyroHeading >= YAWOVER360) {
266
  if (yawGyroHeading >= YAWOVER360) {
265
    yawGyroHeading -= YAWOVER360; // 360 deg. wrap
267
    yawGyroHeading -= YAWOVER360; // 360 deg. wrap
266
  } else if (yawGyroHeading < 0) {
268
  } else if (yawGyroHeading < 0) {
267
    yawGyroHeading += YAWOVER360;
269
    yawGyroHeading += YAWOVER360;
268
  }
270
  }
269
 
271
 
270
  /*
272
  /*
271
   * Pitch axis integration and range boundary wrap.
273
   * Pitch axis integration and range boundary wrap.
272
   */
274
   */
273
  for (axis = PITCH; axis <= ROLL; axis++) {
275
  for (axis = PITCH; axis <= ROLL; axis++) {
274
    angle[axis] += ACRate[axis];
276
    angle[axis] += ACRate[axis];
275
    if (angle[axis] > PITCHROLLOVER180) {
277
    if (angle[axis] > PITCHROLLOVER180) {
276
      angle[axis] -= PITCHROLLOVER360;
278
      angle[axis] -= PITCHROLLOVER360;
277
    } else if (angle[axis] <= -PITCHROLLOVER180) {
279
    } else if (angle[axis] <= -PITCHROLLOVER180) {
278
      angle[axis] += PITCHROLLOVER360;
280
      angle[axis] += PITCHROLLOVER360;
279
    }
281
    }
280
  }
282
  }
281
}
283
}
282
 
284
 
283
/************************************************************************
285
/************************************************************************
284
 * A kind of 0'th order integral correction, that corrects the integrals
286
 * A kind of 0'th order integral correction, that corrects the integrals
285
 * directly. This is the "gyroAccFactor" stuff in the original code.
287
 * directly. This is the "gyroAccFactor" stuff in the original code.
286
 * There is (there) also a drift compensation
288
 * There is (there) also a drift compensation
287
 * - it corrects the differential of the integral = the gyro offsets.
289
 * - it corrects the differential of the integral = the gyro offsets.
288
 * That should only be necessary with drifty gyros like ENC-03.
290
 * That should only be necessary with drifty gyros like ENC-03.
289
 ************************************************************************/
291
 ************************************************************************/
290
void correctIntegralsByAcc0thOrder(void) {
292
void correctIntegralsByAcc0thOrder(void) {
291
  // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities
293
  // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities
292
  // are less than ....., or reintroduce Kalman.
294
  // are less than ....., or reintroduce Kalman.
293
  // Well actually the Z axis acc. check is not so silly.
295
  // Well actually the Z axis acc. check is not so silly.
294
  uint8_t axis;
296
  uint8_t axis;
295
  int32_t temp;
297
  int32_t temp;
296
  debugOut.digital[0] &= ~DEBUG_ACC0THORDER;
298
  debugOut.digital[0] &= ~DEBUG_ACC0THORDER;
297
  debugOut.digital[1] &= ~DEBUG_ACC0THORDER;
299
  debugOut.digital[1] &= ~DEBUG_ACC0THORDER;
298
 
300
 
-
 
301
  if (accVector <= dynamicParams.maxAccVector) {
-
 
302
    debugOut.digital[0] |= DEBUG_ACC0THORDER;
299
  if (1 /*controlActivity <= dynamicParams.maxControlActivityForAcc*/) {
303
 
300
    uint8_t permilleAcc = staticParams.zerothOrderCorrection;
304
    uint8_t permilleAcc = staticParams.zerothOrderCorrection;
301
    int32_t accDerived;
305
    int32_t accDerived;
302
 
306
 
303
    /*
307
    /*
304
    if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active
308
    if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active
305
      permilleAcc /= 2;
309
      permilleAcc /= 2;
306
      debugFullWeight = 0;
310
      debugFullWeight = 0;
307
    }
311
    }
308
 
312
 
309
    if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands. Replace by controlActivity.
313
    if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands. Replace by controlActivity.
310
      permilleAcc /= 2;
314
      permilleAcc /= 2;
311
      debugFullWeight = 0;
315
      debugFullWeight = 0;
312
    */
316
    */
313
 
317
 
314
    if (controlActivity > 10000) { // reduce effect during stick control activity
318
    if (controlActivity > 10000) { // reduce effect during stick control activity
315
      permilleAcc /= 4;
319
      permilleAcc /= 4;
316
      debugOut.digital[0] |= DEBUG_ACC0THORDER;
320
      debugOut.digital[1] |= DEBUG_ACC0THORDER;
317
      if (controlActivity > 20000) { // reduce effect during stick control activity
321
      if (controlActivity > 20000) { // reduce effect during stick control activity
318
        permilleAcc /= 4;
322
        permilleAcc /= 4;
319
        debugOut.digital[1] |= DEBUG_ACC0THORDER;
323
        debugOut.digital[1] |= DEBUG_ACC0THORDER;
320
      }
324
      }
321
    }
325
    }
322
 
326
 
323
    /*
327
    /*
324
     * Add to each sum: The amount by which the angle is changed just below.
328
     * Add to each sum: The amount by which the angle is changed just below.
325
     */
329
     */
326
    for (axis = PITCH; axis <= ROLL; axis++) {
330
    for (axis = PITCH; axis <= ROLL; axis++) {
327
      accDerived = getAngleEstimateFromAcc(axis);
331
      accDerived = getAngleEstimateFromAcc(axis);
328
      debugOut.analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL;
332
      debugOut.analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL;
329
 
333
 
330
      // 1000 * the correction amount that will be added to the gyro angle in next line.
334
      // 1000 * the correction amount that will be added to the gyro angle in next line.
331
      temp = angle[axis];
335
      temp = angle[axis];
332
      angle[axis] = ((int32_t) (1000L - permilleAcc) * temp
336
      angle[axis] = ((int32_t) (1000L - permilleAcc) * temp
333
          + (int32_t) permilleAcc * accDerived) / 1000L;
337
          + (int32_t) permilleAcc * accDerived) / 1000L;
334
      correctionSum[axis] += angle[axis] - temp;
338
      correctionSum[axis] += angle[axis] - temp;
335
    }
339
    }
336
  } else {
340
  } else {
337
    debugOut.analog[9] = 0;
341
    debugOut.analog[9] = 0;
338
    debugOut.analog[10] = 0;
342
    debugOut.analog[10] = 0;
339
 
343
 
340
    // experiment: Kill drift compensation updates when not flying smooth.
344
    // experiment: Kill drift compensation updates when not flying smooth.
341
    // correctionSum[PITCH] = correctionSum[ROLL] = 0;
345
    // correctionSum[PITCH] = correctionSum[ROLL] = 0;
342
  }
346
  }
343
}
347
}
344
 
348
 
345
/************************************************************************
349
/************************************************************************
346
 * This is an attempt to correct not the error in the angle integrals
350
 * This is an attempt to correct not the error in the angle integrals
347
 * (that happens in correctIntegralsByAcc0thOrder above) but rather the
351
 * (that happens in correctIntegralsByAcc0thOrder above) but rather the
348
 * cause of it: Gyro drift, vibration and rounding errors.
352
 * cause of it: Gyro drift, vibration and rounding errors.
349
 * All the corrections made in correctIntegralsByAcc0thOrder over
353
 * All the corrections made in correctIntegralsByAcc0thOrder over
350
 * DRIFTCORRECTION_TIME cycles are summed up. This number is
354
 * DRIFTCORRECTION_TIME cycles are summed up. This number is
351
 * then divided by DRIFTCORRECTION_TIME to get the approx.
355
 * then divided by DRIFTCORRECTION_TIME to get the approx.
352
 * correction that should have been applied to each iteration to fix
356
 * correction that should have been applied to each iteration to fix
353
 * the error. This is then added to the dynamic offsets.
357
 * the error. This is then added to the dynamic offsets.
354
 ************************************************************************/
358
 ************************************************************************/
355
// 2 times / sec. = 488/2
359
// 2 times / sec. = 488/2
356
#define DRIFTCORRECTION_TIME 256L
360
#define DRIFTCORRECTION_TIME 256L
357
void driftCorrection(void) {
361
void driftCorrection(void) {
358
  static int16_t timer = DRIFTCORRECTION_TIME;
362
  static int16_t timer = DRIFTCORRECTION_TIME;
359
  int16_t deltaCorrection;
363
  int16_t deltaCorrection;
360
  int16_t round;
364
  int16_t round;
361
  uint8_t axis;
365
  uint8_t axis;
362
 
366
 
363
  if (!--timer) {
367
  if (!--timer) {
364
    timer = DRIFTCORRECTION_TIME;
368
    timer = DRIFTCORRECTION_TIME;
365
    for (axis = PITCH; axis <= ROLL; axis++) {
369
    for (axis = PITCH; axis <= ROLL; axis++) {
366
      // Take the sum of corrections applied, add it to delta
370
      // Take the sum of corrections applied, add it to delta
367
      if (correctionSum[axis] >=0)
371
      if (correctionSum[axis] >=0)
368
        round = DRIFTCORRECTION_TIME / 2;
372
        round = DRIFTCORRECTION_TIME / 2;
369
      else
373
      else
370
        round = -DRIFTCORRECTION_TIME / 2;
374
        round = -DRIFTCORRECTION_TIME / 2;
371
      deltaCorrection = (correctionSum[axis] + round) / DRIFTCORRECTION_TIME;
375
      deltaCorrection = (correctionSum[axis] + round) / DRIFTCORRECTION_TIME;
372
      // Add the delta to the compensation. So positive delta means, gyro should have higher value.
376
      // Add the delta to the compensation. So positive delta means, gyro should have higher value.
373
      driftComp[axis] += deltaCorrection / staticParams.driftCompDivider;
377
      driftComp[axis] += deltaCorrection / staticParams.driftCompDivider;
374
      CHECK_MIN_MAX(driftComp[axis], -staticParams.driftCompLimit, staticParams.driftCompLimit);
378
      CHECK_MIN_MAX(driftComp[axis], -staticParams.driftCompLimit, staticParams.driftCompLimit);
375
      // DebugOut.Analog[11 + axis] = correctionSum[axis];
379
      // DebugOut.Analog[11 + axis] = correctionSum[axis];
376
      // DebugOut.Analog[16 + axis] = correctionSum[axis];
380
      // DebugOut.Analog[16 + axis] = correctionSum[axis];
377
      debugOut.analog[28 + axis] = driftComp[axis];
381
      debugOut.analog[28 + axis] = driftComp[axis];
378
 
382
 
379
      correctionSum[axis] = 0;
383
      correctionSum[axis] = 0;
380
    }
384
    }
381
  }
385
  }
382
}
386
}
-
 
387
 
-
 
388
void calculateAccVector(void) {
-
 
389
        uint16_t temp;
-
 
390
        temp = filteredAcc[0]/4;
-
 
391
        accVector = temp * temp;
-
 
392
        temp = filteredAcc[1]/4;
-
 
393
        accVector += temp * temp;
-
 
394
        temp = filteredAcc[2]/4;
-
 
395
        accVector += temp * temp;
-
 
396
        debugOut.analog[19] = accVector;
-
 
397
}
383
 
398
 
384
/************************************************************************
399
/************************************************************************
385
 * Main procedure.
400
 * Main procedure.
386
 ************************************************************************/
401
 ************************************************************************/
387
void calculateFlightAttitude(void) {
402
void calculateFlightAttitude(void) {
388
  getAnalogData();
403
  getAnalogData();
-
 
404
  calculateAccVector();
389
  integrate();
405
  integrate();
390
 
406
 
391
#ifdef ATTITUDE_USE_ACC_SENSORS
407
#ifdef ATTITUDE_USE_ACC_SENSORS
392
  correctIntegralsByAcc0thOrder();
408
  correctIntegralsByAcc0thOrder();
393
  driftCorrection();
409
  driftCorrection();
394
#endif
410
#endif
395
}
411
}
396
 
412
 
397
void updateCompass(void) {
413
void updateCompass(void) {
398
  int16_t w, v, r, correction, error;
414
  int16_t w, v, r, correction, error;
399
 
415
 
400
  if (compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) {
416
  if (compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) {
401
    if (controlMixer_testCompassCalState()) {
417
    if (controlMixer_testCompassCalState()) {
402
      compassCalState++;
418
      compassCalState++;
403
      if (compassCalState < 5)
419
      if (compassCalState < 5)
404
        beepNumber(compassCalState);
420
        beepNumber(compassCalState);
405
      else
421
      else
406
        beep(1000);
422
        beep(1000);
407
    }
423
    }
408
  } else {
424
  } else {
409
    // get maximum attitude angle
425
    // get maximum attitude angle
410
    w = abs(angle[PITCH] / 512);
426
    w = abs(angle[PITCH] / 512);
411
    v = abs(angle[ROLL] / 512);
427
    v = abs(angle[ROLL] / 512);
412
    if (v > w)
428
    if (v > w)
413
      w = v;
429
      w = v;
414
    correction = w / 8 + 1;
430
    correction = w / 8 + 1;
415
    // calculate the deviation of the yaw gyro heading and the compass heading
431
    // calculate the deviation of the yaw gyro heading and the compass heading
416
    if (compassHeading < 0)
432
    if (compassHeading < 0)
417
      error = 0; // disable yaw drift compensation if compass heading is undefined
433
      error = 0; // disable yaw drift compensation if compass heading is undefined
418
    else if (abs(yawRate) > 128) { // spinning fast
434
    else if (abs(yawRate) > 128) { // spinning fast
419
      error = 0;
435
      error = 0;
420
    } else {
436
    } else {
421
      // compassHeading - yawGyroHeading, on a -180..179 deg interval.
437
      // compassHeading - yawGyroHeading, on a -180..179 deg interval.
422
      error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW))
438
      error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW))
423
          % 360) - 180;
439
          % 360) - 180;
424
    }
440
    }
425
    if (!ignoreCompassTimer && w < 25) {
441
    if (!ignoreCompassTimer && w < 25) {
426
      yawGyroDrift += error;
442
      yawGyroDrift += error;
427
      // Basically this gets set if we are in "fix" mode, and when starting.
443
      // Basically this gets set if we are in "fix" mode, and when starting.
428
      if (updateCompassCourse) {
444
      if (updateCompassCourse) {
429
        beep(200);
445
        beep(200);
430
        yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW;
446
        yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW;
431
        compassCourse = compassHeading; //(int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW);
447
        compassCourse = compassHeading; //(int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW);
432
        updateCompassCourse = 0;
448
        updateCompassCourse = 0;
433
      }
449
      }
434
    }
450
    }
435
    yawGyroHeading += (error * 8) / correction;
451
    yawGyroHeading += (error * 8) / correction;
436
 
452
 
437
    /*
453
    /*
438
     w = (w * dynamicParams.CompassYawEffect) / 32;
454
     w = (w * dynamicParams.CompassYawEffect) / 32;
439
     w = dynamicParams.CompassYawEffect - w;
455
     w = dynamicParams.CompassYawEffect - w;
440
     */
456
     */
441
    w = dynamicParams.compassYawEffect - (w * dynamicParams.compassYawEffect)
457
    w = dynamicParams.compassYawEffect - (w * dynamicParams.compassYawEffect)
442
        / 32;
458
        / 32;
443
 
459
 
444
    // As readable formula:
460
    // As readable formula:
445
    // w = dynamicParams.CompassYawEffect * (1-w/32);
461
    // w = dynamicParams.CompassYawEffect * (1-w/32);
446
 
462
 
447
    if (w >= 0) { // maxAttitudeAngle < 32
463
    if (w >= 0) { // maxAttitudeAngle < 32
448
      if (!ignoreCompassTimer) {
464
      if (!ignoreCompassTimer) {
449
        /*v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 8;*/
465
        /*v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 8;*/
450
        v = 64 + controlActivity / 100;
466
        v = 64 + controlActivity / 100;
451
        // yawGyroHeading - compassCourse on a -180..179 degree interval.
467
        // yawGyroHeading - compassCourse on a -180..179 degree interval.
452
        r
468
        r
453
            = ((540 + yawGyroHeading / GYRO_DEG_FACTOR_YAW - compassCourse)
469
            = ((540 + yawGyroHeading / GYRO_DEG_FACTOR_YAW - compassCourse)
454
                % 360) - 180;
470
                % 360) - 180;
455
        v = (r * w) / v; // align to compass course
471
        v = (r * w) / v; // align to compass course
456
        // limit yaw rate
472
        // limit yaw rate
457
        w = 3 * dynamicParams.compassYawEffect;
473
        w = 3 * dynamicParams.compassYawEffect;
458
        if (v > w)
474
        if (v > w)
459
          v = w;
475
          v = w;
460
        else if (v < -w)
476
        else if (v < -w)
461
          v = -w;
477
          v = -w;
462
        yawAngleDiff += v;
478
        yawAngleDiff += v;
463
      } else { // wait a while
479
      } else { // wait a while
464
        ignoreCompassTimer--;
480
        ignoreCompassTimer--;
465
      }
481
      }
466
    } else { // ignore compass at extreme attitudes for a while
482
    } else { // ignore compass at extreme attitudes for a while
467
      ignoreCompassTimer = 500;
483
      ignoreCompassTimer = 500;
468
    }
484
    }
469
  }
485
  }
470
}
486
}
471
 
487
 
472
/*
488
/*
473
 * This is part of an experiment to measure average sensor offsets caused by motor vibration,
489
 * This is part of an experiment to measure average sensor offsets caused by motor vibration,
474
 * and to compensate them away. It brings about some improvement, but no miracles.
490
 * and to compensate them away. It brings about some improvement, but no miracles.
475
 * As long as the left stick is kept in the start-motors position, the dynamic compensation
491
 * As long as the left stick is kept in the start-motors position, the dynamic compensation
476
 * will measure the effect of vibration, to use for later compensation. So, one should keep
492
 * will measure the effect of vibration, to use for later compensation. So, one should keep
477
 * the stick in the start-motors position for a few seconds, till all motors run (at the wrong
493
 * the stick in the start-motors position for a few seconds, till all motors run (at the wrong
478
 * speed unfortunately... must find a better way)
494
 * speed unfortunately... must find a better way)
479
 */
495
 */
480
/*
496
/*
481
 void attitude_startDynamicCalibration(void) {
497
 void attitude_startDynamicCalibration(void) {
482
 dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0;
498
 dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0;
483
 savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000;
499
 savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000;
484
 }
500
 }
485
 
501
 
486
 void attitude_continueDynamicCalibration(void) {
502
 void attitude_continueDynamicCalibration(void) {
487
 // measure dynamic offset now...
503
 // measure dynamic offset now...
488
 dynamicCalPitch += hiResPitchGyro;
504
 dynamicCalPitch += hiResPitchGyro;
489
 dynamicCalRoll += hiResRollGyro;
505
 dynamicCalRoll += hiResRollGyro;
490
 dynamicCalYaw += rawYawGyroSum;
506
 dynamicCalYaw += rawYawGyroSum;
491
 dynamicCalCount++;
507
 dynamicCalCount++;
492
 
508
 
493
 // Param6: Manual mode. The offsets are taken from Param7 and Param8.
509
 // Param6: Manual mode. The offsets are taken from Param7 and Param8.
494
 if (dynamicParams.UserParam6 || 1) { // currently always enabled.
510
 if (dynamicParams.UserParam6 || 1) { // currently always enabled.
495
 // manual mode
511
 // manual mode
496
 driftCompPitch = dynamicParams.UserParam7 - 128;
512
 driftCompPitch = dynamicParams.UserParam7 - 128;
497
 driftCompRoll = dynamicParams.UserParam8 - 128;
513
 driftCompRoll = dynamicParams.UserParam8 - 128;
498
 } else {
514
 } else {
499
 // use the sampled value (does not seem to work so well....)
515
 // use the sampled value (does not seem to work so well....)
500
 driftCompPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount;
516
 driftCompPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount;
501
 driftCompRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount;
517
 driftCompRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount;
502
 driftCompYaw = -dynamicCalYaw / dynamicCalCount;
518
 driftCompYaw = -dynamicCalYaw / dynamicCalCount;
503
 }
519
 }
504
 
520
 
505
 // keep resetting these meanwhile, to avoid accumulating errors.
521
 // keep resetting these meanwhile, to avoid accumulating errors.
506
 setStaticAttitudeIntegrals();
522
 setStaticAttitudeIntegrals();
507
 yawAngle = 0;
523
 yawAngle = 0;
508
 }
524
 }
509
 */
525
 */
510
 
526