Rev 1922 | Rev 1926 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1910 | - | 1 | /************************************************************************/ |
2 | /* Flight Attitude */ |
||
3 | /************************************************************************/ |
||
4 | |||
5 | #include <stdlib.h> |
||
6 | #include <avr/io.h> |
||
7 | |||
8 | #include "attitude.h" |
||
9 | #include "dongfangMath.h" |
||
10 | |||
11 | // For scope debugging only! |
||
12 | #include "rc.h" |
||
13 | |||
14 | // where our main data flow comes from. |
||
15 | #include "analog.h" |
||
16 | |||
17 | #include "configuration.h" |
||
18 | #include "output.h" |
||
19 | |||
20 | // Some calculations are performed depending on some stick related things. |
||
21 | #include "controlMixer.h" |
||
22 | |||
23 | // For Servo_On / Off |
||
24 | // #include "timer2.h" |
||
25 | |||
26 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
||
27 | |||
28 | /* |
||
29 | * Gyro readings, as read from the analog module. It would have been nice to flow |
||
30 | * them around between the different calculations as a struct or array (doing |
||
31 | * things functionally without side effects) but this is shorter and probably |
||
32 | * faster too. |
||
33 | * The variables are overwritten at each attitude calculation invocation - the values |
||
34 | * are not preserved or reused. |
||
35 | */ |
||
36 | int16_t rate_ATT[2], yawRate; |
||
37 | |||
38 | // With different (less) filtering |
||
39 | int16_t rate_PID[2]; |
||
40 | int16_t differential[3]; |
||
41 | |||
42 | /* |
||
1922 | - | 43 | * Gyro integrals. These are the rotation angles of the airframe compared to the |
44 | * horizontal plane, yaw relative to yaw at start. Not really used for anything else |
||
45 | * than diagnostics. |
||
1910 | - | 46 | */ |
1922 | - | 47 | int32_t angle[2], yawAngleDiff; |
1910 | - | 48 | |
49 | /* |
||
1922 | - | 50 | * Error integrals. Stick is always positive. Gyro is configurable positive or negative. |
51 | * These represent the deviation of the attitude angle from the desired on each axis. |
||
1910 | - | 52 | */ |
1922 | - | 53 | int32_t error[3]; |
1910 | - | 54 | |
55 | // Yaw angle and compass stuff. |
||
56 | // This is updated/written from MM3. Negative angle indicates invalid data. |
||
57 | int16_t compassHeading = -1; |
||
58 | |||
59 | // This is NOT updated from MM3. Negative angle indicates invalid data. |
||
60 | int16_t compassCourse = -1; |
||
61 | |||
62 | // The difference between the above 2 (heading - course) on a -180..179 degree interval. |
||
63 | // Not necessary. Never read anywhere. |
||
64 | // int16_t compassOffCourse = 0; |
||
65 | |||
66 | uint8_t updateCompassCourse = 0; |
||
67 | uint8_t compassCalState = 0; |
||
68 | uint16_t ignoreCompassTimer = 500; |
||
69 | |||
70 | int32_t yawGyroHeading; // Yaw Gyro Integral supported by compass |
||
71 | int16_t yawGyroDrift; |
||
72 | |||
73 | int16_t correctionSum[2] = { 0, 0 }; |
||
74 | |||
75 | // For NaviCTRL use. |
||
76 | int16_t averageAcc[2] = { 0, 0 }, averageAccCount = 0; |
||
77 | |||
78 | /* |
||
79 | * Experiment: Compensating for dynamic-induced gyro biasing. |
||
80 | */ |
||
81 | int16_t driftComp[2] = { 0, 0 }, driftCompYaw = 0; |
||
82 | // int16_t savedDynamicOffsetPitch = 0, savedDynamicOffsetRoll = 0; |
||
83 | // int32_t dynamicCalPitch, dynamicCalRoll, dynamicCalYaw; |
||
84 | // int16_t dynamicCalCount; |
||
85 | |||
86 | /************************************************************************ |
||
87 | * Set inclination angles from the acc. sensor data. |
||
88 | * If acc. sensors are not used, set to zero. |
||
89 | * TODO: One could use inverse sine to calculate the angles more |
||
90 | * accurately, but since: 1) the angles are rather small at times when |
||
91 | * it makes sense to set the integrals (standing on ground, or flying at |
||
92 | * constant speed, and 2) at small angles a, sin(a) ~= constant * a, |
||
93 | * it is hardly worth the trouble. |
||
94 | ************************************************************************/ |
||
95 | |||
96 | int32_t getAngleEstimateFromAcc(uint8_t axis) { |
||
97 | return GYRO_ACC_FACTOR * (int32_t) filteredAcc[axis]; |
||
98 | } |
||
99 | |||
100 | void setStaticAttitudeAngles(void) { |
||
101 | #ifdef ATTITUDE_USE_ACC_SENSORS |
||
102 | angle[PITCH] = getAngleEstimateFromAcc(PITCH); |
||
103 | angle[ROLL] = getAngleEstimateFromAcc(ROLL); |
||
104 | #else |
||
105 | angle[PITCH] = angle[ROLL] = 0; |
||
106 | #endif |
||
107 | } |
||
108 | |||
109 | /************************************************************************ |
||
110 | * Neutral Readings |
||
111 | ************************************************************************/ |
||
112 | void attitude_setNeutral(void) { |
||
113 | // Servo_Off(); // disable servo output. TODO: Why bother? The servos are going to make a jerk anyway. |
||
114 | driftComp[PITCH] = driftComp[ROLL] = yawGyroDrift = driftCompYaw = 0; |
||
115 | correctionSum[PITCH] = correctionSum[ROLL] = 0; |
||
116 | |||
117 | // Calibrate hardware. |
||
118 | analog_calibrate(); |
||
119 | |||
120 | // reset gyro integrals to acc guessing |
||
121 | setStaticAttitudeAngles(); |
||
122 | yawAngleDiff = 0; |
||
123 | |||
124 | // update compass course to current heading |
||
125 | compassCourse = compassHeading; |
||
126 | |||
127 | // Inititialize YawGyroIntegral value with current compass heading |
||
128 | yawGyroHeading = (int32_t) compassHeading * GYRO_DEG_FACTOR_YAW; |
||
129 | |||
130 | // Servo_On(); //enable servo output |
||
131 | } |
||
132 | |||
133 | /************************************************************************ |
||
134 | * Get sensor data from the analog module, and release the ADC |
||
135 | * TODO: Ultimately, the analog module could do this (instead of dumping |
||
136 | * the values into variables). |
||
137 | * The rate variable end up in a range of about [-1024, 1023]. |
||
138 | *************************************************************************/ |
||
139 | void getAnalogData(void) { |
||
140 | uint8_t axis; |
||
141 | |||
142 | for (axis = PITCH; axis <= ROLL; axis++) { |
||
143 | rate_PID[axis] = gyro_PID[axis] + driftComp[axis]; |
||
144 | rate_ATT[axis] = gyro_ATT[axis] + driftComp[axis]; |
||
145 | differential[axis] = gyroD[axis]; |
||
146 | averageAcc[axis] += acc[axis]; |
||
147 | } |
||
148 | |||
149 | differential[YAW] = gyroD[YAW]; |
||
150 | |||
151 | averageAccCount++; |
||
152 | yawRate = yawGyro + driftCompYaw; |
||
153 | |||
154 | // We are done reading variables from the analog module. |
||
155 | // Interrupt-driven sensor reading may restart. |
||
156 | analogDataReady = 0; |
||
157 | analog_start(); |
||
158 | } |
||
159 | |||
160 | void integrate(void) { |
||
161 | // First, perform axis coupling. If disabled xxxRate is just copied to ACxxxRate. |
||
162 | uint8_t axis; |
||
163 | |||
1924 | - | 164 | error[PITCH] += control[CONTROL_ELEVATOR]; |
165 | error[ROLL] += control[CONTROL_AILERONS]; |
||
166 | error[YAW] += control[CONTROL_RUDDER]; |
||
167 | |||
1910 | - | 168 | if (staticParams.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE) { |
1922 | - | 169 | error[PITCH] += (staticParams.ControlSigns & 1 ? rate_ATT[PITCH] : -rate_ATT[PITCH]); |
170 | error[ROLL] += (staticParams.ControlSigns & 2 ? rate_ATT[ROLL] : -rate_ATT[ROLL]); |
||
171 | error[YAW] += (staticParams.ControlSigns & 4 ? yawRate : -yawRate); |
||
1910 | - | 172 | } else { |
1922 | - | 173 | error[PITCH] += (staticParams.ControlSigns & 1 ? rate_ATT[PITCH] : -rate_ATT[PITCH]); |
174 | error[ROLL] += (staticParams.ControlSigns & 2 ? rate_ATT[ROLL] : -rate_ATT[ROLL]); |
||
175 | error[YAW] += (staticParams.ControlSigns & 4 ? yawRate : -yawRate); |
||
1910 | - | 176 | } |
177 | |||
1922 | - | 178 | for (axis=PITCH; axis<=YAW; axis++) { |
179 | if (error[axis] > ERRORLIMIT) { |
||
180 | error[axis] = ERRORLIMIT; |
||
181 | } else if (angle[axis] <= -ERRORLIMIT) { |
||
182 | angle[axis] = -ERRORLIMIT; |
||
183 | } |
||
184 | } |
||
185 | |||
1910 | - | 186 | /* |
187 | * Yaw |
||
188 | * Calculate yaw gyro integral (~ to rotation angle) |
||
189 | * Limit yawGyroHeading proportional to 0 deg to 360 deg |
||
190 | */ |
||
191 | yawGyroHeading += ACYawRate; |
||
192 | yawAngleDiff += yawRate; |
||
193 | |||
194 | if (yawGyroHeading >= YAWOVER360) { |
||
195 | yawGyroHeading -= YAWOVER360; // 360 deg. wrap |
||
196 | } else if (yawGyroHeading < 0) { |
||
197 | yawGyroHeading += YAWOVER360; |
||
198 | } |
||
199 | |||
200 | /* |
||
201 | * Pitch axis integration and range boundary wrap. |
||
202 | */ |
||
203 | for (axis = PITCH; axis <= ROLL; axis++) { |
||
1922 | - | 204 | angle[axis] += rate_ATT[axis]; |
1910 | - | 205 | if (angle[axis] > PITCHROLLOVER180) { |
206 | angle[axis] -= PITCHROLLOVER360; |
||
207 | } else if (angle[axis] <= -PITCHROLLOVER180) { |
||
208 | angle[axis] += PITCHROLLOVER360; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /************************************************************************ |
||
214 | * A kind of 0'th order integral correction, that corrects the integrals |
||
215 | * directly. This is the "gyroAccFactor" stuff in the original code. |
||
216 | * There is (there) also a drift compensation |
||
217 | * - it corrects the differential of the integral = the gyro offsets. |
||
218 | * That should only be necessary with drifty gyros like ENC-03. |
||
219 | ************************************************************************/ |
||
220 | void correctIntegralsByAcc0thOrder(void) { |
||
221 | // TODO: Consider changing this to: Only correct when integrals are less than ...., or only correct when angular velocities |
||
222 | // are less than ....., or reintroduce Kalman. |
||
223 | // Well actually the Z axis acc. check is not so silly. |
||
224 | uint8_t axis; |
||
225 | int32_t temp; |
||
226 | if (acc[Z] >= -dynamicParams.UserParams[7] && acc[Z] |
||
227 | <= dynamicParams.UserParams[7]) { |
||
228 | DebugOut.Digital[0] |= DEBUG_ACC0THORDER; |
||
229 | |||
230 | uint8_t permilleAcc = staticParams.GyroAccFactor; // NOTE!!! The meaning of this value has changed!! |
||
231 | uint8_t debugFullWeight = 1; |
||
232 | int32_t accDerived; |
||
233 | |||
234 | if ((control[YAW] < -64) || (control[YAW] > 64)) { // reduce further if yaw stick is active |
||
235 | permilleAcc /= 2; |
||
236 | debugFullWeight = 0; |
||
237 | } |
||
238 | |||
239 | if ((maxControl[PITCH] > 64) || (maxControl[ROLL] > 64)) { // reduce effect during stick commands. Replace by controlActivity. |
||
240 | permilleAcc /= 2; |
||
241 | debugFullWeight = 0; |
||
242 | } |
||
243 | |||
244 | if (debugFullWeight) |
||
245 | DebugOut.Digital[1] |= DEBUG_ACC0THORDER; |
||
246 | else |
||
247 | DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER; |
||
248 | |||
249 | /* |
||
250 | * Add to each sum: The amount by which the angle is changed just below. |
||
251 | */ |
||
252 | for (axis = PITCH; axis <= ROLL; axis++) { |
||
253 | accDerived = getAngleEstimateFromAcc(axis); |
||
254 | // DebugOut.Analog[9 + axis] = (10 * accDerived) / GYRO_DEG_FACTOR_PITCHROLL; |
||
255 | |||
256 | // 1000 * the correction amount that will be added to the gyro angle in next line. |
||
257 | temp = angle[axis]; //(permilleAcc * (accDerived - angle[axis])) / 1000; |
||
258 | angle[axis] = ((int32_t) (1000L - permilleAcc) * temp |
||
259 | + (int32_t) permilleAcc * accDerived) / 1000L; |
||
260 | correctionSum[axis] += angle[axis] - temp; |
||
261 | } |
||
262 | } else { |
||
263 | DebugOut.Digital[0] &= ~DEBUG_ACC0THORDER; |
||
264 | DebugOut.Digital[1] &= ~DEBUG_ACC0THORDER; |
||
265 | // DebugOut.Analog[9] = 0; |
||
266 | // DebugOut.Analog[10] = 0; |
||
267 | |||
268 | DebugOut.Analog[16] = 0; |
||
269 | DebugOut.Analog[17] = 0; |
||
270 | // experiment: Kill drift compensation updates when not flying smooth. |
||
271 | correctionSum[PITCH] = correctionSum[ROLL] = 0; |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /************************************************************************ |
||
276 | * This is an attempt to correct not the error in the angle integrals |
||
277 | * (that happens in correctIntegralsByAcc0thOrder above) but rather the |
||
278 | * cause of it: Gyro drift, vibration and rounding errors. |
||
279 | * All the corrections made in correctIntegralsByAcc0thOrder over |
||
280 | * DRIFTCORRECTION_TIME cycles are summed up. This number is |
||
281 | * then divided by DRIFTCORRECTION_TIME to get the approx. |
||
282 | * correction that should have been applied to each iteration to fix |
||
283 | * the error. This is then added to the dynamic offsets. |
||
284 | ************************************************************************/ |
||
285 | // 2 times / sec. = 488/2 |
||
286 | #define DRIFTCORRECTION_TIME 256L |
||
287 | void driftCorrection(void) { |
||
288 | static int16_t timer = DRIFTCORRECTION_TIME; |
||
289 | int16_t deltaCorrection; |
||
290 | int16_t round; |
||
291 | uint8_t axis; |
||
292 | |||
293 | if (!--timer) { |
||
294 | timer = DRIFTCORRECTION_TIME; |
||
295 | for (axis = PITCH; axis <= ROLL; axis++) { |
||
296 | // Take the sum of corrections applied, add it to delta |
||
297 | if (correctionSum[axis] >=0) |
||
298 | round = DRIFTCORRECTION_TIME / 2; |
||
299 | else |
||
300 | round = -DRIFTCORRECTION_TIME / 2; |
||
301 | deltaCorrection = (correctionSum[axis] + round) / DRIFTCORRECTION_TIME; |
||
302 | // Add the delta to the compensation. So positive delta means, gyro should have higher value. |
||
303 | driftComp[axis] += deltaCorrection / staticParams.GyroAccTrim; |
||
304 | CHECK_MIN_MAX(driftComp[axis], -staticParams.DriftComp, staticParams.DriftComp); |
||
305 | // DebugOut.Analog[11 + axis] = correctionSum[axis]; |
||
306 | DebugOut.Analog[16 + axis] = correctionSum[axis]; |
||
307 | DebugOut.Analog[28 + axis] = driftComp[axis]; |
||
308 | |||
309 | correctionSum[axis] = 0; |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | |||
314 | /************************************************************************ |
||
315 | * Main procedure. |
||
316 | ************************************************************************/ |
||
317 | void calculateFlightAttitude(void) { |
||
318 | getAnalogData(); |
||
319 | integrate(); |
||
320 | |||
321 | DebugOut.Analog[3] = rate_PID[PITCH]; |
||
322 | DebugOut.Analog[4] = rate_PID[ROLL]; |
||
323 | DebugOut.Analog[5] = yawRate; |
||
324 | |||
325 | #ifdef ATTITUDE_USE_ACC_SENSORS |
||
326 | correctIntegralsByAcc0thOrder(); |
||
327 | driftCorrection(); |
||
328 | #endif |
||
329 | } |