Rev 901 | Rev 909 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*####################################################################################### |
2 | Flight Control |
||
3 | #######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + Copyright (c) 04.2007 Holger Buss |
||
6 | // + Nur für den privaten Gebrauch |
||
7 | // + www.MikroKopter.com |
||
8 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
886 | killagreg | 9 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
11 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
12 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
1 | ingob | 13 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Verkauf von Luftbildaufnahmen, usw. |
||
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
886 | killagreg | 16 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
1 | ingob | 17 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
19 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
20 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
21 | // + eindeutig als Ursprung verlinkt werden |
||
22 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
23 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
24 | // + Benutzung auf eigene Gefahr |
||
25 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
886 | killagreg | 27 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
1 | ingob | 28 | // + mit unserer Zustimmung zulässig |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
886 | killagreg | 32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
1 | ingob | 33 | // + this list of conditions and the following disclaimer. |
34 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
35 | // + from this software without specific prior written permission. |
||
886 | killagreg | 36 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
1 | ingob | 37 | // + for non-commercial use (directly or indirectly) |
886 | killagreg | 38 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
1 | ingob | 39 | // + with our written permission |
886 | killagreg | 40 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + clearly linked as origin |
||
831 | hbuss | 42 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
1 | ingob | 43 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
44 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
45 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
46 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
47 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
48 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
49 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
492 | hbuss | 50 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
1 | ingob | 51 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
886 | killagreg | 52 | // + POSSIBILITY OF SUCH DAMAGE. |
1 | ingob | 53 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
886 | killagreg | 54 | #include <stdlib.h> |
55 | #include <avr/io.h> |
||
1 | ingob | 56 | |
57 | #include "main.h" |
||
886 | killagreg | 58 | #include "eeprom.h" |
59 | #include "timer0.h" |
||
60 | #include "_Settings.h" |
||
61 | #include "analog.h" |
||
62 | #include "fc.h" |
||
63 | #include "uart.h" |
||
64 | #include "rc.h" |
||
65 | #include "twimaster.h" |
||
66 | #include "timer2.h" |
||
67 | #ifdef USE_KILLAGREG |
||
68 | #include "mm3.h" |
||
69 | #include "gps.h" |
||
70 | #endif |
||
71 | #if !defined (USE_KILLAGREG) && !defined (USE_NAVICTRL) |
||
72 | #include "mk3mag.h" |
||
73 | #endif |
||
74 | #include "led.h" |
||
1 | ingob | 75 | |
886 | killagreg | 76 | volatile uint16_t I2CTimeout = 100; |
77 | // gyro readings |
||
78 | volatile int16_t Reading_GyroPitch, Reading_GyroRoll, Reading_GyroYaw; |
||
79 | // gyro neutral readings |
||
80 | volatile int16_t AdNeutralPitch = 0, AdNeutralRoll = 0, AdNeutralYaw = 0; |
||
81 | volatile int16_t StartNeutralRoll = 0, StartNeutralPitch = 0; |
||
82 | // mean accelerations |
||
83 | volatile int16_t Mean_AccPitch, Mean_AccRoll, Mean_AccTop; |
||
84 | |||
85 | // neutral acceleration readings |
||
86 | volatile int16_t NeutralAccX=0, NeutralAccY=0; |
||
1 | ingob | 87 | volatile float NeutralAccZ = 0; |
88 | |||
886 | killagreg | 89 | // attitude gyro integrals |
90 | volatile int32_t IntegralPitch = 0,IntegralPitch2 = 0; |
||
91 | volatile int32_t IntegralRoll = 0,IntegralRoll2 = 0; |
||
92 | volatile int32_t IntegralYaw = 0; |
||
93 | volatile int32_t Reading_IntegralGyroPitch = 0, Reading_IntegralGyroPitch2 = 0; |
||
94 | volatile int32_t Reading_IntegralGyroRoll = 0, Reading_IntegralGyroRoll2 = 0; |
||
95 | volatile int32_t Reading_IntegralGyroYaw = 0; |
||
96 | volatile int32_t MeanIntegralPitch; |
||
97 | volatile int32_t MeanIntegralRoll; |
||
1 | ingob | 98 | |
886 | killagreg | 99 | // attitude acceleration integrals |
100 | volatile int32_t IntegralAccPitch = 0, IntegralAccRoll = 0; |
||
101 | volatile int32_t Reading_Integral_Top = 0; |
||
102 | |||
103 | // compass course |
||
104 | volatile int16_t CompassHeading = -1; // negative angle indicates invalid data. |
||
105 | volatile int16_t CompassCourse = -1; |
||
106 | volatile int16_t CompassOffCourse = 0; |
||
107 | volatile uint8_t CompassCalState = 0; |
||
108 | uint8_t FunnelCourse = 0; |
||
109 | uint16_t BadCompassHeading = 500; |
||
110 | int32_t YawGyroHeading; |
||
111 | int16_t YawGyroDrift; |
||
112 | |||
113 | |||
114 | int16_t NaviAccPitch = 0, NaviAccRoll = 0, NaviCntAcc = 0; |
||
115 | |||
116 | |||
117 | // flags |
||
118 | uint8_t MotorsOn = 0; |
||
119 | uint8_t EmergencyLanding = 0; |
||
120 | uint16_t Model_Is_Flying = 0; |
||
121 | |||
122 | int32_t TurnOver180Pitch = 250000L, TurnOver180Roll = 250000L; |
||
123 | |||
124 | float Gyro_P_Factor; |
||
125 | float Gyro_I_Factor; |
||
126 | |||
127 | volatile int16_t DiffPitch, DiffRoll; |
||
128 | |||
129 | int16_t Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
||
130 | |||
131 | // setpoints for motors |
||
903 | pangu | 132 | #ifdef HEXAKOPTER |
133 | volatile uint8_t Motor_FrontLeft, Motor_FrontRight, Motor_RearLeft, Motor_RearRight, Motor_Right, Motor_Left; |
||
134 | #else |
||
135 | volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left; //used by twimaster isr |
||
136 | #endif |
||
886 | killagreg | 137 | |
138 | // stick values derived by rc channels readings |
||
139 | int16_t StickPitch = 0, StickRoll = 0, StickYaw = 0, StickThrust = 0; |
||
140 | int16_t GPS_Pitch = 0, GPS_Roll = 0; |
||
141 | |||
142 | int16_t MaxStickPitch = 0, MaxStickRoll = 0; |
||
143 | // stick values derived by uart inputs |
||
144 | int16_t ExternStickPitch = 0, ExternStickRoll = 0, ExternStickYaw = 0, ExternHeightValue = -20; |
||
145 | |||
146 | |||
147 | |||
148 | |||
149 | int16_t ReadingHeight = 0; |
||
150 | int16_t SetPointHeight = 0; |
||
151 | |||
152 | int16_t AttitudeCorrectionRoll = 0, AttitudeCorrectionPitch = 0; |
||
153 | |||
154 | float Ki = FACTOR_I; |
||
155 | |||
156 | uint8_t Looping_Pitch = 0, Looping_Roll = 0; |
||
157 | uint8_t Looping_Left = 0, Looping_Right = 0, Looping_Down = 0, Looping_Top = 0; |
||
158 | |||
159 | |||
160 | fc_param_t FCParam = {48,251,16,58,64,150,150,2,10,0,0,0,0,0,0,0,0,100,70,0,0,100}; |
||
161 | |||
162 | |||
163 | /************************************************************************/ |
||
164 | /* Creates numbeeps beeps at the speaker */ |
||
165 | /************************************************************************/ |
||
166 | void Beep(uint8_t numbeeps) |
||
1 | ingob | 167 | { |
886 | killagreg | 168 | while(numbeeps--) |
169 | { |
||
170 | if(MotorsOn) return; //auf keinen Fall im Flug! |
||
171 | BeepTime = 100; // 0.1 second |
||
172 | Delay_ms(250); // blocks 250 ms as pause to next beep, |
||
173 | // this will block the flight control loop, |
||
174 | // therefore do not use this funktion if motors are running |
||
175 | } |
||
1 | ingob | 176 | } |
177 | |||
886 | killagreg | 178 | /************************************************************************/ |
179 | /* Neutral Readings */ |
||
180 | /************************************************************************/ |
||
1 | ingob | 181 | void SetNeutral(void) |
182 | { |
||
886 | killagreg | 183 | NeutralAccX = 0; |
1 | ingob | 184 | NeutralAccY = 0; |
185 | NeutralAccZ = 0; |
||
886 | killagreg | 186 | AdNeutralPitch = 0; |
187 | AdNeutralRoll = 0; |
||
188 | AdNeutralYaw = 0; |
||
189 | FCParam.Yaw_PosFeedback = 0; |
||
190 | FCParam.Yaw_NegFeedback = 0; |
||
191 | CalibMean(); |
||
395 | hbuss | 192 | Delay_ms_Mess(100); |
886 | killagreg | 193 | CalibMean(); |
194 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)) // Height Control activated? |
||
513 | hbuss | 195 | { |
886 | killagreg | 196 | if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset(); |
513 | hbuss | 197 | } |
886 | killagreg | 198 | AdNeutralPitch = AdValueGyrPitch; |
199 | AdNeutralRoll = AdValueGyrRoll; |
||
200 | AdNeutralYaw = AdValueGyrYaw; |
||
201 | StartNeutralRoll = AdNeutralRoll; |
||
202 | StartNeutralPitch = AdNeutralPitch; |
||
203 | if(GetParamWord(PID_ACC_PITCH) > 1023) |
||
513 | hbuss | 204 | { |
886 | killagreg | 205 | NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY; |
206 | NeutralAccX = abs(Mean_AccPitch) / ACC_AMPLIFY; |
||
207 | NeutralAccZ = Current_AccZ; |
||
513 | hbuss | 208 | } |
886 | killagreg | 209 | else |
210 | { |
||
211 | NeutralAccX = (int16_t)GetParamWord(PID_ACC_PITCH); |
||
212 | NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL); |
||
213 | NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z); |
||
214 | } |
||
215 | Reading_IntegralGyroPitch = 0; |
||
216 | Reading_IntegralGyroPitch2 = 0; |
||
217 | Reading_IntegralGyroRoll = 0; |
||
218 | Reading_IntegralGyroRoll2 = 0; |
||
219 | Reading_IntegralGyroYaw = 0; |
||
220 | Reading_GyroPitch = 0; |
||
221 | Reading_GyroRoll = 0; |
||
222 | Reading_GyroYaw = 0; |
||
223 | StartAirPressure = AirPressure; |
||
224 | HeightD = 0; |
||
225 | Reading_Integral_Top = 0; |
||
226 | CompassCourse = CompassHeading; |
||
227 | BeepTime = 50; |
||
228 | TurnOver180Pitch = ((int32_t) ParamSet.AngleTurnOverPitch * 2500L) +15000L; |
||
229 | TurnOver180Roll = ((int32_t) ParamSet.AngleTurnOverRoll * 2500L) +15000L; |
||
230 | ExternHeightValue = 0; |
||
231 | GPS_Pitch = 0; |
||
232 | GPS_Roll = 0; |
||
233 | YawGyroHeading = CompassHeading * YAW_GYRO_DEG_FACTOR; |
||
234 | YawGyroDrift = 0; |
||
1 | ingob | 235 | } |
236 | |||
886 | killagreg | 237 | /************************************************************************/ |
238 | /* Averaging Measurement Readings */ |
||
239 | /************************************************************************/ |
||
240 | void Mean(void) |
||
241 | { |
||
242 | static int32_t tmpl,tmpl2; |
||
401 | hbuss | 243 | |
886 | killagreg | 244 | // Get offset corrected gyro readings (~ to angular velocity) |
245 | Reading_GyroYaw = AdNeutralYaw - AdValueGyrYaw; |
||
246 | Reading_GyroRoll = AdValueGyrRoll - AdNeutralRoll; |
||
247 | Reading_GyroPitch = AdValueGyrPitch - AdNeutralPitch; |
||
604 | hbuss | 248 | |
886 | killagreg | 249 | // Acceleration Sensor |
250 | // sliding average sensor readings |
||
251 | Mean_AccPitch = ((int32_t)Mean_AccPitch * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccPitch))) / 2L; |
||
252 | Mean_AccRoll = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L; |
||
253 | Mean_AccTop = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L; |
||
254 | |||
255 | // sum sensor readings for later averaging |
||
256 | IntegralAccPitch += ACC_AMPLIFY * AdValueAccPitch; |
||
257 | IntegralAccRoll += ACC_AMPLIFY * AdValueAccRoll; |
||
258 | |||
259 | NaviAccPitch += AdValueAccPitch; |
||
260 | NaviAccRoll += AdValueAccRoll; |
||
805 | hbuss | 261 | NaviCntAcc++; |
882 | hbuss | 262 | |
886 | killagreg | 263 | // Yaw |
264 | // calculate yaw gyro integral (~ to rotation angle) |
||
265 | Reading_IntegralGyroYaw += Reading_GyroYaw; |
||
266 | YawGyroHeading += Reading_GyroYaw; |
||
267 | if(YawGyroHeading >= (360L * YAW_GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * YAW_GYRO_DEG_FACTOR; // 360° Wrap |
||
268 | if(YawGyroHeading < 0) YawGyroHeading += 360L * YAW_GYRO_DEG_FACTOR; |
||
395 | hbuss | 269 | |
1 | ingob | 270 | |
886 | killagreg | 271 | // Coupling fraction |
272 | if(!Looping_Pitch && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) |
||
273 | { |
||
274 | tmpl = (Reading_GyroYaw * Reading_IntegralGyroPitch) / 2048L; |
||
275 | tmpl *= FCParam.Yaw_PosFeedback; |
||
276 | tmpl /= 4096L; |
||
277 | tmpl2 = ( Reading_GyroYaw * Reading_IntegralGyroRoll) / 2048L; |
||
278 | tmpl2 *= FCParam.Yaw_PosFeedback; |
||
279 | tmpl2 /= 4096L; |
||
280 | if(labs(tmpl) > 128 || labs(tmpl2) > 128) FunnelCourse = 1; |
||
281 | } |
||
282 | else tmpl = tmpl2 = 0; |
||
283 | |||
284 | // Roll |
||
285 | Reading_GyroRoll += tmpl; |
||
286 | Reading_GyroRoll += (tmpl2 * FCParam.Yaw_NegFeedback) / 512L; |
||
287 | Reading_IntegralGyroRoll2 += Reading_GyroRoll; |
||
288 | Reading_IntegralGyroRoll += Reading_GyroRoll - AttitudeCorrectionRoll; |
||
289 | if(Reading_IntegralGyroRoll > TurnOver180Roll) |
||
290 | { |
||
291 | Reading_IntegralGyroRoll = -(TurnOver180Roll - 10000L); |
||
292 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
||
293 | } |
||
294 | if(Reading_IntegralGyroRoll < -TurnOver180Roll) |
||
295 | { |
||
296 | Reading_IntegralGyroRoll = (TurnOver180Roll - 10000L); |
||
297 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
||
298 | } |
||
299 | if(AdValueGyrRoll < 15) Reading_GyroRoll = -1000; |
||
300 | if(AdValueGyrRoll < 7) Reading_GyroRoll = -2000; |
||
301 | if(BoardRelease == 10) |
||
302 | { |
||
303 | if(AdValueGyrRoll > 1010) Reading_GyroRoll = +1000; |
||
304 | if(AdValueGyrRoll > 1017) Reading_GyroRoll = +2000; |
||
305 | } |
||
306 | else |
||
307 | { |
||
308 | if(AdValueGyrRoll > 2020) Reading_GyroRoll = +1000; |
||
309 | if(AdValueGyrRoll > 2034) Reading_GyroRoll = +2000; |
||
310 | } |
||
311 | // Pitch |
||
312 | Reading_GyroPitch -= tmpl2; |
||
313 | Reading_GyroPitch -= (tmpl*FCParam.Yaw_NegFeedback) / 512L; |
||
314 | Reading_IntegralGyroPitch2 += Reading_GyroPitch; |
||
315 | Reading_IntegralGyroPitch += Reading_GyroPitch - AttitudeCorrectionPitch; |
||
316 | if(Reading_IntegralGyroPitch > TurnOver180Pitch) |
||
317 | { |
||
318 | Reading_IntegralGyroPitch = -(TurnOver180Pitch - 25000L); |
||
319 | Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch; |
||
320 | } |
||
321 | if(Reading_IntegralGyroPitch < -TurnOver180Pitch) |
||
322 | { |
||
323 | Reading_IntegralGyroPitch = (TurnOver180Pitch - 25000L); |
||
324 | Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch; |
||
325 | } |
||
326 | if(AdValueGyrPitch < 15) Reading_GyroPitch = -1000; |
||
327 | if(AdValueGyrPitch < 7) Reading_GyroPitch = -2000; |
||
328 | if(BoardRelease == 10) |
||
329 | { |
||
330 | if(AdValueGyrPitch > 1010) Reading_GyroPitch = +1000; |
||
331 | if(AdValueGyrPitch > 1017) Reading_GyroPitch = +2000; |
||
332 | } |
||
333 | else |
||
334 | { |
||
335 | if(AdValueGyrPitch > 2020) Reading_GyroPitch = +1000; |
||
336 | if(AdValueGyrPitch > 2034) Reading_GyroPitch = +2000; |
||
337 | } |
||
338 | |||
339 | // start ADC again to capture measurement values for the next loop |
||
340 | ADC_Enable(); |
||
341 | |||
342 | IntegralYaw = Reading_IntegralGyroYaw; |
||
343 | IntegralPitch = Reading_IntegralGyroPitch; |
||
344 | IntegralRoll = Reading_IntegralGyroRoll; |
||
345 | IntegralPitch2 = Reading_IntegralGyroPitch2; |
||
346 | IntegralRoll2 = Reading_IntegralGyroRoll2; |
||
347 | |||
348 | if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && !Looping_Pitch && !Looping_Roll) |
||
349 | { |
||
350 | if(Reading_GyroPitch > 200) Reading_GyroPitch += 4 * (Reading_GyroPitch - 200); |
||
351 | else if(Reading_GyroPitch < -200) Reading_GyroPitch += 4 * (Reading_GyroPitch + 200); |
||
352 | if(Reading_GyroRoll > 200) Reading_GyroRoll += 4 * (Reading_GyroRoll - 200); |
||
353 | else if(Reading_GyroRoll < -200) Reading_GyroRoll += 4 * (Reading_GyroRoll + 200); |
||
354 | } |
||
1 | ingob | 355 | } |
356 | |||
886 | killagreg | 357 | /************************************************************************/ |
358 | /* Averaging Measurement Readings for Calibration */ |
||
359 | /************************************************************************/ |
||
360 | void CalibMean(void) |
||
361 | { |
||
362 | // stop ADC to avoid changing values during calculation |
||
363 | ADC_Disable(); |
||
395 | hbuss | 364 | |
886 | killagreg | 365 | Reading_GyroPitch = AdValueGyrPitch; |
366 | Reading_GyroRoll = AdValueGyrRoll; |
||
367 | Reading_GyroYaw = AdValueGyrYaw; |
||
368 | |||
369 | Mean_AccPitch = ACC_AMPLIFY * (int32_t)AdValueAccPitch; |
||
370 | Mean_AccRoll = ACC_AMPLIFY * (int32_t)AdValueAccRoll; |
||
371 | Mean_AccTop = (int32_t)AdValueAccTop; |
||
372 | // start ADC (enables internal trigger so that the ISR in analog.c |
||
373 | // updates the readings once) |
||
374 | ADC_Enable(); |
||
375 | |||
376 | TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L; |
||
377 | TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L; |
||
1 | ingob | 378 | } |
379 | |||
886 | killagreg | 380 | /************************************************************************/ |
381 | /* Transmit Motor Data via I2C */ |
||
382 | /************************************************************************/ |
||
1 | ingob | 383 | void SendMotorData(void) |
886 | killagreg | 384 | { |
385 | if(MOTOR_OFF || !MotorsOn) |
||
386 | { |
||
903 | pangu | 387 | #ifdef HEXAKOPTER |
388 | Motor_RearLeft = 0; |
||
389 | Motor_FrontLeft = 0; |
||
390 | Motor_RearRight = 0; |
||
391 | Motor_FrontRight = 0; |
||
392 | Motor_Right = 0; |
||
393 | Motor_Left = 0; |
||
394 | if(MotorTest[0]) Motor_FrontLeft = Motor_FrontRight = MotorTest[0]; |
||
395 | if(MotorTest[1]) Motor_RearLeft = Motor_RearRight = MotorTest[1]; |
||
396 | if(MotorTest[2]) Motor_Left = MotorTest[2]; |
||
397 | if(MotorTest[3]) Motor_Right = MotorTest[3]; |
||
398 | } |
||
399 | |||
400 | #else |
||
886 | killagreg | 401 | Motor_Rear = 0; |
402 | Motor_Front = 0; |
||
403 | Motor_Right = 0; |
||
404 | Motor_Left = 0; |
||
405 | if(MotorTest[0]) Motor_Front = MotorTest[0]; |
||
406 | if(MotorTest[1]) Motor_Rear = MotorTest[1]; |
||
407 | if(MotorTest[2]) Motor_Left = MotorTest[2]; |
||
408 | if(MotorTest[3]) Motor_Right = MotorTest[3]; |
||
409 | } |
||
1 | ingob | 410 | |
411 | |||
903 | pangu | 412 | #endif |
1 | ingob | 413 | //Start I2C Interrupt Mode |
414 | twi_state = 0; |
||
415 | motor = 0; |
||
886 | killagreg | 416 | I2C_Start(); |
1 | ingob | 417 | } |
418 | |||
419 | |||
420 | |||
886 | killagreg | 421 | /************************************************************************/ |
422 | /* Maps the parameter to poti values */ |
||
423 | /************************************************************************/ |
||
424 | void ParameterMapping(void) |
||
1 | ingob | 425 | { |
886 | killagreg | 426 | if(RC_Quality > 160) // do the mapping of RC-Potis only if the rc-signal is ok |
427 | // else the last updated values are used |
||
428 | { |
||
429 | //update poti values by rc-signals |
||
430 | #define CHK_POTI(b,a,min,max) { if(a > 250) { if(a == 251) b = Poti1; else if(a == 252) b = Poti2; else if(a == 253) b = Poti3; else if(a == 254) b = Poti4;} else b = a; if(b <= min) b = min; else if(b >= max) b = max;} |
||
431 | CHK_POTI(FCParam.MaxHeight,ParamSet.MaxHeight,0,255); |
||
432 | CHK_POTI(FCParam.Height_D,ParamSet.Height_D,0,100); |
||
433 | CHK_POTI(FCParam.Height_P,ParamSet.Height_P,0,100); |
||
434 | CHK_POTI(FCParam.Height_ACC_Effect,ParamSet.Height_ACC_Effect,0,255); |
||
435 | CHK_POTI(FCParam.CompassYawEffect,ParamSet.CompassYawEffect,0,255); |
||
436 | CHK_POTI(FCParam.Gyro_P,ParamSet.Gyro_P,10,255); |
||
437 | CHK_POTI(FCParam.Gyro_I,ParamSet.Gyro_I,0,255); |
||
438 | CHK_POTI(FCParam.I_Factor,ParamSet.I_Factor,0,255); |
||
439 | CHK_POTI(FCParam.UserParam1,ParamSet.UserParam1,0,255); |
||
440 | CHK_POTI(FCParam.UserParam2,ParamSet.UserParam2,0,255); |
||
441 | CHK_POTI(FCParam.UserParam3,ParamSet.UserParam3,0,255); |
||
442 | CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4,0,255); |
||
443 | CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5,0,255); |
||
444 | CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6,0,255); |
||
445 | CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7,0,255); |
||
446 | CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8,0,255); |
||
447 | CHK_POTI(FCParam.ServoPitchControl,ParamSet.ServoPitchControl,0,255); |
||
448 | CHK_POTI(FCParam.LoopThrustLimit,ParamSet.LoopThrustLimit,0,255); |
||
449 | CHK_POTI(FCParam.Yaw_PosFeedback,ParamSet.Yaw_PosFeedback,0,255); |
||
450 | CHK_POTI(FCParam.Yaw_NegFeedback,ParamSet.Yaw_NegFeedback,0,255); |
||
451 | CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability,0,255); |
||
452 | Ki = (float) FCParam.I_Factor * FACTOR_I; |
||
453 | } |
||
454 | } |
||
1 | ingob | 455 | |
456 | |||
886 | killagreg | 457 | void SetCompassCalState(void) |
458 | { |
||
459 | static uint8_t stick = 1; |
||
460 | |||
461 | // if pitch is centered or top set stick to zero |
||
462 | if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -20) stick = 0; |
||
463 | // if pitch is down trigger to next cal state |
||
464 | if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -70) && !stick) |
||
465 | { |
||
466 | stick = 1; |
||
467 | CompassCalState++; |
||
468 | if(CompassCalState < 5) Beep(CompassCalState); |
||
469 | else BeepTime = 1000; |
||
470 | } |
||
1 | ingob | 471 | } |
472 | |||
473 | |||
819 | hbuss | 474 | |
886 | killagreg | 475 | /************************************************************************/ |
476 | /* MotorControl */ |
||
477 | /************************************************************************/ |
||
478 | void MotorControl(void) |
||
1 | ingob | 479 | { |
886 | killagreg | 480 | int16_t MotorValue, pd_result, h, tmp_int; |
903 | pangu | 481 | int16_t YawMixFraction, ThrustMixFraction, PitchMixFraction, RollMixFraction; |
886 | killagreg | 482 | static int32_t SumPitch = 0, SumRoll = 0; |
483 | static int32_t SetPointYaw = 0; |
||
484 | static int32_t IntegralErrorPitch = 0; |
||
485 | static int32_t IntegralErrorRoll = 0; |
||
486 | static uint16_t RcLostTimer; |
||
487 | static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0; |
||
488 | static uint8_t HeightControlActive = 0; |
||
489 | static int16_t HeightControlThrust = 0; |
||
490 | static int8_t TimerDebugOut = 0; |
||
491 | static uint16_t UpdateCompassCourse = 0; |
||
492 | static int32_t CorrectionPitch, CorrectionRoll; |
||
1 | ingob | 493 | |
886 | killagreg | 494 | Mean(); |
495 | GRN_ON; |
||
1 | ingob | 496 | |
886 | killagreg | 497 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
498 | // determine thrust value |
||
499 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
500 | ThrustMixFraction = StickThrust; |
||
501 | if(ThrustMixFraction < ParamSet.Trust_Min + 10) ThrustMixFraction = ParamSet.Trust_Min + 10; |
||
502 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
503 | // RC-signal is bad |
||
504 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
505 | if(RC_Quality < 120) // the rc-frame signal is not reveived or noisy |
||
506 | { |
||
507 | if(!PcAccess) // if also no PC-Access via UART |
||
508 | { |
||
509 | if(BeepModulation == 0xFFFF) |
||
510 | { |
||
511 | BeepTime = 15000; // 1.5 seconds |
||
512 | BeepModulation = 0x0C00; |
||
513 | } |
||
514 | } |
||
515 | if(RcLostTimer) RcLostTimer--; // decremtent timer after rc sigal lost |
||
516 | else // rc lost countdown finished |
||
517 | { |
||
518 | MotorsOn = 0; // stop all motors |
||
519 | EmergencyLanding = 0; // emergency landing is over |
||
520 | } |
||
521 | ROT_ON; // set red led |
||
522 | if(Model_Is_Flying > 1000) // wahrscheinlich in der Luft --> langsam absenken |
||
523 | { |
||
524 | ThrustMixFraction = ParamSet.EmergencyThrust; // set emergency thrust |
||
525 | EmergencyLanding = 1; // enable emergency landing |
||
526 | // set neutral rc inputs |
||
527 | PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] = 0; |
||
528 | PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
||
529 | PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
||
530 | PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] = 0; |
||
531 | PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
||
532 | PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
||
533 | } |
||
534 | else MotorsOn = 0; // switch of all motors |
||
535 | } // eof RC_Quality < 120 |
||
536 | else |
||
537 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
538 | // RC-signal is good |
||
539 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
540 | if(RC_Quality > 140) |
||
541 | { |
||
542 | EmergencyLanding = 0; // switch off emergency landing if RC-signal is okay |
||
543 | // reset emergency timer |
||
544 | RcLostTimer = ParamSet.EmergencyThrustDuration * 50; |
||
545 | if(ThrustMixFraction > 40) |
||
546 | { |
||
547 | if(Model_Is_Flying < 0xFFFF) Model_Is_Flying++; |
||
548 | } |
||
549 | if(Model_Is_Flying < 256) |
||
550 | { |
||
551 | SumPitch = 0; |
||
552 | SumRoll = 0; |
||
553 | StickYaw = 0; |
||
554 | if(Model_Is_Flying == 250) UpdateCompassCourse = 1; |
||
555 | } |
||
604 | hbuss | 556 | |
886 | killagreg | 557 | if(Poti1 < PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110) Poti1++; else if(Poti1 > PPM_in[ParamSet.ChannelAssignment[CH_POTI1]] + 110 && Poti1) Poti1--; |
558 | if(Poti2 < PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110) Poti2++; else if(Poti2 > PPM_in[ParamSet.ChannelAssignment[CH_POTI2]] + 110 && Poti2) Poti2--; |
||
559 | if(Poti3 < PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110) Poti3++; else if(Poti3 > PPM_in[ParamSet.ChannelAssignment[CH_POTI3]] + 110 && Poti3) Poti3--; |
||
560 | if(Poti4 < PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110) Poti4++; else if(Poti4 > PPM_in[ParamSet.ChannelAssignment[CH_POTI4]] + 110 && Poti4) Poti4--; |
||
561 | //PPM24-Extension |
||
562 | if(Poti5 < PPM_in[9] + 110) Poti5++; else if(Poti5 > PPM_in[9] + 110 && Poti5) Poti5--; |
||
563 | if(Poti6 < PPM_in[10] + 110) Poti6++; else if(Poti6 > PPM_in[10] + 110 && Poti6) Poti6--; |
||
564 | if(Poti7 < PPM_in[11] + 110) Poti7++; else if(Poti7 > PPM_in[11] + 110 && Poti7) Poti7--; |
||
565 | if(Poti8 < PPM_in[12] + 110) Poti8++; else if(Poti8 > PPM_in[12] + 110 && Poti8) Poti8--; |
||
566 | //limit poti values |
||
567 | if(Poti1 < 0) Poti1 = 0; else if(Poti1 > 255) Poti1 = 255; |
||
568 | if(Poti2 < 0) Poti2 = 0; else if(Poti2 > 255) Poti2 = 255; |
||
569 | if(Poti3 < 0) Poti3 = 0; else if(Poti3 > 255) Poti3 = 255; |
||
570 | if(Poti4 < 0) Poti4 = 0; else if(Poti4 > 255) Poti4 = 255; |
||
571 | //PPM24-Extension |
||
572 | if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255; |
||
573 | if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255; |
||
574 | if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255; |
||
575 | if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255; |
||
723 | hbuss | 576 | |
886 | killagreg | 577 | // if motors are off and the thrust stick is in the upper position |
903 | pangu | 578 | if(MotorsOn == 0 && (PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] > 80)) |
886 | killagreg | 579 | { |
580 | // and if the yaw stick is in the leftmost position |
||
581 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
||
582 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
583 | // calibrate the neutral readings of all attitude sensors |
||
584 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
585 | { |
||
586 | // thrust/yaw joystick is top left |
||
587 | // _________ |
||
588 | // |x | |
||
589 | // | | |
||
590 | // | | |
||
591 | // | | |
||
592 | // | | |
||
593 | // ¯¯¯¯¯¯¯¯¯ |
||
594 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
||
595 | { |
||
596 | delay_neutral = 0; |
||
597 | GRN_OFF; |
||
598 | Model_Is_Flying = 0; |
||
599 | // check roll/pitch stick position |
||
600 | // if pitch stick is top or roll stick is left or right --> change parameter setting |
||
601 | // according to roll/pitch stick position |
||
602 | if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70) |
||
603 | { |
||
604 | uint8_t setting = 1; // default |
||
605 | // pitch/roll joystick |
||
606 | // _________ |
||
607 | // |2 3 4| |
||
608 | // | | |
||
609 | // |1 5| |
||
610 | // | | |
||
611 | // | | |
||
612 | // ¯¯¯¯¯¯¯¯¯ |
||
613 | // roll stick leftmost and pitch stick centered --> setting 1 |
||
614 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 1; |
||
615 | // roll stick leftmost and pitch stick topmost --> setting 2 |
||
616 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 2; |
||
617 | // roll stick centered an pitch stick topmost --> setting 3 |
||
618 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 3; |
||
619 | // roll stick rightmost and pitch stick topmost --> setting 4 |
||
620 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 4; |
||
621 | // roll stick rightmost and pitch stick centered --> setting 5 |
||
622 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 5; |
||
623 | // update active parameter set in eeprom |
||
624 | SetActiveParamSet(setting); |
||
625 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
||
626 | SetNeutral(); |
||
627 | Beep(GetActiveParamSet()); |
||
628 | } |
||
629 | else |
||
630 | { |
||
631 | if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE)) |
||
632 | { |
||
633 | // if roll stick is centered and pitch stick is down |
||
634 | if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 20 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -70) |
||
635 | { |
||
636 | // pitch/roll joystick |
||
637 | // _________ |
||
638 | // | | |
||
639 | // | | |
||
640 | // | | |
||
641 | // | | |
||
642 | // | x | |
||
643 | // ¯¯¯¯¯¯¯¯¯ |
||
644 | // enable calibration state of compass |
||
645 | CompassCalState = 1; |
||
646 | BeepTime = 1000; |
||
647 | } |
||
648 | else // pitch and roll are centered |
||
649 | { |
||
650 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
||
651 | SetNeutral(); |
||
652 | Beep(GetActiveParamSet()); |
||
653 | } |
||
654 | } |
||
655 | else // pitch and roll are centered |
||
656 | { |
||
657 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
||
658 | SetNeutral(); |
||
659 | Beep(GetActiveParamSet()); |
||
660 | } |
||
661 | } |
||
662 | } |
||
663 | } |
||
664 | // and if the yaw stick is in the rightmost position |
||
665 | // save the ACC neutral setting to eeprom |
||
666 | else if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
||
667 | { |
||
668 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
||
669 | { |
||
670 | delay_neutral = 0; |
||
671 | GRN_OFF; |
||
672 | SetParamWord(PID_ACC_PITCH, 0xFFFF); // make value invalid |
||
673 | Model_Is_Flying = 0; |
||
674 | SetNeutral(); |
||
675 | // Save ACC neutral settings to eeprom |
||
676 | SetParamWord(PID_ACC_PITCH, (uint16_t)NeutralAccX); |
||
677 | SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY); |
||
678 | SetParamWord(PID_ACC_Z, (uint16_t)NeutralAccZ); |
||
679 | Beep(GetActiveParamSet()); |
||
680 | } |
||
681 | } |
||
682 | else delay_neutral = 0; |
||
683 | } |
||
684 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
685 | // thrust stick is down |
||
686 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
687 | if(PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] < -85) |
||
688 | { |
||
689 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
690 | // and yaw stick is rightmost --> start motors |
||
691 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
692 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
||
693 | { |
||
694 | if(++delay_startmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
||
695 | { |
||
903 | pangu | 696 | delay_startmotors = 0; // do not repeat if once executed |
886 | killagreg | 697 | Model_Is_Flying = 1; |
698 | MotorsOn = 1; |
||
699 | SetPointYaw = 0; |
||
700 | Reading_IntegralGyroYaw = 0; |
||
701 | Reading_IntegralGyroPitch = 0; |
||
702 | Reading_IntegralGyroRoll = 0; |
||
703 | Reading_IntegralGyroPitch2 = IntegralPitch; |
||
704 | Reading_IntegralGyroRoll2 = IntegralRoll; |
||
705 | SumPitch = 0; |
||
706 | SumRoll = 0; |
||
707 | #ifdef USE_KILLAGREG |
||
708 | if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE) |
||
709 | { |
||
710 | GPS_SetHomePosition(); |
||
711 | } |
||
712 | #endif |
||
713 | } |
||
714 | } |
||
715 | else delay_startmotors = 0; // reset delay timer if sticks are not in this position |
||
716 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
717 | // and yaw stick is leftmost --> stop motors |
||
718 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
719 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
||
720 | { |
||
721 | if(++delay_stopmotors > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
||
722 | { |
||
903 | pangu | 723 | delay_stopmotors = 0; // do not repeat if once executed |
886 | killagreg | 724 | Model_Is_Flying = 0; |
725 | MotorsOn = 0; |
||
726 | #ifdef USE_KILLAGREG |
||
727 | if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE) |
||
728 | { |
||
729 | GPS_ClearHomePosition(); |
||
730 | } |
||
731 | #endif |
||
732 | } |
||
733 | } |
||
734 | else delay_stopmotors = 0; // reset delay timer if sticks are not in this position |
||
735 | } |
||
736 | // remapping of paameters only if the signal rc-sigbnal conditions are good |
||
737 | } // eof RC_Quality > 150 |
||
738 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
739 | // new values from RC |
||
740 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
741 | if(!NewPpmData-- || EmergencyLanding) // NewData = 0 means new data from RC |
||
742 | { |
||
743 | int tmp_int; |
||
744 | ParameterMapping(); // remapping params (online poti replacement) |
||
745 | // calculate Stick inputs by rc channels (P) and changing of rc channels (D) |
||
746 | StickPitch = (StickPitch * 3 + PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_P) / 4; |
||
747 | StickPitch += PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_D; |
||
748 | StickPitch -= (GPS_Pitch); |
||
723 | hbuss | 749 | |
886 | killagreg | 750 | StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4; |
751 | StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D; |
||
752 | StickRoll -= (GPS_Roll); |
||
595 | hbuss | 753 | |
886 | killagreg | 754 | // direct mapping of yaw and thrust |
755 | StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]]; |
||
756 | StickThrust = PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] + 120;// shift to positive numbers |
||
1 | ingob | 757 | |
886 | killagreg | 758 | // update gyro control loop factors |
759 | Gyro_P_Factor = ((float) FCParam.Gyro_P + 10.0) / (256.0 / STICK_GAIN); |
||
760 | Gyro_I_Factor = ((float) FCParam.Gyro_I) / (44000 / STICK_GAIN); |
||
761 | |||
595 | hbuss | 762 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
886 | killagreg | 763 | // Digital Control via DubWise |
595 | hbuss | 764 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
492 | hbuss | 765 | |
886 | killagreg | 766 | #define KEY_VALUE (FCParam.UserParam8 * 4) // step width |
767 | if(DubWiseKeys[1]) BeepTime = 10; |
||
768 | if(DubWiseKeys[1] & DUB_KEY_UP) tmp_int = KEY_VALUE; |
||
769 | else if(DubWiseKeys[1] & DUB_KEY_DOWN) tmp_int = -KEY_VALUE; |
||
770 | else tmp_int = 0; |
||
771 | ExternStickPitch = (ExternStickPitch * 7 + tmp_int) / 8; |
||
772 | if(DubWiseKeys[1] & DUB_KEY_LEFT) tmp_int = KEY_VALUE; |
||
773 | else if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; |
||
774 | else tmp_int = 0; |
||
775 | ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8; |
||
492 | hbuss | 776 | |
886 | killagreg | 777 | if(DubWiseKeys[0] & 8) ExternStickYaw = 50;else |
778 | if(DubWiseKeys[0] & 4) ExternStickYaw =-50;else ExternStickYaw = 0; |
||
779 | if(DubWiseKeys[0] & 2) ExternHeightValue++; |
||
780 | if(DubWiseKeys[0] & 16) ExternHeightValue--; |
||
781 | |||
782 | StickPitch += (STICK_GAIN * ExternStickPitch) / 8; |
||
783 | StickRoll += (STICK_GAIN * ExternStickRoll) / 8; |
||
784 | StickYaw += (STICK_GAIN * ExternStickYaw); |
||
785 | |||
595 | hbuss | 786 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
886 | killagreg | 787 | //+ Analog control via serial communication |
595 | hbuss | 788 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
492 | hbuss | 789 | |
886 | killagreg | 790 | if(ExternControl.Config & 0x01 && FCParam.UserParam8 > 128) |
791 | { |
||
792 | StickPitch += (int16_t) ExternControl.Pitch * (int16_t) ParamSet.Stick_P; |
||
793 | StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.Stick_P; |
||
794 | StickYaw += ExternControl.Yaw; |
||
795 | ExternHeightValue = (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain; |
||
796 | if(ExternControl.Thrust < StickThrust) StickThrust = ExternControl.Thrust; |
||
797 | } |
||
798 | if(StickThrust < 0) StickThrust = 0; |
||
723 | hbuss | 799 | |
886 | killagreg | 800 | // disable I part of gyro control feedback |
801 | if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) Gyro_I_Factor = 0; |
||
802 | // avoid negative scaling factors |
||
803 | if(Gyro_P_Factor < 0) Gyro_P_Factor = 0; |
||
804 | if(Gyro_I_Factor < 0) Gyro_I_Factor = 0; |
||
723 | hbuss | 805 | |
886 | killagreg | 806 | |
807 | // update max stick positions for pitch and roll |
||
808 | |||
809 | if(abs(StickPitch / STICK_GAIN) > MaxStickPitch) MaxStickPitch = abs(StickPitch)/STICK_GAIN; |
||
810 | else MaxStickPitch--; |
||
811 | if(abs(StickRoll / STICK_GAIN) > MaxStickRoll) MaxStickRoll = abs(StickRoll)/STICK_GAIN; |
||
812 | else MaxStickRoll--; |
||
813 | |||
814 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
173 | holgerb | 815 | // Looping? |
886 | killagreg | 816 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
173 | holgerb | 817 | |
886 | killagreg | 818 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_LEFT) Looping_Left = 1; |
819 | else |
||
820 | { |
||
821 | if(Looping_Left) // Hysteresis |
||
822 | { |
||
823 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Left = 0; |
||
824 | } |
||
825 | } |
||
826 | if((PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_RIGHT) Looping_Right = 1; |
||
827 | else |
||
828 | { |
||
829 | if(Looping_Right) // Hysteresis |
||
830 | { |
||
831 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0; |
||
832 | } |
||
833 | } |
||
395 | hbuss | 834 | |
886 | killagreg | 835 | if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1; |
836 | else |
||
837 | { |
||
838 | if(Looping_Top) // Hysteresis |
||
839 | { |
||
840 | if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0; |
||
841 | } |
||
842 | } |
||
843 | if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1; |
||
844 | else |
||
845 | { |
||
846 | if(Looping_Down) // Hysteresis |
||
847 | { |
||
848 | if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0; |
||
849 | } |
||
850 | } |
||
395 | hbuss | 851 | |
886 | killagreg | 852 | if(Looping_Left || Looping_Right) Looping_Roll = 1; else Looping_Roll = 0; |
853 | if(Looping_Top || Looping_Down) {Looping_Pitch = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Pitch = 0; |
||
854 | } // End of new RC-Values or Emergency Landing |
||
173 | holgerb | 855 | |
395 | hbuss | 856 | |
886 | killagreg | 857 | if(Looping_Roll) BeepTime = 100; |
858 | if(Looping_Roll || Looping_Pitch) |
||
859 | { |
||
860 | if(ThrustMixFraction > ParamSet.LoopThrustLimit) ThrustMixFraction = ParamSet.LoopThrustLimit; |
||
861 | } |
||
395 | hbuss | 862 | |
863 | |||
886 | killagreg | 864 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
865 | //+ LED Control on J16/J17 |
||
866 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
867 | LED1_Time = FCParam.UserParam7; |
||
868 | LED2_Time = FCParam.UserParam8; |
||
869 | LED_Update(); |
||
395 | hbuss | 870 | |
886 | killagreg | 871 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
872 | // in case of emergency landing |
||
873 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
874 | // set all inputs to save values |
||
875 | if(EmergencyLanding) |
||
876 | { |
||
877 | StickYaw = 0; |
||
878 | StickPitch = 0; |
||
879 | StickRoll = 0; |
||
880 | Gyro_P_Factor = (float) 100 / (256.0 / STICK_GAIN); |
||
881 | Gyro_I_Factor = (float) 120 / (44000 / STICK_GAIN); |
||
882 | Looping_Roll = 0; |
||
883 | Looping_Pitch = 0; |
||
884 | MaxStickPitch = 0; |
||
885 | MaxStickRoll = 0; |
||
886 | } |
||
395 | hbuss | 887 | |
886 | killagreg | 888 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
889 | // Trim Gyro-Integrals to ACC-Signals |
||
890 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
614 | hbuss | 891 | |
886 | killagreg | 892 | #define BALANCE_NUMBER 256L |
893 | // sum for averaging |
||
894 | MeanIntegralPitch += IntegralPitch; |
||
895 | MeanIntegralRoll += IntegralRoll; |
||
614 | hbuss | 896 | |
886 | killagreg | 897 | if(Looping_Pitch || Looping_Roll) // if looping in any direction |
898 | { |
||
899 | // reset averaging for acc and gyro integral as well as gyro integral acc correction |
||
900 | MeasurementCounter = 0; |
||
469 | hbuss | 901 | |
886 | killagreg | 902 | IntegralAccPitch = 0; |
903 | IntegralAccRoll = 0; |
||
614 | hbuss | 904 | |
886 | killagreg | 905 | MeanIntegralPitch = 0; |
906 | MeanIntegralRoll = 0; |
||
395 | hbuss | 907 | |
886 | killagreg | 908 | Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch; |
909 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
||
498 | hbuss | 910 | |
886 | killagreg | 911 | AttitudeCorrectionPitch = 0; |
912 | AttitudeCorrectionRoll = 0; |
||
913 | } |
||
395 | hbuss | 914 | |
886 | killagreg | 915 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
916 | if(!Looping_Pitch && !Looping_Roll) // if not lopping in any direction |
||
917 | { |
||
918 | int32_t tmp_long, tmp_long2; |
||
919 | // determine the deviation of gyro integral from averaged acceleration sensor |
||
920 | tmp_long = (int32_t)(IntegralPitch / ParamSet.GyroAccFactor - (int32_t)Mean_AccPitch); |
||
921 | tmp_long /= 16; |
||
922 | tmp_long2 = (int32_t)(IntegralRoll / ParamSet.GyroAccFactor - (int32_t)Mean_AccRoll); |
||
923 | tmp_long2 /= 16; |
||
395 | hbuss | 924 | |
886 | killagreg | 925 | if((MaxStickPitch > 32) || (MaxStickRoll > 32)) // reduce effect during stick commands |
926 | { |
||
927 | tmp_long /= 3; |
||
928 | tmp_long2 /= 3; |
||
929 | } |
||
930 | if(abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25) // reduce further if yaw stick is active |
||
931 | { |
||
932 | tmp_long /= 3; |
||
933 | tmp_long2 /= 3; |
||
934 | } |
||
395 | hbuss | 935 | |
886 | killagreg | 936 | #define BALANCE 32 |
937 | // limit correction effect |
||
938 | if(tmp_long > BALANCE) tmp_long = BALANCE; |
||
939 | if(tmp_long < -BALANCE) tmp_long =-BALANCE; |
||
940 | if(tmp_long2 > BALANCE) tmp_long2 = BALANCE; |
||
941 | if(tmp_long2 <-BALANCE) tmp_long2 =-BALANCE; |
||
942 | // correct current readings |
||
943 | Reading_IntegralGyroPitch -= tmp_long; |
||
944 | Reading_IntegralGyroRoll -= tmp_long2; |
||
945 | } |
||
946 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
947 | // MeasurementCounter is incremented in the isr of analog.c |
||
948 | if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached |
||
949 | { |
||
950 | static int16_t cnt = 0; |
||
951 | static int8_t last_n_p, last_n_n, last_r_p, last_r_n; |
||
952 | static int32_t MeanIntegralPitch_old, MeanIntegralRoll_old; |
||
720 | ingob | 953 | |
886 | killagreg | 954 | // if not lopping in any direction (this should be alwais the case, |
955 | // because the Measurement counter is reset to 0 if looping in any direction is active.) |
||
956 | if(!Looping_Pitch && !Looping_Roll && !FunnelCourse) |
||
957 | { |
||
958 | // Calculate mean value of the gyro integrals |
||
959 | MeanIntegralPitch /= BALANCE_NUMBER; |
||
960 | MeanIntegralRoll /= BALANCE_NUMBER; |
||
720 | ingob | 961 | |
886 | killagreg | 962 | // Calculate mean of the acceleration values |
963 | IntegralAccPitch = (ParamSet.GyroAccFactor * IntegralAccPitch) / BALANCE_NUMBER; |
||
964 | IntegralAccRoll = (ParamSet.GyroAccFactor * IntegralAccRoll ) / BALANCE_NUMBER; |
||
720 | ingob | 965 | |
886 | killagreg | 966 | // Pitch ++++++++++++++++++++++++++++++++++++++++++++++++ |
967 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
||
968 | IntegralErrorPitch = (int32_t)(MeanIntegralPitch - (int32_t)IntegralAccPitch); |
||
969 | CorrectionPitch = IntegralErrorPitch / ParamSet.GyroAccTrim; |
||
970 | AttitudeCorrectionPitch = CorrectionPitch / BALANCE_NUMBER; |
||
971 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
||
972 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
||
973 | IntegralErrorRoll = (int32_t)(MeanIntegralRoll - (int32_t)IntegralAccRoll); |
||
974 | CorrectionRoll = IntegralErrorRoll / ParamSet.GyroAccTrim; |
||
975 | AttitudeCorrectionRoll = CorrectionRoll / BALANCE_NUMBER; |
||
976 | |||
977 | if((MaxStickPitch > 32) || (MaxStickRoll > 32) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25)) |
||
978 | { |
||
979 | AttitudeCorrectionPitch /= 2; |
||
980 | AttitudeCorrectionRoll /= 2; |
||
981 | } |
||
982 | |||
983 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
984 | // Gyro-Drift ermitteln |
||
985 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
986 | // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor) |
||
987 | IntegralErrorPitch = IntegralPitch2 - IntegralPitch; |
||
988 | Reading_IntegralGyroPitch2 -= IntegralErrorPitch; |
||
989 | // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor) |
||
990 | IntegralErrorRoll = IntegralRoll2 - IntegralRoll; |
||
991 | Reading_IntegralGyroRoll2 -= IntegralErrorRoll; |
||
992 | |||
993 | if(YawGyroDrift > BALANCE_NUMBER/2) AdNeutralYaw++; |
||
994 | if(YawGyroDrift < -BALANCE_NUMBER/2) AdNeutralYaw--; |
||
995 | YawGyroDrift = 0; |
||
996 | /* |
||
997 | DebugOut.Analog[17] = IntegralAccPitch / 26; |
||
998 | DebugOut.Analog[18] = IntegralAccRoll / 26; |
||
999 | DebugOut.Analog[19] = IntegralErrorPitch;// / 26; |
||
1000 | DebugOut.Analog[20] = IntegralErrorRoll;// / 26; |
||
1001 | DebugOut.Analog[21] = MeanIntegralPitch / 26; |
||
1002 | DebugOut.Analog[22] = MeanIntegralRoll / 26; |
||
1003 | //DebugOut.Analog[28] = CorrectionPitch; |
||
1004 | DebugOut.Analog[29] = CorrectionRoll; |
||
1005 | DebugOut.Analog[30] = AttitudeCorrectionRoll * 10; |
||
720 | ingob | 1006 | */ |
886 | killagreg | 1007 | |
1008 | #define ERROR_LIMIT (BALANCE_NUMBER * 4) |
||
1009 | #define ERROR_LIMIT2 (BALANCE_NUMBER * 16) |
||
1010 | #define MOVEMENT_LIMIT 20000 |
||
1011 | // Pitch +++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1012 | cnt = 1;// + labs(IntegralErrorPitch) / 4096; |
||
1013 | CorrectionPitch = 0; |
||
1014 | if(labs(MeanIntegralPitch_old - MeanIntegralPitch) < MOVEMENT_LIMIT) |
||
1015 | { |
||
1016 | if(IntegralErrorPitch > ERROR_LIMIT2) |
||
1017 | { |
||
1018 | if(last_n_p) |
||
1019 | { |
||
1020 | cnt += labs(IntegralErrorPitch) / ERROR_LIMIT2; |
||
1021 | CorrectionPitch = IntegralErrorPitch / 8; |
||
1022 | if(CorrectionPitch > 5000) CorrectionPitch = 5000; |
||
1023 | AttitudeCorrectionPitch += CorrectionPitch / BALANCE_NUMBER; |
||
1024 | } |
||
1025 | else last_n_p = 1; |
||
1026 | } |
||
1027 | else last_n_p = 0; |
||
1028 | if(IntegralErrorPitch < -ERROR_LIMIT2) |
||
1029 | { |
||
1030 | if(last_n_n) |
||
1031 | { |
||
1032 | cnt += labs(IntegralErrorPitch) / ERROR_LIMIT2; |
||
1033 | CorrectionPitch = IntegralErrorPitch / 8; |
||
1034 | if(CorrectionPitch < -5000) CorrectionPitch = -5000; |
||
1035 | AttitudeCorrectionPitch += CorrectionPitch / BALANCE_NUMBER; |
||
1036 | } |
||
1037 | else last_n_n = 1; |
||
1038 | } |
||
1039 | else last_n_n = 0; |
||
1040 | } |
||
1041 | else |
||
1042 | { |
||
1043 | cnt = 0; |
||
1044 | BadCompassHeading = 500; |
||
1045 | } |
||
1046 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
||
1047 | // correct Gyro Offsets |
||
1048 | if(IntegralErrorPitch > ERROR_LIMIT) AdNeutralPitch += cnt; |
||
1049 | if(IntegralErrorPitch < -ERROR_LIMIT) AdNeutralPitch -= cnt; |
||
1050 | |||
1051 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1052 | cnt = 1;// + labs(IntegralErrorPitch) / 4096; |
||
1053 | CorrectionRoll = 0; |
||
1054 | if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < MOVEMENT_LIMIT) |
||
1055 | { |
||
1056 | if(IntegralErrorRoll > ERROR_LIMIT2) |
||
1057 | { |
||
1058 | if(last_r_p) |
||
1059 | { |
||
1060 | cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2; |
||
1061 | CorrectionRoll = IntegralErrorRoll / 8; |
||
1062 | if(CorrectionRoll > 5000) CorrectionRoll = 5000; |
||
1063 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
||
1064 | } |
||
1065 | else last_r_p = 1; |
||
1066 | } |
||
1067 | else last_r_p = 0; |
||
1068 | if(IntegralErrorRoll < -ERROR_LIMIT2) |
||
1069 | { |
||
1070 | if(last_r_n) |
||
1071 | { |
||
1072 | cnt += labs(IntegralErrorRoll) / ERROR_LIMIT2; |
||
1073 | CorrectionRoll = IntegralErrorRoll / 8; |
||
1074 | if(CorrectionRoll < -5000) CorrectionRoll = -5000; |
||
1075 | AttitudeCorrectionRoll += CorrectionRoll / BALANCE_NUMBER; |
||
1076 | } |
||
1077 | else last_r_n = 1; |
||
1078 | } |
||
1079 | else last_r_n = 0; |
||
1080 | } |
||
1081 | else |
||
1082 | { |
||
1083 | cnt = 0; |
||
1084 | BadCompassHeading = 500; |
||
1085 | } |
||
1086 | // correct Gyro Offsets |
||
1087 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
||
1088 | if(IntegralErrorRoll > ERROR_LIMIT) AdNeutralRoll += cnt; |
||
1089 | if(IntegralErrorRoll < -ERROR_LIMIT) AdNeutralRoll -= cnt; |
||
720 | ingob | 1090 | /* |
886 | killagreg | 1091 | DebugOut.Analog[27] = CorrectionRoll; |
1092 | DebugOut.Analog[23] = AdNeutralPitch;//10*(AdNeutralPitch - StartNeutralPitch); |
||
1093 | DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll); |
||
720 | ingob | 1094 | */ |
886 | killagreg | 1095 | } |
1096 | else // looping is active |
||
1097 | { |
||
1098 | AttitudeCorrectionRoll = 0; |
||
1099 | AttitudeCorrectionPitch = 0; |
||
1100 | FunnelCourse = 0; |
||
1101 | } |
||
395 | hbuss | 1102 | |
886 | killagreg | 1103 | // if Gyro_I_Factor == 0 , for example at Heading Hold, ignore attitude correction |
1104 | if(!Gyro_I_Factor) |
||
1105 | { |
||
1106 | AttitudeCorrectionRoll = 0; |
||
1107 | AttitudeCorrectionPitch = 0; |
||
1108 | } |
||
1109 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1110 | MeanIntegralPitch_old = MeanIntegralPitch; |
||
1111 | MeanIntegralRoll_old = MeanIntegralRoll; |
||
1112 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1113 | // reset variables used for averaging |
||
1114 | IntegralAccPitch = 0; |
||
1115 | IntegralAccRoll = 0; |
||
1116 | MeanIntegralPitch = 0; |
||
1117 | MeanIntegralRoll = 0; |
||
1118 | MeasurementCounter = 0; |
||
1119 | } // end of averaging |
||
401 | hbuss | 1120 | |
492 | hbuss | 1121 | |
886 | killagreg | 1122 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1123 | // Yawing |
||
1124 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1125 | if(abs(StickYaw) > 15 ) // yaw stick is activated |
||
1126 | { |
||
1127 | if(!(ParamSet.GlobalConfig & CFG_COMPASS_FIX)) |
||
1128 | { |
||
1129 | UpdateCompassCourse = 1; |
||
1130 | CompassCourse = YawGyroHeading; |
||
1131 | BadCompassHeading = 250; |
||
1132 | } |
||
1133 | } |
||
1134 | // exponential stick sensitivity in yawring rate |
||
1135 | tmp_int = (int32_t) ParamSet.Yaw_P * ((int32_t)StickYaw * abs(StickYaw)) / 512L; // expo y = ax + bx² |
||
1136 | tmp_int += (ParamSet.Yaw_P * StickYaw) / 4; |
||
1137 | SetPointYaw = tmp_int; |
||
1138 | // trimm drift of Reading_IntegralGyroYaw with SetPointYaw(StickYaw) |
||
1139 | Reading_IntegralGyroYaw -= tmp_int; |
||
1140 | // limit the effect |
||
1141 | if(Reading_IntegralGyroYaw > 50000) Reading_IntegralGyroYaw = 50000; |
||
1142 | if(Reading_IntegralGyroYaw <-50000) Reading_IntegralGyroYaw =-50000; |
||
614 | hbuss | 1143 | |
886 | killagreg | 1144 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1145 | // Compass |
||
1146 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1147 | // compass code is used if Compass option is selected |
||
1148 | if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE)) |
||
1149 | { |
||
1150 | int16_t w, v, r,correction, error; |
||
395 | hbuss | 1151 | |
886 | killagreg | 1152 | if(CompassCalState && MotorsOn == 0 ) |
1153 | { |
||
1154 | SetCompassCalState(); |
||
1155 | #ifdef USE_KILLAGREG |
||
1156 | MM3_Calibrate(); |
||
1157 | #endif |
||
1158 | } |
||
1159 | else |
||
1160 | { |
||
1161 | #ifdef USE_KILLAGREG |
||
1162 | static uint8_t updCompass = 0; |
||
1163 | if (!updCompass--) |
||
1164 | { |
||
1165 | updCompass = 49; // update only at 2ms*50 = 100ms (10Hz) |
||
1166 | MM3_Heading(); |
||
1167 | } |
||
1168 | #endif |
||
819 | hbuss | 1169 | |
886 | killagreg | 1170 | // get maximum attitude angle |
1171 | w = abs(IntegralPitch/512); |
||
1172 | v = abs(IntegralRoll /512); |
||
1173 | if(v > w) w = v; |
||
1174 | // update compass course |
||
1175 | if (w < 25 && UpdateCompassCourse && !BadCompassHeading) |
||
1176 | { |
||
1177 | BeepTime = 200; |
||
1178 | CompassCourse = YawGyroHeading / YAW_GYRO_DEG_FACTOR; |
||
1179 | UpdateCompassCourse = 0; |
||
1180 | } |
||
1181 | // calculate the deviation of the yaw gyro heading and the compass heading |
||
1182 | if (CompassHeading < 0) error = 0; // disable yaw drift compensation if compass heading is undefined |
||
1183 | else error = ((540 + CompassHeading - (YawGyroHeading / YAW_GYRO_DEG_FACTOR)) % 360) - 180; |
||
1184 | correction = w / 8 + 1; |
||
1185 | YawGyroHeading += (error * 8) / correction; |
||
1186 | w = (w * FCParam.CompassYawEffect) / 64; |
||
1187 | w = FCParam.CompassYawEffect - w; |
||
1188 | if(w > 0) |
||
1189 | { |
||
1190 | if(BadCompassHeading) |
||
1191 | { // wait a while |
||
1192 | BadCompassHeading--; |
||
1193 | } |
||
1194 | else |
||
1195 | { // |
||
1196 | YawGyroDrift += error; |
||
1197 | v = 64 + (MaxStickPitch + MaxStickRoll) / 8; |
||
1198 | // calc course deviation |
||
1199 | r = ((540 + (YawGyroHeading / YAW_GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180; |
||
1200 | v = (r * w) / v; // align to compass course |
||
1201 | // limit yaw rate |
||
1202 | w = 3 * FCParam.CompassYawEffect; |
||
1203 | if (v > w) v = w; |
||
1204 | else if (v < -w) v = -w; |
||
1205 | Reading_IntegralGyroYaw += v; |
||
1206 | } |
||
1207 | } |
||
1208 | else |
||
1209 | { // ignore compass at extreme attitudes for a while |
||
1210 | BadCompassHeading = 250; |
||
1211 | } |
||
1212 | } |
||
1213 | } |
||
1 | ingob | 1214 | |
886 | killagreg | 1215 | #ifdef USE_KILLAGREG |
1216 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1217 | // GPS |
||
1218 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1219 | if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE) |
||
1220 | { |
||
1221 | GPS_I_Factor = FCParam.UserParam2; |
||
1222 | GPS_P_Factor = FCParam.UserParam5; |
||
1223 | GPS_D_Factor = FCParam.UserParam6; |
||
1224 | if(EmergencyLanding) GPS_Main(230); // enables Comming Home |
||
1225 | else GPS_Main(Poti3); // behavior controlled by Poti3 |
||
1226 | } |
||
1227 | else |
||
1228 | { |
||
1229 | GPS_Pitch = 0; |
||
1230 | GPS_Roll = 0; |
||
1231 | } |
||
1232 | #endif |
||
1233 | |||
1234 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1 | ingob | 1235 | // Debugwerte zuordnen |
886 | killagreg | 1236 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1237 | if(!TimerDebugOut--) |
||
1238 | { |
||
1239 | TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz) |
||
1240 | DebugOut.Analog[0] = IntegralPitch / ParamSet.GyroAccFactor; |
||
1241 | DebugOut.Analog[1] = IntegralRoll / ParamSet.GyroAccFactor; |
||
1242 | DebugOut.Analog[2] = Mean_AccPitch; |
||
1243 | DebugOut.Analog[3] = Mean_AccRoll; |
||
1244 | DebugOut.Analog[4] = Reading_GyroYaw; |
||
1245 | DebugOut.Analog[5] = ReadingHeight; |
||
1246 | DebugOut.Analog[6] = (Reading_Integral_Top / 512); |
||
903 | pangu | 1247 | // DebugOut.Analog[8] = CompassHeading; |
1248 | DebugOut.Analog[8] = RC_Quality; |
||
886 | killagreg | 1249 | DebugOut.Analog[9] = UBat; |
903 | pangu | 1250 | |
1251 | #ifdef HEXAKOPTER |
||
1252 | DebugOut.Analog[10] = Motor_FrontLeft; |
||
1253 | DebugOut.Analog[11] = Motor_FrontRight; |
||
1254 | DebugOut.Analog[12] = Motor_RearLeft; |
||
1255 | DebugOut.Analog[13] = Motor_RearRight; |
||
1256 | DebugOut.Analog[14] = Motor_Left; |
||
1257 | DebugOut.Analog[15] = Motor_Right; |
||
1258 | #else |
||
886 | killagreg | 1259 | DebugOut.Analog[11] = YawGyroHeading / YAW_GYRO_DEG_FACTOR; |
903 | pangu | 1260 | |
1261 | DebugOut.Analog[12] = Motor_Front; |
||
1262 | DebugOut.Analog[13] = Motor_Rear; |
||
1263 | DebugOut.Analog[14] = Motor_Left; |
||
1264 | DebugOut.Analog[15] = Motor_Right; |
||
1265 | #endif |
||
1266 | |||
886 | killagreg | 1267 | DebugOut.Analog[16] = Mean_AccTop; |
805 | hbuss | 1268 | |
886 | killagreg | 1269 | DebugOut.Analog[20] = ServoValue; |
173 | holgerb | 1270 | |
744 | hbuss | 1271 | |
720 | ingob | 1272 | |
886 | killagreg | 1273 | DebugOut.Analog[30] = GPS_Pitch; |
1274 | DebugOut.Analog[31] = GPS_Roll; |
||
805 | hbuss | 1275 | |
886 | killagreg | 1276 | /* DebugOut.Analog[16] = motor_rx[0]; |
1277 | DebugOut.Analog[17] = motor_rx[1]; |
||
1278 | DebugOut.Analog[18] = motor_rx[2]; |
||
1279 | DebugOut.Analog[19] = motor_rx[3]; |
||
1280 | DebugOut.Analog[20] = motor_rx[0] + motor_rx[1] + motor_rx[2] + motor_rx[3]; |
||
1281 | DebugOut.Analog[20] /= 14; |
||
1282 | DebugOut.Analog[21] = motor_rx[4]; |
||
1283 | DebugOut.Analog[22] = motor_rx[5]; |
||
1284 | DebugOut.Analog[23] = motor_rx[6]; |
||
1285 | DebugOut.Analog[24] = motor_rx[7]; |
||
1286 | DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7]; |
||
805 | hbuss | 1287 | |
886 | killagreg | 1288 | DebugOut.Analog[9] = Reading_GyroPitch; |
1289 | DebugOut.Analog[9] = SetPointHeight; |
||
1290 | DebugOut.Analog[10] = Reading_IntegralGyroYaw / 128; |
||
1 | ingob | 1291 | |
886 | killagreg | 1292 | DebugOut.Analog[10] = FCParam.Gyro_I; |
1293 | DebugOut.Analog[10] = ParamSet.Gyro_I; |
||
1294 | DebugOut.Analog[9] = CompassOffCourse; |
||
1295 | DebugOut.Analog[10] = ThrustMixFraction; |
||
1296 | DebugOut.Analog[3] = HeightD * 32; |
||
1297 | DebugOut.Analog[4] = HeightControlThrust; |
||
1298 | */ |
||
1299 | } |
||
604 | hbuss | 1300 | |
886 | killagreg | 1301 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1302 | // calculate control feedback from angle (gyro integral) and agular velocity (gyro signal) |
||
1303 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1 | ingob | 1304 | |
886 | killagreg | 1305 | if(Looping_Pitch) Reading_GyroPitch = Reading_GyroPitch * Gyro_P_Factor; |
1306 | else Reading_GyroPitch = IntegralPitch * Gyro_I_Factor + Reading_GyroPitch * Gyro_P_Factor; |
||
1307 | if(Looping_Roll) Reading_GyroRoll = Reading_GyroRoll * Gyro_P_Factor; |
||
1308 | else Reading_GyroRoll = IntegralRoll * Gyro_I_Factor + Reading_GyroRoll * Gyro_P_Factor; |
||
1309 | Reading_GyroYaw = Reading_GyroYaw * (2 * Gyro_P_Factor) + IntegralYaw * Gyro_I_Factor / 2; |
||
854 | hbuss | 1310 | |
886 | killagreg | 1311 | DebugOut.Analog[21] = Reading_GyroPitch; |
1312 | DebugOut.Analog[22] = Reading_GyroRoll; |
||
1 | ingob | 1313 | |
886 | killagreg | 1314 | // limit control feedback |
1315 | #define MAX_SENSOR (4096 * STICK_GAIN) |
||
1316 | if(Reading_GyroPitch > MAX_SENSOR) Reading_GyroPitch = MAX_SENSOR; |
||
1317 | if(Reading_GyroPitch < -MAX_SENSOR) Reading_GyroPitch = -MAX_SENSOR; |
||
1318 | if(Reading_GyroRoll > MAX_SENSOR) Reading_GyroRoll = MAX_SENSOR; |
||
1319 | if(Reading_GyroRoll < -MAX_SENSOR) Reading_GyroRoll = -MAX_SENSOR; |
||
1320 | if(Reading_GyroYaw > MAX_SENSOR) Reading_GyroYaw = MAX_SENSOR; |
||
1321 | if(Reading_GyroYaw < -MAX_SENSOR) Reading_GyroYaw = -MAX_SENSOR; |
||
855 | hbuss | 1322 | |
886 | killagreg | 1323 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1324 | // Height Control |
||
1325 | // The height control algorithm reduces the thrust but does not increase the thrust. |
||
1326 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1 | ingob | 1327 | |
886 | killagreg | 1328 | ThrustMixFraction *= STICK_GAIN; |
513 | hbuss | 1329 | |
886 | killagreg | 1330 | // If height control is activated and no emergency landing is active |
1331 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) && (!EmergencyLanding) ) |
||
1332 | { |
||
1333 | int tmp_int; |
||
1334 | // if height control is activated by an rc channel |
||
1335 | if(ParamSet.GlobalConfig & CFG_HEIGHT_SWITCH) |
||
1336 | { // check if parameter is less than activation threshold |
||
1337 | if(FCParam.MaxHeight < 50) |
||
1338 | { |
||
1339 | SetPointHeight = ReadingHeight - 20; // update SetPoint with current reading |
||
1340 | HeightControlActive = 0; // disable height control |
||
1341 | } |
||
1342 | else HeightControlActive = 1; // enable height control |
||
1343 | } |
||
1344 | else // no switchable height control |
||
1345 | { |
||
1346 | SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20; |
||
1347 | HeightControlActive = 1; |
||
1348 | } |
||
1349 | // get current height |
||
1350 | h = ReadingHeight; |
||
1351 | // if current height is above the setpoint reduce thrust |
||
1352 | if((h > SetPointHeight) && HeightControlActive) |
||
1353 | { |
||
1354 | // ThrustMixFraction - HightDeviation * P - HeightChange * D - ACCTop * DACC |
||
1355 | // height difference -> P control part |
||
1356 | h = ((h - SetPointHeight) * (int16_t) FCParam.Height_P) / (16 / STICK_GAIN); |
||
1357 | h = ThrustMixFraction - h; // reduce gas |
||
1358 | // height gradient --> D control part |
||
1359 | //h -= (HeightD * FCParam.Height_D) / (8 / STICK_GAIN); // D control part |
||
1360 | h -= (HeightD) / (8 / STICK_GAIN); // D control part |
||
1361 | // acceleration sensor effect |
||
1362 | tmp_int = ((Reading_Integral_Top / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN); |
||
1363 | if(tmp_int > 70 * STICK_GAIN) tmp_int = 70 * STICK_GAIN; |
||
1364 | else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN); |
||
1365 | h -= tmp_int; |
||
1366 | // update height control thrust |
||
1367 | HeightControlThrust = (HeightControlThrust*15 + h) / 16; |
||
1368 | // limit thrust reduction |
||
1369 | if(HeightControlThrust < ParamSet.Height_MinThrust * STICK_GAIN) |
||
1370 | { |
||
1371 | if(ThrustMixFraction >= ParamSet.Height_MinThrust * STICK_GAIN) HeightControlThrust = ParamSet.Height_MinThrust * STICK_GAIN; |
||
1372 | // allows landing also if thrust stick is reduced below min thrust on height control |
||
1373 | if(ThrustMixFraction < ParamSet.Height_MinThrust * STICK_GAIN) HeightControlThrust = ThrustMixFraction; |
||
1374 | } |
||
1375 | // limit thrust to stick setting |
||
1376 | if(HeightControlThrust > ThrustMixFraction) HeightControlThrust = ThrustMixFraction; |
||
1377 | ThrustMixFraction = HeightControlThrust; |
||
1378 | } |
||
1379 | } |
||
1380 | // limit thrust to parameter setting |
||
1381 | if(ThrustMixFraction > (ParamSet.Trust_Max - 20) * STICK_GAIN) ThrustMixFraction = (ParamSet.Trust_Max - 20) * STICK_GAIN; |
||
1382 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1383 | // + Mixer and PI-Controller |
||
1384 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1385 | DebugOut.Analog[7] = ThrustMixFraction; |
||
1386 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1387 | // Yaw-Fraction |
||
1388 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1389 | YawMixFraction = Reading_GyroYaw - SetPointYaw * STICK_GAIN; // yaw controller |
||
1390 | #define MIN_YAWTHRUST (40 * STICK_GAIN) // yaw also below this thrust value |
||
1391 | // limit YawMixFraction |
||
1392 | if(ThrustMixFraction > MIN_YAWTHRUST) |
||
1393 | { |
||
1394 | if(YawMixFraction > (ThrustMixFraction / 2)) YawMixFraction = ThrustMixFraction / 2; |
||
1395 | if(YawMixFraction < -(ThrustMixFraction / 2)) YawMixFraction = -(ThrustMixFraction / 2); |
||
1396 | } |
||
1397 | else |
||
1398 | { |
||
1399 | if(YawMixFraction > (MIN_YAWTHRUST / 2)) YawMixFraction = MIN_YAWTHRUST / 2; |
||
1400 | if(YawMixFraction < -(MIN_YAWTHRUST / 2)) YawMixFraction = -(MIN_YAWTHRUST / 2); |
||
1401 | } |
||
1402 | tmp_int = ParamSet.Trust_Max * STICK_GAIN; |
||
1403 | if(YawMixFraction > ((tmp_int - ThrustMixFraction))) YawMixFraction = ((tmp_int - ThrustMixFraction)); |
||
1404 | if(YawMixFraction < -((tmp_int - ThrustMixFraction))) YawMixFraction = -((tmp_int - ThrustMixFraction)); |
||
173 | holgerb | 1405 | |
886 | killagreg | 1406 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1407 | // Pitch-Axis |
||
1408 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1409 | DiffPitch = Reading_GyroPitch - StickPitch; // get difference |
||
1410 | if(Gyro_I_Factor) SumPitch += IntegralPitch * Gyro_I_Factor - StickPitch; // I-part for attitude control |
||
1411 | else SumPitch += DiffPitch; // I-part for head holding |
||
1412 | if(SumPitch > (STICK_GAIN * 16000L)) SumPitch = (STICK_GAIN * 16000L); |
||
1413 | if(SumPitch < -(STICK_GAIN * 16000L)) SumPitch = -(STICK_GAIN * 16000L); |
||
1414 | pd_result = DiffPitch + Ki * SumPitch; // PI-controller for pitch |
||
1415 | |||
1416 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64; |
||
1417 | if(pd_result > tmp_int) pd_result = tmp_int; |
||
1418 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
||
1419 | |||
903 | pangu | 1420 | PitchMixFraction = pd_result; |
886 | killagreg | 1421 | |
1422 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1423 | // Roll-Axis |
||
1424 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1425 | DiffRoll = Reading_GyroRoll - StickRoll; // get difference |
||
1426 | if(Gyro_I_Factor) SumRoll += IntegralRoll * Gyro_I_Factor - StickRoll; // I-part for attitude control |
||
1427 | else SumRoll += DiffRoll; // I-part for head holding |
||
901 | killagreg | 1428 | if(SumRoll > (STICK_GAIN * 16000L)) SumRoll = (STICK_GAIN * 16000L); |
1429 | if(SumRoll < -(STICK_GAIN * 16000L)) SumRoll = -(STICK_GAIN * 16000L); |
||
886 | killagreg | 1430 | pd_result = DiffRoll + Ki * SumRoll; // PI-controller for roll |
1431 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64; |
||
1432 | if(pd_result > tmp_int) pd_result = tmp_int; |
||
1433 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
||
1434 | |||
903 | pangu | 1435 | RollMixFraction = pd_result; |
1436 | |||
1437 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1438 | // Calculate Motor Mixes |
||
1439 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1440 | |||
1441 | #ifdef HEXAKOPTER |
||
1442 | // Motor FrontLeft |
||
1443 | MotorValue = ThrustMixFraction |
||
1444 | + PitchMixFraction |
||
1445 | + RollMixFraction/2 |
||
1446 | - YawMixFraction; // Mixer |
||
1447 | MotorValue /= STICK_GAIN; |
||
1448 | if (MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
||
1449 | else if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1450 | Motor_FrontLeft = MotorValue; |
||
1451 | |||
1452 | // Motor FrontRight |
||
1453 | MotorValue = ThrustMixFraction |
||
1454 | + PitchMixFraction |
||
1455 | - RollMixFraction/2 |
||
1456 | + YawMixFraction; // Mixer |
||
1457 | MotorValue /= STICK_GAIN; |
||
1458 | if (MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
||
1459 | else if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1460 | Motor_FrontRight = MotorValue; |
||
1461 | |||
1462 | // Motor RearLeft |
||
1463 | MotorValue = ThrustMixFraction |
||
1464 | - PitchMixFraction |
||
1465 | + RollMixFraction/2 |
||
1466 | - YawMixFraction; // Mixer |
||
1467 | MotorValue /= STICK_GAIN; |
||
1468 | if (MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
||
1469 | else if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1470 | Motor_RearLeft = MotorValue; |
||
1471 | |||
1472 | // Motor RearRight |
||
1473 | MotorValue = ThrustMixFraction |
||
1474 | - PitchMixFraction |
||
1475 | - RollMixFraction/2 |
||
1476 | + YawMixFraction; // Mixer |
||
1477 | MotorValue /= STICK_GAIN; |
||
1478 | if (MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
||
1479 | else if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1480 | Motor_RearRight= MotorValue; |
||
1481 | |||
886 | killagreg | 1482 | // Motor Left |
903 | pangu | 1483 | MotorValue = ThrustMixFraction |
1484 | + RollMixFraction |
||
1485 | + YawMixFraction; // Mixer |
||
886 | killagreg | 1486 | MotorValue /= STICK_GAIN; |
903 | pangu | 1487 | if (MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
1488 | else if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1489 | Motor_Left = MotorValue; |
||
1490 | |||
1491 | // Motor Right |
||
1492 | MotorValue = ThrustMixFraction |
||
1493 | - RollMixFraction |
||
1494 | - YawMixFraction; // Mixer |
||
1495 | MotorValue /= STICK_GAIN; |
||
1496 | if (MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
||
1497 | else if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1498 | Motor_Right = MotorValue; |
||
1499 | |||
1500 | #else |
||
1501 | |||
1502 | // Motor Front |
||
1503 | MotorValue = ThrustMixFraction + PitchMixFraction + YawMixFraction; // Mixer |
||
1504 | MotorValue /= STICK_GAIN; |
||
886 | killagreg | 1505 | if ((MotorValue < 0)) MotorValue = 0; |
903 | pangu | 1506 | else if(MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
1507 | if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1508 | Motor_Front = MotorValue; |
||
1509 | |||
1510 | // Motor Rear |
||
1511 | MotorValue = ThrustMixFraction - PitchMixFraction + YawMixFraction; // Mixer |
||
1512 | MotorValue /= STICK_GAIN; |
||
1513 | if ((MotorValue < 0)) MotorValue = 0; |
||
1514 | else if(MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
||
1515 | if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1516 | Motor_Rear = MotorValue; |
||
1517 | |||
1518 | |||
1519 | // Motor Left |
||
1520 | MotorValue = ThrustMixFraction + RollMixFraction - YawMixFraction; // Mixer |
||
1521 | MotorValue /= STICK_GAIN; |
||
1522 | if ((MotorValue < 0)) MotorValue = 0; |
||
886 | killagreg | 1523 | else if(MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
1524 | if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1525 | Motor_Left = MotorValue; |
||
1526 | |||
903 | pangu | 1527 | // Motor Right |
1528 | MotorValue = ThrustMixFraction - RollMixFraction - YawMixFraction; // Mixer |
||
886 | killagreg | 1529 | MotorValue /= STICK_GAIN; |
1530 | if ((MotorValue < 0)) MotorValue = 0; |
||
1531 | else if(MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
||
1532 | if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
||
1533 | Motor_Right = MotorValue; |
||
903 | pangu | 1534 | #endif |
1 | ingob | 1535 | } |
1536 |