Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1775 | - | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
||
3 | // + Nur für den privaten Gebrauch |
||
4 | // + www.MikroKopter.com |
||
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist. |
||
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
11 | // + Verkauf von Luftbildaufnahmen, usw. |
||
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
17 | // + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
18 | // + eindeutig als Ursprung verlinkt und genannt werden |
||
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
21 | // + Benutzung auf eigene Gefahr |
||
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
25 | // + mit unserer Zustimmung zulässig |
||
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
30 | // + this list of conditions and the following disclaimer. |
||
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
32 | // + from this software without specific prior written permission. |
||
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
34 | // + for non-commercial use (directly or indirectly) |
||
35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
36 | // + with our written permission |
||
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
38 | // + clearly linked as origin |
||
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
||
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
48 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
49 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
50 | // + POSSIBILITY OF SUCH DAMAGE. |
||
51 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
52 | |||
53 | #include <stdlib.h> |
||
54 | #include <avr/io.h> |
||
55 | #include "eeprom.h" |
||
56 | #include "flight.h" |
||
57 | |||
58 | // Only for debug. Remove. |
||
59 | //#include "analog.h" |
||
60 | //#include "rc.h" |
||
61 | |||
62 | // Necessary for external control and motor test |
||
63 | #include "uart0.h" |
||
64 | |||
65 | // for scope debugging |
||
66 | #include "rc.h" |
||
67 | |||
68 | #include "twimaster.h" |
||
69 | #include "attitude.h" |
||
70 | #include "controlMixer.h" |
||
71 | #ifdef USE_MK3MAG |
||
72 | #include "gps.h" |
||
73 | #endif |
||
74 | |||
75 | #define CHECK_MIN_MAX(value, min, max) {if(value < min) value = min; else if(value > max) value = max;} |
||
76 | |||
77 | /* |
||
78 | * These are no longer maintained, just left at 0. The original implementation just summed the acc. |
||
79 | * value to them every 2 ms. No filtering or anything. Just a case for an eventual overflow?? Hey??? |
||
80 | */ |
||
81 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
||
82 | |||
83 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
||
84 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
||
85 | |||
86 | // Some integral weight constant... |
||
87 | uint16_t Ki = 10300 / 33; |
||
88 | uint8_t RequiredMotors = 0; |
||
89 | |||
90 | // No support for altitude control right now. |
||
91 | // int16_t SetPointHeight = 0; |
||
92 | |||
93 | /************************************************************************/ |
||
94 | /* Filter for motor value smoothing (necessary???) */ |
||
95 | /************************************************************************/ |
||
96 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
||
97 | switch(dynamicParams.UserParams[5]) { |
||
98 | case 0: |
||
99 | return newvalue; |
||
100 | case 1: |
||
101 | return (oldvalue + newvalue) / 2; |
||
102 | case 2: |
||
103 | if(newvalue > oldvalue) |
||
104 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
||
105 | else |
||
106 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
||
107 | case 3: |
||
108 | if(newvalue < oldvalue) |
||
109 | return (1 * (int16_t)oldvalue + newvalue) / 2; //mean of old and new |
||
110 | else |
||
111 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
||
112 | default: return newvalue; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /************************************************************************/ |
||
117 | /* Neutral Readings */ |
||
118 | /************************************************************************/ |
||
119 | void flight_setNeutral() { |
||
120 | MKFlags |= MKFLAG_CALIBRATE; |
||
121 | |||
122 | // not really used here any more. |
||
123 | dynamicParams.KalmanK = -1; |
||
124 | dynamicParams.KalmanMaxDrift = 0; |
||
125 | dynamicParams.KalmanMaxFusion = 32; |
||
126 | |||
127 | controlMixer_initVariables(); |
||
128 | } |
||
129 | |||
130 | void setFlightParameters(uint8_t _Ki, uint8_t _gyroPFactor, uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
||
131 | Ki = 10300 / _Ki; |
||
132 | gyroPFactor = _gyroPFactor; |
||
133 | gyroIFactor = _gyroIFactor; |
||
134 | yawPFactor = _yawPFactor; |
||
135 | yawIFactor = _yawIFactor; |
||
136 | } |
||
137 | |||
138 | void setNormalFlightParameters(void) { |
||
139 | setFlightParameters(dynamicParams.IFactor + 1, |
||
140 | dynamicParams.GyroP + 10, |
||
141 | staticParams.GlobalConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.GyroI, |
||
142 | dynamicParams.GyroP + 10, |
||
143 | dynamicParams.UserParams[6] |
||
144 | ); |
||
145 | } |
||
146 | |||
147 | void setStableFlightParameters(void) { |
||
148 | setFlightParameters(33, 90, 120, 90, 120); |
||
149 | } |
||
150 | |||
151 | |||
152 | /************************************************************************/ |
||
153 | /* Main Flight Control */ |
||
154 | /************************************************************************/ |
||
155 | void flight_control(void) { |
||
156 | int16_t tmp_int; |
||
157 | // Mixer Fractions that are combined for Motor Control |
||
158 | int16_t yawTerm, throttleTerm, term[2]; |
||
159 | |||
160 | // PID controller variables |
||
161 | int16_t PDPart[2], PDPartYaw, PPart[2]; |
||
162 | static int32_t IPart[2] = {0,0}; |
||
163 | // static int32_t yawControlRate = 0; |
||
164 | |||
165 | // Removed. Too complicated, and apparently not necessary with MEMS gyros anyway. |
||
166 | // static int32_t IntegralGyroPitchError = 0, IntegralGyroRollError = 0; |
||
167 | // static int32_t CorrectionPitch, CorrectionRoll; |
||
168 | |||
169 | static uint16_t emergencyFlightTime; |
||
170 | static int8_t debugDataTimer = 1; |
||
171 | |||
172 | // High resolution motor values for smoothing of PID motor outputs |
||
173 | static int16_t motorFilters[MAX_MOTORS]; |
||
174 | |||
175 | uint8_t i, axis; |
||
176 | |||
177 | // Fire the main flight attitude calculation, including integration of angles. |
||
178 | |||
179 | calculateFlightAttitude(); |
||
180 | |||
181 | /* |
||
182 | * TODO: update should: Set the stick variables if good signal, set them to zero if bad. |
||
183 | * Set variables also. |
||
184 | */ |
||
185 | // start part 1: 750-800 usec. |
||
186 | // start part 1a: 750-800 usec. |
||
187 | // start part1b: 700 usec |
||
188 | // start part1c: 700 usec!!!!!!!!! WAY too slow. |
||
189 | controlMixer_update(); |
||
190 | // end part1c |
||
191 | |||
192 | throttleTerm = controlThrottle; |
||
193 | if(throttleTerm < staticParams.MinThrottle + 10) throttleTerm = staticParams.MinThrottle + 10; |
||
194 | else if(throttleTerm > staticParams.MaxThrottle - 20) throttleTerm = (staticParams.MaxThrottle - 20); |
||
195 | |||
196 | // end part1b: 700 usec. |
||
197 | /************************************************************************/ |
||
198 | /* RC-signal is bad */ |
||
199 | /* This part could be abstracted, as having yet another control input */ |
||
200 | /* to the control mixer: An emergency autopilot control. */ |
||
201 | /************************************************************************/ |
||
202 | |||
203 | if(controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
||
204 | RED_ON; |
||
205 | beepRCAlarm(); |
||
206 | |||
207 | if(emergencyFlightTime) { |
||
208 | // continue emergency flight |
||
209 | emergencyFlightTime--; |
||
210 | if(isFlying > 256) { |
||
211 | // We're probably still flying. Descend slowly. |
||
212 | throttleTerm = staticParams.EmergencyGas; // Set emergency throttle |
||
213 | MKFlags |= (MKFLAG_EMERGENCY_LANDING); // Set flag for emergency landing |
||
214 | setStableFlightParameters(); |
||
215 | } else { |
||
216 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
||
217 | } |
||
218 | } else { |
||
219 | // end emergency flight (just cut the motors???) |
||
220 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_LANDING); |
||
221 | } |
||
222 | } else { |
||
223 | // signal is acceptable |
||
224 | if(controlMixer_getSignalQuality() > SIGNAL_BAD) { |
||
225 | // Reset emergency landing control variables. |
||
226 | MKFlags &= ~(MKFLAG_EMERGENCY_LANDING); // clear flag for emergency landing |
||
227 | // The time is in whole seconds. |
||
228 | emergencyFlightTime = (uint16_t)staticParams.EmergencyGasDuration * 488; |
||
229 | } |
||
230 | |||
231 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
||
232 | if(throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
||
233 | // increment flight-time counter until overflow. |
||
234 | if(isFlying != 0xFFFF) isFlying++; |
||
235 | } else |
||
236 | /* |
||
237 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
||
238 | * Probably to avoid integration effects that will cause the copter to spin |
||
239 | * or flip when taking off. |
||
240 | */ |
||
241 | if(isFlying < 256) { |
||
242 | IPart[PITCH] = IPart[ROLL] = 0; |
||
243 | // TODO: Don't stomp on other modules' variables!!! |
||
244 | controlYaw = 0; |
||
245 | if(isFlying == 250) { |
||
246 | // HC_setGround(); |
||
247 | updateCompassCourse = 1; |
||
248 | yawAngleDiff = 0; |
||
249 | } |
||
250 | } else { |
||
251 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
||
252 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
||
253 | MKFlags |= (MKFLAG_FLY); |
||
254 | } |
||
255 | |||
256 | commands_handleCommands(); |
||
257 | |||
258 | // if(controlMixer_getSignalQuality() >= SIGNAL_GOOD) { |
||
259 | setNormalFlightParameters(); |
||
260 | // } |
||
261 | } // end else (not bad signal case) |
||
262 | // end part1a: 750-800 usec. |
||
263 | /* |
||
264 | * Looping the H&I way basically is just a matter of turning off attitude angle measurement |
||
265 | * by integration (because 300 deg/s gyros are too slow) and turning down the throttle. |
||
266 | * This is the throttle part. |
||
267 | */ |
||
268 | if(looping) { |
||
269 | if(throttleTerm > staticParams.LoopGasLimit) throttleTerm = staticParams.LoopGasLimit; |
||
270 | } |
||
271 | |||
272 | /************************************************************************/ |
||
273 | /* Yawing */ |
||
274 | /************************************************************************/ |
||
275 | if(abs(controlYaw) > 4 * staticParams.StickYawP) { // yaw stick is activated |
||
276 | badCompassHeading = 1000; |
||
277 | if(!(staticParams.GlobalConfig & CFG_COMPASS_FIX)) { |
||
278 | updateCompassCourse = 1; |
||
279 | } |
||
280 | } |
||
281 | |||
282 | // yawControlRate = controlYaw; |
||
283 | |||
284 | // Trim drift of yawAngleDiff with controlYaw. |
||
285 | // TODO: We want NO feedback of control related stuff to the attitude related stuff. |
||
286 | // This seems to be used as: Difference desired <--> real heading. |
||
287 | yawAngleDiff -= controlYaw; |
||
288 | |||
289 | // limit the effect |
||
290 | CHECK_MIN_MAX(yawAngleDiff, -50000, 50000); |
||
291 | |||
292 | /************************************************************************/ |
||
293 | /* Compass is currently not supported. */ |
||
294 | /************************************************************************/ |
||
295 | if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) { |
||
296 | updateCompass(); |
||
297 | } |
||
298 | |||
299 | #if defined (USE_MK3MAG) |
||
300 | /************************************************************************/ |
||
301 | /* GPS is currently not supported. */ |
||
302 | /************************************************************************/ |
||
303 | if(staticParams.GlobalConfig & CFG_GPS_ACTIVE) { |
||
304 | GPS_Main(); |
||
305 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); |
||
306 | } |
||
307 | else { |
||
308 | // GPSStickPitch = 0; |
||
309 | // GPSStickRoll = 0; |
||
310 | } |
||
311 | #endif |
||
312 | // end part 1: 750-800 usec. |
||
313 | // start part 3: 350 - 400 usec. |
||
314 | #define SENSOR_LIMIT (4096 * 4) |
||
315 | /************************************************************************/ |
||
316 | |||
317 | /* Calculate control feedback from angle (gyro integral) */ |
||
318 | /* and angular velocity (gyro signal) */ |
||
319 | /************************************************************************/ |
||
320 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
||
321 | for (axis=PITCH; axis<=ROLL; axis++) { |
||
322 | if(looping & ((1<<4)<<axis)) { |
||
323 | PPart[axis] = 0; |
||
324 | } else { // TODO: Where do the 44000 come from??? |
||
325 | PPart[axis] = angle[axis] * gyroIFactor / (44000 / CONTROL_SCALING); // P-Part - Proportional to Integral |
||
326 | } |
||
327 | |||
328 | /* |
||
329 | * Now blend in the D-part - proportional to the Differential of the integral = the rate. |
||
330 | * Read this as: PDPart = PPart + rate_PID * pfactor * CONTROL_SCALING |
||
331 | * where pfactor is in [0..1]. |
||
332 | */ |
||
333 | PDPart[axis] = PPart[axis] + (int32_t)((int32_t)rate_PID[axis] * gyroPFactor / (256L / CONTROL_SCALING)) |
||
334 | + (differential[axis] * (int16_t)dynamicParams.GyroD) / 16; |
||
335 | |||
336 | CHECK_MIN_MAX(PDPart[axis], -SENSOR_LIMIT, SENSOR_LIMIT); |
||
337 | } |
||
338 | |||
339 | PDPartYaw = |
||
340 | (int32_t)(yawRate * 2 * (int32_t)yawPFactor) / (256L / CONTROL_SCALING) |
||
341 | + (int32_t)(yawAngleDiff * yawIFactor) / (2 * (44000 / CONTROL_SCALING)); |
||
342 | |||
343 | // limit control feedback |
||
344 | CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
||
345 | |||
346 | /* |
||
347 | * Compose throttle term. |
||
348 | * If a Bl-Ctrl is missing, prevent takeoff. |
||
349 | */ |
||
350 | if(missingMotor) { |
||
351 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
||
352 | if((isFlying > 1) && (isFlying < 50) && (throttleTerm > 0)) |
||
353 | isFlying = 1; // keep within lift off condition |
||
354 | throttleTerm = staticParams.MinThrottle; // reduce gas to min to avoid lift of |
||
355 | } |
||
356 | |||
357 | throttleTerm *= CONTROL_SCALING; |
||
358 | |||
359 | /* |
||
360 | * Compose yaw term. |
||
361 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
||
362 | * However, at low throttle the yaw term is limited to a fixed value, |
||
363 | * and at high throttle it is limited by the throttle reserve (the difference |
||
364 | * between current throttle and maximum throttle). |
||
365 | */ |
||
366 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
||
367 | yawTerm = PDPartYaw - controlYaw * CONTROL_SCALING; |
||
368 | // limit yawTerm |
||
369 | if(throttleTerm > MIN_YAWGAS) { |
||
370 | CHECK_MIN_MAX(yawTerm, - (throttleTerm / 2), (throttleTerm / 2)); |
||
371 | } else { |
||
372 | CHECK_MIN_MAX(yawTerm, - (MIN_YAWGAS / 2), (MIN_YAWGAS / 2)); |
||
373 | } |
||
374 | |||
375 | tmp_int = staticParams.MaxThrottle * CONTROL_SCALING; |
||
376 | CHECK_MIN_MAX(yawTerm, -(tmp_int - throttleTerm), (tmp_int - throttleTerm)); |
||
377 | |||
378 | tmp_int = (int32_t)((int32_t)dynamicParams.DynamicStability * (int32_t)(throttleTerm + abs(yawTerm) / 2)) / 64; |
||
379 | |||
380 | for (axis=PITCH; axis<=ROLL; axis++) { |
||
381 | /* |
||
382 | * Compose pitch and roll terms. This is finally where the sticks come into play. |
||
383 | */ |
||
384 | if(gyroIFactor) { |
||
385 | // Integration mode: Integrate (angle - stick) = the difference between angle and stick pos. |
||
386 | // That means: Holding the stick a little forward will, at constant flight attitude, cause this to grow (decline??) over time. |
||
387 | // TODO: Find out why this seems to be proportional to stick position - not integrating it at all. |
||
388 | IPart[axis] += PPart[axis] - control[axis]; // Integrate difference between P part (the angle) and the stick pos. |
||
389 | } else { |
||
390 | // "HH" mode: Integrate (rate - stick) = the difference between rotation rate and stick pos. |
||
391 | // To keep up with a full stick PDPart should be about 156... |
||
392 | IPart[axis] += PDPart[axis] - control[axis]; // With gyroIFactor == 0, PDPart is really just a D-part. Integrate D-part (the rot. rate) and the stick pos. |
||
393 | } |
||
394 | |||
395 | // TODO: From which planet comes the 16000? |
||
396 | CHECK_MIN_MAX(IPart[axis], -(CONTROL_SCALING * 16000L), (CONTROL_SCALING * 16000L)); |
||
397 | // Add (P, D) parts minus stick pos. to the scaled-down I part. |
||
398 | term[axis] = PDPart[axis] - control[axis] + IPart[axis] / Ki; // PID-controller for pitch |
||
399 | |||
400 | /* |
||
401 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
||
402 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
||
403 | * (max. pitch or roll term is the throttle value). |
||
404 | * TODO: Why a growing function of yaw? |
||
405 | */ |
||
406 | CHECK_MIN_MAX(term[axis], -tmp_int, tmp_int); |
||
407 | } |
||
408 | // end part 3: 350 - 400 usec. |
||
409 | |||
410 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
411 | // Universal Mixer |
||
412 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
||
413 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
414 | |||
415 | for(i = 0; i < MAX_MOTORS; i++) { |
||
416 | int16_t tmp; |
||
417 | if (MKFlags & MKFLAG_MOTOR_RUN && Mixer.Motor[i][MIX_THROTTLE] > 0) { |
||
418 | tmp = ((int32_t)throttleTerm * Mixer.Motor[i][MIX_THROTTLE]) / 64L; |
||
419 | tmp += ((int32_t)term[PITCH] * Mixer.Motor[i][MIX_PITCH]) / 64L; |
||
420 | tmp += ((int32_t)term[ROLL] * Mixer.Motor[i][MIX_ROLL]) / 64L; |
||
421 | tmp += ((int32_t)yawTerm * Mixer.Motor[i][MIX_YAW]) / 64L; |
||
422 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
||
423 | tmp = motorFilters[i] / CONTROL_SCALING; |
||
424 | // So this was the THIRD time a throttle was limited. But should the limitation |
||
425 | // apply to the common throttle signal (the one used for setting the "power" of |
||
426 | // all motors together) or should it limit the throttle set for each motor, |
||
427 | // including mix components of pitch, roll and yaw? I think only the common |
||
428 | // throttle should be limited. |
||
429 | // CHECK_MIN_MAX(tmp, staticParams.MinThrottle, staticParams.MaxThrottle); |
||
430 | DebugOut.Analog[22+i] = tmp; |
||
431 | CHECK_MIN_MAX(tmp, 1, 255); |
||
432 | Motor[i].SetPoint = tmp; |
||
433 | } |
||
434 | else if (motorTestActive) { |
||
435 | Motor[i].SetPoint = motorTest[i]; |
||
436 | } else { |
||
437 | Motor[i].SetPoint = 0; |
||
438 | } |
||
439 | } |
||
440 | I2C_Start(TWI_STATE_MOTOR_TX); |
||
441 | |||
442 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
443 | // Debugging |
||
444 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
445 | if(!(--debugDataTimer)) { |
||
446 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
||
447 | DebugOut.Analog[0] = (10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
||
448 | DebugOut.Analog[1] = (10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL; // in 0.1 deg |
||
449 | DebugOut.Analog[2] = yawGyroHeading / GYRO_DEG_FACTOR_YAW; |
||
450 | |||
451 | /* |
||
452 | DebugOut.Analog[23] = (yawRate * 2 * (int32_t)yawPFactor) / (256L / CONTROL_SCALING); |
||
453 | DebugOut.Analog[24] = controlYaw; |
||
454 | DebugOut.Analog[25] = yawAngleDiff / 100L; |
||
455 | */ |
||
456 | |||
457 | DebugOut.Analog[26] = accNoisePeak[PITCH]; |
||
458 | DebugOut.Analog[27] = accNoisePeak[ROLL]; |
||
459 | |||
460 | DebugOut.Analog[30] = gyroNoisePeak[PITCH]; |
||
461 | DebugOut.Analog[31] = gyroNoisePeak[ROLL]; |
||
462 | } |
||
463 | } |