Rev 1963 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1963 | Rev 1978 | ||
---|---|---|---|
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 | - | ||
74 | // For Servo_On / Off |
- | |
75 | // #include "timer2.h" |
- | |
76 | 73 | ||
77 | #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;} |
78 | 75 | ||
79 | /* |
76 | /* |
80 | * 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 |
81 | * 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 |
82 | * things functionally without side effects) but this is shorter and probably |
79 | * things functionally without side effects) but this is shorter and probably |
83 | * faster too. |
80 | * faster too. |
84 | * The variables are overwritten at each attitude calculation invocation - the values |
81 | * The variables are overwritten at each attitude calculation invocation - the values |
85 | * are not preserved or reused. |
82 | * are not preserved or reused. |
86 | */ |
83 | */ |
87 | int16_t rate_ATT[2], yawRate; |
84 | int16_t rate_ATT[2], yawRate; |
88 | 85 | ||
89 | // With different (less) filtering |
86 | // With different (less) filtering |
90 | int16_t rate_PID[2]; |
87 | int16_t rate_PID[2]; |
91 | int16_t differential[2]; |
88 | int16_t differential[2]; |
92 | 89 | ||
93 | /* |
90 | /* |
94 | * Gyro readings, after performing "axis coupling" - that is, the transfomation |
91 | * Gyro readings, after performing "axis coupling" - that is, the transfomation |
95 | * 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 |
96 | * 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 |
97 | * copied into these directly. |
94 | * copied into these directly. |
98 | * 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. |
99 | * The variables are overwritten at each attitude calculation invocation - the values |
96 | * The variables are overwritten at each attitude calculation invocation - the values |
100 | * are not preserved or reused. |
97 | * are not preserved or reused. |
101 | */ |
98 | */ |
102 | int16_t ACRate[2], ACYawRate; |
99 | int16_t ACRate[2], ACYawRate; |
103 | 100 | ||
104 | /* |
101 | /* |
105 | * 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 |
106 | * horizontal plane, yaw relative to yaw at start. |
103 | * horizontal plane, yaw relative to yaw at start. |
107 | */ |
104 | */ |
108 | int32_t angle[2], yawAngleDiff; |
105 | int32_t angle[2], yawAngleDiff; |
109 | 106 | ||
110 | int readingHeight = 0; |
107 | int readingHeight = 0; |
111 | 108 | ||
112 | // Yaw angle and compass stuff. |
109 | // Yaw angle and compass stuff. |
113 | 110 | ||
114 | // This is updated/written from MM3. Negative angle indicates invalid data. |
111 | // This is updated/written from MM3. Negative angle indicates invalid data. |
115 | int16_t compassHeading = -1; |
112 | int16_t compassHeading = -1; |
116 | 113 | ||
117 | // This is NOT updated from MM3. Negative angle indicates invalid data. |
114 | // This is NOT updated from MM3. Negative angle indicates invalid data. |
118 | int16_t compassCourse = -1; |
115 | int16_t compassCourse = -1; |
119 | 116 | ||
120 | // 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. |
121 | // Not necessary. Never read anywhere. |
118 | // Not necessary. Never read anywhere. |
122 | // int16_t compassOffCourse = 0; |
119 | // int16_t compassOffCourse = 0; |
123 | 120 | ||
124 | uint8_t updateCompassCourse = 0; |
121 | uint8_t updateCompassCourse = 0; |
125 | uint8_t compassCalState = 0; |
122 | uint8_t compassCalState = 0; |
126 | uint16_t ignoreCompassTimer = 500; |
123 | uint16_t ignoreCompassTimer = 500; |
127 | 124 | ||
128 | int32_t yawGyroHeading; // Yaw Gyro Integral supported by compass |
125 | int32_t yawGyroHeading; // Yaw Gyro Integral supported by compass |
129 | int16_t yawGyroDrift; |
126 | int16_t yawGyroDrift; |
130 | 127 | ||
131 | #define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L) |
128 | #define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L) |
132 | #define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L) |
129 | #define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L) |
133 | #define YAWOVER360 (GYRO_DEG_FACTOR_YAW * 360L) |
130 | #define YAWOVER360 (GYRO_DEG_FACTOR_YAW * 360L) |
134 | 131 | ||
135 | int16_t correctionSum[2] = { 0, 0 }; |
132 | int16_t correctionSum[2] = { 0, 0 }; |
136 | 133 | ||
137 | // For NaviCTRL use. |
134 | // For NaviCTRL use. |
138 | int16_t averageAcc[2] = { 0, 0 }, averageAccCount = 0; |
135 | int16_t averageAcc[2] = { 0, 0 }, averageAccCount = 0; |
139 | 136 | ||
140 | /* |
137 | /* |
141 | * Experiment: Compensating for dynamic-induced gyro biasing. |
138 | * Experiment: Compensating for dynamic-induced gyro biasing. |
142 | */ |
139 | */ |
143 | int16_t driftComp[2] = { 0, 0 }, driftCompYaw = 0; |
140 | int16_t driftComp[2] = { 0, 0 }, driftCompYaw = 0; |
144 | // int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0; |
141 | // int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0; |
145 | // int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw; |
142 | // int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw; |
146 | // int16_t dynamicCalCount; |
143 | // int16_t dynamicCalCount; |
147 | 144 | ||
148 | /************************************************************************ |
145 | /************************************************************************ |
149 | * Set inclination angles from the acc. sensor data. |
146 | * Set inclination angles from the acc. sensor data. |
150 | * If acc. sensors are not used, set to zero. |
147 | * If acc. sensors are not used, set to zero. |
151 | * TODO: One could use inverse sine to calculate the angles more |
148 | * TODO: One could use inverse sine to calculate the angles more |
152 | * accurately, but since: 1) the angles are rather small at times when |
149 | * accurately, but since: 1) the angles are rather small at times when |
153 | * it makes sense to set the integrals (standing on ground, or flying at |
150 | * it makes sense to set the integrals (standing on ground, or flying at |
154 | * constant speed, and 2) at small angles a, sin(a) ~= constant * a, |
151 | * constant speed, and 2) at small angles a, sin(a) ~= constant * a, |
155 | * it is hardly worth the trouble. |
152 | * it is hardly worth the trouble. |
156 | ************************************************************************/ |
153 | ************************************************************************/ |
157 | 154 | ||
158 | int32_t getAngleEstimateFromAcc(uint8_t axis) { |
155 | int32_t getAngleEstimateFromAcc(uint8_t axis) { |
159 | return GYRO_ACC_FACTOR * (int32_t) filteredAcc[axis]; |
156 | return GYRO_ACC_FACTOR * (int32_t) filteredAcc[axis]; |
160 | } |
157 | } |
161 | 158 | ||
162 | void setStaticAttitudeAngles(void) { |
159 | void setStaticAttitudeAngles(void) { |
163 | #ifdef ATTITUDE_USE_ACC_SENSORS |
160 | #ifdef ATTITUDE_USE_ACC_SENSORS |
164 | angle[PITCH] = getAngleEstimateFromAcc(PITCH); |
161 | angle[PITCH] = getAngleEstimateFromAcc(PITCH); |
165 | angle[ROLL] = getAngleEstimateFromAcc(ROLL); |
162 | angle[ROLL] = getAngleEstimateFromAcc(ROLL); |
166 | #else |
163 | #else |
167 | angle[PITCH] = angle[ROLL] = 0; |
164 | angle[PITCH] = angle[ROLL] = 0; |
168 | #endif |
165 | #endif |
169 | } |
166 | } |
170 | 167 | ||
171 | /************************************************************************ |
168 | /************************************************************************ |
172 | * Neutral Readings |
169 | * Neutral Readings |
173 | ************************************************************************/ |
170 | ************************************************************************/ |
174 | void attitude_setNeutral(void) { |
171 | void attitude_setNeutral(void) { |
175 | // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway. |
172 | // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway. |
176 | dynamicParams.axisCoupling1 = dynamicParams.axisCoupling2 = 0; |
173 | dynamicParams.axisCoupling1 = dynamicParams.axisCoupling2 = 0; |
177 | 174 | ||
178 | driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0; |
175 | driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0; |
179 | correctionSum[PITCH] = correctionSum[ROLL] = 0; |
176 | correctionSum[PITCH] = correctionSum[ROLL] = 0; |
180 | 177 | ||
181 | // Calibrate hardware. |
178 | // Calibrate hardware. |
182 | analog_setNeutral(); |
179 | analog_setNeutral(); |
183 | 180 | ||
184 | // reset gyro integrals to acc guessing |
181 | // reset gyro integrals to acc guessing |
185 | setStaticAttitudeAngles(); |
182 | setStaticAttitudeAngles(); |
186 | yawAngleDiff = 0; |
183 | yawAngleDiff = 0; |
187 | 184 | ||
188 | // update compass course to current heading |
185 | // update compass course to current heading |
189 | compassCourse = compassHeading; |
186 | compassCourse = compassHeading; |
190 | 187 | ||
191 | // Inititialize YawGyroIntegral value with current compass heading |
188 | // Inititialize YawGyroIntegral value with current compass heading |
192 | yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
189 | yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
193 | 190 | ||
194 | // Servo_On(); //enable servo output |
191 | // Servo_On(); //enable servo output |
195 | } |
192 | } |
196 | 193 | ||
197 | /************************************************************************ |
194 | /************************************************************************ |
198 | * Get sensor data from the analog module, and release the ADC |
195 | * Get sensor data from the analog module, and release the ADC |
199 | * TODO: Ultimately, the analog module could do this (instead of dumping |
196 | * TODO: Ultimately, the analog module could do this (instead of dumping |
200 | * the values into variables). |
197 | * the values into variables). |
201 | * The rate variable end up in a range of about [-1024, 1023]. |
198 | * The rate variable end up in a range of about [-1024, 1023]. |
202 | *************************************************************************/ |
199 | *************************************************************************/ |
203 | void getAnalogData(void) { |
200 | void getAnalogData(void) { |
204 | uint8_t axis; |
201 | uint8_t axis; |
205 | 202 | ||
206 | analog_update(); |
203 | analog_update(); |
207 | 204 | ||
208 | for (axis = PITCH; axis <= ROLL; axis++) { |
205 | for (axis = PITCH; axis <= ROLL; axis++) { |
209 | rate_PID[axis] = gyro_PID[axis] + driftComp[axis]; |
206 | rate_PID[axis] = gyro_PID[axis] + driftComp[axis]; |
210 | rate_ATT[axis] = gyro_ATT[axis] + driftComp[axis]; |
207 | rate_ATT[axis] = gyro_ATT[axis] + driftComp[axis]; |
211 | differential[axis] = gyroD[axis]; |
208 | differential[axis] = gyroD[axis]; |
212 | averageAcc[axis] += acc[axis]; |
209 | averageAcc[axis] += acc[axis]; |
213 | } |
210 | } |
214 | 211 | ||
215 | averageAccCount++; |
212 | averageAccCount++; |
216 | yawRate = yawGyro + driftCompYaw; |
213 | yawRate = yawGyro + driftCompYaw; |
217 | 214 | ||
218 | // We are done reading variables from the analog module. |
215 | // We are done reading variables from the analog module. |
219 | // Interrupt-driven sensor reading may restart. |
216 | // Interrupt-driven sensor reading may restart. |
220 | startAnalogConversionCycle(); |
217 | startAnalogConversionCycle(); |
221 | } |
218 | } |
222 | 219 | ||
223 | /* |
220 | /* |
224 | * This is the standard flight-style coordinate system transformation |
221 | * This is the standard flight-style coordinate system transformation |
225 | * (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 |
226 | * 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 |
227 | * changed accordingly. |
224 | * changed accordingly. |
228 | */ |
225 | */ |
229 | void trigAxisCoupling(void) { |
226 | void trigAxisCoupling(void) { |
230 | int16_t cospitch = int_cos(angle[PITCH]); |
227 | int16_t cospitch = int_cos(angle[PITCH]); |
231 | int16_t cosroll = int_cos(angle[ROLL]); |
228 | int16_t cosroll = int_cos(angle[ROLL]); |
232 | int16_t sinroll = int_sin(angle[ROLL]); |
229 | int16_t sinroll = int_sin(angle[ROLL]); |
233 | 230 | ||
234 | ACRate[PITCH] = (((int32_t)rate_ATT[PITCH] * cosroll - (int32_t)yawRate |
231 | ACRate[PITCH] = (((int32_t)rate_ATT[PITCH] * cosroll - (int32_t)yawRate |
235 | * sinroll) >> MATH_UNIT_FACTOR_LOG); |
232 | * sinroll) >> MATH_UNIT_FACTOR_LOG); |
236 | 233 | ||
237 | ACRate[ROLL] = rate_ATT[ROLL] + (((((int32_t)rate_ATT[PITCH] * sinroll |
234 | ACRate[ROLL] = rate_ATT[ROLL] + (((((int32_t)rate_ATT[PITCH] * sinroll |
238 | + (int32_t)yawRate * cosroll) >> MATH_UNIT_FACTOR_LOG) * int_tan( |
235 | + (int32_t)yawRate * cosroll) >> MATH_UNIT_FACTOR_LOG) * int_tan( |
239 | angle[PITCH])) >> MATH_UNIT_FACTOR_LOG); |
236 | angle[PITCH])) >> MATH_UNIT_FACTOR_LOG); |
240 | 237 | ||
241 | ACYawRate = ((int32_t)rate_ATT[PITCH] * sinroll + (int32_t)yawRate * cosroll) / cospitch; |
238 | ACYawRate = ((int32_t)rate_ATT[PITCH] * sinroll + (int32_t)yawRate * cosroll) / cospitch; |
242 | 239 | ||
243 | 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; |
244 | } |
241 | } |
245 | 242 | ||
246 | // 480 usec with axis coupling - almost no time without. |
243 | // 480 usec with axis coupling - almost no time without. |
247 | void integrate(void) { |
244 | void integrate(void) { |
248 | // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate. |
245 | // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate. |
249 | uint8_t axis; |
246 | uint8_t axis; |
250 | 247 | ||
251 | if (staticParams.bitConfig & CFG_AXIS_COUPLING_ACTIVE) { |
248 | if (staticParams.bitConfig & CFG_AXIS_COUPLING_ACTIVE) { |
252 | trigAxisCoupling(); |
249 | trigAxisCoupling(); |
253 | } else { |
250 | } else { |
254 | ACRate[PITCH] = rate_ATT[PITCH]; |
251 | ACRate[PITCH] = rate_ATT[PITCH]; |
255 | ACRate[ROLL] = rate_ATT[ROLL]; |
252 | ACRate[ROLL] = rate_ATT[ROLL]; |
256 | ACYawRate = yawRate; |
253 | ACYawRate = yawRate; |
257 | } |
254 | } |
258 | 255 | ||
259 | /* |
256 | /* |
260 | * Yaw |
257 | * Yaw |
261 | * Calculate yaw gyro integral (~ to rotation angle) |
258 | * Calculate yaw gyro integral (~ to rotation angle) |
262 | * Limit yawGyroHeading proportional to 0 deg to 360 deg |
259 | * Limit yawGyroHeading proportional to 0 deg to 360 deg |
263 | */ |
260 | */ |
264 | yawGyroHeading += ACYawRate; |
261 | yawGyroHeading += ACYawRate; |
265 | yawAngleDiff += yawRate; |
262 | yawAngleDiff += yawRate; |
266 | 263 | ||
267 | if (yawGyroHeading >= YAWOVER360) { |
264 | if (yawGyroHeading >= YAWOVER360) { |
268 | yawGyroHeading -= YAWOVER360; // 360 deg. wrap |
265 | yawGyroHeading -= YAWOVER360; // 360 deg. wrap |
269 | } else if (yawGyroHeading < 0) { |
266 | } else if (yawGyroHeading < 0) { |
270 | yawGyroHeading += YAWOVER360; |
267 | yawGyroHeading += YAWOVER360; |
271 | } |
268 | } |
272 | 269 | ||
273 | /* |
270 | /* |
274 | * Pitch axis integration and range boundary wrap. |
271 | * Pitch axis integration and range boundary wrap. |
275 | */ |
272 | */ |
276 | for (axis = PITCH; axis <= ROLL; axis++) { |
273 | for (axis = PITCH; axis <= ROLL; axis++) { |
277 | angle[axis] += ACRate[axis]; |
274 | angle[axis] += ACRate[axis]; |
278 | if (angle[axis] > PITCHROLLOVER180) { |
275 | if (angle[axis] > PITCHROLLOVER180) { |
279 | angle[axis] -= PITCHROLLOVER360; |
276 | angle[axis] -= PITCHROLLOVER360; |
280 | } else if (angle[axis] <= -PITCHROLLOVER180) { |
277 | } else if (angle[axis] <= -PITCHROLLOVER180) { |
281 | angle[axis] += PITCHROLLOVER360; |
278 | angle[axis] += PITCHROLLOVER360; |
282 | } |
279 | } |
283 | } |
280 | } |
284 | } |
281 | } |
285 | 282 | ||
286 | /************************************************************************ |
283 | /************************************************************************ |
287 | * A kind of 0'th order integral correction, that corrects the integrals |
284 | * A kind of 0'th order integral correction, that corrects the integrals |
288 | * directly. This is the "gyroAccFactor" stuff in the original code. |
285 | * directly. This is the "gyroAccFactor" stuff in the original code. |
289 | * There is (there) also a drift compensation |
286 | * There is (there) also a drift compensation |
290 | * - it corrects the differential of the integral = the gyro offsets. |
287 | * - it corrects the differential of the integral = the gyro offsets. |
291 | * That should only be necessary with drifty gyros like ENC-03. |
288 | * That should only be necessary with drifty gyros like ENC-03. |
292 | ************************************************************************/ |
289 | ************************************************************************/ |
293 | void correctIntegralsByAcc0thOrder(void) { |
290 | void correctIntegralsByAcc0thOrder(void) { |
294 | // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities |
291 | // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities |
295 | // are less than ....., or reintroduce Kalman. |
292 | // are less than ....., or reintroduce Kalman. |
296 | // Well actually the Z axis acc. check is not so silly. |
293 | // Well actually the Z axis acc. check is not so silly. |
297 | uint8_t axis; |
294 | uint8_t axis; |
298 | int32_t temp; |
295 | int32_t temp; |
299 | debugOut.digital[0] &= ~DEBUG_ACC0THORDER; |
296 | debugOut.digital[0] &= ~DEBUG_ACC0THORDER; |
300 | debugOut.digital[1] &= ~DEBUG_ACC0THORDER; |
297 | debugOut.digital[1] &= ~DEBUG_ACC0THORDER; |
301 | 298 | ||
302 | if (1 /*controlActivity <= dynamicParams.maxControlActivityForAcc*/) { |
299 | if (1 /*controlActivity <= dynamicParams.maxControlActivityForAcc*/) { |
303 | uint8_t permilleAcc = staticParams.zerothOrderCorrection; |
300 | uint8_t permilleAcc = staticParams.zerothOrderCorrection; |
304 | int32_t accDerived; |
301 | int32_t accDerived; |
305 | 302 | ||
306 | /* |
303 | /* |
307 | if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active |
304 | if ((controlYaw < -64) || (controlYaw > 64)) { // reduce further if yaw stick is active |
308 | permilleAcc /= 2; |
305 | permilleAcc /= 2; |
309 | debugFullWeight = 0; |
306 | debugFullWeight = 0; |
310 | } |
307 | } |
311 | 308 | ||
312 | if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands. Replace by controlActivity. |
309 | if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands. Replace by controlActivity. |
313 | permilleAcc /= 2; |
310 | permilleAcc /= 2; |
314 | debugFullWeight = 0; |
311 | debugFullWeight = 0; |
315 | */ |
312 | */ |
316 | 313 | ||
317 | if (controlActivity > 10000) { // reduce effect during stick control activity |
314 | if (controlActivity > 10000) { // reduce effect during stick control activity |
318 | permilleAcc /= 4; |
315 | permilleAcc /= 4; |
319 | debugOut.digital[0] |= DEBUG_ACC0THORDER; |
316 | debugOut.digital[0] |= DEBUG_ACC0THORDER; |
320 | if (controlActivity > 20000) { // reduce effect during stick control activity |
317 | if (controlActivity > 20000) { // reduce effect during stick control activity |
321 | permilleAcc /= 4; |
318 | permilleAcc /= 4; |
322 | debugOut.digital[1] |= DEBUG_ACC0THORDER; |
319 | debugOut.digital[1] |= DEBUG_ACC0THORDER; |
323 | } |
320 | } |
324 | } |
321 | } |
325 | 322 | ||
326 | /* |
323 | /* |
327 | * Add to each sum: The amount by which the angle is changed just below. |
324 | * Add to each sum: The amount by which the angle is changed just below. |
328 | */ |
325 | */ |
329 | for (axis = PITCH; axis <= ROLL; axis++) { |
326 | for (axis = PITCH; axis <= ROLL; axis++) { |
330 | accDerived = getAngleEstimateFromAcc(axis); |
327 | accDerived = getAngleEstimateFromAcc(axis); |
331 | debugOut.analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL; |
328 | debugOut.analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL; |
332 | 329 | ||
333 | // 1000 * the correction amount that will be added to the gyro angle in next line. |
330 | // 1000 * the correction amount that will be added to the gyro angle in next line. |
334 | temp = angle[axis]; |
331 | temp = angle[axis]; |
335 | angle[axis] = ((int32_t) (1000L - permilleAcc) * temp |
332 | angle[axis] = ((int32_t) (1000L - permilleAcc) * temp |
336 | + (int32_t) permilleAcc * accDerived) / 1000L; |
333 | + (int32_t) permilleAcc * accDerived) / 1000L; |
337 | correctionSum[axis] += angle[axis] - temp; |
334 | correctionSum[axis] += angle[axis] - temp; |
338 | } |
335 | } |
339 | } else { |
336 | } else { |
340 | debugOut.analog[9] = 0; |
337 | debugOut.analog[9] = 0; |
341 | debugOut.analog[10] = 0; |
338 | debugOut.analog[10] = 0; |
342 | 339 | ||
343 | // experiment: Kill drift compensation updates when not flying smooth. |
340 | // experiment: Kill drift compensation updates when not flying smooth. |
344 | // correctionSum[PITCH] = correctionSum[ROLL] = 0; |
341 | // correctionSum[PITCH] = correctionSum[ROLL] = 0; |
345 | } |
342 | } |
346 | } |
343 | } |
347 | 344 | ||
348 | /************************************************************************ |
345 | /************************************************************************ |
349 | * This is an attempt to correct not the error in the angle integrals |
346 | * This is an attempt to correct not the error in the angle integrals |
350 | * (that happens in correctIntegralsByAcc0thOrder above) but rather the |
347 | * (that happens in correctIntegralsByAcc0thOrder above) but rather the |
351 | * cause of it: Gyro drift, vibration and rounding errors. |
348 | * cause of it: Gyro drift, vibration and rounding errors. |
352 | * All the corrections made in correctIntegralsByAcc0thOrder over |
349 | * All the corrections made in correctIntegralsByAcc0thOrder over |
353 | * DRIFTCORRECTION_TIME cycles are summed up. This number is |
350 | * DRIFTCORRECTION_TIME cycles are summed up. This number is |
354 | * then divided by DRIFTCORRECTION_TIME to get the approx. |
351 | * then divided by DRIFTCORRECTION_TIME to get the approx. |
355 | * correction that should have been applied to each iteration to fix |
352 | * correction that should have been applied to each iteration to fix |
356 | * the error. This is then added to the dynamic offsets. |
353 | * the error. This is then added to the dynamic offsets. |
357 | ************************************************************************/ |
354 | ************************************************************************/ |
358 | // 2 times / sec. = 488/2 |
355 | // 2 times / sec. = 488/2 |
359 | #define DRIFTCORRECTION_TIME 256L |
356 | #define DRIFTCORRECTION_TIME 256L |
360 | void driftCorrection(void) { |
357 | void driftCorrection(void) { |
361 | static int16_t timer = DRIFTCORRECTION_TIME; |
358 | static int16_t timer = DRIFTCORRECTION_TIME; |
362 | int16_t deltaCorrection; |
359 | int16_t deltaCorrection; |
363 | int16_t round; |
360 | int16_t round; |
364 | uint8_t axis; |
361 | uint8_t axis; |
365 | 362 | ||
366 | if (!--timer) { |
363 | if (!--timer) { |
367 | timer = DRIFTCORRECTION_TIME; |
364 | timer = DRIFTCORRECTION_TIME; |
368 | for (axis = PITCH; axis <= ROLL; axis++) { |
365 | for (axis = PITCH; axis <= ROLL; axis++) { |
369 | // Take the sum of corrections applied, add it to delta |
366 | // Take the sum of corrections applied, add it to delta |
370 | if (correctionSum[axis] >=0) |
367 | if (correctionSum[axis] >=0) |
371 | round = DRIFTCORRECTION_TIME / 2; |
368 | round = DRIFTCORRECTION_TIME / 2; |
372 | else |
369 | else |
373 | round = -DRIFTCORRECTION_TIME / 2; |
370 | round = -DRIFTCORRECTION_TIME / 2; |
374 | deltaCorrection = (correctionSum[axis] + round) / DRIFTCORRECTION_TIME; |
371 | deltaCorrection = (correctionSum[axis] + round) / DRIFTCORRECTION_TIME; |
375 | // Add the delta to the compensation. So positive delta means, gyro should have higher value. |
372 | // Add the delta to the compensation. So positive delta means, gyro should have higher value. |
376 | driftComp[axis] += deltaCorrection / staticParams.driftCompDivider; |
373 | driftComp[axis] += deltaCorrection / staticParams.driftCompDivider; |
377 | CHECK_MIN_MAX(driftComp[axis], -staticParams.driftCompLimit, staticParams.driftCompLimit); |
374 | CHECK_MIN_MAX(driftComp[axis], -staticParams.driftCompLimit, staticParams.driftCompLimit); |
378 | // DebugOut.Analog[11 + axis] = correctionSum[axis]; |
375 | // DebugOut.Analog[11 + axis] = correctionSum[axis]; |
379 | // DebugOut.Analog[16 + axis] = correctionSum[axis]; |
376 | // DebugOut.Analog[16 + axis] = correctionSum[axis]; |
380 | debugOut.analog[28 + axis] = driftComp[axis]; |
377 | debugOut.analog[28 + axis] = driftComp[axis]; |
381 | 378 | ||
382 | correctionSum[axis] = 0; |
379 | correctionSum[axis] = 0; |
383 | } |
380 | } |
384 | } |
381 | } |
385 | } |
382 | } |
386 | 383 | ||
387 | /************************************************************************ |
384 | /************************************************************************ |
388 | * Main procedure. |
385 | * Main procedure. |
389 | ************************************************************************/ |
386 | ************************************************************************/ |
390 | void calculateFlightAttitude(void) { |
387 | void calculateFlightAttitude(void) { |
391 | getAnalogData(); |
388 | getAnalogData(); |
392 | integrate(); |
389 | integrate(); |
393 | 390 | ||
394 | #ifdef ATTITUDE_USE_ACC_SENSORS |
391 | #ifdef ATTITUDE_USE_ACC_SENSORS |
395 | correctIntegralsByAcc0thOrder(); |
392 | correctIntegralsByAcc0thOrder(); |
396 | driftCorrection(); |
393 | driftCorrection(); |
397 | #endif |
394 | #endif |
398 | } |
395 | } |
399 | 396 | ||
400 | void updateCompass(void) { |
397 | void updateCompass(void) { |
401 | int16_t w, v, r, correction, error; |
398 | int16_t w, v, r, correction, error; |
402 | 399 | ||
403 | if (compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) { |
400 | if (compassCalState && !(MKFlags & MKFLAG_MOTOR_RUN)) { |
404 | if (controlMixer_testCompassCalState()) { |
401 | if (controlMixer_testCompassCalState()) { |
405 | compassCalState++; |
402 | compassCalState++; |
406 | if (compassCalState < 5) |
403 | if (compassCalState < 5) |
407 | beepNumber(compassCalState); |
404 | beepNumber(compassCalState); |
408 | else |
405 | else |
409 | beep(1000); |
406 | beep(1000); |
410 | } |
407 | } |
411 | } else { |
408 | } else { |
412 | // get maximum attitude angle |
409 | // get maximum attitude angle |
413 | w = abs(angle[PITCH] / 512); |
410 | w = abs(angle[PITCH] / 512); |
414 | v = abs(angle[ROLL] / 512); |
411 | v = abs(angle[ROLL] / 512); |
415 | if (v > w) |
412 | if (v > w) |
416 | w = v; |
413 | w = v; |
417 | correction = w / 8 + 1; |
414 | correction = w / 8 + 1; |
418 | // 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 |
419 | if (compassHeading < 0) |
416 | if (compassHeading < 0) |
420 | error = 0; // disable yaw drift compensation if compass heading is undefined |
417 | error = 0; // disable yaw drift compensation if compass heading is undefined |
421 | else if (abs(yawRate) > 128) { // spinning fast |
418 | else if (abs(yawRate) > 128) { // spinning fast |
422 | error = 0; |
419 | error = 0; |
423 | } else { |
420 | } else { |
424 | // compassHeading - yawGyroHeading, on a -180..179 deg interval. |
421 | // compassHeading - yawGyroHeading, on a -180..179 deg interval. |
425 | error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) |
422 | error = ((540 + compassHeading - (yawGyroHeading / GYRO_DEG_FACTOR_YAW)) |
426 | % 360) - 180; |
423 | % 360) - 180; |
427 | } |
424 | } |
428 | if (!ignoreCompassTimer && w < 25) { |
425 | if (!ignoreCompassTimer && w < 25) { |
429 | yawGyroDrift += error; |
426 | yawGyroDrift += error; |
430 | // Basically this gets set if we are in "fix" mode, and when starting. |
427 | // Basically this gets set if we are in "fix" mode, and when starting. |
431 | if (updateCompassCourse) { |
428 | if (updateCompassCourse) { |
432 | beep(200); |
429 | beep(200); |
433 | yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
430 | yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
434 | compassCourse = compassHeading; //(int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW); |
431 | compassCourse = compassHeading; //(int16_t)(yawGyroHeading / GYRO_DEG_FACTOR_YAW); |
435 | updateCompassCourse = 0; |
432 | updateCompassCourse = 0; |
436 | } |
433 | } |
437 | } |
434 | } |
438 | yawGyroHeading += (error * 8) / correction; |
435 | yawGyroHeading += (error * 8) / correction; |
439 | 436 | ||
440 | /* |
437 | /* |
441 | w = (w * dynamicParams.CompassYawEffect) / 32; |
438 | w = (w * dynamicParams.CompassYawEffect) / 32; |
442 | w = dynamicParams.CompassYawEffect - w; |
439 | w = dynamicParams.CompassYawEffect - w; |
443 | */ |
440 | */ |
444 | w = dynamicParams.compassYawEffect - (w * dynamicParams.compassYawEffect) |
441 | w = dynamicParams.compassYawEffect - (w * dynamicParams.compassYawEffect) |
445 | / 32; |
442 | / 32; |
446 | 443 | ||
447 | // As readable formula: |
444 | // As readable formula: |
448 | // w = dynamicParams.CompassYawEffect * (1-w/32); |
445 | // w = dynamicParams.CompassYawEffect * (1-w/32); |
449 | 446 | ||
450 | if (w >= 0) { // maxAttitudeAngle < 32 |
447 | if (w >= 0) { // maxAttitudeAngle < 32 |
451 | if (!ignoreCompassTimer) { |
448 | if (!ignoreCompassTimer) { |
452 | /*v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 8;*/ |
449 | /*v = 64 + (maxControl[PITCH] + maxControl[ROLL]) / 8;*/ |
453 | v = 64 + controlActivity / 100; |
450 | v = 64 + controlActivity / 100; |
454 | // yawGyroHeading - compassCourse on a -180..179 degree interval. |
451 | // yawGyroHeading - compassCourse on a -180..179 degree interval. |
455 | r |
452 | r |
456 | = ((540 + yawGyroHeading / GYRO_DEG_FACTOR_YAW - compassCourse) |
453 | = ((540 + yawGyroHeading / GYRO_DEG_FACTOR_YAW - compassCourse) |
457 | % 360) - 180; |
454 | % 360) - 180; |
458 | v = (r * w) / v; // align to compass course |
455 | v = (r * w) / v; // align to compass course |
459 | // limit yaw rate |
456 | // limit yaw rate |
460 | w = 3 * dynamicParams.compassYawEffect; |
457 | w = 3 * dynamicParams.compassYawEffect; |
461 | if (v > w) |
458 | if (v > w) |
462 | v = w; |
459 | v = w; |
463 | else if (v < -w) |
460 | else if (v < -w) |
464 | v = -w; |
461 | v = -w; |
465 | yawAngleDiff += v; |
462 | yawAngleDiff += v; |
466 | } else { // wait a while |
463 | } else { // wait a while |
467 | ignoreCompassTimer--; |
464 | ignoreCompassTimer--; |
468 | } |
465 | } |
469 | } else { // ignore compass at extreme attitudes for a while |
466 | } else { // ignore compass at extreme attitudes for a while |
470 | ignoreCompassTimer = 500; |
467 | ignoreCompassTimer = 500; |
471 | } |
468 | } |
472 | } |
469 | } |
473 | } |
470 | } |
474 | 471 | ||
475 | /* |
472 | /* |
476 | * This is part of an experiment to measure average sensor offsets caused by motor vibration, |
473 | * This is part of an experiment to measure average sensor offsets caused by motor vibration, |
477 | * and to compensate them away. It brings about some improvement, but no miracles. |
474 | * and to compensate them away. It brings about some improvement, but no miracles. |
478 | * As long as the left stick is kept in the start-motors position, the dynamic compensation |
475 | * As long as the left stick is kept in the start-motors position, the dynamic compensation |
479 | * will measure the effect of vibration, to use for later compensation. So, one should keep |
476 | * will measure the effect of vibration, to use for later compensation. So, one should keep |
480 | * the stick in the start-motors position for a few seconds, till all motors run (at the wrong |
477 | * the stick in the start-motors position for a few seconds, till all motors run (at the wrong |
481 | * speed unfortunately... must find a better way) |
478 | * speed unfortunately... must find a better way) |
482 | */ |
479 | */ |
483 | /* |
480 | /* |
484 | void attitude_startDynamicCalibration(void) { |
481 | void attitude_startDynamicCalibration(void) { |
485 | dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0; |
482 | dynamicCalPitch = dynamicCalRoll = dynamicCalYaw = dynamicCalCount = 0; |
486 | savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000; |
483 | savedDynamicOffsetPitch = savedDynamicOffsetRoll = 1000; |
487 | } |
484 | } |
488 | 485 | ||
489 | void attitude_continueDynamicCalibration(void) { |
486 | void attitude_continueDynamicCalibration(void) { |
490 | // measure dynamic offset now... |
487 | // measure dynamic offset now... |
491 | dynamicCalPitch += hiResPitchGyro; |
488 | dynamicCalPitch += hiResPitchGyro; |
492 | dynamicCalRoll += hiResRollGyro; |
489 | dynamicCalRoll += hiResRollGyro; |
493 | dynamicCalYaw += rawYawGyroSum; |
490 | dynamicCalYaw += rawYawGyroSum; |
494 | dynamicCalCount++; |
491 | dynamicCalCount++; |
495 | 492 | ||
496 | // Param6: Manual mode. The offsets are taken from Param7 and Param8. |
493 | // Param6: Manual mode. The offsets are taken from Param7 and Param8. |
497 | if (dynamicParams.UserParam6 || 1) { // currently always enabled. |
494 | if (dynamicParams.UserParam6 || 1) { // currently always enabled. |
498 | // manual mode |
495 | // manual mode |
499 | driftCompPitch = dynamicParams.UserParam7 - 128; |
496 | driftCompPitch = dynamicParams.UserParam7 - 128; |
500 | driftCompRoll = dynamicParams.UserParam8 - 128; |
497 | driftCompRoll = dynamicParams.UserParam8 - 128; |
501 | } else { |
498 | } else { |
502 | // use the sampled value (does not seem to work so well....) |
499 | // use the sampled value (does not seem to work so well....) |
503 | driftCompPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount; |
500 | driftCompPitch = savedDynamicOffsetPitch = -dynamicCalPitch / dynamicCalCount; |
504 | driftCompRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount; |
501 | driftCompRoll = savedDynamicOffsetRoll = -dynamicCalRoll / dynamicCalCount; |
505 | driftCompYaw = -dynamicCalYaw / dynamicCalCount; |
502 | driftCompYaw = -dynamicCalYaw / dynamicCalCount; |
506 | } |
503 | } |
507 | 504 | ||
508 | // keep resetting these meanwhile, to avoid accumulating errors. |
505 | // keep resetting these meanwhile, to avoid accumulating errors. |
509 | setStaticAttitudeIntegrals(); |
506 | setStaticAttitudeIntegrals(); |
510 | yawAngle = 0; |
507 | yawAngle = 0; |
511 | } |
508 | } |
512 | */ |
509 | */ |
513 | 510 |