Rev 1635 | Rev 1646 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1635 | Rev 1645 | ||
---|---|---|---|
Line 63... | Line 63... | ||
63 | 63 | ||
64 | // For reading and writing acc. meter offsets. |
64 | // For reading and writing acc. meter offsets. |
Line 65... | Line 65... | ||
65 | #include "eeprom.h" |
65 | #include "eeprom.h" |
66 | - | ||
67 | /* |
- | |
68 | * Arrays could have been used for the 2 * 3 axes, but despite some repetition, |
- | |
69 | * the code is easier to read without. |
66 | |
70 | * |
- | |
71 | * For each A/D conversion cycle, each channel (eg. the yaw gyro, or the Z axis |
67 | /* |
72 | * accelerometer) is sampled a number of times (see array channelsForStates), and |
68 | * For each A/D conversion cycle, each analog channel is sampled a number of times |
73 | * the results for each channel are summed. Here are those for the gyros and the |
69 | * (see array channelsForStates), and the results for each channel are summed. |
74 | * acc. meters. They are not zero-offset. |
70 | * Here are those for the gyros and the acc. meters. They are not zero-offset. |
75 | * They are exported in the analog.h file - but please do not use them! The only |
71 | * They are exported in the analog.h file - but please do not use them! The only |
76 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
72 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
77 | * the offsets with the DAC. |
73 | * the offsets with the DAC. |
78 | */ |
74 | */ |
79 | volatile int16_t rawPitchGyroSum, rawRollGyroSum, rawYawGyroSum; |
75 | volatile int16_t rawGyroSum[2], rawYawGyroSum; |
80 | volatile int16_t pitchAxisAcc = 0, rollAxisAcc = 0, ZAxisAcc = 0; |
- | |
81 | volatile int16_t filteredPitchAxisAcc = 0, filteredRollAxisAcc = 0; |
- | |
Line 82... | Line 76... | ||
82 | 76 | volatile int16_t acc[2] = {0,0}, ZAcc = 0; |
|
83 | // that float one - "Top" - is missing. |
77 | volatile int16_t filteredAcc[2] = {0,0}; |
84 | 78 | ||
85 | /* |
79 | /* |
86 | * These 4 exported variables are zero-offset. The "filtered" ones are |
- | |
87 | * (if configured to with the GYROS_SECONDORDERFILTER define) low pass |
80 | * These 4 exported variables are zero-offset. The "PID" ones are used |
88 | * filtered versions of the other 2. |
81 | * in the attitude control as rotation rates. The "ATT" ones are for |
89 | * They are derived from the "raw" values above, by zero-offsetting. |
82 | * integration to angles. |
90 | */ |
83 | */ |
91 | volatile int16_t hiResPitchGyro = 0, hiResRollGyro = 0; |
84 | volatile int16_t gyro_PID[2]; |
Line 92... | Line 85... | ||
92 | volatile int16_t filteredHiResPitchGyro = 0, filteredHiResRollGyro = 0; |
85 | volatile int16_t gyro_ATT[2]; |
93 | volatile int16_t pitchGyroD = 0, rollGyroD = 0; |
86 | volatile int16_t gyroD[2]; |
94 | volatile int16_t yawGyro = 0; |
87 | volatile int16_t yawGyro = 0; |
95 | 88 | ||
96 | /* |
89 | /* |
97 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
90 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
98 | * standing still. They are used for adjusting the gyro and acc. meter values |
91 | * standing still. They are used for adjusting the gyro and acc. meter values |
Line 99... | Line 92... | ||
99 | * to be zero when the copter stands still. |
92 | * to be centered on zero. |
100 | */ |
93 | */ |
101 | volatile int16_t pitchOffset, rollOffset, yawOffset; |
94 | volatile int16_t gyroOffset[2], yawGyroOffset; |
102 | volatile int16_t pitchAxisAccOffset, rollAxisAccOffset, ZAxisAccOffset; |
95 | volatile int16_t accOffset[2], ZAccOffset; |
103 | 96 | ||
104 | /* |
97 | /* |
105 | * This allows some experimentation with the gyro filters. |
98 | * This allows some experimentation with the gyro filters. |
106 | * Should be replaced by #define's later on... |
99 | * Should be replaced by #define's later on... |
Line -... | Line 100... | ||
- | 100 | */ |
|
107 | */ |
101 | volatile uint8_t GYROS_FIRSTORDERFILTER; |
- | 102 | volatile uint8_t GYROS_SECONDORDERFILTER; |
|
108 | volatile uint8_t GYROS_FIRSTORDERFILTER; |
103 | volatile uint8_t GYROS_DFILTER; |
109 | volatile uint8_t GYROS_SECONDORDERFILTER; |
104 | volatile uint8_t ACC_FILTER; |
110 | volatile uint8_t GYROS_DFILTER; |
105 | |
111 | volatile uint8_t ACC_FILTER; |
106 | /* |
112 | 107 | * Air pressure measurement. |
|
Line 113... | Line 108... | ||
113 | // Air pressure (no support right now). |
108 | */ |
114 | // volatile int32_t AirPressure = 32000; |
109 | #define MIN_RAWPRESSURE 200 |
115 | // volatile uint8_t average_pressure = 0; |
110 | #define MAX_RAWPRESSURE (1023-MIN_RAWPRESSURE) |
116 | // volatile int16_t StartAirPressure; |
111 | volatile uint8_t rangewidth = 53; |
117 | // volatile uint16_t ReadingAirPressure = 1023; |
112 | volatile uint16_t rawAirPressure; |
118 | // volatile int16_t HeightD = 0; |
113 | volatile uint16_t filteredAirPressure; |
Line 119... | Line -... | ||
119 | - | ||
120 | /* |
- | |
121 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
114 | |
122 | * That is divided by 3 below, for a final 10.34 per volt. |
115 | /* |
123 | * So the initial value of 100 is for 9.7 volts. |
116 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
124 | */ |
117 | * That is divided by 3 below, for a final 10.34 per volt. |
125 | volatile int16_t UBat = 100; |
118 | * So the initial value of 100 is for 9.7 volts. |
Line 126... | Line 119... | ||
126 | 119 | */ |
|
127 | volatile int16_t filteredAirPressure; |
120 | volatile int16_t UBat = 100; |
128 | 121 | ||
129 | /* |
122 | /* |
130 | * Control and status. |
123 | * Control and status. |
Line 131... | Line 124... | ||
131 | */ |
124 | */ |
132 | volatile uint16_t ADCycleCount = 0; |
125 | volatile uint16_t ADCycleCount = 0; |
133 | volatile uint8_t analogDataReady = 1; |
126 | volatile uint8_t analogDataReady = 1; |
134 | 127 | ||
135 | /* |
128 | /* |
136 | * Experiment: Measuring vibration-induced sensor noise. |
129 | * Experiment: Measuring vibration-induced sensor noise. |
137 | */ |
130 | */ |
138 | volatile uint16_t pitchGyroNoisePeak, rollGyroNoisePeak; |
131 | volatile uint16_t gyroNoisePeak[2]; |
139 | volatile uint16_t pitchAccNoisePeak, rollAccNoisePeak; |
132 | volatile uint16_t accNoisePeak[2]; |
Line 140... | Line 133... | ||
140 | 133 | ||
141 | // ADC channels |
134 | // ADC channels |
142 | #define AD_GYRO_YAW 0 |
135 | #define AD_GYRO_YAW 0 |
143 | #define AD_GYRO_ROLL 1 |
136 | #define AD_GYRO_ROLL 1 |
Line 226... | Line 219... | ||
226 | } else { |
219 | } else { |
227 | *noiseMeasurement = 0; |
220 | *noiseMeasurement = 0; |
228 | } |
221 | } |
229 | } |
222 | } |
Line 230... | Line -... | ||
230 | - | ||
231 | - | ||
232 | #define ADCENTER (1023/2) |
- | |
233 | #define HALFRANGE 400 |
- | |
234 | uint8_t stepsize = 53; |
- | |
235 | 223 | ||
236 | uint16_t getAbsPressure(int advalue) { |
224 | uint16_t getAbsPressure(int advalue) { |
237 | return (uint16_t)OCR0A * (uint16_t)stepsize + advalue; |
225 | return (uint16_t)OCR0A * (uint16_t)rangewidth + advalue; |
Line 238... | Line 226... | ||
238 | } |
226 | } |
239 | 227 | ||
240 | uint16_t filterAirPressure(uint16_t rawpressure) { |
228 | uint16_t filterAirPressure(uint16_t rawpressure) { |
Line 241... | Line 229... | ||
241 | return rawpressure; |
229 | return rawpressure; |
242 | } |
230 | } |
243 | - | ||
244 | /*****************************************************/ |
231 | |
245 | /* Interrupt Service Routine for ADC */ |
232 | /***************************************************** |
246 | /*****************************************************/ |
233 | * Interrupt Service Routine for ADC |
247 | // Runs at 312.5 kHz or 3.2 µs |
- | |
- | 234 | * Runs at 312.5 kHz or 3.2 µs. When all states are |
|
248 | // When all states are processed the interrupt is disabled |
235 | * processed the interrupt is disabled and further |
249 | // and the update of further AD conversions is stopped. |
236 | * AD conversions are stopped. |
250 | 237 | *****************************************************/ |
|
251 | ISR(ADC_vect) { |
- | |
- | 238 | ISR(ADC_vect) { |
|
252 | static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
239 | static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
253 | static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0}; |
240 | static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0}; |
Line 254... | Line 241... | ||
254 | 241 | static uint8_t pressure_wait = 10; |
|
255 | uint8_t i; |
242 | uint8_t i, axis; |
Line 256... | Line 243... | ||
256 | int16_t step = OCR0A; |
243 | int16_t range; |
Line 257... | Line 244... | ||
257 | 244 | ||
258 | // for various filters... |
245 | // for various filters... |
259 | static int16_t pitchGyroFilter, rollGyroFilter, tempOffsetGyro; |
246 | int16_t tempOffsetGyro, tempGyro; |
260 | 247 | ||
261 | sensorInputs[ad_channel] += ADC; |
248 | sensorInputs[ad_channel] += ADC; |
262 | 249 | ||
263 | /* |
250 | /* |
264 | * Actually we don't need this "switch". We could do all the sampling into the |
251 | * Actually we don't need this "switch". We could do all the sampling into the |
265 | * sensorInputs array first, and all the processing after the last sample. |
252 | * sensorInputs array first, and all the processing after the last sample. |
266 | */ |
253 | */ |
267 | switch(state++) { |
254 | switch(state++) { |
268 | case 7: // Z acc |
255 | case 7: // Z acc |
Line 269... | Line 256... | ||
269 | #ifdef ACC_REVERSE_ZAXIS |
256 | #ifdef ACC_REVERSE_ZAXIS |
270 | ZAxisAcc = -ZAxisAccOffset - sensorInputs[AD_ACC_Z]; |
257 | ZAcc = -ZAccOffset - sensorInputs[AD_ACC_Z]; |
271 | #else |
258 | #else |
272 | ZAxisAcc = sensorInputs[AD_ACC_Z] - ZAxisAccOffset; |
259 | ZAcc = sensorInputs[AD_ACC_Z] - ZAccOffset; |
273 | #endif |
260 | #endif |
274 | break; |
261 | break; |
275 | 262 | ||
276 | case 10: // yaw gyro |
263 | case 10: // yaw gyro |
Line 277... | Line 264... | ||
277 | rawYawGyroSum = sensorInputs[AD_GYRO_YAW]; |
264 | rawYawGyroSum = sensorInputs[AD_GYRO_YAW]; |
278 | #ifdef GYRO_REVERSE_YAW |
265 | #ifdef GYRO_REVERSE_YAW |
279 | yawGyro = rawYawGyroSum - yawOffset; |
266 | yawGyro = rawYawGyroSum - yawGyroOffset; |
280 | #else |
267 | #else |
281 | yawGyro = yawOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3). |
268 | yawGyro = yawGyroOffset - rawYawGyroSum; // negative is "default" (FC 1.0-1.3). |
282 | #endif |
269 | #endif |
283 | break; |
270 | break; |
Line 284... | Line 271... | ||
284 | 271 | ||
285 | case 11: // pitch axis acc. |
272 | case 11: // pitch axis acc. |
Line 286... | Line 273... | ||
286 | #ifdef ACC_REVERSE_PITCHAXIS |
273 | #ifdef ACC_REVERSE_PITCHAXIS |
287 | pitchAxisAcc = -pitchAxisAccOffset - sensorInputs[AD_ACC_PITCH]; |
274 | acc[PITCH] = -accOffset[PITCH] - sensorInputs[AD_ACC_PITCH]; |
288 | #else |
275 | #else |
289 | pitchAxisAcc = sensorInputs[AD_ACC_PITCH] - pitchAxisAccOffset; |
276 | acc[PITCH] = sensorInputs[AD_ACC_PITCH] - accOffset[PITCH]; |
290 | #endif |
277 | #endif |
291 | filteredPitchAxisAcc = (filteredPitchAxisAcc * (ACC_FILTER-1) + pitchAxisAcc) / ACC_FILTER; |
278 | filteredAcc[PITCH] = (filteredAcc[PITCH] * (ACC_FILTER-1) + acc[PITCH]) / ACC_FILTER; |
292 | 279 | ||
293 | measureNoise(pitchAxisAcc, &pitchAccNoisePeak, 1); |
280 | measureNoise(acc[PITCH], &accNoisePeak[PITCH], 1); |
294 | break; |
281 | break; |
295 | 282 | ||
296 | case 12: // roll axis acc. |
283 | case 12: // roll axis acc. |
- | 284 | #ifdef ACC_REVERSE_ROLLAXIS |
|
- | 285 | acc[ROLL] = sensorInputs[AD_ACC_ROLL] - accOffset[ROLL]; |
|
- | 286 | #else |
|
- | 287 | acc[ROLL] = -accOffset[ROLL] - sensorInputs[AD_ACC_ROLL]; |
|
- | 288 | #endif |
|
- | 289 | filteredAcc[ROLL] = (filteredAcc[ROLL] * (ACC_FILTER-1) + acc[ROLL]) / ACC_FILTER; |
|
297 | #ifdef ACC_REVERSE_ROLLAXIS |
290 | measureNoise(acc[ROLL], &accNoisePeak[ROLL], 1); |
- | 291 | break; |
|
298 | rollAxisAcc = sensorInputs[AD_ACC_ROLL] - rollAxisAccOffset; |
292 | |
299 | #else |
293 | case 13: // air pressure |
300 | rollAxisAcc = -rollAxisAccOffset - sensorInputs[AD_ACC_ROLL]; |
294 | if (pressure_wait) { |
- | 295 | // A range switch was done recently. Wait for steadying. |
|
301 | #endif |
296 | pressure_wait--; |
302 | filteredRollAxisAcc = (filteredRollAxisAcc * (ACC_FILTER-1) + rollAxisAcc) / ACC_FILTER; |
- | |
303 | measureNoise(rollAxisAcc, &rollAccNoisePeak, 1); |
297 | break; |
304 | break; |
298 | } |
305 | 299 | range = OCR0A; |
|
306 | case 13: // air pressure |
300 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
- | 301 | if (rawAirPressure < MIN_RAWPRESSURE) { |
|
307 | if (sensorInputs[AD_AIRPRESSURE] < ADCENTER-HALFRANGE) { |
302 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
308 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
- | |
309 | step -= ((HALFRANGE-sensorInputs[AD_AIRPRESSURE]) / stepsize + 1); |
303 | range -= (MAX_RAWPRESSURE - rawAirPressure) / rangewidth - 1; |
310 | if (step<0) step = 0; |
304 | if (range < 0) range = 0; |
311 | OCR0A = step; |
305 | pressure_wait = (OCR0A - range) * 4; |
- | 306 | OCR0A = range; |
|
- | 307 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
|
- | 308 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
|
- | 309 | range += (rawAirPressure - MIN_RAWPRESSURE) / rangewidth - 1; |
|
312 | // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off) |
310 | if (range > 254) range = 254; |
Line -... | Line 311... | ||
- | 311 | pressure_wait = (range - OCR0A) * 4; |
|
313 | } else if (sensorInputs[AD_AIRPRESSURE] > ADCENTER+HALFRANGE) { |
312 | OCR0A = range; |
- | 313 | } else { |
|
314 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
314 | filteredAirPressure = filterAirPressure(getAbsPressure(rawAirPressure)); |
- | 315 | } |
|
- | 316 | ||
- | 317 | DebugOut.Analog[12] = range; |
|
- | 318 | DebugOut.Analog[13] = rawAirPressure; |
|
315 | step += ((sensorInputs[AD_AIRPRESSURE] - HALFRANGE)/stepsize + 1); |
319 | DebugOut.Analog[14] = filteredAirPressure; |
316 | if (step>254) step = 254; |
320 | break; |
- | 321 | ||
- | 322 | case 14: |
|
- | 323 | case 15: // pitch or roll gyro. |
|
- | 324 | axis = state - 15; |
|
- | 325 | tempGyro = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH - axis]; |
|
- | 326 | // DebugOut.Analog[6 + 3 * axis ] = tempGyro; |
|
317 | OCR0A = step; |
327 | /* |
318 | // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off) |
328 | * Process the gyro data for the PID controller. |
- | 329 | */ |
|
- | 330 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
|
- | 331 | // gyro with a wider range, and helps counter saturation at full control. |
|
319 | } else { |
332 | |
320 | filteredAirPressure = filterAirPressure(getAbsPressure(sensorInputs[AD_AIRPRESSURE])); |
333 | if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) { |
321 | } |
334 | if (tempGyro < SENSOR_MIN_PITCHROLL) { |
322 | break; |
335 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
323 | 336 | } |
|
- | 337 | else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
|
324 | case 14: // pitch gyro |
338 | tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL; |
325 | rawPitchGyroSum = sensorInputs[AD_GYRO_PITCH]; |
339 | } |
- | 340 | } |
|
326 | // Filter already before offsetting. The offsetting resolution improvement obtained by divding by |
341 | |
327 | // GYROS_FIRSTORDERFILTER _after_ offsetting is too small to be worth pursuing. |
342 | // 2) Apply sign and offset, scale before filtering. |
- | 343 | if (GYROS_REVERSE[axis]) { |
|
328 | pitchGyroFilter = (pitchGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawPitchGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
344 | tempOffsetGyro = (gyroOffset[axis] - tempGyro) * GYRO_FACTOR_PITCHROLL; |
329 | // Offset to 0. |
345 | } else { |
330 | #ifdef GYROS_REVERSE_PITCH |
- | |
- | 346 | tempOffsetGyro = (tempGyro - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL; |
|
331 | tempOffsetGyro = pitchOffset - pitchGyroFilter; |
347 | } |
- | 348 | ||
332 | #else |
349 | // 3) Scale and filter. |
- | 350 | tempOffsetGyro = (gyro_PID[axis] * (GYROS_PIDFILTER-1) + tempOffsetGyro) / GYROS_PIDFILTER; |
|
333 | tempOffsetGyro = pitchGyroFilter - pitchOffset; |
351 | |
- | 352 | // 4) Measure noise. |
|
334 | #endif |
353 | measureNoise(tempOffsetGyro, &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
- | 354 | ||
335 | // Calculate the delta from last shot and filter it. |
355 | // 5) Differential measurement. |
336 | pitchGyroD = (pitchGyroD * (GYROS_DFILTER-1) + (tempOffsetGyro - hiResPitchGyro)) / GYROS_DFILTER; |
356 | gyroD[axis] = (gyroD[axis] * (GYROS_DFILTER-1) + (tempOffsetGyro - gyro_PID[axis])) / GYROS_DFILTER; |
337 | // How we can overwrite the last value. This value is used for the D part of the PID controller. |
357 | |
338 | hiResPitchGyro = tempOffsetGyro; |
358 | // 6) Done. |
339 | // Filter a little more. This value is used in integration to angles. |
359 | gyro_PID[axis] = tempOffsetGyro; |
340 | filteredHiResPitchGyro = (filteredHiResPitchGyro * (GYROS_SECONDORDERFILTER-1) + hiResPitchGyro) / GYROS_SECONDORDERFILTER; |
360 | |
341 | measureNoise(hiResPitchGyro, &pitchGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING); |
- | |
- | 361 | /* |
|
342 | break; |
362 | * Now process the data for attitude angles. |
343 | 363 | */ |
|
344 | case 15: // Roll gyro. Works the same as pitch. |
- | |
345 | rawRollGyroSum = sensorInputs[AD_GYRO_ROLL]; |
364 | tempGyro = rawGyroSum[axis]; |
Line 346... | Line 365... | ||
346 | rollGyroFilter = (rollGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawRollGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
365 | |
347 | #ifdef GYRO_REVERSE_ROLL |
366 | // 1) Apply sign and offset, scale before filtering. |
348 | tempOffsetGyro = rollOffset - rollGyroFilter; |
367 | if (GYROS_REVERSE[axis]) { |
Line 388... | Line 407... | ||
388 | GYROS_FIRSTORDERFILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1; |
407 | GYROS_FIRSTORDERFILTER = (dynamicParams.UserParams[4] & 0b00000011) + 1; |
389 | GYROS_SECONDORDERFILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1; |
408 | GYROS_SECONDORDERFILTER = ((dynamicParams.UserParams[4] & 0b00001100) >> 2) + 1; |
390 | GYROS_DFILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1; |
409 | GYROS_DFILTER = ((dynamicParams.UserParams[4] & 0b00110000) >> 4) + 1; |
391 | ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1; |
410 | ACC_FILTER = ((dynamicParams.UserParams[4] & 0b11000000) >> 6) + 1; |
Line 392... | Line 411... | ||
392 | 411 | ||
Line 393... | Line 412... | ||
393 | pitchOffset = rollOffset = yawOffset = 0; |
412 | gyroOffset[PITCH] = gyroOffset[ROLL] = yawGyroOffset = 0; |
Line 394... | Line 413... | ||
394 | 413 | ||
395 | gyro_calibrate(); |
414 | gyro_calibrate(); |
396 | 415 | ||
397 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
416 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
398 | for(i=0; i < GYRO_OFFSET_CYCLES; i++) { |
417 | for(i=0; i < GYRO_OFFSET_CYCLES; i++) { |
399 | Delay_ms_Mess(10); |
418 | Delay_ms_Mess(10); |
400 | _pitchOffset += rawPitchGyroSum * GYRO_FACTOR_PITCHROLL; |
419 | _pitchOffset += rawGyroSum[PITCH]; |
Line 401... | Line 420... | ||
401 | _rollOffset += rawRollGyroSum * GYRO_FACTOR_PITCHROLL; |
420 | _rollOffset += rawGyroSum[ROLL]; |
402 | _yawOffset += rawYawGyroSum; |
421 | _yawOffset += rawYawGyroSum; |
403 | } |
422 | } |
Line 404... | Line 423... | ||
404 | 423 | ||
405 | pitchOffset = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
- | |
406 | rollOffset = (_rollOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
- | |
407 | yawOffset = (_yawOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
424 | gyroOffset[PITCH] = (_pitchOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
408 | - | ||
Line 409... | Line 425... | ||
409 | filteredHiResPitchGyro = filteredHiResRollGyro = 0; |
425 | gyroOffset[ROLL] = (_rollOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
410 | 426 | yawGyroOffset = (_yawOffset + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
|
411 | pitchAxisAccOffset = (int16_t)GetParamWord(PID_ACC_NICK); |
427 | |
Line 412... | Line 428... | ||
412 | rollAxisAccOffset = (int16_t)GetParamWord(PID_ACC_ROLL); |
428 | gyro_PID[PITCH] = gyro_PID[ROLL] = 0; |
413 | ZAxisAccOffset = (int16_t)GetParamWord(PID_ACC_TOP); |
429 | gyro_ATT[PITCH] = gyro_ATT[ROLL] = 0; |
414 | 430 | ||
415 | // Noise is relative to offset. So, reset noise measurements when |
431 | // Noise is relative to offset. So, reset noise measurements when |
Line 416... | Line 432... | ||
416 | // changing offsets. |
432 | // changing offsets. |
417 | pitchGyroNoisePeak = rollGyroNoisePeak = 0; |
433 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
418 | 434 | ||
Line 430... | Line 446... | ||
430 | */ |
446 | */ |
431 | void analog_calibrateAcc(void) { |
447 | void analog_calibrateAcc(void) { |
432 | #define ACC_OFFSET_CYCLES 10 |
448 | #define ACC_OFFSET_CYCLES 10 |
433 | uint8_t i; |
449 | uint8_t i; |
434 | int32_t _pitchAxisOffset = 0, _rollAxisOffset = 0, _ZAxisOffset = 0; |
450 | int32_t _pitchAxisOffset = 0, _rollAxisOffset = 0, _ZAxisOffset = 0; |
- | 451 | // int16_t pressureDiff, savedRawAirPressure; |
|
Line 435... | Line 452... | ||
435 | 452 | ||
Line 436... | Line 453... | ||
436 | pitchAxisAccOffset = rollAxisAccOffset = ZAxisAccOffset = 0; |
453 | accOffset[PITCH] = accOffset[ROLL] = ZAccOffset = 0; |
437 | 454 | ||
438 | for(i=0; i < ACC_OFFSET_CYCLES; i++) { |
455 | for(i=0; i < ACC_OFFSET_CYCLES; i++) { |
439 | Delay_ms_Mess(10); |
456 | Delay_ms_Mess(10); |
440 | _pitchAxisOffset += pitchAxisAcc; |
457 | _pitchAxisOffset += acc[PITCH]; |
441 | _rollAxisOffset += rollAxisAcc; |
458 | _rollAxisOffset += acc[ROLL]; |
Line 442... | Line 459... | ||
442 | _ZAxisOffset += ZAxisAcc; |
459 | _ZAxisOffset += ZAcc; |
443 | } |
460 | } |
444 | 461 | ||
445 | // Save ACC neutral settings to eeprom |
462 | // Save ACC neutral settings to eeprom |
Line 446... | Line 463... | ||
446 | SetParamWord(PID_ACC_NICK, (uint16_t)((_pitchAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
463 | SetParamWord(PID_ACC_PITCH, (uint16_t)((_pitchAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
447 | SetParamWord(PID_ACC_ROLL, (uint16_t)((_rollAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
464 | SetParamWord(PID_ACC_ROLL, (uint16_t)((_rollAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
448 | SetParamWord(PID_ACC_TOP, (uint16_t)((_ZAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
465 | SetParamWord(PID_ACC_TOP, (uint16_t)((_ZAxisOffset + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES)); |
- | 466 | ||
- | 467 | // Noise is relative to offset. So, reset noise measurements when |
|
- | 468 | // changing offsets. |
|
- | 469 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
|
- | 470 | // Setting offset values has an influence in the analog.c ISR |
|
- | 471 | // Therefore run measurement for 100ms to achive stable readings |
|
- | 472 | // Delay_ms_Mess(100); |
|
- | 473 | ||
- | 474 | // Set the feedback so that air pressure ends up in the middle of the range. |
|
- | 475 | // (raw pressure high --> OCR0A also high...) |
|
- | 476 | // OCR0A += (rawAirPressure - 512) / rangewidth; |
|
- | 477 | // Delay_ms_Mess(500); |
|
- | 478 | ||
- | 479 | /* |
|
- | 480 | pressureDiff = 0; |
|
- | 481 | DebugOut.Analog[16] = rawAirPressure; |
|
- | 482 | ||
- | 483 | #define PRESSURE_CAL_CYCLE_COUNT 2 |
|
- | 484 | for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) { |
|
- | 485 | savedRawAirPressure = rawAirPressure; |
|
- | 486 | OCR0A++; |
|
- | 487 | Delay_ms_Mess(200); |
|
- | 488 | // raw pressure will decrease. |
|
- | 489 | pressureDiff += (savedRawAirPressure - rawAirPressure); |
|
- | 490 | ||
- | 491 | savedRawAirPressure = rawAirPressure; |
|
- | 492 | OCR0A--; |
|
- | 493 | Delay_ms_Mess(200); |
|
- | 494 | // raw pressure will increase. |
|
- | 495 | pressureDiff += (rawAirPressure - savedRawAirPressure); |
|
- | 496 | } |
|
449 | 497 |