Rev 908 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 908 | Rev 911 | ||
---|---|---|---|
Line 74... | Line 74... | ||
74 | #endif |
74 | #endif |
75 | #include "led.h" |
75 | #include "led.h" |
Line 76... | Line 76... | ||
76 | 76 | ||
77 | volatile uint16_t I2CTimeout = 100; |
77 | volatile uint16_t I2CTimeout = 100; |
78 | // gyro readings |
78 | // gyro readings |
79 | volatile int16_t Reading_GyroPitch, Reading_GyroRoll, Reading_GyroYaw; |
79 | volatile int16_t Reading_GyroNick, Reading_GyroRoll, Reading_GyroYaw; |
80 | // gyro neutral readings |
80 | // gyro neutral readings |
81 | volatile int16_t AdNeutralPitch = 0, AdNeutralRoll = 0, AdNeutralYaw = 0; |
81 | volatile int16_t AdNeutralNick = 0, AdNeutralRoll = 0, AdNeutralYaw = 0; |
82 | volatile int16_t StartNeutralRoll = 0, StartNeutralPitch = 0; |
82 | volatile int16_t StartNeutralRoll = 0, StartNeutralNick = 0; |
83 | // mean accelerations |
83 | // mean accelerations |
Line 84... | Line 84... | ||
84 | volatile int16_t Mean_AccPitch, Mean_AccRoll, Mean_AccTop; |
84 | volatile int16_t Mean_AccNick, Mean_AccRoll, Mean_AccTop; |
85 | 85 | ||
86 | // neutral acceleration readings |
86 | // neutral acceleration readings |
Line 87... | Line 87... | ||
87 | volatile int16_t NeutralAccX=0, NeutralAccY=0; |
87 | volatile int16_t NeutralAccX=0, NeutralAccY=0; |
88 | volatile float NeutralAccZ = 0; |
88 | volatile float NeutralAccZ = 0; |
89 | 89 | ||
90 | // attitude gyro integrals |
90 | // attitude gyro integrals |
91 | volatile int32_t IntegralPitch = 0,IntegralPitch2 = 0; |
91 | volatile int32_t IntegralNick = 0,IntegralNick2 = 0; |
92 | volatile int32_t IntegralRoll = 0,IntegralRoll2 = 0; |
92 | volatile int32_t IntegralRoll = 0,IntegralRoll2 = 0; |
93 | volatile int32_t IntegralYaw = 0; |
93 | volatile int32_t IntegralYaw = 0; |
94 | volatile int32_t Reading_IntegralGyroPitch = 0, Reading_IntegralGyroPitch2 = 0; |
94 | volatile int32_t Reading_IntegralGyroNick = 0, Reading_IntegralGyroNick2 = 0; |
95 | volatile int32_t Reading_IntegralGyroRoll = 0, Reading_IntegralGyroRoll2 = 0; |
95 | volatile int32_t Reading_IntegralGyroRoll = 0, Reading_IntegralGyroRoll2 = 0; |
Line 96... | Line 96... | ||
96 | volatile int32_t Reading_IntegralGyroYaw = 0; |
96 | volatile int32_t Reading_IntegralGyroYaw = 0; |
97 | volatile int32_t MeanIntegralPitch; |
97 | volatile int32_t MeanIntegralNick; |
98 | volatile int32_t MeanIntegralRoll; |
98 | volatile int32_t MeanIntegralRoll; |
Line 99... | Line 99... | ||
99 | 99 | ||
100 | // attitude acceleration integrals |
100 | // attitude acceleration integrals |
101 | volatile int32_t IntegralAccPitch = 0, IntegralAccRoll = 0; |
101 | volatile int32_t IntegralAccNick = 0, IntegralAccRoll = 0; |
Line 110... | Line 110... | ||
110 | uint16_t BadCompassHeading = 500; |
110 | uint16_t BadCompassHeading = 500; |
111 | int32_t YawGyroHeading; |
111 | int32_t YawGyroHeading; |
112 | int16_t YawGyroDrift; |
112 | int16_t YawGyroDrift; |
Line 113... | Line 113... | ||
113 | 113 | ||
Line 114... | Line 114... | ||
114 | 114 | ||
115 | int16_t NaviAccPitch = 0, NaviAccRoll = 0, NaviCntAcc = 0; |
115 | int16_t NaviAccNick = 0, NaviAccRoll = 0, NaviCntAcc = 0; |
116 | 116 | ||
117 | 117 | ||
Line 118... | Line 118... | ||
118 | // flags |
118 | // flags |
Line 119... | Line 119... | ||
119 | uint8_t MotorsOn = 0; |
119 | uint8_t MotorsOn = 0; |
120 | uint8_t EmergencyLanding = 0; |
120 | uint8_t EmergencyLanding = 0; |
Line 121... | Line 121... | ||
121 | uint16_t Model_Is_Flying = 0; |
121 | uint16_t Model_Is_Flying = 0; |
Line 122... | Line 122... | ||
122 | 122 | ||
Line 123... | Line 123... | ||
123 | int32_t TurnOver180Pitch = 250000L, TurnOver180Roll = 250000L; |
123 | int32_t TurnOver180Nick = 250000L, TurnOver180Roll = 250000L; |
124 | 124 | ||
Line 125... | Line 125... | ||
125 | float Gyro_P_Factor; |
125 | float Gyro_P_Factor; |
126 | float Gyro_I_Factor; |
126 | float Gyro_I_Factor; |
127 | 127 | ||
Line 128... | Line 128... | ||
128 | volatile int16_t DiffPitch, DiffRoll; |
128 | volatile int16_t DiffNick, DiffRoll; |
129 | 129 | ||
130 | int16_t Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
130 | int16_t Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0; |
Line 131... | Line 131... | ||
131 | 131 | ||
132 | // setpoints for motors |
132 | // setpoints for motors |
Line 133... | Line 133... | ||
133 | volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left; |
133 | volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left; |
Line 134... | Line 134... | ||
134 | 134 | ||
Line 135... | Line 135... | ||
135 | // stick values derived by rc channels readings |
135 | // stick values derived by rc channels readings |
136 | int16_t StickPitch = 0, StickRoll = 0, StickYaw = 0, StickThrust = 0; |
136 | int16_t StickNick = 0, StickRoll = 0, StickYaw = 0, StickGas = 0; |
Line 137... | Line 137... | ||
137 | int16_t GPS_Pitch = 0, GPS_Roll = 0; |
137 | int16_t GPS_Nick = 0, GPS_Roll = 0; |
Line 178... | Line 178... | ||
178 | void SetNeutral(void) |
178 | void SetNeutral(void) |
179 | { |
179 | { |
180 | NeutralAccX = 0; |
180 | NeutralAccX = 0; |
181 | NeutralAccY = 0; |
181 | NeutralAccY = 0; |
182 | NeutralAccZ = 0; |
182 | NeutralAccZ = 0; |
183 | AdNeutralPitch = 0; |
183 | AdNeutralNick = 0; |
184 | AdNeutralRoll = 0; |
184 | AdNeutralRoll = 0; |
185 | AdNeutralYaw = 0; |
185 | AdNeutralYaw = 0; |
186 | FCParam.Yaw_PosFeedback = 0; |
186 | FCParam.Yaw_PosFeedback = 0; |
187 | FCParam.Yaw_NegFeedback = 0; |
187 | FCParam.Yaw_NegFeedback = 0; |
188 | CalibMean(); |
188 | CalibMean(); |
Line 190... | Line 190... | ||
190 | CalibMean(); |
190 | CalibMean(); |
191 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)) // Height Control activated? |
191 | if((ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)) // Height Control activated? |
192 | { |
192 | { |
193 | if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset(); |
193 | if((ReadingAirPressure > 950) || (ReadingAirPressure < 750)) SearchAirPressureOffset(); |
194 | } |
194 | } |
195 | AdNeutralPitch = AdValueGyrPitch; |
195 | AdNeutralNick = AdValueGyrNick; |
196 | AdNeutralRoll = AdValueGyrRoll; |
196 | AdNeutralRoll = AdValueGyrRoll; |
197 | AdNeutralYaw = AdValueGyrYaw; |
197 | AdNeutralYaw = AdValueGyrYaw; |
198 | StartNeutralRoll = AdNeutralRoll; |
198 | StartNeutralRoll = AdNeutralRoll; |
199 | StartNeutralPitch = AdNeutralPitch; |
199 | StartNeutralNick = AdNeutralNick; |
200 | if(GetParamWord(PID_ACC_PITCH) > 1023) |
200 | if(GetParamWord(PID_ACC_NICK) > 1023) |
201 | { |
201 | { |
202 | NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY; |
202 | NeutralAccY = abs(Mean_AccRoll) / ACC_AMPLIFY; |
203 | NeutralAccX = abs(Mean_AccPitch) / ACC_AMPLIFY; |
203 | NeutralAccX = abs(Mean_AccNick) / ACC_AMPLIFY; |
204 | NeutralAccZ = Current_AccZ; |
204 | NeutralAccZ = Current_AccZ; |
205 | } |
205 | } |
206 | else |
206 | else |
207 | { |
207 | { |
208 | NeutralAccX = (int16_t)GetParamWord(PID_ACC_PITCH); |
208 | NeutralAccX = (int16_t)GetParamWord(PID_ACC_NICK); |
209 | NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL); |
209 | NeutralAccY = (int16_t)GetParamWord(PID_ACC_ROLL); |
210 | NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z); |
210 | NeutralAccZ = (int16_t)GetParamWord(PID_ACC_Z); |
211 | } |
211 | } |
212 | Reading_IntegralGyroPitch = 0; |
212 | Reading_IntegralGyroNick = 0; |
213 | Reading_IntegralGyroPitch2 = 0; |
213 | Reading_IntegralGyroNick2 = 0; |
214 | Reading_IntegralGyroRoll = 0; |
214 | Reading_IntegralGyroRoll = 0; |
215 | Reading_IntegralGyroRoll2 = 0; |
215 | Reading_IntegralGyroRoll2 = 0; |
216 | Reading_IntegralGyroYaw = 0; |
216 | Reading_IntegralGyroYaw = 0; |
217 | Reading_GyroPitch = 0; |
217 | Reading_GyroNick = 0; |
218 | Reading_GyroRoll = 0; |
218 | Reading_GyroRoll = 0; |
219 | Reading_GyroYaw = 0; |
219 | Reading_GyroYaw = 0; |
220 | StartAirPressure = AirPressure; |
220 | StartAirPressure = AirPressure; |
221 | HeightD = 0; |
221 | HeightD = 0; |
222 | Reading_Integral_Top = 0; |
222 | Reading_Integral_Top = 0; |
223 | CompassCourse = CompassHeading; |
223 | CompassCourse = CompassHeading; |
224 | BeepTime = 50; |
224 | BeepTime = 50; |
225 | TurnOver180Pitch = ((int32_t) ParamSet.AngleTurnOverPitch * 2500L) +15000L; |
225 | TurnOver180Nick = ((int32_t) ParamSet.AngleTurnOverNick * 2500L) +15000L; |
226 | TurnOver180Roll = ((int32_t) ParamSet.AngleTurnOverRoll * 2500L) +15000L; |
226 | TurnOver180Roll = ((int32_t) ParamSet.AngleTurnOverRoll * 2500L) +15000L; |
227 | ExternHeightValue = 0; |
227 | ExternHeightValue = 0; |
228 | GPS_Pitch = 0; |
228 | GPS_Nick = 0; |
229 | GPS_Roll = 0; |
229 | GPS_Roll = 0; |
230 | YawGyroHeading = CompassHeading * YAW_GYRO_DEG_FACTOR; |
230 | YawGyroHeading = CompassHeading * YAW_GYRO_DEG_FACTOR; |
231 | YawGyroDrift = 0; |
231 | YawGyroDrift = 0; |
232 | } |
232 | } |
Line 239... | Line 239... | ||
239 | static int32_t tmpl,tmpl2; |
239 | static int32_t tmpl,tmpl2; |
Line 240... | Line 240... | ||
240 | 240 | ||
241 | // Get offset corrected gyro readings (~ to angular velocity) |
241 | // Get offset corrected gyro readings (~ to angular velocity) |
242 | Reading_GyroYaw = AdNeutralYaw - AdValueGyrYaw; |
242 | Reading_GyroYaw = AdNeutralYaw - AdValueGyrYaw; |
243 | Reading_GyroRoll = AdValueGyrRoll - AdNeutralRoll; |
243 | Reading_GyroRoll = AdValueGyrRoll - AdNeutralRoll; |
Line 244... | Line 244... | ||
244 | Reading_GyroPitch = AdValueGyrPitch - AdNeutralPitch; |
244 | Reading_GyroNick = AdValueGyrNick - AdNeutralNick; |
245 | 245 | ||
246 | // Acceleration Sensor |
246 | // Acceleration Sensor |
247 | // sliding average sensor readings |
247 | // sliding average sensor readings |
248 | Mean_AccPitch = ((int32_t)Mean_AccPitch * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccPitch))) / 2L; |
248 | Mean_AccNick = ((int32_t)Mean_AccNick * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccNick))) / 2L; |
Line 249... | Line 249... | ||
249 | Mean_AccRoll = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L; |
249 | Mean_AccRoll = ((int32_t)Mean_AccRoll * 1 + ((ACC_AMPLIFY * (int32_t)AdValueAccRoll))) / 2L; |
250 | Mean_AccTop = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L; |
250 | Mean_AccTop = ((int32_t)Mean_AccTop * 1 + ((int32_t)AdValueAccTop)) / 2L; |
251 | 251 | ||
Line 252... | Line 252... | ||
252 | // sum sensor readings for later averaging |
252 | // sum sensor readings for later averaging |
253 | IntegralAccPitch += ACC_AMPLIFY * AdValueAccPitch; |
253 | IntegralAccNick += ACC_AMPLIFY * AdValueAccNick; |
254 | IntegralAccRoll += ACC_AMPLIFY * AdValueAccRoll; |
254 | IntegralAccRoll += ACC_AMPLIFY * AdValueAccRoll; |
Line 255... | Line 255... | ||
255 | 255 | ||
256 | NaviAccPitch += AdValueAccPitch; |
256 | NaviAccNick += AdValueAccNick; |
Line 264... | Line 264... | ||
264 | if(YawGyroHeading >= (360L * YAW_GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * YAW_GYRO_DEG_FACTOR; // 360° Wrap |
264 | if(YawGyroHeading >= (360L * YAW_GYRO_DEG_FACTOR)) YawGyroHeading -= 360L * YAW_GYRO_DEG_FACTOR; // 360° Wrap |
265 | if(YawGyroHeading < 0) YawGyroHeading += 360L * YAW_GYRO_DEG_FACTOR; |
265 | if(YawGyroHeading < 0) YawGyroHeading += 360L * YAW_GYRO_DEG_FACTOR; |
Line 266... | Line 266... | ||
266 | 266 | ||
267 | 267 | ||
268 | // Coupling fraction |
268 | // Coupling fraction |
269 | if(!Looping_Pitch && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) |
269 | if(!Looping_Nick && !Looping_Roll && (ParamSet.GlobalConfig & CFG_AXIS_COUPLING_ACTIVE)) |
270 | { |
270 | { |
271 | tmpl = (Reading_GyroYaw * Reading_IntegralGyroPitch) / 2048L; |
271 | tmpl = (Reading_GyroYaw * Reading_IntegralGyroNick) / 2048L; |
272 | tmpl *= FCParam.Yaw_PosFeedback; |
272 | tmpl *= FCParam.Yaw_PosFeedback; |
273 | tmpl /= 4096L; |
273 | tmpl /= 4096L; |
274 | tmpl2 = ( Reading_GyroYaw * Reading_IntegralGyroRoll) / 2048L; |
274 | tmpl2 = ( Reading_GyroYaw * Reading_IntegralGyroRoll) / 2048L; |
Line 303... | Line 303... | ||
303 | else |
303 | else |
304 | { |
304 | { |
305 | if(AdValueGyrRoll > 2020) Reading_GyroRoll = +1000; |
305 | if(AdValueGyrRoll > 2020) Reading_GyroRoll = +1000; |
306 | if(AdValueGyrRoll > 2034) Reading_GyroRoll = +2000; |
306 | if(AdValueGyrRoll > 2034) Reading_GyroRoll = +2000; |
307 | } |
307 | } |
308 | // Pitch |
308 | // Nick |
309 | Reading_GyroPitch -= tmpl2; |
309 | Reading_GyroNick -= tmpl2; |
310 | Reading_GyroPitch -= (tmpl*FCParam.Yaw_NegFeedback) / 512L; |
310 | Reading_GyroNick -= (tmpl*FCParam.Yaw_NegFeedback) / 512L; |
311 | Reading_IntegralGyroPitch2 += Reading_GyroPitch; |
311 | Reading_IntegralGyroNick2 += Reading_GyroNick; |
312 | Reading_IntegralGyroPitch += Reading_GyroPitch - AttitudeCorrectionPitch; |
312 | Reading_IntegralGyroNick += Reading_GyroNick - AttitudeCorrectionNick; |
313 | if(Reading_IntegralGyroPitch > TurnOver180Pitch) |
313 | if(Reading_IntegralGyroNick > TurnOver180Nick) |
314 | { |
314 | { |
315 | Reading_IntegralGyroPitch = -(TurnOver180Pitch - 25000L); |
315 | Reading_IntegralGyroNick = -(TurnOver180Nick - 25000L); |
316 | Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch; |
316 | Reading_IntegralGyroNick2 = Reading_IntegralGyroNick; |
317 | } |
317 | } |
318 | if(Reading_IntegralGyroPitch < -TurnOver180Pitch) |
318 | if(Reading_IntegralGyroNick < -TurnOver180Nick) |
319 | { |
319 | { |
320 | Reading_IntegralGyroPitch = (TurnOver180Pitch - 25000L); |
320 | Reading_IntegralGyroNick = (TurnOver180Nick - 25000L); |
321 | Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch; |
321 | Reading_IntegralGyroNick2 = Reading_IntegralGyroNick; |
322 | } |
322 | } |
323 | if(AdValueGyrPitch < 15) Reading_GyroPitch = -1000; |
323 | if(AdValueGyrNick < 15) Reading_GyroNick = -1000; |
324 | if(AdValueGyrPitch < 7) Reading_GyroPitch = -2000; |
324 | if(AdValueGyrNick < 7) Reading_GyroNick = -2000; |
325 | if(BoardRelease == 10) |
325 | if(BoardRelease == 10) |
326 | { |
326 | { |
327 | if(AdValueGyrPitch > 1010) Reading_GyroPitch = +1000; |
327 | if(AdValueGyrNick > 1010) Reading_GyroNick = +1000; |
328 | if(AdValueGyrPitch > 1017) Reading_GyroPitch = +2000; |
328 | if(AdValueGyrNick > 1017) Reading_GyroNick = +2000; |
329 | } |
329 | } |
330 | else |
330 | else |
331 | { |
331 | { |
332 | if(AdValueGyrPitch > 2020) Reading_GyroPitch = +1000; |
332 | if(AdValueGyrNick > 2020) Reading_GyroNick = +1000; |
333 | if(AdValueGyrPitch > 2034) Reading_GyroPitch = +2000; |
333 | if(AdValueGyrNick > 2034) Reading_GyroNick = +2000; |
334 | } |
334 | } |
Line 335... | Line 335... | ||
335 | 335 | ||
336 | // start ADC again to capture measurement values for the next loop |
336 | // start ADC again to capture measurement values for the next loop |
Line 337... | Line 337... | ||
337 | ADC_Enable(); |
337 | ADC_Enable(); |
338 | 338 | ||
339 | IntegralYaw = Reading_IntegralGyroYaw; |
339 | IntegralYaw = Reading_IntegralGyroYaw; |
340 | IntegralPitch = Reading_IntegralGyroPitch; |
340 | IntegralNick = Reading_IntegralGyroNick; |
341 | IntegralRoll = Reading_IntegralGyroRoll; |
341 | IntegralRoll = Reading_IntegralGyroRoll; |
Line 342... | Line 342... | ||
342 | IntegralPitch2 = Reading_IntegralGyroPitch2; |
342 | IntegralNick2 = Reading_IntegralGyroNick2; |
343 | IntegralRoll2 = Reading_IntegralGyroRoll2; |
343 | IntegralRoll2 = Reading_IntegralGyroRoll2; |
344 | 344 | ||
345 | if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && !Looping_Pitch && !Looping_Roll) |
345 | if((ParamSet.GlobalConfig & CFG_ROTARY_RATE_LIMITER) && !Looping_Nick && !Looping_Roll) |
346 | { |
346 | { |
347 | if(Reading_GyroPitch > 200) Reading_GyroPitch += 4 * (Reading_GyroPitch - 200); |
347 | if(Reading_GyroNick > 200) Reading_GyroNick += 4 * (Reading_GyroNick - 200); |
348 | else if(Reading_GyroPitch < -200) Reading_GyroPitch += 4 * (Reading_GyroPitch + 200); |
348 | else if(Reading_GyroNick < -200) Reading_GyroNick += 4 * (Reading_GyroNick + 200); |
349 | if(Reading_GyroRoll > 200) Reading_GyroRoll += 4 * (Reading_GyroRoll - 200); |
349 | if(Reading_GyroRoll > 200) Reading_GyroRoll += 4 * (Reading_GyroRoll - 200); |
Line 357... | Line 357... | ||
357 | void CalibMean(void) |
357 | void CalibMean(void) |
358 | { |
358 | { |
359 | // stop ADC to avoid changing values during calculation |
359 | // stop ADC to avoid changing values during calculation |
360 | ADC_Disable(); |
360 | ADC_Disable(); |
Line 361... | Line 361... | ||
361 | 361 | ||
362 | Reading_GyroPitch = AdValueGyrPitch; |
362 | Reading_GyroNick = AdValueGyrNick; |
363 | Reading_GyroRoll = AdValueGyrRoll; |
363 | Reading_GyroRoll = AdValueGyrRoll; |
Line 364... | Line 364... | ||
364 | Reading_GyroYaw = AdValueGyrYaw; |
364 | Reading_GyroYaw = AdValueGyrYaw; |
365 | 365 | ||
366 | Mean_AccPitch = ACC_AMPLIFY * (int32_t)AdValueAccPitch; |
366 | Mean_AccNick = ACC_AMPLIFY * (int32_t)AdValueAccNick; |
367 | Mean_AccRoll = ACC_AMPLIFY * (int32_t)AdValueAccRoll; |
367 | Mean_AccRoll = ACC_AMPLIFY * (int32_t)AdValueAccRoll; |
368 | Mean_AccTop = (int32_t)AdValueAccTop; |
368 | Mean_AccTop = (int32_t)AdValueAccTop; |
369 | // start ADC (enables internal trigger so that the ISR in analog.c |
369 | // start ADC (enables internal trigger so that the ISR in analog.c |
Line 370... | Line 370... | ||
370 | // updates the readings once) |
370 | // updates the readings once) |
371 | ADC_Enable(); |
371 | ADC_Enable(); |
372 | 372 | ||
Line 373... | Line 373... | ||
373 | TurnOver180Pitch = (int32_t) ParamSet.AngleTurnOverPitch * 2500L; |
373 | TurnOver180Nick = (int32_t) ParamSet.AngleTurnOverNick * 2500L; |
374 | TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L; |
374 | TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L; |
Line 428... | Line 428... | ||
428 | CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4,0,255); |
428 | CHK_POTI(FCParam.UserParam4,ParamSet.UserParam4,0,255); |
429 | CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5,0,255); |
429 | CHK_POTI(FCParam.UserParam5,ParamSet.UserParam5,0,255); |
430 | CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6,0,255); |
430 | CHK_POTI(FCParam.UserParam6,ParamSet.UserParam6,0,255); |
431 | CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7,0,255); |
431 | CHK_POTI(FCParam.UserParam7,ParamSet.UserParam7,0,255); |
432 | CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8,0,255); |
432 | CHK_POTI(FCParam.UserParam8,ParamSet.UserParam8,0,255); |
433 | CHK_POTI(FCParam.ServoPitchControl,ParamSet.ServoPitchControl,0,255); |
433 | CHK_POTI(FCParam.ServoNickControl,ParamSet.ServoNickControl,0,255); |
434 | CHK_POTI(FCParam.LoopThrustLimit,ParamSet.LoopThrustLimit,0,255); |
434 | CHK_POTI(FCParam.LoopGasLimit,ParamSet.LoopGasLimit,0,255); |
435 | CHK_POTI(FCParam.Yaw_PosFeedback,ParamSet.Yaw_PosFeedback,0,255); |
435 | CHK_POTI(FCParam.Yaw_PosFeedback,ParamSet.Yaw_PosFeedback,0,255); |
436 | CHK_POTI(FCParam.Yaw_NegFeedback,ParamSet.Yaw_NegFeedback,0,255); |
436 | CHK_POTI(FCParam.Yaw_NegFeedback,ParamSet.Yaw_NegFeedback,0,255); |
437 | CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability,0,255); |
437 | CHK_POTI(FCParam.DynamicStability,ParamSet.DynamicStability,0,255); |
438 | Ki = (float) FCParam.I_Factor * FACTOR_I; |
438 | Ki = (float) FCParam.I_Factor * FACTOR_I; |
439 | } |
439 | } |
Line 442... | Line 442... | ||
442 | 442 | ||
443 | void SetCompassCalState(void) |
443 | void SetCompassCalState(void) |
444 | { |
444 | { |
Line 445... | Line 445... | ||
445 | static uint8_t stick = 1; |
445 | static uint8_t stick = 1; |
446 | 446 | ||
447 | // if pitch is centered or top set stick to zero |
447 | // if nick is centered or top set stick to zero |
448 | if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -20) stick = 0; |
448 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -20) stick = 0; |
449 | // if pitch is down trigger to next cal state |
449 | // if nick is down trigger to next cal state |
450 | if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -70) && !stick) |
450 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) && !stick) |
451 | { |
451 | { |
452 | stick = 1; |
452 | stick = 1; |
453 | CompassCalState++; |
453 | CompassCalState++; |
Line 462... | Line 462... | ||
462 | /* MotorControl */ |
462 | /* MotorControl */ |
463 | /************************************************************************/ |
463 | /************************************************************************/ |
464 | void MotorControl(void) |
464 | void MotorControl(void) |
465 | { |
465 | { |
466 | int16_t MotorValue, pd_result, h, tmp_int; |
466 | int16_t MotorValue, pd_result, h, tmp_int; |
467 | int16_t YawMixFraction, ThrustMixFraction; |
467 | int16_t YawMixFraction, GasMixFraction; |
468 | static int32_t SumPitch = 0, SumRoll = 0; |
468 | static int32_t SumNick = 0, SumRoll = 0; |
469 | static int32_t SetPointYaw = 0; |
469 | static int32_t SetPointYaw = 0; |
470 | static int32_t IntegralErrorPitch = 0; |
470 | static int32_t IntegralErrorNick = 0; |
471 | static int32_t IntegralErrorRoll = 0; |
471 | static int32_t IntegralErrorRoll = 0; |
472 | static uint16_t RcLostTimer; |
472 | static uint16_t RcLostTimer; |
473 | static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0; |
473 | static uint8_t delay_neutral = 0, delay_startmotors = 0, delay_stopmotors = 0; |
474 | static uint8_t HeightControlActive = 0; |
474 | static uint8_t HeightControlActive = 0; |
475 | static int16_t HeightControlThrust = 0; |
475 | static int16_t HeightControlGas = 0; |
476 | static int8_t TimerDebugOut = 0; |
476 | static int8_t TimerDebugOut = 0; |
477 | static uint16_t UpdateCompassCourse = 0; |
477 | static uint16_t UpdateCompassCourse = 0; |
478 | static int32_t CorrectionPitch, CorrectionRoll; |
478 | static int32_t CorrectionNick, CorrectionRoll; |
Line 479... | Line 479... | ||
479 | 479 | ||
480 | Mean(); |
480 | Mean(); |
Line 481... | Line 481... | ||
481 | GRN_ON; |
481 | GRN_ON; |
482 | 482 | ||
483 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
483 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
484 | // determine thrust value |
484 | // determine gas value |
485 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
485 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
486 | ThrustMixFraction = StickThrust; |
486 | GasMixFraction = StickGas; |
487 | if(ThrustMixFraction < ParamSet.Trust_Min + 10) ThrustMixFraction = ParamSet.Trust_Min + 10; |
487 | if(GasMixFraction < ParamSet.Gas_Min + 10) GasMixFraction = ParamSet.Gas_Min + 10; |
488 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
488 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
489 | // RC-signal is bad |
489 | // RC-signal is bad |
490 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
490 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
Line 505... | Line 505... | ||
505 | EmergencyLanding = 0; // emergency landing is over |
505 | EmergencyLanding = 0; // emergency landing is over |
506 | } |
506 | } |
507 | ROT_ON; // set red led |
507 | ROT_ON; // set red led |
508 | if(Model_Is_Flying > 1000) // wahrscheinlich in der Luft --> langsam absenken |
508 | if(Model_Is_Flying > 1000) // wahrscheinlich in der Luft --> langsam absenken |
509 | { |
509 | { |
510 | ThrustMixFraction = ParamSet.EmergencyThrust; // set emergency thrust |
510 | GasMixFraction = ParamSet.EmergencyGas; // set emergency gas |
511 | EmergencyLanding = 1; // enable emergency landing |
511 | EmergencyLanding = 1; // enable emergency landing |
512 | // set neutral rc inputs |
512 | // set neutral rc inputs |
513 | PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] = 0; |
513 | PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
514 | PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
514 | PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
515 | PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
515 | PPM_diff[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
516 | PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] = 0; |
516 | PPM_in[ParamSet.ChannelAssignment[CH_NICK]] = 0; |
517 | PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
517 | PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] = 0; |
518 | PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
518 | PPM_in[ParamSet.ChannelAssignment[CH_YAW]] = 0; |
519 | } |
519 | } |
520 | else MotorsOn = 0; // switch of all motors |
520 | else MotorsOn = 0; // switch of all motors |
521 | } // eof RC_Quality < 120 |
521 | } // eof RC_Quality < 120 |
Line 525... | Line 525... | ||
525 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
525 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
526 | if(RC_Quality > 140) |
526 | if(RC_Quality > 140) |
527 | { |
527 | { |
528 | EmergencyLanding = 0; // switch off emergency landing if RC-signal is okay |
528 | EmergencyLanding = 0; // switch off emergency landing if RC-signal is okay |
529 | // reset emergency timer |
529 | // reset emergency timer |
530 | RcLostTimer = ParamSet.EmergencyThrustDuration * 50; |
530 | RcLostTimer = ParamSet.EmergencyGasDuration * 50; |
531 | if(ThrustMixFraction > 40) |
531 | if(GasMixFraction > 40) |
532 | { |
532 | { |
533 | if(Model_Is_Flying < 0xFFFF) Model_Is_Flying++; |
533 | if(Model_Is_Flying < 0xFFFF) Model_Is_Flying++; |
534 | } |
534 | } |
535 | if(Model_Is_Flying < 256) |
535 | if(Model_Is_Flying < 256) |
536 | { |
536 | { |
537 | SumPitch = 0; |
537 | SumNick = 0; |
538 | SumRoll = 0; |
538 | SumRoll = 0; |
539 | StickYaw = 0; |
539 | StickYaw = 0; |
540 | if(Model_Is_Flying == 250) UpdateCompassCourse = 1; |
540 | if(Model_Is_Flying == 250) UpdateCompassCourse = 1; |
541 | } |
541 | } |
Line 558... | Line 558... | ||
558 | if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255; |
558 | if(Poti5 < 0) Poti5 = 0; else if(Poti5 > 255) Poti5 = 255; |
559 | if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255; |
559 | if(Poti6 < 0) Poti6 = 0; else if(Poti6 > 255) Poti6 = 255; |
560 | if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255; |
560 | if(Poti7 < 0) Poti7 = 0; else if(Poti7 > 255) Poti7 = 255; |
561 | if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255; |
561 | if(Poti8 < 0) Poti8 = 0; else if(Poti8 > 255) Poti8 = 255; |
Line 562... | Line 562... | ||
562 | 562 | ||
563 | // if motors are off and the thrust stick is in the upper position |
563 | // if motors are off and the gas stick is in the upper position |
564 | if((PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] > 80) && MotorsOn == 0) |
564 | if((PPM_in[ParamSet.ChannelAssignment[CH_GAS]] > 80) && MotorsOn == 0) |
565 | { |
565 | { |
566 | // and if the yaw stick is in the leftmost position |
566 | // and if the yaw stick is in the leftmost position |
567 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
567 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] > 75) |
568 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
568 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
569 | // calibrate the neutral readings of all attitude sensors |
569 | // calibrate the neutral readings of all attitude sensors |
570 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
570 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
571 | { |
571 | { |
572 | // thrust/yaw joystick is top left |
572 | // gas/yaw joystick is top left |
573 | // _________ |
573 | // _________ |
574 | // |x | |
574 | // |x | |
575 | // | | |
575 | // | | |
576 | // | | |
576 | // | | |
Line 580... | Line 580... | ||
580 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
580 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
581 | { |
581 | { |
582 | delay_neutral = 0; |
582 | delay_neutral = 0; |
583 | GRN_OFF; |
583 | GRN_OFF; |
584 | Model_Is_Flying = 0; |
584 | Model_Is_Flying = 0; |
585 | // check roll/pitch stick position |
585 | // check roll/nick stick position |
586 | // if pitch stick is top or roll stick is left or right --> change parameter setting |
586 | // if nick stick is top or roll stick is left or right --> change parameter setting |
587 | // according to roll/pitch stick position |
587 | // according to roll/nick stick position |
588 | if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70) |
588 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70 || abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) > 70) |
589 | { |
589 | { |
590 | uint8_t setting = 1; // default |
590 | uint8_t setting = 1; // default |
591 | // pitch/roll joystick |
591 | // nick/roll joystick |
592 | // _________ |
592 | // _________ |
593 | // |2 3 4| |
593 | // |2 3 4| |
594 | // | | |
594 | // | | |
595 | // |1 5| |
595 | // |1 5| |
596 | // | | |
596 | // | | |
597 | // | | |
597 | // | | |
598 | // ¯¯¯¯¯¯¯¯¯ |
598 | // ¯¯¯¯¯¯¯¯¯ |
599 | // roll stick leftmost and pitch stick centered --> setting 1 |
599 | // roll stick leftmost and nick stick centered --> setting 1 |
600 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 1; |
600 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 1; |
601 | // roll stick leftmost and pitch stick topmost --> setting 2 |
601 | // roll stick leftmost and nick stick topmost --> setting 2 |
602 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 2; |
602 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 2; |
603 | // roll stick centered an pitch stick topmost --> setting 3 |
603 | // roll stick centered an nick stick topmost --> setting 3 |
604 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 3; |
604 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] < 70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 3; |
605 | // roll stick rightmost and pitch stick topmost --> setting 4 |
605 | // roll stick rightmost and nick stick topmost --> setting 4 |
606 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > 70) setting = 4; |
606 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > 70) setting = 4; |
607 | // roll stick rightmost and pitch stick centered --> setting 5 |
607 | // roll stick rightmost and nick stick centered --> setting 5 |
608 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < 70) setting = 5; |
608 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] <-70 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < 70) setting = 5; |
609 | // update active parameter set in eeprom |
609 | // update active parameter set in eeprom |
610 | SetActiveParamSet(setting); |
610 | SetActiveParamSet(setting); |
611 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
611 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
612 | SetNeutral(); |
612 | SetNeutral(); |
613 | Beep(GetActiveParamSet()); |
613 | Beep(GetActiveParamSet()); |
614 | } |
614 | } |
615 | else |
615 | else |
616 | { |
616 | { |
617 | if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE)) |
617 | if((ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE)) |
618 | { |
618 | { |
619 | // if roll stick is centered and pitch stick is down |
619 | // if roll stick is centered and nick stick is down |
620 | if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 20 && PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -70) |
620 | if (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < 20 && PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -70) |
621 | { |
621 | { |
622 | // pitch/roll joystick |
622 | // nick/roll joystick |
623 | // _________ |
623 | // _________ |
624 | // | | |
624 | // | | |
625 | // | | |
625 | // | | |
626 | // | | |
626 | // | | |
627 | // | | |
627 | // | | |
Line 629... | Line 629... | ||
629 | // ¯¯¯¯¯¯¯¯¯ |
629 | // ¯¯¯¯¯¯¯¯¯ |
630 | // enable calibration state of compass |
630 | // enable calibration state of compass |
631 | CompassCalState = 1; |
631 | CompassCalState = 1; |
632 | BeepTime = 1000; |
632 | BeepTime = 1000; |
633 | } |
633 | } |
634 | else // pitch and roll are centered |
634 | else // nick and roll are centered |
635 | { |
635 | { |
636 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
636 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
637 | SetNeutral(); |
637 | SetNeutral(); |
638 | Beep(GetActiveParamSet()); |
638 | Beep(GetActiveParamSet()); |
639 | } |
639 | } |
640 | } |
640 | } |
641 | else // pitch and roll are centered |
641 | else // nick and roll are centered |
642 | { |
642 | { |
643 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
643 | ParamSet_ReadFromEEProm(GetActiveParamSet()); |
644 | SetNeutral(); |
644 | SetNeutral(); |
645 | Beep(GetActiveParamSet()); |
645 | Beep(GetActiveParamSet()); |
646 | } |
646 | } |
Line 653... | Line 653... | ||
653 | { |
653 | { |
654 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
654 | if(++delay_neutral > 200) // not immediately (wait 200 loops = 200 * 2ms = 0.4 s) |
655 | { |
655 | { |
656 | delay_neutral = 0; |
656 | delay_neutral = 0; |
657 | GRN_OFF; |
657 | GRN_OFF; |
658 | SetParamWord(PID_ACC_PITCH, 0xFFFF); // make value invalid |
658 | SetParamWord(PID_ACC_NICK, 0xFFFF); // make value invalid |
659 | Model_Is_Flying = 0; |
659 | Model_Is_Flying = 0; |
660 | SetNeutral(); |
660 | SetNeutral(); |
661 | // Save ACC neutral settings to eeprom |
661 | // Save ACC neutral settings to eeprom |
662 | SetParamWord(PID_ACC_PITCH, (uint16_t)NeutralAccX); |
662 | SetParamWord(PID_ACC_NICK, (uint16_t)NeutralAccX); |
663 | SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY); |
663 | SetParamWord(PID_ACC_ROLL, (uint16_t)NeutralAccY); |
664 | SetParamWord(PID_ACC_Z, (uint16_t)NeutralAccZ); |
664 | SetParamWord(PID_ACC_Z, (uint16_t)NeutralAccZ); |
665 | Beep(GetActiveParamSet()); |
665 | Beep(GetActiveParamSet()); |
666 | } |
666 | } |
667 | } |
667 | } |
668 | else delay_neutral = 0; |
668 | else delay_neutral = 0; |
669 | } |
669 | } |
670 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
670 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
671 | // thrust stick is down |
671 | // gas stick is down |
672 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
672 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
673 | if(PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] < -85) |
673 | if(PPM_in[ParamSet.ChannelAssignment[CH_GAS]] < -85) |
674 | { |
674 | { |
675 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
675 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
676 | // and yaw stick is rightmost --> start motors |
676 | // and yaw stick is rightmost --> start motors |
677 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
677 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
678 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
678 | if(PPM_in[ParamSet.ChannelAssignment[CH_YAW]] < -75) |
Line 682... | Line 682... | ||
682 | delay_startmotors = 200; // do not repeat if once executed |
682 | delay_startmotors = 200; // do not repeat if once executed |
683 | Model_Is_Flying = 1; |
683 | Model_Is_Flying = 1; |
684 | MotorsOn = 1; |
684 | MotorsOn = 1; |
685 | SetPointYaw = 0; |
685 | SetPointYaw = 0; |
686 | Reading_IntegralGyroYaw = 0; |
686 | Reading_IntegralGyroYaw = 0; |
687 | Reading_IntegralGyroPitch = 0; |
687 | Reading_IntegralGyroNick = 0; |
688 | Reading_IntegralGyroRoll = 0; |
688 | Reading_IntegralGyroRoll = 0; |
689 | Reading_IntegralGyroPitch2 = IntegralPitch; |
689 | Reading_IntegralGyroNick2 = IntegralNick; |
690 | Reading_IntegralGyroRoll2 = IntegralRoll; |
690 | Reading_IntegralGyroRoll2 = IntegralRoll; |
691 | SumPitch = 0; |
691 | SumNick = 0; |
692 | SumRoll = 0; |
692 | SumRoll = 0; |
693 | #if defined (USE_KILLAGREG) || defined (USE_MK3MAG) |
693 | #if defined (USE_KILLAGREG) || defined (USE_MK3MAG) |
694 | if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE) |
694 | if(ParamSet.GlobalConfig & CFG_GPS_ACTIVE) |
695 | { |
695 | { |
696 | GPS_SetHomePosition(); |
696 | GPS_SetHomePosition(); |
Line 727... | Line 727... | ||
727 | if(!NewPpmData-- || EmergencyLanding) // NewData = 0 means new data from RC |
727 | if(!NewPpmData-- || EmergencyLanding) // NewData = 0 means new data from RC |
728 | { |
728 | { |
729 | int tmp_int; |
729 | int tmp_int; |
730 | ParameterMapping(); // remapping params (online poti replacement) |
730 | ParameterMapping(); // remapping params (online poti replacement) |
731 | // calculate Stick inputs by rc channels (P) and changing of rc channels (D) |
731 | // calculate Stick inputs by rc channels (P) and changing of rc channels (D) |
732 | StickPitch = (StickPitch * 3 + PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_P) / 4; |
732 | StickNick = (StickNick * 3 + PPM_in[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_P) / 4; |
733 | StickPitch += PPM_diff[ParamSet.ChannelAssignment[CH_PITCH]] * ParamSet.Stick_D; |
733 | StickNick += PPM_diff[ParamSet.ChannelAssignment[CH_NICK]] * ParamSet.Stick_D; |
734 | StickPitch -= (GPS_Pitch); |
734 | StickNick -= (GPS_Nick); |
Line 735... | Line 735... | ||
735 | 735 | ||
736 | StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4; |
736 | StickRoll = (StickRoll * 3 + PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_P) / 4; |
737 | StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D; |
737 | StickRoll += PPM_diff[ParamSet.ChannelAssignment[CH_ROLL]] * ParamSet.Stick_D; |
Line 738... | Line 738... | ||
738 | StickRoll -= (GPS_Roll); |
738 | StickRoll -= (GPS_Roll); |
739 | 739 | ||
740 | // direct mapping of yaw and thrust |
740 | // direct mapping of yaw and gas |
Line 741... | Line 741... | ||
741 | StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]]; |
741 | StickYaw = -PPM_in[ParamSet.ChannelAssignment[CH_YAW]]; |
742 | StickThrust = PPM_in[ParamSet.ChannelAssignment[CH_THRUST]] + 120;// shift to positive numbers |
742 | StickGas = PPM_in[ParamSet.ChannelAssignment[CH_GAS]] + 120;// shift to positive numbers |
743 | 743 | ||
Line 752... | Line 752... | ||
752 | #define KEY_VALUE (FCParam.UserParam8 * 4) // step width |
752 | #define KEY_VALUE (FCParam.UserParam8 * 4) // step width |
753 | if(DubWiseKeys[1]) BeepTime = 10; |
753 | if(DubWiseKeys[1]) BeepTime = 10; |
754 | if(DubWiseKeys[1] & DUB_KEY_UP) tmp_int = KEY_VALUE; |
754 | if(DubWiseKeys[1] & DUB_KEY_UP) tmp_int = KEY_VALUE; |
755 | else if(DubWiseKeys[1] & DUB_KEY_DOWN) tmp_int = -KEY_VALUE; |
755 | else if(DubWiseKeys[1] & DUB_KEY_DOWN) tmp_int = -KEY_VALUE; |
756 | else tmp_int = 0; |
756 | else tmp_int = 0; |
757 | ExternStickPitch = (ExternStickPitch * 7 + tmp_int) / 8; |
757 | ExternStickNick = (ExternStickNick * 7 + tmp_int) / 8; |
758 | if(DubWiseKeys[1] & DUB_KEY_LEFT) tmp_int = KEY_VALUE; |
758 | if(DubWiseKeys[1] & DUB_KEY_LEFT) tmp_int = KEY_VALUE; |
759 | else if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; |
759 | else if(DubWiseKeys[1] & DUB_KEY_RIGHT) tmp_int = -KEY_VALUE; |
760 | else tmp_int = 0; |
760 | else tmp_int = 0; |
761 | ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8; |
761 | ExternStickRoll = (ExternStickRoll * 7 + tmp_int) / 8; |
Line 762... | Line 762... | ||
762 | 762 | ||
763 | if(DubWiseKeys[0] & 8) ExternStickYaw = 50;else |
763 | if(DubWiseKeys[0] & 8) ExternStickYaw = 50;else |
764 | if(DubWiseKeys[0] & 4) ExternStickYaw =-50;else ExternStickYaw = 0; |
764 | if(DubWiseKeys[0] & 4) ExternStickYaw =-50;else ExternStickYaw = 0; |
765 | if(DubWiseKeys[0] & 2) ExternHeightValue++; |
765 | if(DubWiseKeys[0] & 2) ExternHeightValue++; |
Line 766... | Line 766... | ||
766 | if(DubWiseKeys[0] & 16) ExternHeightValue--; |
766 | if(DubWiseKeys[0] & 16) ExternHeightValue--; |
767 | 767 | ||
768 | StickPitch += (STICK_GAIN * ExternStickPitch) / 8; |
768 | StickNick += (STICK_GAIN * ExternStickNick) / 8; |
Line 769... | Line 769... | ||
769 | StickRoll += (STICK_GAIN * ExternStickRoll) / 8; |
769 | StickRoll += (STICK_GAIN * ExternStickRoll) / 8; |
770 | StickYaw += (STICK_GAIN * ExternStickYaw); |
770 | StickYaw += (STICK_GAIN * ExternStickYaw); |
771 | 771 | ||
Line 772... | Line 772... | ||
772 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
772 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
773 | //+ Analog control via serial communication |
773 | //+ Analog control via serial communication |
774 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
774 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
775 | 775 | ||
776 | if(ExternControl.Config & 0x01 && FCParam.UserParam8 > 128) |
776 | if(ExternControl.Config & 0x01 && FCParam.UserParam8 > 128) |
777 | { |
777 | { |
778 | StickPitch += (int16_t) ExternControl.Pitch * (int16_t) ParamSet.Stick_P; |
778 | StickNick += (int16_t) ExternControl.Nick * (int16_t) ParamSet.Stick_P; |
779 | StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.Stick_P; |
779 | StickRoll += (int16_t) ExternControl.Roll * (int16_t) ParamSet.Stick_P; |
780 | StickYaw += ExternControl.Yaw; |
780 | StickYaw += ExternControl.Yaw; |
Line 781... | Line 781... | ||
781 | ExternHeightValue = (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain; |
781 | ExternHeightValue = (int16_t) ExternControl.Height * (int16_t)ParamSet.Height_Gain; |
782 | if(ExternControl.Thrust < StickThrust) StickThrust = ExternControl.Thrust; |
782 | if(ExternControl.Gas < StickGas) StickGas = ExternControl.Gas; |
783 | } |
783 | } |
784 | if(StickThrust < 0) StickThrust = 0; |
784 | if(StickGas < 0) StickGas = 0; |
785 | 785 | ||
Line 786... | Line 786... | ||
786 | // disable I part of gyro control feedback |
786 | // disable I part of gyro control feedback |
Line 787... | Line 787... | ||
787 | if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) Gyro_I_Factor = 0; |
787 | if(ParamSet.GlobalConfig & CFG_HEADING_HOLD) Gyro_I_Factor = 0; |
788 | // avoid negative scaling factors |
788 | // avoid negative scaling factors |
789 | if(Gyro_P_Factor < 0) Gyro_P_Factor = 0; |
789 | if(Gyro_P_Factor < 0) Gyro_P_Factor = 0; |
790 | if(Gyro_I_Factor < 0) Gyro_I_Factor = 0; |
790 | if(Gyro_I_Factor < 0) Gyro_I_Factor = 0; |
Line 791... | Line 791... | ||
791 | 791 | ||
792 | 792 | ||
Line 816... | Line 816... | ||
816 | { |
816 | { |
817 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0; |
817 | if(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Right = 0; |
818 | } |
818 | } |
819 | } |
819 | } |
Line 820... | Line 820... | ||
820 | 820 | ||
821 | if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1; |
821 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_UP) Looping_Top = 1; |
822 | else |
822 | else |
823 | { |
823 | { |
824 | if(Looping_Top) // Hysteresis |
824 | if(Looping_Top) // Hysteresis |
825 | { |
825 | { |
826 | if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0; |
826 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < (ParamSet.LoopThreshold - ParamSet.LoopHysteresis))) Looping_Top = 0; |
827 | } |
827 | } |
828 | } |
828 | } |
829 | if((PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1; |
829 | if((PPM_in[ParamSet.ChannelAssignment[CH_NICK]] < -ParamSet.LoopThreshold) && ParamSet.LoopConfig & CFG_LOOP_DOWN) Looping_Down = 1; |
830 | else |
830 | else |
831 | { |
831 | { |
832 | if(Looping_Down) // Hysteresis |
832 | if(Looping_Down) // Hysteresis |
833 | { |
833 | { |
834 | if(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0; |
834 | if(PPM_in[ParamSet.ChannelAssignment[CH_NICK]] > -(ParamSet.LoopThreshold - ParamSet.LoopHysteresis)) Looping_Down = 0; |
835 | } |
835 | } |
Line 836... | Line 836... | ||
836 | } |
836 | } |
837 | 837 | ||
838 | if(Looping_Left || Looping_Right) Looping_Roll = 1; else Looping_Roll = 0; |
838 | if(Looping_Left || Looping_Right) Looping_Roll = 1; else Looping_Roll = 0; |
Line 839... | Line 839... | ||
839 | if(Looping_Top || Looping_Down) {Looping_Pitch = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Pitch = 0; |
839 | if(Looping_Top || Looping_Down) {Looping_Nick = 1; Looping_Roll = 0; Looping_Left = 0; Looping_Right = 0;} else Looping_Nick = 0; |
840 | } // End of new RC-Values or Emergency Landing |
840 | } // End of new RC-Values or Emergency Landing |
841 | 841 | ||
842 | 842 | ||
843 | if(Looping_Roll) BeepTime = 100; |
843 | if(Looping_Roll) BeepTime = 100; |
Line 844... | Line 844... | ||
844 | if(Looping_Roll || Looping_Pitch) |
844 | if(Looping_Roll || Looping_Nick) |
845 | { |
845 | { |
Line 859... | Line 859... | ||
859 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
859 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
860 | // set all inputs to save values |
860 | // set all inputs to save values |
861 | if(EmergencyLanding) |
861 | if(EmergencyLanding) |
862 | { |
862 | { |
863 | StickYaw = 0; |
863 | StickYaw = 0; |
864 | StickPitch = 0; |
864 | StickNick = 0; |
865 | StickRoll = 0; |
865 | StickRoll = 0; |
866 | Gyro_P_Factor = (float) 100 / (256.0 / STICK_GAIN); |
866 | Gyro_P_Factor = (float) 100 / (256.0 / STICK_GAIN); |
867 | Gyro_I_Factor = (float) 120 / (44000 / STICK_GAIN); |
867 | Gyro_I_Factor = (float) 120 / (44000 / STICK_GAIN); |
868 | Looping_Roll = 0; |
868 | Looping_Roll = 0; |
869 | Looping_Pitch = 0; |
869 | Looping_Nick = 0; |
870 | MaxStickPitch = 0; |
870 | MaxStickNick = 0; |
871 | MaxStickRoll = 0; |
871 | MaxStickRoll = 0; |
872 | } |
872 | } |
Line 873... | Line 873... | ||
873 | 873 | ||
874 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
874 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
875 | // Trim Gyro-Integrals to ACC-Signals |
875 | // Trim Gyro-Integrals to ACC-Signals |
Line 876... | Line 876... | ||
876 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
876 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
877 | 877 | ||
878 | #define BALANCE_NUMBER 256L |
878 | #define BALANCE_NUMBER 256L |
879 | // sum for averaging |
879 | // sum for averaging |
Line 880... | Line 880... | ||
880 | MeanIntegralPitch += IntegralPitch; |
880 | MeanIntegralNick += IntegralNick; |
881 | MeanIntegralRoll += IntegralRoll; |
881 | MeanIntegralRoll += IntegralRoll; |
882 | 882 | ||
883 | if(Looping_Pitch || Looping_Roll) // if looping in any direction |
883 | if(Looping_Nick || Looping_Roll) // if looping in any direction |
Line 884... | Line 884... | ||
884 | { |
884 | { |
885 | // reset averaging for acc and gyro integral as well as gyro integral acc correction |
885 | // reset averaging for acc and gyro integral as well as gyro integral acc correction |
Line 886... | Line 886... | ||
886 | MeasurementCounter = 0; |
886 | MeasurementCounter = 0; |
887 | 887 | ||
Line 888... | Line 888... | ||
888 | IntegralAccPitch = 0; |
888 | IntegralAccNick = 0; |
889 | IntegralAccRoll = 0; |
889 | IntegralAccRoll = 0; |
Line 890... | Line 890... | ||
890 | 890 | ||
891 | MeanIntegralPitch = 0; |
891 | MeanIntegralNick = 0; |
892 | MeanIntegralRoll = 0; |
892 | MeanIntegralRoll = 0; |
Line 893... | Line 893... | ||
893 | 893 | ||
894 | Reading_IntegralGyroPitch2 = Reading_IntegralGyroPitch; |
894 | Reading_IntegralGyroNick2 = Reading_IntegralGyroNick; |
895 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
895 | Reading_IntegralGyroRoll2 = Reading_IntegralGyroRoll; |
896 | 896 | ||
897 | AttitudeCorrectionPitch = 0; |
897 | AttitudeCorrectionNick = 0; |
898 | AttitudeCorrectionRoll = 0; |
898 | AttitudeCorrectionRoll = 0; |
899 | } |
899 | } |
900 | 900 | ||
901 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
901 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
Line 902... | Line 902... | ||
902 | if(!Looping_Pitch && !Looping_Roll) // if not lopping in any direction |
902 | if(!Looping_Nick && !Looping_Roll) // if not lopping in any direction |
903 | { |
903 | { |
904 | int32_t tmp_long, tmp_long2; |
904 | int32_t tmp_long, tmp_long2; |
905 | // determine the deviation of gyro integral from averaged acceleration sensor |
905 | // determine the deviation of gyro integral from averaged acceleration sensor |
906 | tmp_long = (int32_t)(IntegralPitch / ParamSet.GyroAccFactor - (int32_t)Mean_AccPitch); |
906 | tmp_long = (int32_t)(IntegralNick / ParamSet.GyroAccFactor - (int32_t)Mean_AccNick); |
907 | tmp_long /= 16; |
907 | tmp_long /= 16; |
Line 924... | Line 924... | ||
924 | if(tmp_long > BALANCE) tmp_long = BALANCE; |
924 | if(tmp_long > BALANCE) tmp_long = BALANCE; |
925 | if(tmp_long < -BALANCE) tmp_long =-BALANCE; |
925 | if(tmp_long < -BALANCE) tmp_long =-BALANCE; |
926 | if(tmp_long2 > BALANCE) tmp_long2 = BALANCE; |
926 | if(tmp_long2 > BALANCE) tmp_long2 = BALANCE; |
927 | if(tmp_long2 <-BALANCE) tmp_long2 =-BALANCE; |
927 | if(tmp_long2 <-BALANCE) tmp_long2 =-BALANCE; |
928 | // correct current readings |
928 | // correct current readings |
929 | Reading_IntegralGyroPitch -= tmp_long; |
929 | Reading_IntegralGyroNick -= tmp_long; |
930 | Reading_IntegralGyroRoll -= tmp_long2; |
930 | Reading_IntegralGyroRoll -= tmp_long2; |
931 | } |
931 | } |
932 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
932 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
933 | // MeasurementCounter is incremented in the isr of analog.c |
933 | // MeasurementCounter is incremented in the isr of analog.c |
934 | if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached |
934 | if(MeasurementCounter >= BALANCE_NUMBER) // averaging number has reached |
935 | { |
935 | { |
936 | static int16_t cnt = 0; |
936 | static int16_t cnt = 0; |
937 | static int8_t last_n_p, last_n_n, last_r_p, last_r_n; |
937 | static int8_t last_n_p, last_n_n, last_r_p, last_r_n; |
938 | static int32_t MeanIntegralPitch_old, MeanIntegralRoll_old; |
938 | static int32_t MeanIntegralNick_old, MeanIntegralRoll_old; |
Line 939... | Line 939... | ||
939 | 939 | ||
940 | // if not lopping in any direction (this should be alwais the case, |
940 | // if not lopping in any direction (this should be alwais the case, |
941 | // because the Measurement counter is reset to 0 if looping in any direction is active.) |
941 | // because the Measurement counter is reset to 0 if looping in any direction is active.) |
942 | if(!Looping_Pitch && !Looping_Roll && !FunnelCourse) |
942 | if(!Looping_Nick && !Looping_Roll && !FunnelCourse) |
943 | { |
943 | { |
944 | // Calculate mean value of the gyro integrals |
944 | // Calculate mean value of the gyro integrals |
945 | MeanIntegralPitch /= BALANCE_NUMBER; |
945 | MeanIntegralNick /= BALANCE_NUMBER; |
Line 946... | Line 946... | ||
946 | MeanIntegralRoll /= BALANCE_NUMBER; |
946 | MeanIntegralRoll /= BALANCE_NUMBER; |
947 | 947 | ||
948 | // Calculate mean of the acceleration values |
948 | // Calculate mean of the acceleration values |
Line 949... | Line 949... | ||
949 | IntegralAccPitch = (ParamSet.GyroAccFactor * IntegralAccPitch) / BALANCE_NUMBER; |
949 | IntegralAccNick = (ParamSet.GyroAccFactor * IntegralAccNick) / BALANCE_NUMBER; |
950 | IntegralAccRoll = (ParamSet.GyroAccFactor * IntegralAccRoll ) / BALANCE_NUMBER; |
950 | IntegralAccRoll = (ParamSet.GyroAccFactor * IntegralAccRoll ) / BALANCE_NUMBER; |
951 | 951 | ||
952 | // Pitch ++++++++++++++++++++++++++++++++++++++++++++++++ |
952 | // Nick ++++++++++++++++++++++++++++++++++++++++++++++++ |
953 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
953 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
954 | IntegralErrorPitch = (int32_t)(MeanIntegralPitch - (int32_t)IntegralAccPitch); |
954 | IntegralErrorNick = (int32_t)(MeanIntegralNick - (int32_t)IntegralAccNick); |
955 | CorrectionPitch = IntegralErrorPitch / ParamSet.GyroAccTrim; |
955 | CorrectionNick = IntegralErrorNick / ParamSet.GyroAccTrim; |
956 | AttitudeCorrectionPitch = CorrectionPitch / BALANCE_NUMBER; |
956 | AttitudeCorrectionNick = CorrectionNick / BALANCE_NUMBER; |
957 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
957 | // Roll ++++++++++++++++++++++++++++++++++++++++++++++++ |
958 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
958 | // Calculate deviation of the averaged gyro integral and the averaged acceleration integral |
Line 959... | Line 959... | ||
959 | IntegralErrorRoll = (int32_t)(MeanIntegralRoll - (int32_t)IntegralAccRoll); |
959 | IntegralErrorRoll = (int32_t)(MeanIntegralRoll - (int32_t)IntegralAccRoll); |
960 | CorrectionRoll = IntegralErrorRoll / ParamSet.GyroAccTrim; |
960 | CorrectionRoll = IntegralErrorRoll / ParamSet.GyroAccTrim; |
961 | AttitudeCorrectionRoll = CorrectionRoll / BALANCE_NUMBER; |
961 | AttitudeCorrectionRoll = CorrectionRoll / BALANCE_NUMBER; |
962 | 962 | ||
963 | if((MaxStickPitch > 32) || (MaxStickRoll > 32) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25)) |
963 | if((MaxStickNick > 32) || (MaxStickRoll > 32) || (abs(PPM_in[ParamSet.ChannelAssignment[CH_YAW]]) > 25)) |
Line 964... | Line 964... | ||
964 | { |
964 | { |
965 | AttitudeCorrectionPitch /= 2; |
965 | AttitudeCorrectionNick /= 2; |
966 | AttitudeCorrectionRoll /= 2; |
966 | AttitudeCorrectionRoll /= 2; |
967 | } |
967 | } |
968 | 968 | ||
969 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
969 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
970 | // Gyro-Drift ermitteln |
970 | // Gyro-Drift ermitteln |
971 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
971 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
972 | // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor) |
972 | // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor) |
Line 973... | Line 973... | ||
973 | IntegralErrorPitch = IntegralPitch2 - IntegralPitch; |
973 | IntegralErrorNick = IntegralNick2 - IntegralNick; |
974 | Reading_IntegralGyroPitch2 -= IntegralErrorPitch; |
974 | Reading_IntegralGyroNick2 -= IntegralErrorNick; |
975 | // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor) |
975 | // deviation of gyro nick integral (IntegralNick is corrected by averaged acc sensor) |
976 | IntegralErrorRoll = IntegralRoll2 - IntegralRoll; |
976 | IntegralErrorRoll = IntegralRoll2 - IntegralRoll; |
977 | Reading_IntegralGyroRoll2 -= IntegralErrorRoll; |
977 | Reading_IntegralGyroRoll2 -= IntegralErrorRoll; |
978 | 978 | ||
979 | if(YawGyroDrift > BALANCE_NUMBER/2) AdNeutralYaw++; |
979 | if(YawGyroDrift > BALANCE_NUMBER/2) AdNeutralYaw++; |
980 | if(YawGyroDrift < -BALANCE_NUMBER/2) AdNeutralYaw--; |
980 | if(YawGyroDrift < -BALANCE_NUMBER/2) AdNeutralYaw--; |
981 | YawGyroDrift = 0; |
981 | YawGyroDrift = 0; |
982 | /* |
982 | /* |
983 | DebugOut.Analog[17] = IntegralAccPitch / 26; |
983 | DebugOut.Analog[17] = IntegralAccNick / 26; |
984 | DebugOut.Analog[18] = IntegralAccRoll / 26; |
984 | DebugOut.Analog[18] = IntegralAccRoll / 26; |
985 | DebugOut.Analog[19] = IntegralErrorPitch;// / 26; |
985 | DebugOut.Analog[19] = IntegralErrorNick;// / 26; |
986 | DebugOut.Analog[20] = IntegralErrorRoll;// / 26; |
986 | DebugOut.Analog[20] = IntegralErrorRoll;// / 26; |
Line 987... | Line 987... | ||
987 | DebugOut.Analog[21] = MeanIntegralPitch / 26; |
987 | DebugOut.Analog[21] = MeanIntegralNick / 26; |
988 | DebugOut.Analog[22] = MeanIntegralRoll / 26; |
988 | DebugOut.Analog[22] = MeanIntegralRoll / 26; |
989 | //DebugOut.Analog[28] = CorrectionPitch; |
989 | //DebugOut.Analog[28] = CorrectionNick; |
990 | DebugOut.Analog[29] = CorrectionRoll; |
990 | DebugOut.Analog[29] = CorrectionRoll; |
991 | DebugOut.Analog[30] = AttitudeCorrectionRoll * 10; |
991 | DebugOut.Analog[30] = AttitudeCorrectionRoll * 10; |
992 | */ |
992 | */ |
993 | 993 | ||
994 | #define ERROR_LIMIT (BALANCE_NUMBER * 4) |
994 | #define ERROR_LIMIT (BALANCE_NUMBER * 4) |
995 | #define ERROR_LIMIT2 (BALANCE_NUMBER * 16) |
995 | #define ERROR_LIMIT2 (BALANCE_NUMBER * 16) |
996 | #define MOVEMENT_LIMIT 20000 |
996 | #define MOVEMENT_LIMIT 20000 |
997 | // Pitch +++++++++++++++++++++++++++++++++++++++++++++++++ |
997 | // Nick +++++++++++++++++++++++++++++++++++++++++++++++++ |
998 | cnt = 1;// + labs(IntegralErrorPitch) / 4096; |
998 | cnt = 1;// + labs(IntegralErrorNick) / 4096; |
999 | CorrectionPitch = 0; |
999 | CorrectionNick = 0; |
1000 | if(labs(MeanIntegralPitch_old - MeanIntegralPitch) < MOVEMENT_LIMIT) |
1000 | if(labs(MeanIntegralNick_old - MeanIntegralNick) < MOVEMENT_LIMIT) |
1001 | { |
1001 | { |
1002 | if(IntegralErrorPitch > ERROR_LIMIT2) |
1002 | if(IntegralErrorNick > ERROR_LIMIT2) |
1003 | { |
1003 | { |
1004 | if(last_n_p) |
1004 | if(last_n_p) |
1005 | { |
1005 | { |
1006 | cnt += labs(IntegralErrorPitch) / ERROR_LIMIT2; |
1006 | cnt += labs(IntegralErrorNick) / ERROR_LIMIT2; |
1007 | CorrectionPitch = IntegralErrorPitch / 8; |
1007 | CorrectionNick = IntegralErrorNick / 8; |
1008 | if(CorrectionPitch > 5000) CorrectionPitch = 5000; |
1008 | if(CorrectionNick > 5000) CorrectionNick = 5000; |
1009 | AttitudeCorrectionPitch += CorrectionPitch / BALANCE_NUMBER; |
1009 | AttitudeCorrectionNick += CorrectionNick / BALANCE_NUMBER; |
1010 | } |
1010 | } |
1011 | else last_n_p = 1; |
1011 | else last_n_p = 1; |
1012 | } |
1012 | } |
1013 | else last_n_p = 0; |
1013 | else last_n_p = 0; |
1014 | if(IntegralErrorPitch < -ERROR_LIMIT2) |
1014 | if(IntegralErrorNick < -ERROR_LIMIT2) |
1015 | { |
1015 | { |
1016 | if(last_n_n) |
1016 | if(last_n_n) |
1017 | { |
1017 | { |
1018 | cnt += labs(IntegralErrorPitch) / ERROR_LIMIT2; |
1018 | cnt += labs(IntegralErrorNick) / ERROR_LIMIT2; |
1019 | CorrectionPitch = IntegralErrorPitch / 8; |
1019 | CorrectionNick = IntegralErrorNick / 8; |
Line 1029... | Line 1029... | ||
1029 | cnt = 0; |
1029 | cnt = 0; |
1030 | BadCompassHeading = 500; |
1030 | BadCompassHeading = 500; |
1031 | } |
1031 | } |
1032 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1032 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1033 | // correct Gyro Offsets |
1033 | // correct Gyro Offsets |
1034 | if(IntegralErrorPitch > ERROR_LIMIT) AdNeutralPitch += cnt; |
1034 | if(IntegralErrorNick > ERROR_LIMIT) AdNeutralNick += cnt; |
1035 | if(IntegralErrorPitch < -ERROR_LIMIT) AdNeutralPitch -= cnt; |
1035 | if(IntegralErrorNick < -ERROR_LIMIT) AdNeutralNick -= cnt; |
Line 1036... | Line 1036... | ||
1036 | 1036 | ||
1037 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
1037 | // Roll +++++++++++++++++++++++++++++++++++++++++++++++++ |
1038 | cnt = 1;// + labs(IntegralErrorPitch) / 4096; |
1038 | cnt = 1;// + labs(IntegralErrorNick) / 4096; |
1039 | CorrectionRoll = 0; |
1039 | CorrectionRoll = 0; |
1040 | if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < MOVEMENT_LIMIT) |
1040 | if(labs(MeanIntegralRoll_old - MeanIntegralRoll) < MOVEMENT_LIMIT) |
1041 | { |
1041 | { |
1042 | if(IntegralErrorRoll > ERROR_LIMIT2) |
1042 | if(IntegralErrorRoll > ERROR_LIMIT2) |
Line 1073... | Line 1073... | ||
1073 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1073 | if(cnt > ParamSet.DriftComp) cnt = ParamSet.DriftComp; |
1074 | if(IntegralErrorRoll > ERROR_LIMIT) AdNeutralRoll += cnt; |
1074 | if(IntegralErrorRoll > ERROR_LIMIT) AdNeutralRoll += cnt; |
1075 | if(IntegralErrorRoll < -ERROR_LIMIT) AdNeutralRoll -= cnt; |
1075 | if(IntegralErrorRoll < -ERROR_LIMIT) AdNeutralRoll -= cnt; |
1076 | /* |
1076 | /* |
1077 | DebugOut.Analog[27] = CorrectionRoll; |
1077 | DebugOut.Analog[27] = CorrectionRoll; |
1078 | DebugOut.Analog[23] = AdNeutralPitch;//10*(AdNeutralPitch - StartNeutralPitch); |
1078 | DebugOut.Analog[23] = AdNeutralNick;//10*(AdNeutralNick - StartNeutralNick); |
1079 | DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll); |
1079 | DebugOut.Analog[24] = 10*(AdNeutralRoll - StartNeutralRoll); |
1080 | */ |
1080 | */ |
1081 | } |
1081 | } |
1082 | else // looping is active |
1082 | else // looping is active |
1083 | { |
1083 | { |
1084 | AttitudeCorrectionRoll = 0; |
1084 | AttitudeCorrectionRoll = 0; |
1085 | AttitudeCorrectionPitch = 0; |
1085 | AttitudeCorrectionNick = 0; |
1086 | FunnelCourse = 0; |
1086 | FunnelCourse = 0; |
1087 | } |
1087 | } |
Line 1088... | Line 1088... | ||
1088 | 1088 | ||
1089 | // if Gyro_I_Factor == 0 , for example at Heading Hold, ignore attitude correction |
1089 | // if Gyro_I_Factor == 0 , for example at Heading Hold, ignore attitude correction |
1090 | if(!Gyro_I_Factor) |
1090 | if(!Gyro_I_Factor) |
1091 | { |
1091 | { |
1092 | AttitudeCorrectionRoll = 0; |
1092 | AttitudeCorrectionRoll = 0; |
1093 | AttitudeCorrectionPitch = 0; |
1093 | AttitudeCorrectionNick = 0; |
1094 | } |
1094 | } |
1095 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1095 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1096 | MeanIntegralPitch_old = MeanIntegralPitch; |
1096 | MeanIntegralNick_old = MeanIntegralNick; |
1097 | MeanIntegralRoll_old = MeanIntegralRoll; |
1097 | MeanIntegralRoll_old = MeanIntegralRoll; |
1098 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1098 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1099 | // reset variables used for averaging |
1099 | // reset variables used for averaging |
1100 | IntegralAccPitch = 0; |
1100 | IntegralAccNick = 0; |
1101 | IntegralAccRoll = 0; |
1101 | IntegralAccRoll = 0; |
1102 | MeanIntegralPitch = 0; |
1102 | MeanIntegralNick = 0; |
1103 | MeanIntegralRoll = 0; |
1103 | MeanIntegralRoll = 0; |
1104 | MeasurementCounter = 0; |
1104 | MeasurementCounter = 0; |
Line 1152... | Line 1152... | ||
1152 | MM3_Heading(); |
1152 | MM3_Heading(); |
1153 | } |
1153 | } |
1154 | #endif |
1154 | #endif |
Line 1155... | Line 1155... | ||
1155 | 1155 | ||
1156 | // get maximum attitude angle |
1156 | // get maximum attitude angle |
1157 | w = abs(IntegralPitch/512); |
1157 | w = abs(IntegralNick/512); |
1158 | v = abs(IntegralRoll /512); |
1158 | v = abs(IntegralRoll /512); |
1159 | if(v > w) w = v; |
1159 | if(v > w) w = v; |
1160 | // update compass course |
1160 | // update compass course |
1161 | if (w < 25 && UpdateCompassCourse && !BadCompassHeading) |
1161 | if (w < 25 && UpdateCompassCourse && !BadCompassHeading) |
Line 1178... | Line 1178... | ||
1178 | BadCompassHeading--; |
1178 | BadCompassHeading--; |
1179 | } |
1179 | } |
1180 | else |
1180 | else |
1181 | { // |
1181 | { // |
1182 | YawGyroDrift += error; |
1182 | YawGyroDrift += error; |
1183 | v = 64 + (MaxStickPitch + MaxStickRoll) / 8; |
1183 | v = 64 + (MaxStickNick + MaxStickRoll) / 8; |
1184 | // calc course deviation |
1184 | // calc course deviation |
1185 | r = ((540 + (YawGyroHeading / YAW_GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180; |
1185 | r = ((540 + (YawGyroHeading / YAW_GYRO_DEG_FACTOR) - CompassCourse) % 360) - 180; |
1186 | v = (r * w) / v; // align to compass course |
1186 | v = (r * w) / v; // align to compass course |
1187 | // limit yaw rate |
1187 | // limit yaw rate |
1188 | w = 3 * FCParam.CompassYawEffect; |
1188 | w = 3 * FCParam.CompassYawEffect; |
Line 1210... | Line 1210... | ||
1210 | if(EmergencyLanding) GPS_Main(230); // enables Comming Home |
1210 | if(EmergencyLanding) GPS_Main(230); // enables Comming Home |
1211 | else GPS_Main(Poti3); // behavior controlled by Poti3 |
1211 | else GPS_Main(Poti3); // behavior controlled by Poti3 |
1212 | } |
1212 | } |
1213 | else |
1213 | else |
1214 | { |
1214 | { |
1215 | GPS_Pitch = 0; |
1215 | GPS_Nick = 0; |
1216 | GPS_Roll = 0; |
1216 | GPS_Roll = 0; |
1217 | } |
1217 | } |
1218 | #endif |
1218 | #endif |
Line 1219... | Line 1219... | ||
1219 | 1219 | ||
1220 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1220 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1221 | // Debugwerte zuordnen |
1221 | // Debugwerte zuordnen |
1222 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1222 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1223 | if(!TimerDebugOut--) |
1223 | if(!TimerDebugOut--) |
1224 | { |
1224 | { |
1225 | TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz) |
1225 | TimerDebugOut = 24; // update debug outputs every 25*2ms = 50 ms (20Hz) |
1226 | DebugOut.Analog[0] = IntegralPitch / ParamSet.GyroAccFactor; |
1226 | DebugOut.Analog[0] = IntegralNick / ParamSet.GyroAccFactor; |
1227 | DebugOut.Analog[1] = IntegralRoll / ParamSet.GyroAccFactor; |
1227 | DebugOut.Analog[1] = IntegralRoll / ParamSet.GyroAccFactor; |
1228 | DebugOut.Analog[2] = Mean_AccPitch; |
1228 | DebugOut.Analog[2] = Mean_AccNick; |
1229 | DebugOut.Analog[3] = Mean_AccRoll; |
1229 | DebugOut.Analog[3] = Mean_AccRoll; |
1230 | DebugOut.Analog[4] = Reading_GyroYaw; |
1230 | DebugOut.Analog[4] = Reading_GyroYaw; |
1231 | DebugOut.Analog[5] = ReadingHeight; |
1231 | DebugOut.Analog[5] = ReadingHeight; |
1232 | DebugOut.Analog[6] = (Reading_Integral_Top / 512); |
1232 | DebugOut.Analog[6] = (Reading_Integral_Top / 512); |
Line 1238... | Line 1238... | ||
1238 | 1238 | ||
Line 1239... | Line 1239... | ||
1239 | DebugOut.Analog[20] = ServoValue; |
1239 | DebugOut.Analog[20] = ServoValue; |
1240 | 1240 | ||
Line 1241... | Line 1241... | ||
1241 | 1241 | ||
1242 | 1242 | ||
1243 | DebugOut.Analog[30] = GPS_Pitch; |
1243 | DebugOut.Analog[30] = GPS_Nick; |
Line 1253... | Line 1253... | ||
1253 | DebugOut.Analog[22] = motor_rx[5]; |
1253 | DebugOut.Analog[22] = motor_rx[5]; |
1254 | DebugOut.Analog[23] = motor_rx[6]; |
1254 | DebugOut.Analog[23] = motor_rx[6]; |
1255 | DebugOut.Analog[24] = motor_rx[7]; |
1255 | DebugOut.Analog[24] = motor_rx[7]; |
1256 | DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7]; |
1256 | DebugOut.Analog[25] = motor_rx[4] + motor_rx[5] + motor_rx[6] + motor_rx[7]; |
Line 1257... | Line 1257... | ||
1257 | 1257 | ||
1258 | DebugOut.Analog[9] = Reading_GyroPitch; |
1258 | DebugOut.Analog[9] = Reading_GyroNick; |
1259 | DebugOut.Analog[9] = SetPointHeight; |
1259 | DebugOut.Analog[9] = SetPointHeight; |
Line 1260... | Line 1260... | ||
1260 | DebugOut.Analog[10] = Reading_IntegralGyroYaw / 128; |
1260 | DebugOut.Analog[10] = Reading_IntegralGyroYaw / 128; |
1261 | 1261 | ||
1262 | DebugOut.Analog[10] = FCParam.Gyro_I; |
1262 | DebugOut.Analog[10] = FCParam.Gyro_I; |
1263 | DebugOut.Analog[10] = ParamSet.Gyro_I; |
1263 | DebugOut.Analog[10] = ParamSet.Gyro_I; |
1264 | DebugOut.Analog[9] = CompassOffCourse; |
1264 | DebugOut.Analog[9] = CompassOffCourse; |
1265 | DebugOut.Analog[10] = ThrustMixFraction; |
1265 | DebugOut.Analog[10] = GasMixFraction; |
1266 | DebugOut.Analog[3] = HeightD * 32; |
1266 | DebugOut.Analog[3] = HeightD * 32; |
1267 | DebugOut.Analog[4] = HeightControlThrust; |
1267 | DebugOut.Analog[4] = HeightControlGas; |
Line 1268... | Line 1268... | ||
1268 | */ |
1268 | */ |
1269 | } |
1269 | } |
1270 | 1270 | ||
Line 1271... | Line 1271... | ||
1271 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1271 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1272 | // calculate control feedback from angle (gyro integral) and agular velocity (gyro signal) |
1272 | // calculate control feedback from angle (gyro integral) and agular velocity (gyro signal) |
1273 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1273 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1274 | 1274 | ||
1275 | if(Looping_Pitch) Reading_GyroPitch = Reading_GyroPitch * Gyro_P_Factor; |
1275 | if(Looping_Nick) Reading_GyroNick = Reading_GyroNick * Gyro_P_Factor; |
Line 1276... | Line 1276... | ||
1276 | else Reading_GyroPitch = IntegralPitch * Gyro_I_Factor + Reading_GyroPitch * Gyro_P_Factor; |
1276 | else Reading_GyroNick = IntegralNick * Gyro_I_Factor + Reading_GyroNick * Gyro_P_Factor; |
1277 | if(Looping_Roll) Reading_GyroRoll = Reading_GyroRoll * Gyro_P_Factor; |
1277 | if(Looping_Roll) Reading_GyroRoll = Reading_GyroRoll * Gyro_P_Factor; |
Line 1278... | Line 1278... | ||
1278 | else Reading_GyroRoll = IntegralRoll * Gyro_I_Factor + Reading_GyroRoll * Gyro_P_Factor; |
1278 | else Reading_GyroRoll = IntegralRoll * Gyro_I_Factor + Reading_GyroRoll * Gyro_P_Factor; |
1279 | Reading_GyroYaw = Reading_GyroYaw * (2 * Gyro_P_Factor) + IntegralYaw * Gyro_I_Factor / 2; |
1279 | Reading_GyroYaw = Reading_GyroYaw * (2 * Gyro_P_Factor) + IntegralYaw * Gyro_I_Factor / 2; |
1280 | 1280 | ||
1281 | DebugOut.Analog[21] = Reading_GyroPitch; |
1281 | DebugOut.Analog[21] = Reading_GyroNick; |
1282 | DebugOut.Analog[22] = Reading_GyroRoll; |
1282 | DebugOut.Analog[22] = Reading_GyroRoll; |
1283 | 1283 | ||
1284 | // limit control feedback |
1284 | // limit control feedback |
1285 | #define MAX_SENSOR (4096 * STICK_GAIN) |
1285 | #define MAX_SENSOR (4096 * STICK_GAIN) |
Line 1286... | Line 1286... | ||
1286 | if(Reading_GyroPitch > MAX_SENSOR) Reading_GyroPitch = MAX_SENSOR; |
1286 | if(Reading_GyroNick > MAX_SENSOR) Reading_GyroNick = MAX_SENSOR; |
1287 | if(Reading_GyroPitch < -MAX_SENSOR) Reading_GyroPitch = -MAX_SENSOR; |
1287 | if(Reading_GyroNick < -MAX_SENSOR) Reading_GyroNick = -MAX_SENSOR; |
1288 | if(Reading_GyroRoll > MAX_SENSOR) Reading_GyroRoll = MAX_SENSOR; |
1288 | if(Reading_GyroRoll > MAX_SENSOR) Reading_GyroRoll = MAX_SENSOR; |
1289 | if(Reading_GyroRoll < -MAX_SENSOR) Reading_GyroRoll = -MAX_SENSOR; |
1289 | if(Reading_GyroRoll < -MAX_SENSOR) Reading_GyroRoll = -MAX_SENSOR; |
Line 1290... | Line 1290... | ||
1290 | if(Reading_GyroYaw > MAX_SENSOR) Reading_GyroYaw = MAX_SENSOR; |
1290 | if(Reading_GyroYaw > MAX_SENSOR) Reading_GyroYaw = MAX_SENSOR; |
Line 1291... | Line 1291... | ||
1291 | if(Reading_GyroYaw < -MAX_SENSOR) Reading_GyroYaw = -MAX_SENSOR; |
1291 | if(Reading_GyroYaw < -MAX_SENSOR) Reading_GyroYaw = -MAX_SENSOR; |
1292 | 1292 | ||
1293 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1293 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1294 | // Height Control |
1294 | // Height Control |
Line 1316... | Line 1316... | ||
1316 | SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20; |
1316 | SetPointHeight = ((int16_t) ExternHeightValue + (int16_t) FCParam.MaxHeight) * (int16_t)ParamSet.Height_Gain - 20; |
1317 | HeightControlActive = 1; |
1317 | HeightControlActive = 1; |
1318 | } |
1318 | } |
1319 | // get current height |
1319 | // get current height |
1320 | h = ReadingHeight; |
1320 | h = ReadingHeight; |
1321 | // if current height is above the setpoint reduce thrust |
1321 | // if current height is above the setpoint reduce gas |
1322 | if((h > SetPointHeight) && HeightControlActive) |
1322 | if((h > SetPointHeight) && HeightControlActive) |
1323 | { |
1323 | { |
1324 | // ThrustMixFraction - HightDeviation * P - HeightChange * D - ACCTop * DACC |
1324 | // GasMixFraction - HightDeviation * P - HeightChange * D - ACCTop * DACC |
1325 | // height difference -> P control part |
1325 | // height difference -> P control part |
1326 | h = ((h - SetPointHeight) * (int16_t) FCParam.Height_P) / (16 / STICK_GAIN); |
1326 | h = ((h - SetPointHeight) * (int16_t) FCParam.Height_P) / (16 / STICK_GAIN); |
1327 | h = ThrustMixFraction - h; // reduce gas |
1327 | h = GasMixFraction - h; // reduce gas |
1328 | // height gradient --> D control part |
1328 | // height gradient --> D control part |
1329 | //h -= (HeightD * FCParam.Height_D) / (8 / STICK_GAIN); // D control part |
1329 | //h -= (HeightD * FCParam.Height_D) / (8 / STICK_GAIN); // D control part |
1330 | h -= (HeightD) / (8 / STICK_GAIN); // D control part |
1330 | h -= (HeightD) / (8 / STICK_GAIN); // D control part |
1331 | // acceleration sensor effect |
1331 | // acceleration sensor effect |
1332 | tmp_int = ((Reading_Integral_Top / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN); |
1332 | tmp_int = ((Reading_Integral_Top / 128) * (int32_t) FCParam.Height_ACC_Effect) / (128 / STICK_GAIN); |
1333 | if(tmp_int > 70 * STICK_GAIN) tmp_int = 70 * STICK_GAIN; |
1333 | if(tmp_int > 70 * STICK_GAIN) tmp_int = 70 * STICK_GAIN; |
1334 | else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN); |
1334 | else if(tmp_int < -(70 * STICK_GAIN)) tmp_int = -(70 * STICK_GAIN); |
1335 | h -= tmp_int; |
1335 | h -= tmp_int; |
1336 | // update height control thrust |
1336 | // update height control gas |
1337 | HeightControlThrust = (HeightControlThrust*15 + h) / 16; |
1337 | HeightControlGas = (HeightControlGas*15 + h) / 16; |
1338 | // limit thrust reduction |
1338 | // limit gas reduction |
1339 | if(HeightControlThrust < ParamSet.Height_MinThrust * STICK_GAIN) |
1339 | if(HeightControlGas < ParamSet.Height_MinGas * STICK_GAIN) |
1340 | { |
1340 | { |
1341 | if(ThrustMixFraction >= ParamSet.Height_MinThrust * STICK_GAIN) HeightControlThrust = ParamSet.Height_MinThrust * STICK_GAIN; |
1341 | if(GasMixFraction >= ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = ParamSet.Height_MinGas * STICK_GAIN; |
1342 | // allows landing also if thrust stick is reduced below min thrust on height control |
1342 | // allows landing also if gas stick is reduced below min gas on height control |
1343 | if(ThrustMixFraction < ParamSet.Height_MinThrust * STICK_GAIN) HeightControlThrust = ThrustMixFraction; |
1343 | if(GasMixFraction < ParamSet.Height_MinGas * STICK_GAIN) HeightControlGas = GasMixFraction; |
1344 | } |
1344 | } |
1345 | // limit thrust to stick setting |
1345 | // limit gas to stick setting |
1346 | if(HeightControlThrust > ThrustMixFraction) HeightControlThrust = ThrustMixFraction; |
1346 | if(HeightControlGas > GasMixFraction) HeightControlGas = GasMixFraction; |
1347 | ThrustMixFraction = HeightControlThrust; |
1347 | GasMixFraction = HeightControlGas; |
1348 | } |
1348 | } |
1349 | } |
1349 | } |
1350 | // limit thrust to parameter setting |
1350 | // limit gas to parameter setting |
1351 | if(ThrustMixFraction > (ParamSet.Trust_Max - 20) * STICK_GAIN) ThrustMixFraction = (ParamSet.Trust_Max - 20) * STICK_GAIN; |
1351 | if(GasMixFraction > (ParamSet.Gas_Max - 20) * STICK_GAIN) GasMixFraction = (ParamSet.Gas_Max - 20) * STICK_GAIN; |
1352 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1352 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1353 | // + Mixer and PI-Controller |
1353 | // + Mixer and PI-Controller |
1354 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1354 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1355 | DebugOut.Analog[7] = ThrustMixFraction; |
1355 | DebugOut.Analog[7] = GasMixFraction; |
1356 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1356 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1357 | // Yaw-Fraction |
1357 | // Yaw-Fraction |
1358 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1358 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1359 | YawMixFraction = Reading_GyroYaw - SetPointYaw * STICK_GAIN; // yaw controller |
1359 | YawMixFraction = Reading_GyroYaw - SetPointYaw * STICK_GAIN; // yaw controller |
1360 | #define MIN_YAWTHRUST (40 * STICK_GAIN) // yaw also below this thrust value |
1360 | #define MIN_YAWGAS (40 * STICK_GAIN) // yaw also below this gas value |
1361 | // limit YawMixFraction |
1361 | // limit YawMixFraction |
1362 | if(ThrustMixFraction > MIN_YAWTHRUST) |
1362 | if(GasMixFraction > MIN_YAWGAS) |
1363 | { |
1363 | { |
1364 | if(YawMixFraction > (ThrustMixFraction / 2)) YawMixFraction = ThrustMixFraction / 2; |
1364 | if(YawMixFraction > (GasMixFraction / 2)) YawMixFraction = GasMixFraction / 2; |
1365 | if(YawMixFraction < -(ThrustMixFraction / 2)) YawMixFraction = -(ThrustMixFraction / 2); |
1365 | if(YawMixFraction < -(GasMixFraction / 2)) YawMixFraction = -(GasMixFraction / 2); |
1366 | } |
1366 | } |
1367 | else |
1367 | else |
1368 | { |
1368 | { |
1369 | if(YawMixFraction > (MIN_YAWTHRUST / 2)) YawMixFraction = MIN_YAWTHRUST / 2; |
1369 | if(YawMixFraction > (MIN_YAWGAS / 2)) YawMixFraction = MIN_YAWGAS / 2; |
1370 | if(YawMixFraction < -(MIN_YAWTHRUST / 2)) YawMixFraction = -(MIN_YAWTHRUST / 2); |
1370 | if(YawMixFraction < -(MIN_YAWGAS / 2)) YawMixFraction = -(MIN_YAWGAS / 2); |
1371 | } |
1371 | } |
1372 | tmp_int = ParamSet.Trust_Max * STICK_GAIN; |
1372 | tmp_int = ParamSet.Gas_Max * STICK_GAIN; |
1373 | if(YawMixFraction > ((tmp_int - ThrustMixFraction))) YawMixFraction = ((tmp_int - ThrustMixFraction)); |
1373 | if(YawMixFraction > ((tmp_int - GasMixFraction))) YawMixFraction = ((tmp_int - GasMixFraction)); |
1374 | if(YawMixFraction < -((tmp_int - ThrustMixFraction))) YawMixFraction = -((tmp_int - ThrustMixFraction)); |
1374 | if(YawMixFraction < -((tmp_int - GasMixFraction))) YawMixFraction = -((tmp_int - GasMixFraction)); |
Line 1375... | Line 1375... | ||
1375 | 1375 | ||
1376 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1376 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1377 | // Pitch-Axis |
1377 | // Nick-Axis |
1378 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1378 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1379 | DiffPitch = Reading_GyroPitch - StickPitch; // get difference |
1379 | DiffNick = Reading_GyroNick - StickNick; // get difference |
1380 | if(Gyro_I_Factor) SumPitch += IntegralPitch * Gyro_I_Factor - StickPitch; // I-part for attitude control |
1380 | if(Gyro_I_Factor) SumNick += IntegralNick * Gyro_I_Factor - StickNick; // I-part for attitude control |
1381 | else SumPitch += DiffPitch; // I-part for head holding |
1381 | else SumNick += DiffNick; // I-part for head holding |
1382 | if(SumPitch > (STICK_GAIN * 16000L)) SumPitch = (STICK_GAIN * 16000L); |
1382 | if(SumNick > (STICK_GAIN * 16000L)) SumNick = (STICK_GAIN * 16000L); |
1383 | if(SumPitch < -(STICK_GAIN * 16000L)) SumPitch = -(STICK_GAIN * 16000L); |
1383 | if(SumNick < -(STICK_GAIN * 16000L)) SumNick = -(STICK_GAIN * 16000L); |
Line 1384... | Line 1384... | ||
1384 | pd_result = DiffPitch + Ki * SumPitch; // PI-controller for pitch |
1384 | pd_result = DiffNick + Ki * SumNick; // PI-controller for nick |
1385 | 1385 | ||
1386 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64; |
1386 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64; |
Line 1387... | Line 1387... | ||
1387 | if(pd_result > tmp_int) pd_result = tmp_int; |
1387 | if(pd_result > tmp_int) pd_result = tmp_int; |
1388 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
1388 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
1389 | 1389 | ||
1390 | // Motor Front |
1390 | // Motor Front |
1391 | MotorValue = ThrustMixFraction + pd_result + YawMixFraction; // Mixer |
1391 | MotorValue = GasMixFraction + pd_result + YawMixFraction; // Mixer |
1392 | MotorValue /= STICK_GAIN; |
1392 | MotorValue /= STICK_GAIN; |
1393 | if ((MotorValue < 0)) MotorValue = 0; |
1393 | if ((MotorValue < 0)) MotorValue = 0; |
Line 1394... | Line 1394... | ||
1394 | else if(MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
1394 | else if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1395 | if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
1395 | if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1396 | Motor_Front = MotorValue; |
1396 | Motor_Front = MotorValue; |
1397 | 1397 | ||
1398 | // Motor Rear |
1398 | // Motor Rear |
1399 | MotorValue = ThrustMixFraction - pd_result + YawMixFraction; // Mixer |
1399 | MotorValue = GasMixFraction - pd_result + YawMixFraction; // Mixer |
1400 | MotorValue /= STICK_GAIN; |
1400 | MotorValue /= STICK_GAIN; |
1401 | if ((MotorValue < 0)) MotorValue = 0; |
1401 | if ((MotorValue < 0)) MotorValue = 0; |
1402 | else if(MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
1402 | else if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1403 | if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
1403 | if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1404 | Motor_Rear = MotorValue; |
1404 | Motor_Rear = MotorValue; |
1405 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1405 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1406 | // Roll-Axis |
1406 | // Roll-Axis |
1407 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1407 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1408 | DiffRoll = Reading_GyroRoll - StickRoll; // get difference |
1408 | DiffRoll = Reading_GyroRoll - StickRoll; // get difference |
1409 | if(Gyro_I_Factor) SumRoll += IntegralRoll * Gyro_I_Factor - StickRoll; // I-part for attitude control |
1409 | if(Gyro_I_Factor) SumRoll += IntegralRoll * Gyro_I_Factor - StickRoll; // I-part for attitude control |
1410 | else SumRoll += DiffRoll; // I-part for head holding |
1410 | else SumRoll += DiffRoll; // I-part for head holding |
1411 | if(SumRoll > (STICK_GAIN * 16000L)) SumRoll = (STICK_GAIN * 16000L); |
1411 | if(SumRoll > (STICK_GAIN * 16000L)) SumRoll = (STICK_GAIN * 16000L); |
1412 | if(SumRoll < -(STICK_GAIN * 16000L)) SumRoll = -(STICK_GAIN * 16000L); |
1412 | if(SumRoll < -(STICK_GAIN * 16000L)) SumRoll = -(STICK_GAIN * 16000L); |
Line 1413... | Line 1413... | ||
1413 | pd_result = DiffRoll + Ki * SumRoll; // PI-controller for roll |
1413 | pd_result = DiffRoll + Ki * SumRoll; // PI-controller for roll |
1414 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64; |
1414 | tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(GasMixFraction + abs(YawMixFraction)/2)) / 64; |
1415 | if(pd_result > tmp_int) pd_result = tmp_int; |
1415 | if(pd_result > tmp_int) pd_result = tmp_int; |
1416 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
1416 | if(pd_result < -tmp_int) pd_result = -tmp_int; |
1417 | 1417 | ||
1418 | // Motor Left |
1418 | // Motor Left |
1419 | MotorValue = ThrustMixFraction + pd_result - YawMixFraction; // Mixer |
1419 | MotorValue = GasMixFraction + pd_result - YawMixFraction; // Mixer |
Line 1420... | Line 1420... | ||
1420 | MotorValue /= STICK_GAIN; |
1420 | MotorValue /= STICK_GAIN; |
1421 | if ((MotorValue < 0)) MotorValue = 0; |
1421 | if ((MotorValue < 0)) MotorValue = 0; |
1422 | else if(MotorValue > ParamSet.Trust_Max) MotorValue = ParamSet.Trust_Max; |
1422 | else if(MotorValue > ParamSet.Gas_Max) MotorValue = ParamSet.Gas_Max; |
1423 | if (MotorValue < ParamSet.Trust_Min) MotorValue = ParamSet.Trust_Min; |
1423 | if (MotorValue < ParamSet.Gas_Min) MotorValue = ParamSet.Gas_Min; |
1424 | Motor_Left = MotorValue; |
1424 | Motor_Left = MotorValue; |
1425 | 1425 | ||
1426 | // Motor Right |
1426 | // Motor Right |
1427 | MotorValue = ThrustMixFraction - pd_result - YawMixFraction; // Mixer |
1427 | MotorValue = GasMixFraction - pd_result - YawMixFraction; // Mixer |