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