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