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