Rev 2073 | Rev 2086 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2073 | Rev 2085 | ||
---|---|---|---|
1 | #include <stdlib.h> |
1 | #include <stdlib.h> |
2 | #include <avr/io.h> |
2 | #include <avr/io.h> |
3 | #include "eeprom.h" |
3 | #include "eeprom.h" |
4 | #include "flight.h" |
4 | #include "flight.h" |
5 | #include "output.h" |
5 | #include "output.h" |
6 | #include "uart0.h" |
6 | #include "uart0.h" |
7 | 7 | ||
8 | // Necessary for external control and motor test |
8 | // Necessary for external control and motor test |
9 | #include "twimaster.h" |
9 | #include "twimaster.h" |
10 | #include "attitude.h" |
10 | #include "attitude.h" |
11 | #include "controlMixer.h" |
11 | #include "controlMixer.h" |
12 | #include "commands.h" |
12 | #include "commands.h" |
13 | #include "heightControl.h" |
13 | #include "heightControl.h" |
14 | 14 | ||
15 | #ifdef USE_MK3MAG |
15 | #ifdef USE_MK3MAG |
16 | #include "mk3mag.h" |
16 | #include "mk3mag.h" |
17 | #include "compassControl.h" |
17 | #include "compassControl.h" |
18 | #endif |
18 | #endif |
19 | 19 | ||
20 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
20 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
21 | 21 | ||
22 | /* |
22 | /* |
23 | * These are no longer maintained, just left at 0. The original implementation just summed the acc. |
23 | * These are no longer maintained, just left at 0. The original implementation just summed the acc. |
24 | * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
24 | * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
25 | */ |
25 | */ |
26 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
26 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
27 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
27 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
28 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
28 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
29 | uint8_t invKi; |
29 | uint8_t ki; |
30 | int32_t IPart[2]; |
30 | int32_t IPart[2]; |
31 | 31 | ||
32 | /************************************************************************/ |
32 | /************************************************************************/ |
33 | /* Filter for motor value smoothing (necessary???) */ |
33 | /* Filter for motor value smoothing (necessary???) */ |
34 | /************************************************************************/ |
34 | /************************************************************************/ |
35 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
35 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
36 | switch (staticParams.motorSmoothing) { |
36 | switch (staticParams.motorSmoothing) { |
37 | case 0: |
37 | case 0: |
38 | return newvalue; |
38 | return newvalue; |
39 | case 1: |
39 | case 1: |
40 | return (oldvalue + newvalue) / 2; |
40 | return (oldvalue + newvalue) / 2; |
41 | case 2: |
41 | case 2: |
42 | if (newvalue > oldvalue) |
42 | if (newvalue > oldvalue) |
43 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
43 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
44 | else |
44 | else |
45 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
45 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
46 | case 3: |
46 | case 3: |
47 | if (newvalue < oldvalue) |
47 | if (newvalue < oldvalue) |
48 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
48 | return (1 * (int16_t) oldvalue + newvalue) / 2; //mean of old and new |
49 | else |
49 | else |
50 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
50 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
51 | default: |
51 | default: |
52 | return newvalue; |
52 | return newvalue; |
53 | } |
53 | } |
54 | } |
54 | } |
55 | 55 | ||
56 | void flight_setParameters(uint8_t _invKi, uint8_t _gyroPFactor, |
56 | void flight_setParameters(uint8_t _ki, uint8_t _gyroPFactor, |
57 | uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
57 | uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
58 | invKi = _invKi; |
58 | ki = _ki; |
59 | gyroPFactor = _gyroPFactor; |
59 | gyroPFactor = _gyroPFactor; |
60 | gyroIFactor = _gyroIFactor; |
60 | gyroIFactor = _gyroIFactor; |
61 | yawPFactor = _yawPFactor; |
61 | yawPFactor = _yawPFactor; |
62 | yawIFactor = _yawIFactor; |
62 | yawIFactor = _yawIFactor; |
63 | } |
63 | } |
64 | 64 | ||
65 | void flight_setGround() { |
65 | void flight_setGround() { |
66 | // Just reset all I terms. |
66 | // Just reset all I terms. |
67 | IPart[PITCH] = IPart[ROLL] = 0; |
67 | IPart[PITCH] = IPart[ROLL] = 0; |
68 | headingError = 0; |
68 | headingError = 0; |
69 | } |
69 | } |
70 | 70 | ||
71 | void flight_takeOff() { |
71 | void flight_takeOff() { |
72 | // This is for GPS module to mark home position. |
72 | // This is for GPS module to mark home position. |
73 | // TODO: What a disgrace, change it. |
73 | // TODO: What a disgrace, change it. |
74 | MKFlags |= MKFLAG_CALIBRATE; |
74 | MKFlags |= MKFLAG_CALIBRATE; |
75 | 75 | ||
76 | HC_setGround(); |
76 | HC_setGround(); |
77 | #ifdef USE_MK3MAG |
77 | #ifdef USE_MK3MAG |
78 | attitude_resetHeadingToMagnetic(); |
78 | attitude_resetHeadingToMagnetic(); |
79 | compass_setTakeoffHeading(heading); |
79 | compass_setTakeoffHeading(heading); |
80 | #endif |
80 | #endif |
81 | } |
81 | } |
82 | 82 | ||
83 | /************************************************************************/ |
83 | /************************************************************************/ |
84 | /* Main Flight Control */ |
84 | /* Main Flight Control */ |
85 | /************************************************************************/ |
85 | /************************************************************************/ |
86 | void flight_control(void) { |
86 | void flight_control(void) { |
87 | int16_t tmp_int; |
87 | int16_t tmp_int; |
88 | // Mixer Fractions that are combined for Motor Control |
88 | // Mixer Fractions that are combined for Motor Control |
89 | int16_t yawTerm, throttleTerm, term[2]; |
89 | int16_t yawTerm, throttleTerm, term[2]; |
90 | 90 | ||
91 | // PID controller variables |
91 | // PID controller variables |
92 | int16_t PDPart; |
92 | int16_t PDPart; |
93 | static int8_t debugDataTimer = 1; |
93 | static int8_t debugDataTimer = 1; |
94 | 94 | ||
95 | // High resolution motor values for smoothing of PID motor outputs |
95 | // High resolution motor values for smoothing of PID motor outputs |
96 | static int16_t motorFilters[MAX_MOTORS]; |
96 | static int16_t motorFilters[MAX_MOTORS]; |
97 | 97 | ||
98 | uint8_t i, axis; |
98 | uint8_t i, axis; |
99 | 99 | ||
100 | throttleTerm = controls[CONTROL_THROTTLE]; |
100 | throttleTerm = controls[CONTROL_THROTTLE]; |
101 | 101 | ||
102 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
102 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
103 | // increment flight-time counter until overflow. |
103 | // increment flight-time counter until overflow. |
104 | if (isFlying != 0xFFFF) |
104 | if (isFlying != 0xFFFF) |
105 | isFlying++; |
105 | isFlying++; |
106 | } |
106 | } |
107 | /* |
107 | /* |
108 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
108 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
109 | * Probably to avoid integration effects that will cause the copter to spin |
109 | * Probably to avoid integration effects that will cause the copter to spin |
110 | * or flip when taking off. |
110 | * or flip when taking off. |
111 | */ |
111 | */ |
112 | if (isFlying < 256) { |
112 | if (isFlying < 256) { |
113 | flight_setGround(); |
113 | flight_setGround(); |
114 | if (isFlying == 250) |
114 | if (isFlying == 250) |
115 | flight_takeOff(); |
115 | flight_takeOff(); |
116 | } |
116 | } |
117 | 117 | ||
118 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
118 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
119 | if (throttleTerm < staticParams.minThrottle + 10) |
119 | if (throttleTerm < staticParams.minThrottle + 10) |
120 | throttleTerm = staticParams.minThrottle + 10; |
120 | throttleTerm = staticParams.minThrottle + 10; |
121 | else if (throttleTerm > staticParams.maxThrottle - 20) |
121 | else if (throttleTerm > staticParams.maxThrottle - 20) |
122 | throttleTerm = (staticParams.maxThrottle - 20); |
122 | throttleTerm = (staticParams.maxThrottle - 20); |
123 | 123 | ||
124 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
124 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
125 | throttleTerm *= CONTROL_SCALING; |
125 | throttleTerm *= CONTROL_SCALING; |
126 | 126 | ||
127 | // end part 1: 750-800 usec. |
127 | // end part 1: 750-800 usec. |
128 | // start part 3: 350 - 400 usec. |
128 | // start part 3: 350 - 400 usec. |
129 | #define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW) |
129 | #define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW) |
130 | // This is where control affects the target heading. It also (later) directly controls yaw. |
130 | // This is where control affects the target heading. It also (later) directly controls yaw. |
131 | headingError -= controls[CONTROL_YAW]; |
131 | headingError -= controls[CONTROL_YAW]; |
132 | 132 | ||
133 | if (headingError < -YAW_I_LIMIT) |
133 | if (headingError < -YAW_I_LIMIT) |
134 | headingError = -YAW_I_LIMIT; |
134 | headingError = -YAW_I_LIMIT; |
135 | else if (headingError > YAW_I_LIMIT) |
135 | else if (headingError > YAW_I_LIMIT) |
136 | headingError = YAW_I_LIMIT; |
136 | headingError = YAW_I_LIMIT; |
137 | 137 | ||
138 | PDPart = (int32_t) (headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4); |
138 | PDPart = (int32_t) (headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4); |
139 | // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on. |
139 | // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on. |
140 | PDPart += (int32_t) (yawRate * yawPFactor) / (GYRO_DEG_FACTOR_YAW >> 5); |
140 | PDPart += (int32_t) (yawRate * yawPFactor) / (GYRO_DEG_FACTOR_YAW >> 5); |
141 | 141 | ||
142 | // Lets not limit P and D. |
142 | // Lets not limit P and D. |
143 | // CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
143 | // CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
144 | 144 | ||
145 | /* |
145 | /* |
146 | * Compose yaw term. |
146 | * Compose yaw term. |
147 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
147 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
148 | * However, at low throttle the yaw term is limited to a fixed value, |
148 | * However, at low throttle the yaw term is limited to a fixed value, |
149 | * and at high throttle it is limited by the throttle reserve (the difference |
149 | * and at high throttle it is limited by the throttle reserve (the difference |
150 | * between current throttle and maximum throttle). |
150 | * between current throttle and maximum throttle). |
151 | */ |
151 | */ |
152 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
152 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
153 | yawTerm = PDPart - controls[CONTROL_YAW] * CONTROL_SCALING; |
153 | yawTerm = PDPart - controls[CONTROL_YAW] * CONTROL_SCALING; |
154 | // Limit yawTerm |
154 | // Limit yawTerm |
155 | debugOut.digital[0] &= ~DEBUG_CLIP; |
155 | debugOut.digital[0] &= ~DEBUG_CLIP; |
156 | if (throttleTerm > MIN_YAWGAS) { |
156 | if (throttleTerm > MIN_YAWGAS) { |
157 | if (yawTerm < -throttleTerm / 2) { |
157 | if (yawTerm < -throttleTerm / 2) { |
158 | debugOut.digital[0] |= DEBUG_CLIP; |
158 | debugOut.digital[0] |= DEBUG_CLIP; |
159 | yawTerm = -throttleTerm / 2; |
159 | yawTerm = -throttleTerm / 2; |
160 | } else if (yawTerm > throttleTerm / 2) { |
160 | } else if (yawTerm > throttleTerm / 2) { |
161 | debugOut.digital[0] |= DEBUG_CLIP; |
161 | debugOut.digital[0] |= DEBUG_CLIP; |
162 | yawTerm = throttleTerm / 2; |
162 | yawTerm = throttleTerm / 2; |
163 | } |
163 | } |
164 | } else { |
164 | } else { |
165 | if (yawTerm < -MIN_YAWGAS / 2) { |
165 | if (yawTerm < -MIN_YAWGAS / 2) { |
166 | debugOut.digital[0] |= DEBUG_CLIP; |
166 | debugOut.digital[0] |= DEBUG_CLIP; |
167 | yawTerm = -MIN_YAWGAS / 2; |
167 | yawTerm = -MIN_YAWGAS / 2; |
168 | } else if (yawTerm > MIN_YAWGAS / 2) { |
168 | } else if (yawTerm > MIN_YAWGAS / 2) { |
169 | debugOut.digital[0] |= DEBUG_CLIP; |
169 | debugOut.digital[0] |= DEBUG_CLIP; |
170 | yawTerm = MIN_YAWGAS / 2; |
170 | yawTerm = MIN_YAWGAS / 2; |
171 | } |
171 | } |
172 | } |
172 | } |
173 | 173 | ||
174 | tmp_int = staticParams.maxThrottle * CONTROL_SCALING; |
174 | tmp_int = staticParams.maxThrottle * CONTROL_SCALING; |
175 | 175 | ||
176 | if (yawTerm < -(tmp_int - throttleTerm)) { |
176 | if (yawTerm < -(tmp_int - throttleTerm)) { |
177 | yawTerm = -(tmp_int - throttleTerm); |
177 | yawTerm = -(tmp_int - throttleTerm); |
178 | debugOut.digital[0] |= DEBUG_CLIP; |
178 | debugOut.digital[0] |= DEBUG_CLIP; |
179 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
179 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
180 | yawTerm = (tmp_int - throttleTerm); |
180 | yawTerm = (tmp_int - throttleTerm); |
181 | debugOut.digital[0] |= DEBUG_CLIP; |
181 | debugOut.digital[0] |= DEBUG_CLIP; |
182 | } |
182 | } |
183 | 183 | ||
184 | debugOut.digital[1] &= ~DEBUG_CLIP; |
184 | debugOut.digital[1] &= ~DEBUG_CLIP; |
185 | 185 | ||
186 | tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + (abs(yawTerm) >> 1)) >> 6); |
186 | tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + (abs(yawTerm) >> 1)) >> 6); |
187 | //tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
187 | //tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
188 | 188 | ||
189 | /************************************************************************/ |
189 | /************************************************************************/ |
190 | /* Calculate control feedback from angle (gyro integral) */ |
190 | /* Calculate control feedback from angle (gyro integral) */ |
191 | /* and angular velocity (gyro signal) */ |
191 | /* and angular velocity (gyro signal) */ |
192 | /************************************************************************/ |
192 | /************************************************************************/ |
193 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
193 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
194 | for (axis = PITCH; axis <= ROLL; axis++) { |
194 | for (axis = PITCH; axis <= ROLL; axis++) { |
195 | PDPart = (int32_t) rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4); |
195 | PDPart = (int32_t) rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4); |
196 | // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick. |
196 | // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick. |
197 | // In HH mode, the I part is summed from P and D of gyros minus stick. |
197 | // In HH mode, the I part is summed from P and D of gyros minus stick. |
198 | if (gyroIFactor) { |
198 | if (gyroIFactor) { |
199 | int16_t iDiff = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2); |
199 | int16_t iDiff = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2); |
200 | if (axis == 0) debugOut.analog[28] = iDiff; |
200 | if (axis == 0) debugOut.analog[28] = iDiff; |
201 | PDPart += iDiff; |
201 | PDPart += iDiff; |
202 | IPart[axis] += iDiff - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
202 | IPart[axis] += iDiff - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
203 | } else { |
203 | } else { |
204 | IPart[axis] += PDPart - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
204 | IPart[axis] += PDPart - controls[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
205 | } |
205 | } |
206 | 206 | ||
207 | // With normal Ki, limit I parts to +/- 205 (of about 1024) |
207 | // With normal Ki, limit I parts to +/- 205 (of about 1024) |
208 | if (IPart[axis] < -64000) { |
208 | if (IPart[axis] < -64000) { |
209 | IPart[axis] = -64000; |
209 | IPart[axis] = -64000; |
210 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
210 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
211 | } else if (IPart[axis] > 64000) { |
211 | } else if (IPart[axis] > 64000) { |
212 | IPart[axis] = 64000; |
212 | IPart[axis] = 64000; |
213 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
213 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
214 | } |
214 | } |
215 | 215 | ||
216 | PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16; |
216 | PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16; |
217 | 217 | ||
218 | term[axis] = PDPart - controls[axis] + (((int32_t) IPart[axis] * invKi) >> 14); |
218 | term[axis] = PDPart - controls[axis] + (((int32_t) IPart[axis] * ki) >> 14); |
219 | term[axis] += (dynamicParams.levelCorrection[axis] - 128); |
219 | term[axis] += (dynamicParams.levelCorrection[axis] - 128); |
220 | 220 | ||
221 | /* |
221 | /* |
222 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
222 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
223 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
223 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
224 | * (max. pitch or roll term is the throttle value). |
224 | * (max. pitch or roll term is the throttle value). |
225 | * OOPS: Is not applied at all. |
225 | * OOPS: Is not applied at all. |
226 | * TODO: Why a growing function of yaw? |
226 | * TODO: Why a growing function of yaw? |
227 | */ |
227 | */ |
228 | if (term[axis] < -tmp_int) { |
228 | if (term[axis] < -tmp_int) { |
229 | debugOut.digital[1] |= DEBUG_CLIP; |
229 | debugOut.digital[1] |= DEBUG_CLIP; |
230 | term[axis] = -tmp_int; |
230 | term[axis] = -tmp_int; |
231 | } else if (term[axis] > tmp_int) { |
231 | } else if (term[axis] > tmp_int) { |
232 | debugOut.digital[1] |= DEBUG_CLIP; |
232 | debugOut.digital[1] |= DEBUG_CLIP; |
233 | term[axis] = tmp_int; |
233 | term[axis] = tmp_int; |
234 | } |
234 | } |
235 | } |
235 | } |
236 | 236 | ||
237 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
237 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
238 | // Universal Mixer |
238 | // Universal Mixer |
239 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
239 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
240 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
240 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
241 | 241 | ||
242 | if (!(--debugDataTimer)) { |
242 | if (!(--debugDataTimer)) { |
243 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
243 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
244 | debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
244 | debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
245 | debugOut.analog[1] = attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
245 | debugOut.analog[1] = attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
246 | debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW; |
246 | debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW; |
247 | 247 | ||
248 | debugOut.analog[3] = rate_ATT[PITCH]; |
248 | debugOut.analog[3] = rate_ATT[PITCH]; |
249 | debugOut.analog[4] = rate_ATT[ROLL]; |
249 | debugOut.analog[4] = rate_ATT[ROLL]; |
250 | debugOut.analog[5] = yawRate; |
250 | debugOut.analog[5] = yawRate; |
251 | } |
251 | } |
252 | 252 | ||
253 | debugOut.analog[8] = yawTerm; |
253 | debugOut.analog[8] = yawTerm; |
254 | debugOut.analog[9] = throttleTerm; |
254 | debugOut.analog[9] = throttleTerm; |
255 | 255 | ||
256 | //debugOut.analog[16] = gyroActivity; |
256 | //debugOut.analog[16] = gyroActivity; |
257 | 257 | ||
258 | for (i = 0; i < MAX_MOTORS; i++) { |
258 | for (i = 0; i < MAX_MOTORS; i++) { |
259 | int32_t tmp; |
259 | int32_t tmp; |
260 | uint8_t throttle; |
260 | uint8_t throttle; |
261 | 261 | ||
262 | tmp = (int32_t) throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE]; |
262 | tmp = (int32_t) throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE]; |
263 | tmp += (int32_t) term[PITCH] * mixerMatrix.motor[i][MIX_PITCH]; |
263 | tmp += (int32_t) term[PITCH] * mixerMatrix.motor[i][MIX_PITCH]; |
264 | tmp += (int32_t) term[ROLL] * mixerMatrix.motor[i][MIX_ROLL]; |
264 | tmp += (int32_t) term[ROLL] * mixerMatrix.motor[i][MIX_ROLL]; |
265 | tmp += (int32_t) yawTerm * mixerMatrix.motor[i][MIX_YAW]; |
265 | tmp += (int32_t) yawTerm * mixerMatrix.motor[i][MIX_YAW]; |
266 | tmp = tmp >> 6; |
266 | tmp = tmp >> 6; |
267 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
267 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
268 | // Now we scale back down to a 0..255 range. |
268 | // Now we scale back down to a 0..255 range. |
269 | tmp = motorFilters[i] / MOTOR_SCALING; |
269 | tmp = motorFilters[i] / MOTOR_SCALING; |
270 | 270 | ||
271 | // So this was the THIRD time a throttle was limited. But should the limitation |
271 | // So this was the THIRD time a throttle was limited. But should the limitation |
272 | // apply to the common throttle signal (the one used for setting the "power" of |
272 | // apply to the common throttle signal (the one used for setting the "power" of |
273 | // all motors together) or should it limit the throttle set for each motor, |
273 | // all motors together) or should it limit the throttle set for each motor, |
274 | // including mix components of pitch, roll and yaw? I think only the common |
274 | // including mix components of pitch, roll and yaw? I think only the common |
275 | // throttle should be limited. |
275 | // throttle should be limited. |
276 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
276 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
277 | // Apply to individual signals instead. |
277 | // Apply to individual signals instead. |
278 | CHECK_MIN_MAX(tmp, 1, 255); |
278 | CHECK_MIN_MAX(tmp, 1, 255); |
279 | throttle = tmp; |
279 | throttle = tmp; |
280 | 280 | ||
281 | /* |
281 | /* |
282 | if (i < 4) |
282 | if (i < 4) |
283 | debugOut.analog[10 + i] = throttle; |
283 | debugOut.analog[10 + i] = throttle; |
284 | */ |
284 | */ |
285 | 285 | ||
286 | if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) { |
286 | if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) { |
287 | motor[i].throttle = throttle; |
287 | motor[i].throttle = throttle; |
288 | } else if (motorTestActive) { |
288 | } else if (motorTestActive) { |
289 | motor[i].throttle = motorTest[i]; |
289 | motor[i].throttle = motorTest[i]; |
290 | } else { |
290 | } else { |
291 | motor[i].throttle = 0; |
291 | motor[i].throttle = 0; |
292 | } |
292 | } |
293 | } |
293 | } |
294 | 294 | ||
295 | I2C_Start(TWI_STATE_MOTOR_TX); |
295 | I2C_Start(TWI_STATE_MOTOR_TX); |
296 | } |
296 | } |
297 | 297 |