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