Rev 2053 | Rev 2057 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2053 | Rev 2055 | ||
---|---|---|---|
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 (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 sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
- | |
18 | // + eindeutig als Ursprung verlinkt 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 example: 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// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
- | |
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
- | |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
- | |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
51 | - | ||
52 | #include <stdlib.h> |
1 | #include <stdlib.h> |
53 | #include <avr/io.h> |
2 | #include <avr/io.h> |
54 | #include "eeprom.h" |
3 | #include "eeprom.h" |
55 | #include "flight.h" |
4 | #include "flight.h" |
56 | #include "output.h" |
5 | #include "output.h" |
57 | #include "uart0.h" |
6 | #include "uart0.h" |
58 | 7 | ||
59 | // Necessary for external control and motor test |
8 | // Necessary for external control and motor test |
60 | #include "twimaster.h" |
9 | #include "twimaster.h" |
61 | #include "attitude.h" |
10 | #include "attitude.h" |
62 | #include "controlMixer.h" |
11 | #include "controlMixer.h" |
63 | #include "commands.h" |
12 | #include "commands.h" |
64 | #include "heightControl.h" |
13 | #include "heightControl.h" |
65 | 14 | ||
66 | #ifdef USE_MK3MAG |
15 | #ifdef USE_MK3MAG |
67 | #include "mk3mag.h" |
16 | #include "mk3mag.h" |
68 | #include "compassControl.h" |
17 | #include "compassControl.h" |
69 | #endif |
18 | #endif |
70 | 19 | ||
71 | #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;} |
72 | 21 | ||
73 | /* |
22 | /* |
74 | * 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. |
75 | * 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??? |
76 | */ |
25 | */ |
77 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
26 | // int16_t naviAccPitch = 0, naviAccRoll = 0, naviCntAcc = 0; |
78 | - | ||
79 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
27 | uint8_t gyroPFactor, gyroIFactor; // the PD factors for the attitude control |
80 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
28 | uint8_t yawPFactor, yawIFactor; // the PD factors for the yaw control |
81 | uint8_t invKi = 64; |
29 | uint8_t invKi; |
- | 30 | int32_t IPart[2]; |
|
82 | 31 | ||
83 | /************************************************************************/ |
32 | /************************************************************************/ |
84 | /* Filter for motor value smoothing (necessary???) */ |
33 | /* Filter for motor value smoothing (necessary???) */ |
85 | /************************************************************************/ |
34 | /************************************************************************/ |
86 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
35 | int16_t motorFilter(int16_t newvalue, int16_t oldvalue) { |
87 | switch (staticParams.motorSmoothing) { |
36 | switch (staticParams.motorSmoothing) { |
88 | case 0: |
37 | case 0: |
89 | return newvalue; |
38 | return newvalue; |
90 | case 1: |
39 | case 1: |
91 | return (oldvalue + newvalue) / 2; |
40 | return (oldvalue + newvalue) / 2; |
92 | case 2: |
41 | case 2: |
93 | if (newvalue > oldvalue) |
42 | if (newvalue > oldvalue) |
94 | 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 |
95 | else |
44 | else |
96 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
45 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
97 | case 3: |
46 | case 3: |
98 | if (newvalue < oldvalue) |
47 | if (newvalue < oldvalue) |
99 | 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 |
100 | else |
49 | else |
101 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
50 | return newvalue - (oldvalue - newvalue) * 1; // 2 * new - old |
102 | default: |
51 | default: |
103 | return newvalue; |
52 | return newvalue; |
104 | } |
53 | } |
105 | } |
54 | } |
106 | - | ||
107 | /************************************************************************/ |
- | |
108 | /* Neutral Readings */ |
- | |
109 | /************************************************************************/ |
- | |
110 | void flight_setNeutral() { |
- | |
111 | MKFlags |= MKFLAG_CALIBRATE; |
- | |
112 | // not really used here any more. |
- | |
113 | /* |
- | |
114 | dynamicParams.KalmanK = -1; |
- | |
115 | dynamicParams.KalmanMaxDrift = 0; |
- | |
116 | dynamicParams.KalmanMaxFusion = 32; |
- | |
117 | */ |
- | |
118 | controlMixer_initVariables(); |
- | |
119 | } |
- | |
120 | 55 | ||
121 | void setFlightParameters(uint8_t _invKi, uint8_t _gyroPFactor, |
56 | void flight_setParameters(uint8_t _invKi, uint8_t _gyroPFactor, |
122 | uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
57 | uint8_t _gyroIFactor, uint8_t _yawPFactor, uint8_t _yawIFactor) { |
123 | invKi = _invKi; |
58 | invKi = _invKi; |
124 | gyroPFactor = _gyroPFactor; |
59 | gyroPFactor = _gyroPFactor; |
125 | gyroIFactor = _gyroIFactor; |
60 | gyroIFactor = _gyroIFactor; |
126 | yawPFactor = _yawPFactor; |
61 | yawPFactor = _yawPFactor; |
127 | yawIFactor = _yawIFactor; |
62 | yawIFactor = _yawIFactor; |
128 | } |
63 | } |
129 | 64 | ||
130 | void setNormalFlightParameters(void) { |
65 | void flight_setGround() { |
131 | setFlightParameters( |
66 | // Just reset all I terms. |
132 | staticParams.IFactor, |
67 | IPart[PITCH] = IPart[ROLL] = 0; |
133 | dynamicParams.gyroP, |
- | |
134 | staticParams.bitConfig & CFG_HEADING_HOLD ? 0 : dynamicParams.gyroI, |
- | |
135 | dynamicParams.gyroP, |
- | |
136 | staticParams.yawIFactor |
- | |
137 | ); |
68 | headingError = 0; |
138 | } |
69 | } |
- | 70 | ||
- | 71 | void flight_takeOff() { |
|
139 | 72 | HC_setGround(); |
|
- | 73 | #ifdef USE_MK3MAG |
|
- | 74 | attitude_resetHeadingToMagnetic(); |
|
140 | void setStableFlightParameters(void) { |
75 | compass_setTakeoffHeading(heading); |
141 | setFlightParameters(0, 90, 120, 90, 120); |
76 | #endif |
142 | } |
77 | } |
143 | 78 | ||
144 | /************************************************************************/ |
79 | /************************************************************************/ |
145 | /* Main Flight Control */ |
80 | /* Main Flight Control */ |
146 | /************************************************************************/ |
81 | /************************************************************************/ |
147 | void flight_control(void) { |
82 | void flight_control(void) { |
148 | uint16_t tmp_int; |
83 | int16_t tmp_int; |
149 | // Mixer Fractions that are combined for Motor Control |
84 | // Mixer Fractions that are combined for Motor Control |
150 | int16_t yawTerm, throttleTerm, term[2]; |
85 | int16_t yawTerm, throttleTerm, term[2]; |
151 | 86 | ||
152 | // PID controller variables |
87 | // PID controller variables |
153 | int16_t PDPart; |
88 | int16_t PDPart; |
154 | static int32_t IPart[2] = {0, 0}; |
- | |
155 | static uint16_t emergencyFlightTime; |
- | |
156 | static int8_t debugDataTimer = 1; |
89 | static int8_t debugDataTimer = 1; |
157 | 90 | ||
158 | // High resolution motor values for smoothing of PID motor outputs |
91 | // High resolution motor values for smoothing of PID motor outputs |
159 | static int16_t motorFilters[MAX_MOTORS]; |
92 | static int16_t motorFilters[MAX_MOTORS]; |
160 | 93 | ||
161 | uint8_t i, axis; |
94 | uint8_t i, axis; |
162 | 95 | ||
163 | throttleTerm = controls[CONTROL_THROTTLE]; |
96 | throttleTerm = controls[CONTROL_THROTTLE]; |
- | 97 | ||
- | 98 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
|
- | 99 | // increment flight-time counter until overflow. |
|
- | 100 | if (isFlying != 0xFFFF) |
|
- | 101 | isFlying++; |
|
- | 102 | } |
|
- | 103 | /* |
|
- | 104 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
|
- | 105 | * Probably to avoid integration effects that will cause the copter to spin |
|
- | 106 | * or flip when taking off. |
|
- | 107 | */ |
|
- | 108 | if (isFlying < 256) { |
|
- | 109 | flight_setGround(); |
|
- | 110 | if (isFlying == 250) |
|
- | 111 | flight_takeOff(); |
|
- | 112 | } |
|
164 | 113 | ||
165 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
114 | // This check removed. Is done on a per-motor basis, after output matrix multiplication. |
166 | if (throttleTerm < staticParams.minThrottle + 10) |
115 | if (throttleTerm < staticParams.minThrottle + 10) |
167 | throttleTerm = staticParams.minThrottle + 10; |
116 | throttleTerm = staticParams.minThrottle + 10; |
168 | else if (throttleTerm > staticParams.maxThrottle - 20) |
117 | else if (throttleTerm > staticParams.maxThrottle - 20) |
169 | throttleTerm = (staticParams.maxThrottle - 20); |
118 | throttleTerm = (staticParams.maxThrottle - 20); |
170 | - | ||
171 | /************************************************************************/ |
- | |
172 | /* RC-signal is bad */ |
119 | |
173 | /* This part could be abstracted, as having yet another control input */ |
- | |
174 | /* to the control mixer: An emergency autopilot control. */ |
- | |
175 | /************************************************************************/ |
- | |
176 | - | ||
177 | if (controlMixer_getSignalQuality() <= SIGNAL_BAD) { // the rc-frame signal is not reveived or noisy |
- | |
178 | if (controlMixer_didReceiveSignal) beepRCAlarm(); // Only make alarm if a control signal was received before the signal loss. |
- | |
179 | if (emergencyFlightTime) { |
- | |
180 | // continue emergency flight |
120 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
181 | emergencyFlightTime--; |
- | |
182 | if (isFlying > 256) { |
- | |
183 | // We're probably still flying. Descend slowly. |
- | |
184 | throttleTerm = staticParams.emergencyThrottle; // Set emergency throttle |
- | |
185 | MKFlags |= (MKFLAG_EMERGENCY_FLIGHT); // Set flag for emergency landing |
- | |
186 | setStableFlightParameters(); |
- | |
187 | } else { |
- | |
188 | MKFlags &= ~(MKFLAG_MOTOR_RUN); // Probably not flying, and bad R/C signal. Kill motors. |
- | |
189 | } |
- | |
190 | } else { |
121 | throttleTerm *= CONTROL_SCALING; |
191 | // end emergency flight (just cut the motors???) |
- | |
192 | MKFlags &= ~(MKFLAG_MOTOR_RUN | MKFLAG_EMERGENCY_FLIGHT); |
- | |
193 | } |
- | |
194 | } else { |
- | |
195 | // signal is acceptable |
- | |
196 | if (controlMixer_getSignalQuality() > SIGNAL_BAD) { |
- | |
197 | // Reset emergency landing control variables. |
- | |
198 | MKFlags &= ~(MKFLAG_EMERGENCY_FLIGHT); // clear flag for emergency landing |
- | |
199 | // The time is in whole seconds. |
- | |
200 | if (staticParams.emergencyFlightDuration > (65535-F_MAINLOOP)/F_MAINLOOP) |
- | |
201 | emergencyFlightTime = 0xffff; |
- | |
202 | else |
- | |
203 | emergencyFlightTime = (uint16_t)staticParams.emergencyFlightDuration * F_MAINLOOP; |
- | |
204 | } |
- | |
205 | - | ||
206 | // If some throttle is given, and the motor-run flag is on, increase the probability that we are flying. |
- | |
207 | if (throttleTerm > 40 && (MKFlags & MKFLAG_MOTOR_RUN)) { |
- | |
208 | // increment flight-time counter until overflow. |
- | |
209 | if (isFlying != 0xFFFF) |
- | |
210 | isFlying++; |
- | |
211 | } else |
- | |
212 | /* |
- | |
213 | * When standing on the ground, do not apply I controls and zero the yaw stick. |
- | |
214 | * Probably to avoid integration effects that will cause the copter to spin |
- | |
215 | * or flip when taking off. |
- | |
216 | */ |
- | |
217 | if (isFlying < 256) { |
- | |
218 | IPart[PITCH] = IPart[ROLL] = 0; |
- | |
219 | if (isFlying == 250) { |
- | |
220 | HC_setGround(); |
- | |
221 | #ifdef USE_MK3MAG |
- | |
222 | attitude_resetHeadingToMagnetic(); |
- | |
223 | compass_setTakeoffHeading(heading); |
- | |
224 | #endif |
- | |
225 | // Set target heading to the one just gotten off compass. |
- | |
226 | // targetHeading = heading; |
- | |
227 | } |
- | |
228 | } else { |
- | |
229 | // Set fly flag. TODO: Hmmm what can we trust - the isFlying counter or the flag? |
- | |
230 | // Answer: The counter. The flag is not read from anywhere anyway... except the NC maybe. |
- | |
231 | MKFlags |= (MKFLAG_FLY); |
- | |
232 | } |
- | |
233 | - | ||
234 | commands_handleCommands(); |
- | |
235 | setNormalFlightParameters(); |
- | |
236 | } // end else (not bad signal case) |
122 | // TODO: We dont need to repeat this for every iteration! |
237 | 123 | ||
238 | // end part 1: 750-800 usec. |
124 | // end part 1: 750-800 usec. |
239 | // start part 3: 350 - 400 usec. |
125 | // start part 3: 350 - 400 usec. |
240 | #define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW) |
126 | #define YAW_I_LIMIT (45L * GYRO_DEG_FACTOR_YAW) |
241 | // This is where control affects the target heading. It also (later) directly controls yaw. |
127 | // This is where control affects the target heading. It also (later) directly controls yaw. |
242 | headingError -= controls[CONTROL_YAW]; |
128 | headingError -= controls[CONTROL_YAW]; |
243 | debugOut.analog[28] = headingError / 100; |
129 | if (headingError < -YAW_I_LIMIT) |
- | 130 | headingError = -YAW_I_LIMIT; |
|
244 | if (headingError < -YAW_I_LIMIT) headingError = -YAW_I_LIMIT; |
131 | if (headingError > YAW_I_LIMIT) |
245 | if (headingError > YAW_I_LIMIT) headingError = YAW_I_LIMIT; |
132 | headingError = YAW_I_LIMIT; |
246 | 133 | ||
247 | PDPart = (int32_t)(headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4); |
134 | PDPart = (int32_t) (headingError * yawIFactor) / (GYRO_DEG_FACTOR_YAW << 4); |
248 | // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on. |
- | |
249 | PDPart += (int32_t)(yawRate * yawPFactor) / (GYRO_DEG_FACTOR_YAW >> 5); |
135 | // Ehhhhh here is something with desired yaw rate, not?? Ahh OK it gets added in later on. |
250 | - | ||
251 | /* |
- | |
252 | * Compose throttle term. |
- | |
253 | * If a Bl-Ctrl is missing, prevent takeoff. |
- | |
254 | */ |
- | |
255 | if (missingMotor) { |
136 | PDPart += (int32_t) (yawRate * yawPFactor) / (GYRO_DEG_FACTOR_YAW >> 5); |
256 | // if we are in the lift off condition. Hmmmmmm when is throttleTerm == 0 anyway??? |
- | |
257 | if (isFlying > 1 && isFlying < 50 && throttleTerm > 0) |
- | |
258 | isFlying = 1; // keep within lift off condition |
- | |
259 | throttleTerm = staticParams.minThrottle; // reduce gas to min to avoid lift of |
- | |
260 | } |
- | |
261 | 137 | ||
262 | // Scale up to higher resolution. Hmm why is it not (from controlMixer and down) scaled already? |
138 | // Lets not limit P and D. |
263 | throttleTerm *= CONTROL_SCALING; |
139 | // CHECK_MIN_MAX(PDPartYaw, -SENSOR_LIMIT, SENSOR_LIMIT); |
264 | 140 | ||
265 | /* |
141 | /* |
266 | * Compose yaw term. |
142 | * Compose yaw term. |
267 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
143 | * The yaw term is limited: Absolute value is max. = the throttle term / 2. |
268 | * However, at low throttle the yaw term is limited to a fixed value, |
144 | * However, at low throttle the yaw term is limited to a fixed value, |
269 | * and at high throttle it is limited by the throttle reserve (the difference |
145 | * and at high throttle it is limited by the throttle reserve (the difference |
270 | * between current throttle and maximum throttle). |
146 | * between current throttle and maximum throttle). |
271 | */ |
147 | */ |
272 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
148 | #define MIN_YAWGAS (40 * CONTROL_SCALING) // yaw also below this gas value |
273 | yawTerm = PDPart - controls[CONTROL_YAW] * CONTROL_SCALING; |
149 | yawTerm = PDPart - controls[CONTROL_YAW] * CONTROL_SCALING; |
274 | // Limit yawTerm |
150 | // Limit yawTerm |
275 | debugOut.digital[0] &= ~DEBUG_CLIP; |
151 | debugOut.digital[0] &= ~DEBUG_CLIP; |
276 | if (throttleTerm > MIN_YAWGAS) { |
152 | if (throttleTerm > MIN_YAWGAS) { |
277 | if (yawTerm < -throttleTerm / 2) { |
153 | if (yawTerm < -throttleTerm / 2) { |
278 | debugOut.digital[0] |= DEBUG_CLIP; |
154 | debugOut.digital[0] |= DEBUG_CLIP; |
279 | yawTerm = -throttleTerm / 2; |
155 | yawTerm = -throttleTerm / 2; |
280 | } else if (yawTerm > throttleTerm / 2) { |
156 | } else if (yawTerm > throttleTerm / 2) { |
281 | debugOut.digital[0] |= DEBUG_CLIP; |
157 | debugOut.digital[0] |= DEBUG_CLIP; |
282 | yawTerm = throttleTerm / 2; |
158 | yawTerm = throttleTerm / 2; |
283 | } |
159 | } |
284 | } else { |
160 | } else { |
285 | if (yawTerm < -MIN_YAWGAS / 2) { |
161 | if (yawTerm < -MIN_YAWGAS / 2) { |
286 | debugOut.digital[0] |= DEBUG_CLIP; |
162 | debugOut.digital[0] |= DEBUG_CLIP; |
287 | yawTerm = -MIN_YAWGAS / 2; |
163 | yawTerm = -MIN_YAWGAS / 2; |
288 | } else if (yawTerm > MIN_YAWGAS / 2) { |
164 | } else if (yawTerm > MIN_YAWGAS / 2) { |
289 | debugOut.digital[0] |= DEBUG_CLIP; |
165 | debugOut.digital[0] |= DEBUG_CLIP; |
290 | yawTerm = MIN_YAWGAS / 2; |
166 | yawTerm = MIN_YAWGAS / 2; |
291 | } |
167 | } |
292 | } |
168 | } |
293 | 169 | ||
294 | tmp_int = staticParams.maxThrottle * CONTROL_SCALING; |
170 | tmp_int = staticParams.maxThrottle * CONTROL_SCALING; |
- | 171 | ||
295 | if (yawTerm < -(tmp_int - throttleTerm)) { |
172 | if (yawTerm < -(tmp_int - throttleTerm)) { |
296 | yawTerm = -(tmp_int - throttleTerm); |
173 | yawTerm = -(tmp_int - throttleTerm); |
297 | debugOut.digital[0] |= DEBUG_CLIP; |
174 | debugOut.digital[0] |= DEBUG_CLIP; |
298 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
175 | } else if (yawTerm > (tmp_int - throttleTerm)) { |
299 | yawTerm = (tmp_int - throttleTerm); |
176 | yawTerm = (tmp_int - throttleTerm); |
300 | debugOut.digital[0] |= DEBUG_CLIP; |
177 | debugOut.digital[0] |= DEBUG_CLIP; |
301 | } |
178 | } |
302 | 179 | ||
303 | debugOut.digital[1] &= ~DEBUG_CLIP; |
180 | debugOut.digital[1] &= ~DEBUG_CLIP; |
304 | 181 | ||
- | 182 | tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + (abs(yawTerm) >> 1)) >> 6); |
|
305 | tmp_int = ((uint16_t)dynamicParams.dynamicStability * ((uint16_t)throttleTerm + abs(yawTerm) / 2)) >> 6; |
183 | //tmp_int = (int32_t) ((int32_t) dynamicParams.dynamicStability * (int32_t) (throttleTerm + abs(yawTerm) / 2)) / 64; |
306 | 184 | ||
307 | /************************************************************************/ |
185 | /************************************************************************/ |
308 | /* Calculate control feedback from angle (gyro integral) */ |
186 | /* Calculate control feedback from angle (gyro integral) */ |
309 | /* and angular velocity (gyro signal) */ |
187 | /* and angular velocity (gyro signal) */ |
310 | /************************************************************************/ |
188 | /************************************************************************/ |
311 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
189 | // The P-part is the P of the PID controller. That's the angle integrals (not rates). |
312 | for (axis = PITCH; axis <= ROLL; axis++) { |
190 | for (axis = PITCH; axis <= ROLL; axis++) { |
313 | int16_t iDiff; |
- | |
314 | iDiff = PDPart = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 3); |
- | |
315 | PDPart += (int32_t)rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4); |
191 | PDPart = (int32_t) rate_PID[axis] * gyroPFactor / (GYRO_DEG_FACTOR_PITCHROLL >> 4); |
316 | PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16; |
192 | PDPart += (differential[axis] * (int16_t) dynamicParams.gyroD) / 16; |
317 | // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick. |
193 | // In acc. mode the I part is summed only from the attitude (IFaktor) angle minus stick. |
318 | // In HH mode, the I part is summed from P and D of gyros minus stick. |
194 | // In HH mode, the I part is summed from P and D of gyros minus stick. |
319 | if (gyroIFactor) { |
195 | if (gyroIFactor) { |
- | 196 | int16_t iDiff = attitude[axis] * gyroIFactor / (GYRO_DEG_FACTOR_PITCHROLL << 3); |
|
- | 197 | PDPart += iDiff; |
|
320 | 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. |
198 | 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. |
321 | } else { |
199 | } else { |
322 | 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. |
200 | 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. |
323 | } |
201 | } |
324 | 202 | ||
325 | // With normal Ki, limit effect to +/- 205 (of 1024!!!) |
203 | // With normal Ki, limit I parts to +/- 205 (of about 1024) |
326 | if (IPart[axis] < -64000) { |
204 | if (IPart[axis] < -64000) { |
327 | IPart[axis] = -64000; |
205 | IPart[axis] = -64000; |
328 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
206 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
329 | } else if (IPart[axis] > 64000) { |
207 | } else if (IPart[axis] > 64000) { |
330 | IPart[axis] = 64000; |
208 | IPart[axis] = 64000; |
331 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
209 | debugOut.digital[1] |= DEBUG_FLIGHTCLIP; |
332 | } |
210 | } |
333 | 211 | ||
334 | term[axis] = PDPart - controls[axis] + ((int32_t)IPart[axis] * invKi) >> 14; |
212 | term[axis] = PDPart - controls[axis] + (((int32_t) IPart[axis] * invKi) >> 14); |
- | 213 | term[axis] += (dynamicParams.levelCorrection[axis] - 128); |
|
335 | term[axis] += (dynamicParams.levelCorrection[axis] - 128); |
214 | |
336 | /* |
215 | /* |
337 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
216 | * Apply "dynamic stability" - that is: Limit pitch and roll terms to a growing function of throttle and yaw(!). |
338 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
217 | * The higher the dynamic stability parameter, the wider the bounds. 64 seems to be a kind of unity |
339 | * (max. pitch or roll term is the throttle value). |
218 | * (max. pitch or roll term is the throttle value). |
340 | * TODO: Why a growing function of yaw? |
219 | * TODO: Why a growing function of yaw? |
341 | */ |
220 | */ |
342 | if (term[axis] < -tmp_int) { |
221 | if (term[axis] < -tmp_int) { |
343 | debugOut.digital[1] |= DEBUG_CLIP; |
222 | debugOut.digital[1] |= DEBUG_CLIP; |
- | 223 | term[axis] = -tmp_int; |
|
344 | } else if (term[axis] > tmp_int) { |
224 | } else if (term[axis] > tmp_int) { |
345 | debugOut.digital[1] |= DEBUG_CLIP; |
225 | debugOut.digital[1] |= DEBUG_CLIP; |
- | 226 | term[axis] = tmp_int; |
|
346 | } |
227 | } |
347 | } |
228 | } |
348 | 229 | ||
349 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
230 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
350 | // Universal Mixer |
231 | // Universal Mixer |
351 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
232 | // Each (pitch, roll, throttle, yaw) term is in the range [0..255 * CONTROL_SCALING]. |
352 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
233 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | 234 | ||
- | 235 | if (!(--debugDataTimer)) { |
|
353 | 236 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
|
354 | debugOut.analog[3] = rate_ATT[PITCH]; |
237 | debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
355 | debugOut.analog[4] = rate_ATT[ROLL]; |
238 | debugOut.analog[1] = attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL / 10); // in 0.1 deg |
356 | debugOut.analog[5] = yawRate; |
239 | debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW; |
357 | 240 | ||
358 | debugOut.analog[6] = filteredAcc[PITCH]; |
241 | debugOut.analog[3] = rate_ATT[PITCH]; |
359 | debugOut.analog[7] = filteredAcc[ROLL]; |
242 | debugOut.analog[4] = rate_ATT[ROLL]; |
- | 243 | debugOut.analog[5] = yawRate; |
|
360 | debugOut.analog[8] = filteredAcc[Z]; |
244 | } |
361 | 245 | ||
362 | debugOut.analog[13] = term[PITCH]; |
- | |
363 | debugOut.analog[14] = term[ROLL]; |
246 | debugOut.analog[8] = yawTerm; |
- | 247 | debugOut.analog[9] = throttleTerm; |
|
364 | debugOut.analog[15] = yawTerm; |
248 | |
365 | debugOut.analog[16] = throttleTerm; |
249 | debugOut.analog[16] = gyroActivity; |
366 | 250 | ||
367 | for (i = 0; i < MAX_MOTORS; i++) { |
251 | for (i = 0; i < MAX_MOTORS; i++) { |
368 | int32_t tmp; |
252 | int32_t tmp; |
369 | uint8_t throttle; |
253 | uint8_t throttle; |
370 | 254 | ||
371 | tmp = (int32_t)throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE]; |
255 | tmp = (int32_t) throttleTerm * mixerMatrix.motor[i][MIX_THROTTLE]; |
372 | tmp += (int32_t)term[PITCH] * mixerMatrix.motor[i][MIX_PITCH]; |
256 | tmp += (int32_t) term[PITCH] * mixerMatrix.motor[i][MIX_PITCH]; |
373 | tmp += (int32_t)term[ROLL] * mixerMatrix.motor[i][MIX_ROLL]; |
257 | tmp += (int32_t) term[ROLL] * mixerMatrix.motor[i][MIX_ROLL]; |
374 | tmp += (int32_t)yawTerm * mixerMatrix.motor[i][MIX_YAW]; |
258 | tmp += (int32_t) yawTerm * mixerMatrix.motor[i][MIX_YAW]; |
375 | tmp = tmp >> 6; |
259 | tmp = tmp >> 6; |
376 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
260 | motorFilters[i] = motorFilter(tmp, motorFilters[i]); |
377 | // Now we scale back down to a 0..255 range. |
261 | // Now we scale back down to a 0..255 range. |
378 | tmp = motorFilters[i] / MOTOR_SCALING; |
262 | tmp = motorFilters[i] / MOTOR_SCALING; |
379 | 263 | ||
380 | // So this was the THIRD time a throttle was limited. But should the limitation |
264 | // So this was the THIRD time a throttle was limited. But should the limitation |
381 | // apply to the common throttle signal (the one used for setting the "power" of |
265 | // apply to the common throttle signal (the one used for setting the "power" of |
382 | // all motors together) or should it limit the throttle set for each motor, |
266 | // all motors together) or should it limit the throttle set for each motor, |
383 | // including mix components of pitch, roll and yaw? I think only the common |
267 | // including mix components of pitch, roll and yaw? I think only the common |
384 | // throttle should be limited. |
268 | // throttle should be limited. |
385 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
269 | // --> WRONG. This caused motors to stall completely in tight maneuvers. |
386 | // Apply to individual signals instead. |
270 | // Apply to individual signals instead. |
387 | CHECK_MIN_MAX(tmp, 1, 255); |
271 | CHECK_MIN_MAX(tmp, 1, 255); |
388 | throttle = tmp; |
272 | throttle = tmp; |
- | 273 | ||
389 | 274 | if (i < 4) |
|
390 | // if (i < 4) debugOut.analog[22 + i] = throttle; |
275 | debugOut.analog[10 + i] = throttle; |
391 | 276 | ||
392 | if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) { |
277 | if ((MKFlags & MKFLAG_MOTOR_RUN) && mixerMatrix.motor[i][MIX_THROTTLE] > 0) { |
393 | motor[i].throttle = throttle; |
278 | motor[i].throttle = throttle; |
394 | } else if (motorTestActive) { |
279 | } else if (motorTestActive) { |
395 | motor[i].throttle = motorTest[i]; |
280 | motor[i].throttle = motorTest[i]; |
396 | } else { |
281 | } else { |
397 | motor[i].throttle = 0; |
282 | motor[i].throttle = 0; |
398 | } |
283 | } |
399 | } |
284 | } |
400 | 285 | ||
401 | I2C_Start(TWI_STATE_MOTOR_TX); |
286 | I2C_Start(TWI_STATE_MOTOR_TX); |
402 | - | ||
403 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
404 | // Debugging |
- | |
405 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
406 | if (!(--debugDataTimer)) { |
- | |
407 | debugDataTimer = 24; // update debug outputs at 488 / 24 = 20.3 Hz. |
- | |
408 | debugOut.analog[0] = attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg |
- | |
409 | debugOut.analog[1] = attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL/10); // in 0.1 deg |
- | |
410 | debugOut.analog[2] = heading / GYRO_DEG_FACTOR_YAW; |
- | |
411 | } |
- | |
412 | } |
287 | } |
413 | 288 |