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