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