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