Rev 2096 | Rev 2102 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2096 | - | 1 | #include <avr/io.h> |
1910 | - | 2 | #include <avr/interrupt.h> |
3 | #include <avr/pgmspace.h> |
||
2096 | - | 4 | #include <stdlib.h> |
1910 | - | 5 | |
6 | #include "analog.h" |
||
7 | #include "attitude.h" |
||
8 | #include "sensors.h" |
||
2096 | - | 9 | #include "printf_P.h" |
1910 | - | 10 | |
11 | // for Delay functions |
||
12 | #include "timer0.h" |
||
13 | |||
14 | // For reading and writing acc. meter offsets. |
||
15 | #include "eeprom.h" |
||
16 | |||
2096 | - | 17 | // For debugOut |
1910 | - | 18 | #include "output.h" |
19 | |||
2096 | - | 20 | // set ADC enable & ADC Start Conversion & ADC Interrupt Enable bit |
21 | #define startADC() (ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE)) |
||
22 | |||
23 | const char* recal = ", recalibration needed."; |
||
24 | |||
1910 | - | 25 | /* |
26 | * For each A/D conversion cycle, each analog channel is sampled a number of times |
||
27 | * (see array channelsForStates), and the results for each channel are summed. |
||
28 | * Here are those for the gyros and the acc. meters. They are not zero-offset. |
||
29 | * They are exported in the analog.h file - but please do not use them! The only |
||
30 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
||
31 | * the offsets with the DAC. |
||
32 | */ |
||
2096 | - | 33 | volatile uint16_t sensorInputs[8]; |
2099 | - | 34 | //int16_t acc[3]; |
35 | //int16_t filteredAcc[3] = { 0,0,0 }; |
||
1910 | - | 36 | |
37 | /* |
||
38 | * These 4 exported variables are zero-offset. The "PID" ones are used |
||
39 | * in the attitude control as rotation rates. The "ATT" ones are for |
||
40 | * integration to angles. |
||
41 | */ |
||
2099 | - | 42 | int16_t gyro_PID[3]; |
43 | int16_t gyro_ATT[3]; |
||
44 | int16_t gyroD[3]; |
||
2096 | - | 45 | int16_t gyroDWindow[2][GYRO_D_WINDOW_LENGTH]; |
46 | uint8_t gyroDWindowIdx = 0; |
||
47 | int16_t dHeight; |
||
48 | uint32_t gyroActivity; |
||
49 | |||
1910 | - | 50 | /* |
51 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
||
52 | * standing still. They are used for adjusting the gyro and acc. meter values |
||
53 | * to be centered on zero. |
||
54 | */ |
||
55 | |||
2096 | - | 56 | sensorOffset_t gyroOffset; |
57 | sensorOffset_t accOffset; |
||
58 | sensorOffset_t gyroAmplifierOffset; |
||
1910 | - | 59 | |
60 | /* |
||
2096 | - | 61 | * In the MK coordinate system, nose-down is positive and left-roll is positive. |
62 | * If a sensor is used in an orientation where one but not both of the axes has |
||
63 | * an opposite sign, PR_ORIENTATION_REVERSED is set to 1 (true). |
||
64 | * Transform: |
||
65 | * pitch <- pp*pitch + pr*roll |
||
66 | * roll <- rp*pitch + rr*roll |
||
67 | * Not reversed, GYRO_QUADRANT: |
||
68 | * 0: pp=1, pr=0, rp=0, rr=1 // 0 degrees |
||
69 | * 1: pp=1, pr=-1,rp=1, rr=1 // +45 degrees |
||
70 | * 2: pp=0, pr=-1,rp=1, rr=0 // +90 degrees |
||
71 | * 3: pp=-1,pr=-1,rp=1, rr=1 // +135 degrees |
||
72 | * 4: pp=-1,pr=0, rp=0, rr=-1 // +180 degrees |
||
73 | * 5: pp=-1,pr=1, rp=-1,rr=-1 // +225 degrees |
||
74 | * 6: pp=0, pr=1, rp=-1,rr=0 // +270 degrees |
||
75 | * 7: pp=1, pr=1, rp=-1,rr=1 // +315 degrees |
||
76 | * Reversed, GYRO_QUADRANT: |
||
77 | * 0: pp=-1,pr=0, rp=0, rr=1 // 0 degrees with pitch reversed |
||
78 | * 1: pp=-1,pr=-1,rp=-1,rr=1 // +45 degrees with pitch reversed |
||
79 | * 2: pp=0, pr=-1,rp=-1,rr=0 // +90 degrees with pitch reversed |
||
80 | * 3: pp=1, pr=-1,rp=-1,rr=1 // +135 degrees with pitch reversed |
||
81 | * 4: pp=1, pr=0, rp=0, rr=-1 // +180 degrees with pitch reversed |
||
82 | * 5: pp=1, pr=1, rp=1, rr=-1 // +225 degrees with pitch reversed |
||
83 | * 6: pp=0, pr=1, rp=1, rr=0 // +270 degrees with pitch reversed |
||
84 | * 7: pp=-1,pr=1, rp=1, rr=1 // +315 degrees with pitch reversed |
||
85 | */ |
||
86 | |||
2099 | - | 87 | void rotate(int16_t* result, uint8_t quadrant, uint8_t reversePR, uint8_t reverseYaw) { |
2096 | - | 88 | static const int8_t rotationTab[] = {1,1,0,-1,-1,-1,0,1}; |
89 | // Pitch to Pitch part |
||
2099 | - | 90 | int8_t xx = reversePR ? rotationTab[(quadrant+4)%8] : rotationTab[quadrant]; |
2096 | - | 91 | // Roll to Pitch part |
92 | int8_t xy = rotationTab[(quadrant+2)%8]; |
||
93 | // Pitch to Roll part |
||
2099 | - | 94 | int8_t yx = reversePR ? rotationTab[(quadrant+2)%8] : rotationTab[(quadrant+6)%8]; |
2096 | - | 95 | // Roll to Roll part |
96 | int8_t yy = rotationTab[quadrant]; |
||
97 | |||
98 | int16_t xIn = result[0]; |
||
99 | result[0] = xx*xIn + xy*result[1]; |
||
100 | result[1] = yx*xIn + yy*result[1]; |
||
101 | |||
102 | if (quadrant & 1) { |
||
103 | // A rotation was used above, where the factors were too large by sqrt(2). |
||
104 | // So, we multiply by 2^n/sqt(2) and right shift n bits, as to divide by sqrt(2). |
||
105 | // A suitable value for n: Sample is 11 bits. After transformation it is the sum |
||
106 | // of 2 11 bit numbers, so 12 bits. We have 4 bits left... |
||
107 | result[0] = (result[0]*11) >> 4; |
||
108 | result[1] = (result[1]*11) >> 4; |
||
109 | } |
||
2099 | - | 110 | |
111 | if (reverseYaw) |
||
112 | result[3] =-result[3]; |
||
2096 | - | 113 | } |
114 | |||
115 | /* |
||
2099 | - | 116 | * Airspeed |
1910 | - | 117 | */ |
2096 | - | 118 | uint16_t simpleAirPressure; |
1910 | - | 119 | |
2096 | - | 120 | // Value of AIRPRESSURE_OVERSAMPLING samples, with range, filtered. |
2099 | - | 121 | // int32_t filteredAirPressure; |
1910 | - | 122 | |
2096 | - | 123 | #define MAX_AIRPRESSURE_WINDOW_LENGTH 32 |
124 | int16_t airPressureWindow[MAX_AIRPRESSURE_WINDOW_LENGTH]; |
||
125 | int32_t windowedAirPressure; |
||
126 | uint8_t windowPtr = 0; |
||
127 | |||
1910 | - | 128 | // Partial sum of AIRPRESSURE_SUMMATION_FACTOR samples. |
2096 | - | 129 | int32_t airPressureSum; |
1910 | - | 130 | |
131 | // The number of samples summed into airPressureSum so far. |
||
2096 | - | 132 | uint8_t pressureMeasurementCount; |
1910 | - | 133 | |
2099 | - | 134 | |
1910 | - | 135 | /* |
136 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
||
137 | * That is divided by 3 below, for a final 10.34 per volt. |
||
138 | * So the initial value of 100 is for 9.7 volts. |
||
139 | */ |
||
2096 | - | 140 | int16_t UBat = 100; |
1910 | - | 141 | |
142 | /* |
||
143 | * Control and status. |
||
144 | */ |
||
145 | volatile uint8_t analogDataReady = 1; |
||
146 | |||
147 | /* |
||
148 | * Experiment: Measuring vibration-induced sensor noise. |
||
149 | */ |
||
2096 | - | 150 | uint16_t gyroNoisePeak[3]; |
1910 | - | 151 | |
2096 | - | 152 | volatile uint8_t adState; |
153 | volatile uint8_t adChannel; |
||
154 | |||
1910 | - | 155 | // ADC channels |
156 | #define AD_GYRO_YAW 0 |
||
157 | #define AD_GYRO_ROLL 1 |
||
158 | #define AD_GYRO_PITCH 2 |
||
159 | #define AD_AIRPRESSURE 3 |
||
160 | #define AD_UBAT 4 |
||
161 | #define AD_ACC_Z 5 |
||
162 | #define AD_ACC_ROLL 6 |
||
163 | #define AD_ACC_PITCH 7 |
||
164 | |||
165 | /* |
||
166 | * Table of AD converter inputs for each state. |
||
167 | * The number of samples summed for each channel is equal to |
||
168 | * the number of times the channel appears in the array. |
||
169 | * The max. number of samples that can be taken in 2 ms is: |
||
170 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
||
171 | * loop needs a little time between reading AD values and |
||
172 | * re-enabling ADC, the real limit is (how much?) lower. |
||
173 | * The acc. sensor is sampled even if not used - or installed |
||
174 | * at all. The cost is not significant. |
||
175 | */ |
||
176 | |||
177 | const uint8_t channelsForStates[] PROGMEM = { |
||
2099 | - | 178 | AD_GYRO_PITCH, |
179 | AD_GYRO_ROLL, |
||
180 | AD_GYRO_YAW, |
||
1910 | - | 181 | |
2099 | - | 182 | AD_AIRPRESSURE, |
183 | |||
184 | AD_GYRO_PITCH, |
||
185 | AD_GYRO_ROLL, |
||
186 | AD_GYRO_YAW, |
||
187 | |||
188 | AD_UBAT, |
||
189 | |||
190 | AD_GYRO_PITCH, |
||
191 | AD_GYRO_ROLL, |
||
192 | AD_GYRO_YAW, |
||
1910 | - | 193 | |
2099 | - | 194 | AD_AIRPRESSURE, |
1910 | - | 195 | |
2099 | - | 196 | AD_GYRO_PITCH, |
197 | AD_GYRO_ROLL, |
||
198 | AD_GYRO_YAW |
||
1910 | - | 199 | }; |
200 | |||
201 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
||
202 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
||
203 | |||
204 | void analog_init(void) { |
||
205 | uint8_t sreg = SREG; |
||
206 | // disable all interrupts before reconfiguration |
||
207 | cli(); |
||
208 | |||
209 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
||
210 | DDRA = 0x00; |
||
211 | PORTA = 0x00; |
||
212 | // Digital Input Disable Register 0 |
||
213 | // Disable digital input buffer for analog adc_channel pins |
||
214 | DIDR0 = 0xFF; |
||
215 | // external reference, adjust data to the right |
||
2096 | - | 216 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
1910 | - | 217 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
2096 | - | 218 | ADMUX = (ADMUX & 0xE0); |
1910 | - | 219 | //Set ADC Control and Status Register A |
220 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
||
2096 | - | 221 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
1910 | - | 222 | //Set ADC Control and Status Register B |
223 | //Trigger Source to Free Running Mode |
||
2096 | - | 224 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
225 | |||
226 | for (uint8_t i=0; i<MAX_AIRPRESSURE_WINDOW_LENGTH; i++) { |
||
227 | airPressureWindow[i] = 0; |
||
228 | } |
||
229 | windowedAirPressure = 0; |
||
230 | |||
231 | startAnalogConversionCycle(); |
||
232 | |||
1910 | - | 233 | // restore global interrupt flags |
234 | SREG = sreg; |
||
235 | } |
||
236 | |||
2096 | - | 237 | uint16_t rawGyroValue(uint8_t axis) { |
238 | return sensorInputs[AD_GYRO_PITCH-axis]; |
||
239 | } |
||
240 | |||
2099 | - | 241 | /* |
2096 | - | 242 | uint16_t rawAccValue(uint8_t axis) { |
243 | return sensorInputs[AD_ACC_PITCH-axis]; |
||
244 | } |
||
2099 | - | 245 | */ |
2096 | - | 246 | |
1910 | - | 247 | void measureNoise(const int16_t sensor, |
248 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
||
249 | if (sensor > (int16_t) (*noiseMeasurement)) { |
||
250 | *noiseMeasurement = sensor; |
||
251 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
||
252 | *noiseMeasurement = -sensor; |
||
253 | } else if (*noiseMeasurement > damping) { |
||
254 | *noiseMeasurement -= damping; |
||
255 | } else { |
||
256 | *noiseMeasurement = 0; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | /* |
||
261 | * Min.: 0 |
||
262 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
||
263 | */ |
||
264 | uint16_t getSimplePressure(int advalue) { |
||
2099 | - | 265 | return advalue; |
1910 | - | 266 | } |
267 | |||
2096 | - | 268 | void startAnalogConversionCycle(void) { |
269 | analogDataReady = 0; |
||
270 | |||
271 | // Stop the sampling. Cycle is over. |
||
272 | for (uint8_t i = 0; i < 8; i++) { |
||
273 | sensorInputs[i] = 0; |
||
274 | } |
||
275 | adState = 0; |
||
276 | adChannel = AD_GYRO_PITCH; |
||
277 | ADMUX = (ADMUX & 0xE0) | adChannel; |
||
278 | startADC(); |
||
1910 | - | 279 | } |
280 | |||
281 | /***************************************************** |
||
282 | * Interrupt Service Routine for ADC |
||
2096 | - | 283 | * Runs at 312.5 kHz or 3.2 �s. When all states are |
284 | * processed further conversions are stopped. |
||
1910 | - | 285 | *****************************************************/ |
286 | ISR(ADC_vect) { |
||
2096 | - | 287 | sensorInputs[adChannel] += ADC; |
288 | // set up for next state. |
||
289 | adState++; |
||
290 | if (adState < sizeof(channelsForStates)) { |
||
291 | adChannel = pgm_read_byte(&channelsForStates[adState]); |
||
292 | // set adc muxer to next adChannel |
||
293 | ADMUX = (ADMUX & 0xE0) | adChannel; |
||
294 | // after full cycle stop further interrupts |
||
295 | startADC(); |
||
296 | } else { |
||
297 | analogDataReady = 1; |
||
298 | // do not restart ADC converter. |
||
299 | } |
||
300 | } |
||
1910 | - | 301 | |
2099 | - | 302 | /* |
2096 | - | 303 | void measureGyroActivity(int16_t newValue) { |
304 | gyroActivity += (uint32_t)((int32_t)newValue * newValue); |
||
305 | } |
||
1910 | - | 306 | |
2096 | - | 307 | #define GADAMPING 6 |
308 | void dampenGyroActivity(void) { |
||
309 | static uint8_t cnt = 0; |
||
310 | if (++cnt >= IMUConfig.gyroActivityDamping) { |
||
311 | cnt = 0; |
||
312 | gyroActivity *= (uint32_t)((1L<<GADAMPING)-1); |
||
313 | gyroActivity >>= GADAMPING; |
||
314 | } |
||
315 | } |
||
316 | */ |
||
1910 | - | 317 | |
2096 | - | 318 | void analog_updateGyros(void) { |
319 | // for various filters... |
||
2099 | - | 320 | int16_t tempOffsetGyro[3], tempGyro; |
2096 | - | 321 | |
322 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
||
2099 | - | 323 | for (uint8_t axis=0; axis<3; axis++) { |
2096 | - | 324 | tempGyro = rawGyroValue(axis); |
325 | /* |
||
326 | * Process the gyro data for the PID controller. |
||
327 | */ |
||
328 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
||
329 | // gyro with a wider range, and helps counter saturation at full control. |
||
330 | |||
331 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
||
2099 | - | 332 | if (tempGyro < SENSOR_MIN) { |
2096 | - | 333 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
334 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
||
2099 | - | 335 | } else if (tempGyro > SENSOR_MAX) { |
2096 | - | 336 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
2099 | - | 337 | tempGyro = (tempGyro - SENSOR_MAX) * EXTRAPOLATION_SLOPE + SENSOR_MAX; |
2096 | - | 338 | } |
339 | } |
||
1910 | - | 340 | |
2096 | - | 341 | // 2) Apply sign and offset, scale before filtering. |
2099 | - | 342 | tempOffsetGyro[axis] = (tempGyro - gyroOffset.offsets[axis]); |
2096 | - | 343 | } |
1910 | - | 344 | |
2096 | - | 345 | // 2.1: Transform axes. |
2099 | - | 346 | rotate(tempOffsetGyro, IMUConfig.gyroQuadrant, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_PR, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_YAW); |
1910 | - | 347 | |
2099 | - | 348 | for (uint8_t axis=0; axis<3; axis++) { |
2096 | - | 349 | // 3) Filter. |
350 | tempOffsetGyro[axis] = (gyro_PID[axis] * (IMUConfig.gyroPIDFilterConstant - 1) + tempOffsetGyro[axis]) / IMUConfig.gyroPIDFilterConstant; |
||
1910 | - | 351 | |
2096 | - | 352 | // 4) Measure noise. |
353 | measureNoise(tempOffsetGyro[axis], &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
||
1910 | - | 354 | |
2096 | - | 355 | // 5) Differential measurement. |
356 | // gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro[axis] - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
||
357 | int16_t diff = tempOffsetGyro[axis] - gyro_PID[axis]; |
||
358 | gyroD[axis] -= gyroDWindow[axis][gyroDWindowIdx]; |
||
359 | gyroD[axis] += diff; |
||
360 | gyroDWindow[axis][gyroDWindowIdx] = diff; |
||
1910 | - | 361 | |
2096 | - | 362 | // 6) Done. |
363 | gyro_PID[axis] = tempOffsetGyro[axis]; |
||
1910 | - | 364 | |
2096 | - | 365 | // Prepare tempOffsetGyro for next calculation below... |
2099 | - | 366 | tempOffsetGyro[axis] = (rawGyroValue(axis) - gyroOffset.offsets[axis]); |
2096 | - | 367 | } |
1910 | - | 368 | |
2096 | - | 369 | /* |
370 | * Now process the data for attitude angles. |
||
371 | */ |
||
2099 | - | 372 | rotate(tempOffsetGyro, IMUConfig.gyroQuadrant, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_PR, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_YAW); |
1910 | - | 373 | |
2099 | - | 374 | // dampenGyroActivity(); |
375 | gyro_ATT[PITCH] = tempOffsetGyro[PITCH]; |
||
376 | gyro_ATT[ROLL] = tempOffsetGyro[ROLL]; |
||
1910 | - | 377 | |
2099 | - | 378 | /* |
379 | measureGyroActivity(gyroD[PITCH]); |
||
380 | measureGyroActivity(gyroD[ROLL]); |
||
2096 | - | 381 | measureGyroActivity(yawGyro); |
2099 | - | 382 | */ |
1910 | - | 383 | |
2096 | - | 384 | if (++gyroDWindowIdx >= IMUConfig.gyroDWindowLength) { |
385 | gyroDWindowIdx = 0; |
||
386 | } |
||
387 | } |
||
1910 | - | 388 | |
2096 | - | 389 | void analog_updateAirPressure(void) { |
2099 | - | 390 | uint16_t rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
391 | simpleAirPressure = rawAirPressure; |
||
2096 | - | 392 | } |
1910 | - | 393 | |
2096 | - | 394 | void analog_updateBatteryVoltage(void) { |
395 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
||
396 | // This is divided by 3 --> 10.34 counts per volt. |
||
397 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
||
1910 | - | 398 | } |
399 | |||
2096 | - | 400 | void analog_update(void) { |
401 | analog_updateGyros(); |
||
2099 | - | 402 | // analog_updateAccelerometers(); |
2096 | - | 403 | analog_updateAirPressure(); |
404 | analog_updateBatteryVoltage(); |
||
405 | #ifdef USE_MK3MAG |
||
406 | magneticHeading = volatileMagneticHeading; |
||
407 | #endif |
||
2099 | - | 408 | |
2096 | - | 409 | } |
1910 | - | 410 | |
2096 | - | 411 | void analog_setNeutral() { |
412 | gyro_init(); |
||
413 | |||
414 | if (gyroOffset_readFromEEProm()) { |
||
415 | printf("gyro offsets invalid%s",recal); |
||
2099 | - | 416 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_OVERSAMPLING; |
417 | gyroOffset.offsets[YAW] = 512 * GYRO_OVERSAMPLING; |
||
2096 | - | 418 | } |
2099 | - | 419 | |
420 | /* |
||
2096 | - | 421 | if (accOffset_readFromEEProm()) { |
422 | printf("acc. meter offsets invalid%s",recal); |
||
423 | accOffset.offsets[PITCH] = accOffset.offsets[ROLL] = 512 * ACC_OVERSAMPLING_XY; |
||
424 | accOffset.offsets[Z] = 717 * ACC_OVERSAMPLING_Z; |
||
425 | } |
||
2099 | - | 426 | */ |
1910 | - | 427 | |
2096 | - | 428 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
2099 | - | 429 | for (uint8_t i=PITCH; i<=YAW; i++) { |
2096 | - | 430 | gyroNoisePeak[i] = 0; |
431 | gyroD[i] = 0; |
||
432 | for (uint8_t j=0; j<GYRO_D_WINDOW_LENGTH; j++) { |
||
433 | gyroDWindow[i][j] = 0; |
||
434 | } |
||
435 | } |
||
436 | // Setting offset values has an influence in the analog.c ISR |
||
437 | // Therefore run measurement for 100ms to achive stable readings |
||
438 | delay_ms_with_adc_measurement(100, 0); |
||
1910 | - | 439 | |
2096 | - | 440 | gyroActivity = 0; |
441 | } |
||
1910 | - | 442 | |
2096 | - | 443 | void analog_calibrateGyros(void) { |
444 | #define GYRO_OFFSET_CYCLES 32 |
||
445 | uint8_t i, axis; |
||
446 | int32_t offsets[3] = { 0, 0, 0 }; |
||
447 | gyro_calibrate(); |
||
448 | |||
449 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
||
450 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
||
451 | delay_ms_with_adc_measurement(10, 1); |
||
452 | for (axis = PITCH; axis <= YAW; axis++) { |
||
453 | offsets[axis] += rawGyroValue(axis); |
||
454 | } |
||
455 | } |
||
456 | |||
457 | for (axis = PITCH; axis <= YAW; axis++) { |
||
458 | gyroOffset.offsets[axis] = (offsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
||
1910 | - | 459 | |
2099 | - | 460 | int16_t min = (512-200) * GYRO_OVERSAMPLING; |
461 | int16_t max = (512+200) * GYRO_OVERSAMPLING; |
||
2096 | - | 462 | if(gyroOffset.offsets[axis] < min || gyroOffset.offsets[axis] > max) |
463 | versionInfo.hardwareErrors[0] |= FC_ERROR0_GYRO_PITCH << axis; |
||
464 | } |
||
1910 | - | 465 | |
2096 | - | 466 | gyroOffset_writeToEEProm(); |
467 | startAnalogConversionCycle(); |
||
1910 | - | 468 | } |