Rev 2158 | Rev 2164 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2158 | Rev 2160 | ||
---|---|---|---|
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 ki; |
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 _ki, 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 | ki = _ki; |
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 | HC_setGround(); |
75 | HC_setGround(); |
76 | #ifdef USE_MK3MAG |
76 | #ifdef USE_MK3MAG |
77 | attitude_resetHeadingToMagnetic(); |
77 | attitude_resetHeadingToMagnetic(); |
78 | compass_setTakeoffHeading(heading); |
78 | compass_setTakeoffHeading(heading); |
79 | #endif |
79 | #endif |
80 | } |
80 | } |
81 | 81 | ||
82 | /************************************************************************/ |
82 | /************************************************************************/ |
83 | /* Main Flight Control */ |
83 | /* Main Flight Control */ |
84 | /************************************************************************/ |
84 | /************************************************************************/ |
85 | void flight_control(void) { |
85 | void flight_control(void) { |
86 | int16_t tmp_int; |
86 | int16_t tmp_int; |
87 | // Mixer Fractions that are combined for Motor Control |
87 | // Mixer Fractions that are combined for Motor Control |
88 | int16_t yawTerm, throttleTerm, term[2]; |
88 | int16_t yawTerm, throttleTerm, term[2]; |
89 | 89 | ||
90 | // PID controller variables |
90 | // PID controller variables |
91 | int16_t PDPart; |
91 | int16_t PDPart; |
92 | static int8_t debugDataTimer = 1; |
92 | static int8_t debugDataTimer = 1; |
93 | 93 | ||
94 | // High resolution motor values for smoothing of PID motor outputs |
94 | // High resolution motor values for smoothing of PID motor outputs |
95 | static int16_t motorFilters[MAX_MOTORS]; |
95 | static int16_t motorFilters[MAX_MOTORS]; |
96 | 96 | ||
97 | uint8_t i, axis; |
97 | uint8_t i, axis; |
98 | 98 | ||
99 | throttleTerm = controls[CONTROL_THROTTLE]; |
99 | throttleTerm = controls[CONTROL_THROTTLE]; |
100 | 100 | ||
101 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
101 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
102 | // increment flight-time counter until overflow. |
102 | // increment flight-time counter until overflow. |
103 | if (isFlying != 0xFFFF) |
103 | if (isFlying != 0xFFFF) |
104 | isFlying++; |
104 | isFlying++; |
105 | } |
105 | } |
106 | /* |
106 | /* |
107 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
107 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
108 | * Probably to avoid integration effects that will cause the copter to spin |
108 | * Probably to avoid integration effects that will cause the copter to spin |
109 | * or flip when taking off. |
109 | * or flip when taking off. |
110 | */ |
110 | */ |
111 | if (isFlying < 256) { |
111 | if (isFlying < 256) { |
112 | flight_setGround(); |
112 | flight_setGround(); |
113 | if (isFlying == 250) |
113 | if (isFlying == 250) |
114 | flight_takeOff(); |
114 | flight_takeOff(); |
115 | } |
115 | } |
116 | 116 | ||
117 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
117 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
118 | if (throttleTerm < staticParams.minThrottle + 10) |
118 | if (throttleTerm < staticParams.minThrottle + 10) |
119 | throttleTerm = staticParams.minThrottle + 10; |
119 | throttleTerm = staticParams.minThrottle + 10; |
120 | else if (throttleTerm > staticParams.maxThrottle - 20) |
120 | else if (throttleTerm > staticParams.maxThrottle - 20) |
121 | throttleTerm = (staticParams.maxThrottle - 20); |
121 | throttleTerm = (staticParams.maxThrottle - 20); |
122 | 122 | ||
123 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
123 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
124 | throttleTerm *= CONTROL_SCALING; |
124 | throttleTerm *= CONTROL_SCALING; |
125 | 125 | ||
126 | // end part 1: 750-800 usec. |
126 | // end part 1: 750-800 usec. |
127 | // start part 3: 350 - 400 usec. |
127 | // start part 3: 350 - 400 usec. |
128 | #define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW) |
128 | #define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW) |
129 | // This is where control affects the target heading. It also (later) directly controls yaw. |
129 | // This is where control affects the target heading. It also (later) directly controls yaw. |
130 | headingError -= controls[CONTROL_YAW]; |
130 | headingError -= controls[CONTROL_YAW]; |
131 | 131 | ||
132 | if (headingError < -YAW_I_LIMIT) |
132 | if (headingError < -YAW_I_LIMIT) |
133 | headingError = -YAW_I_LIMIT; |
133 | headingError = -YAW_I_LIMIT; |
134 | else if (headingError > YAW_I_LIMIT) |
134 | else if (headingError > YAW_I_LIMIT) |
135 | headingError = YAW_I_LIMIT; |
135 | headingError = YAW_I_LIMIT; |
136 | 136 | ||
137 | PDPart = (int32_t) (headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4); |
137 | PDPart = (int32_t) (headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4); |
138 | // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on. |
138 | // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on. |
139 | PDPart += (int32_t) (yawRate * yawPFactor) / (GYRO_DEG_FACTOR_YAW >> 5); |
139 | PDPart += (int32_t) (yawRate * yawPFactor) / (GYRO_DEG_FACTOR_YAW >> 5); |
140 | 140 | ||
141 | // Lets not limit P and D. |
141 | // Lets not limit P and D. |
142 | // CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
142 | // CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
143 | 143 | ||
144 | /* |
144 | /* |
145 | * Compose yaw term. |
145 | * Compose yaw term. |
146 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
146 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
147 | * However, at low throttle the yaw term is limited to a fixed value, |
147 | * However, at low throttle the yaw term is limited to a fixed value, |
148 | * and at high throttle it is limited by the throttle reserve (the difference |
148 | * and at high throttle it is limited by the throttle reserve (the difference |
149 | * between current throttle and maximum throttle). |
149 | * between current throttle and maximum throttle). |
150 | */ |
150 | */ |
151 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
151 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
152 | yawTerm = PDPart - controls[CONTROL_YAW] * CONTROL_SCALING; |
152 | yawTerm = PDPart - controls[CONTROL_YAW] * CONTROL_SCALING; |
153 | // Limit yawTerm |
153 | // Limit yawTerm |
154 | debugOut.digital[0] &= ~DEBUG_CLIP; |
154 | debugOut.digital[0] &= ~DEBUG_CLIP; |
155 | if (throttleTerm > MIN_YAWGAS) { |
155 | if (throttleTerm > MIN_YAWGAS) { |
156 | if (yawTerm < -throttleTerm / 2) { |
156 | if (yawTerm < -throttleTerm / 2) { |
157 | debugOut.digital[0] |= DEBUG_CLIP; |
157 | debugOut.digital[0] |= DEBUG_CLIP; |
158 | yawTerm = -throttleTerm / 2; |
158 | yawTerm = -throttleTerm / 2; |
159 | } else if (yawTerm > throttleTerm / 2) { |
159 | } else if (yawTerm > throttleTerm / 2) { |
160 | debugOut.digital[0] |= DEBUG_CLIP; |
160 | debugOut.digital[0] |= DEBUG_CLIP; |
161 | yawTerm = throttleTerm / 2; |
161 | yawTerm = throttleTerm / 2; |
162 | } |
162 | } |
163 | } else { |
163 | } else { |
164 | if (yawTerm < -MIN_YAWGAS / 2) { |
164 | if (yawTerm < -MIN_YAWGAS / 2) { |
165 | debugOut.digital[0] |= DEBUG_CLIP; |
165 | debugOut.digital[0] |= DEBUG_CLIP; |
166 | yawTerm = -MIN_YAWGAS / 2; |
166 | yawTerm = -MIN_YAWGAS / 2; |
167 | } else if (yawTerm > MIN_YAWGAS / 2) { |
167 | } else if (yawTerm > MIN_YAWGAS / 2) { |
168 | debugOut.digital[0] |= DEBUG_CLIP; |
168 | debugOut.digital[0] |= DEBUG_CLIP; |
169 | yawTerm = MIN_YAWGAS / 2; |
169 | yawTerm = MIN_YAWGAS / 2; |
170 | } |
170 | } |
171 | } |
171 | } |
172 | 172 | ||
173 | tmp_int = staticParams.maxThrottle * CONTROL_SCALING; |
173 | tmp_int = staticParams.maxThrottle * CONTROL_SCALING; |
174 | 174 | ||
175 | if (yawTerm < -(tmp_int - throttleTerm)) { |
175 | if (yawTerm < -(tmp_int - throttleTerm)) { |
176 | yawTerm = -(tmp_int - throttleTerm); |
176 | yawTerm = -(tmp_int - throttleTerm); |
177 | debugOut.digital[0] |= DEBUG_CLIP; |
177 | debugOut.digital[0] |= DEBUG_CLIP; |
178 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
178 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
179 | yawTerm = (tmp_int - throttleTerm); |
179 | yawTerm = (tmp_int - throttleTerm); |
180 | debugOut.digital[0] |= DEBUG_CLIP; |
180 | debugOut.digital[0] |= DEBUG_CLIP; |
181 | } |
181 | } |
182 | 182 | ||
183 | debugOut.digital[1] &= ~DEBUG_CLIP; |
183 | debugOut.digital[1] &= ~DEBUG_CLIP; |
184 | 184 | ||
185 | tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + (abs(yawTerm) >> 1)) >> 6); |
185 | tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + (abs(yawTerm) >> 1)) >> 6); |
186 | //tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
186 | //tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
187 | 187 | ||
188 | /************************************************************************/ |
188 | /************************************************************************/ |
189 | /* Calculate control feedback from angle (gyro integral) */ |
189 | /* Calculate control feedback from angle (gyro integral) */ |
190 | /* and angular velocity (gyro signal) */ |
190 | /* and angular velocity (gyro signal) */ |
191 | /************************************************************************/ |
191 | /************************************************************************/ |
192 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
192 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
193 | for (axis = PITCH; axis <= ROLL; axis++) { |
193 | for (axis = PITCH; axis <= ROLL; axis++) { |
194 | PDPart = (int32_t) rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4); |
194 | PDPart = (int32_t) rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4); |
195 | // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick. |
195 | // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick. |
196 | // In HH mode, the I part is summed from P and D of gyros minus stick. |
196 | // In HH mode, the I part is summed from P and D of gyros minus stick. |
197 | if (gyroIFactor) { |
197 | if (gyroIFactor) { |
198 | int16_t iDiff = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2); |
198 | int16_t iDiff = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2); |
199 | //if (axis == 0) debugOut.analog[28] = iDiff; |
199 | //if (axis == 0) debugOut.analog[28] = iDiff; |
200 | PDPart += iDiff; |
200 | PDPart += iDiff; |
201 | 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. |
201 | 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 | } else { |
202 | } else { |
203 | 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. |
203 | 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 | } |
204 | } |
205 | 205 | ||
206 | // So (int32_t) rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4) + |
206 | // So (int32_t) rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4) + |
207 | // attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2) - controls[axis] |
207 | // attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2) - controls[axis] |
208 | // We can ignore the rate: attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2) - controls[axis] |
208 | // We can ignore the rate: attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 2) - controls[axis] |
209 | // That is: attitudeAngle[degrees] * gyroIFactor/4 - controls[axis] |
209 | // That is: attitudeAngle[degrees] * gyroIFactor/4 - controls[axis] |
210 | // So attitude attained at attitudeAngle[degrees] * gyroIFactor/4 == controls[axis] |
210 | // So attitude attained at attitudeAngle[degrees] * gyroIFactor/4 == controls[axis] |
211 | 211 | ||
212 | // With normal Ki, limit I parts to +/- 205 (of about 1024) |
212 | // With normal Ki, limit I parts to +/- 205 (of about 1024) |
213 | if (IPart[axis] < -64000) { |
213 | if (IPart[axis] < -64000) { |
214 | IPart[axis] = -64000; |
214 | IPart[axis] = -64000; |
215 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
215 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
216 | } else if (IPart[axis] > 64000) { |
216 | } else if (IPart[axis] > 64000) { |
217 | IPart[axis] = 64000; |
217 | IPart[axis] = 64000; |
218 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
218 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
219 | } |
219 | } |
220 | 220 | ||
221 | PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16; |
221 | PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16; |
222 | 222 | ||
223 | term[axis] = PDPart - controls[axis] + (((int32_t) IPart[axis] * ki) >> 14); |
223 | term[axis] = PDPart - controls[axis] + (((int32_t) IPart[axis] * ki) >> 14); |
224 | term[axis] += (dynamicParams.levelCorrection[axis] - 128); |
224 | term[axis] += (dynamicParams.levelCorrection[axis] - 128); |
225 | 225 | ||
226 | /* |
226 | /* |
227 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
227 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
228 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
228 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
229 | * (max. pitch or roll term is the throttle value). |
229 | * (max. pitch or roll term is the throttle value). |
230 | * OOPS: Is not applied at all. |
230 | * OOPS: Is not applied at all. |
231 | * TODO: Why a growing function of yaw? |
231 | * TODO: Why a growing function of yaw? |
232 | */ |
232 | */ |
233 | if (term[axis] < -tmp_int) { |
233 | if (term[axis] < -tmp_int) { |
234 | debugOut.digital[1] |= DEBUG_CLIP; |
234 | debugOut.digital[1] |= DEBUG_CLIP; |
235 | term[axis] = -tmp_int; |
235 | term[axis] = -tmp_int; |
236 | } else if (term[axis] > tmp_int) { |
236 | } else if (term[axis] > tmp_int) { |
237 | debugOut.digital[1] |= DEBUG_CLIP; |
237 | debugOut.digital[1] |= DEBUG_CLIP; |
238 | term[axis] = tmp_int; |
238 | term[axis] = tmp_int; |
239 | } |
239 | } |
240 | } |
240 | } |
241 | 241 | ||
242 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
242 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
243 | // Universal Mixer |
243 | // Universal Mixer |
244 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
244 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
245 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
245 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
246 | 246 | ||
247 | if (!(--debugDataTimer)) { |
247 | if (!(--debugDataTimer)) { |
248 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
248 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
249 | debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
249 | debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
250 | debugOut.analog[1] = attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
250 | debugOut.analog[1] = attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
251 | debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW; |
251 | debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW; |
- | 252 | ||
- | 253 | debugOut.analog[16] = acc[PITCH]; |
|
- | 254 | debugOut.analog[17] = acc[ROLL]; |
|
252 | 255 | ||
253 | debugOut.analog[3] = rate_ATT[PITCH]; |
256 | debugOut.analog[3] = rate_ATT[PITCH]; |
254 | debugOut.analog[4] = rate_ATT[ROLL]; |
257 | debugOut.analog[4] = rate_ATT[ROLL]; |
255 | debugOut.analog[5] = yawRate; |
258 | debugOut.analog[5] = yawRate; |
256 | } |
259 | } |
- | 260 | ||
- | 261 | /* |
|
- | 262 | debugOut.analog[6] = term[PITCH]; |
|
257 | 263 | debugOut.analog[7] = term[ROLL]; |
|
258 | debugOut.analog[8] = yawTerm; |
264 | debugOut.analog[8] = yawTerm; |
259 | debugOut.analog[9] = throttleTerm; |
265 | debugOut.analog[9] = throttleTerm; |
260 | - | ||
261 | //debugOut.analog[16] = gyroActivity; |
266 | */ |
262 | 267 | ||
263 | for (i = 0; i < MAX_MOTORS; i++) { |
268 | for (i = 0; i < MAX_MOTORS; i++) { |
264 | int32_t tmp; |
269 | int32_t tmp; |
265 | uint8_t throttle; |
270 | uint8_t throttle; |
266 | 271 | ||
267 | tmp = (int32_t) throttleTerm * motorMixer.matrix[i][MIX_THROTTLE]; |
272 | tmp = (int32_t) throttleTerm * motorMixer.matrix[i][MIX_THROTTLE]; |
268 | tmp += (int32_t) term[PITCH] * motorMixer.matrix[i][MIX_PITCH]; |
273 | tmp += (int32_t) term[PITCH] * motorMixer.matrix[i][MIX_PITCH]; |
269 | tmp += (int32_t) term[ROLL] * motorMixer.matrix[i][MIX_ROLL]; |
274 | tmp += (int32_t) term[ROLL] * motorMixer.matrix[i][MIX_ROLL]; |
270 | tmp += (int32_t) yawTerm * motorMixer.matrix[i][MIX_YAW]; |
275 | tmp += (int32_t) yawTerm * motorMixer.matrix[i][MIX_YAW]; |
271 | tmp = tmp >> 6; |
276 | tmp = tmp >> 6; |
272 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
277 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
273 | // Now we scale back down to a 0..255 range. |
278 | // Now we scale back down to a 0..255 range. |
274 | tmp = motorFilters[i] / MOTOR_SCALING; |
279 | tmp = motorFilters[i] / MOTOR_SCALING; |
275 | 280 | ||
276 | // So this was the THIRD time a throttle was limited. But should the limitation |
281 | // So this was the THIRD time a throttle was limited. But should the limitation |
277 | // apply to the common throttle signal (the one used for setting the "power" of |
282 | // apply to the common throttle signal (the one used for setting the "power" of |
278 | // all motors together) or should it limit the throttle set for each motor, |
283 | // all motors together) or should it limit the throttle set for each motor, |
279 | // including mix components of pitch, roll and yaw? I think only the common |
284 | // including mix components of pitch, roll and yaw? I think only the common |
280 | // throttle should be limited. |
285 | // throttle should be limited. |
281 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
286 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
282 | // Apply to individual signals instead. |
287 | // Apply to individual signals instead. |
283 | CHECK_MIN_MAX(tmp, 1, 255); |
288 | CHECK_MIN_MAX(tmp, 1, 255); |
284 | throttle = tmp; |
289 | throttle = tmp; |
285 | 290 | ||
286 | /* |
291 | /* |
287 | if (i < 4) |
292 | if (i < 4) |
288 | debugOut.analog[10 + i] = throttle; |
293 | debugOut.analog[10 + i] = throttle; |
289 | */ |
294 | */ |
290 | 295 | ||
291 | if ((MKFlags & MKFLAG_MOTOR_RUN) && motorMixer.matrix[i][MIX_THROTTLE] > 0) { |
296 | if ((MKFlags & MKFLAG_MOTOR_RUN) && motorMixer.matrix[i][MIX_THROTTLE] > 0) { |
292 | motor[i].throttle = throttle; |
297 | motor[i].throttle = throttle; |
293 | } else if (motorTestActive) { |
298 | } else if (motorTestActive) { |
294 | motor[i].throttle = motorTest[i]; |
299 | motor[i].throttle = motorTest[i]; |
295 | } else { |
300 | } else { |
296 | motor[i].throttle = 0; |
301 | motor[i].throttle = 0; |
297 | } |
302 | } |
298 | } |
303 | } |
299 | 304 | ||
300 | I2C_Start(TWI_STATE_MOTOR_TX); |
305 | I2C_Start(TWI_STATE_MOTOR_TX); |
301 | } |
306 | } |
302 | 307 |