Rev 1927 | Rev 2096 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1910 | - | 1 | #include <avr/interrupt.h> |
2 | #include <avr/pgmspace.h> |
||
3 | |||
4 | #include "analog.h" |
||
5 | #include "attitude.h" |
||
6 | #include "sensors.h" |
||
7 | |||
8 | // for Delay functions |
||
9 | #include "timer0.h" |
||
10 | |||
11 | // For DebugOut |
||
12 | #include "uart0.h" |
||
13 | |||
14 | // For reading and writing acc. meter offsets. |
||
15 | #include "eeprom.h" |
||
16 | |||
17 | // For DebugOut.Digital |
||
18 | #include "output.h" |
||
19 | |||
20 | /* |
||
21 | * For each A/D conversion cycle, each analog channel is sampled a number of times |
||
22 | * (see array channelsForStates), and the results for each channel are summed. |
||
23 | * Here are those for the gyros and the acc. meters. They are not zero-offset. |
||
24 | * They are exported in the analog.h file - but please do not use them! The only |
||
25 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
||
26 | * the offsets with the DAC. |
||
27 | */ |
||
28 | volatile int16_t rawGyroSum[3]; |
||
29 | volatile int16_t acc[3]; |
||
30 | volatile int16_t filteredAcc[2] = { 0,0 }; |
||
31 | |||
32 | /* |
||
33 | * These 4 exported variables are zero-offset. The "PID" ones are used |
||
34 | * in the attitude control as rotation rates. The "ATT" ones are for |
||
35 | * integration to angles. |
||
36 | */ |
||
37 | volatile int16_t gyro_PID[2]; |
||
38 | volatile int16_t gyro_ATT[2]; |
||
39 | volatile int16_t gyroD[3]; |
||
40 | volatile int16_t yawGyro; |
||
41 | |||
42 | /* |
||
43 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
||
44 | * standing still. They are used for adjusting the gyro and acc. meter values |
||
45 | * to be centered on zero. |
||
46 | */ |
||
47 | volatile int16_t gyroOffset[3] = { 512 * GYRO_SUMMATION_FACTOR_PITCHROLL, 512 |
||
48 | * GYRO_SUMMATION_FACTOR_PITCHROLL, 512 * GYRO_SUMMATION_FACTOR_YAW }; |
||
49 | |||
50 | volatile int16_t accOffset[3] = { 512 * ACC_SUMMATION_FACTOR_PITCHROLL, 512 |
||
51 | * ACC_SUMMATION_FACTOR_PITCHROLL, 512 * ACC_SUMMATION_FACTOR_Z }; |
||
52 | |||
53 | /* |
||
54 | * Air pressure |
||
55 | */ |
||
56 | volatile uint8_t rangewidth = 106; |
||
57 | |||
58 | // Direct from sensor, irrespective of range. |
||
59 | // volatile uint16_t rawAirPressure; |
||
60 | |||
61 | // Value of 2 samples, with range. |
||
62 | volatile uint16_t simpleAirPressure; |
||
63 | |||
64 | // Value of AIRPRESSURE_SUMMATION_FACTOR samples, with range, filtered. |
||
65 | volatile int32_t filteredAirPressure; |
||
66 | |||
67 | // Partial sum of AIRPRESSURE_SUMMATION_FACTOR samples. |
||
68 | volatile int32_t airPressureSum; |
||
69 | |||
70 | // The number of samples summed into airPressureSum so far. |
||
71 | volatile uint8_t pressureMeasurementCount; |
||
72 | |||
73 | /* |
||
74 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
||
75 | * That is divided by 3 below, for a final 10.34 per volt. |
||
76 | * So the initial value of 100 is for 9.7 volts. |
||
77 | */ |
||
78 | volatile int16_t UBat = 100; |
||
79 | |||
80 | /* |
||
81 | * Control and status. |
||
82 | */ |
||
83 | volatile uint16_t ADCycleCount = 0; |
||
84 | volatile uint8_t analogDataReady = 1; |
||
85 | |||
86 | /* |
||
87 | * Experiment: Measuring vibration-induced sensor noise. |
||
88 | */ |
||
89 | volatile uint16_t gyroNoisePeak[2]; |
||
90 | volatile uint16_t accNoisePeak[2]; |
||
91 | |||
92 | // ADC channels |
||
93 | #define AD_GYRO_YAW 0 |
||
94 | #define AD_GYRO_ROLL 1 |
||
95 | #define AD_GYRO_PITCH 2 |
||
96 | #define AD_AIRPRESSURE 3 |
||
97 | #define AD_UBAT 4 |
||
98 | #define AD_ACC_Z 5 |
||
99 | #define AD_ACC_ROLL 6 |
||
100 | #define AD_ACC_PITCH 7 |
||
101 | |||
102 | /* |
||
103 | * Table of AD converter inputs for each state. |
||
104 | * The number of samples summed for each channel is equal to |
||
105 | * the number of times the channel appears in the array. |
||
106 | * The max. number of samples that can be taken in 2 ms is: |
||
107 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
||
108 | * loop needs a little time between reading AD values and |
||
109 | * re-enabling ADC, the real limit is (how much?) lower. |
||
110 | * The acc. sensor is sampled even if not used - or installed |
||
111 | * at all. The cost is not significant. |
||
112 | */ |
||
113 | |||
114 | const uint8_t channelsForStates[] PROGMEM = { |
||
115 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, |
||
116 | AD_ACC_PITCH, AD_ACC_ROLL, AD_AIRPRESSURE, |
||
117 | |||
118 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_ACC_Z, // at 8, measure Z acc. |
||
119 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, // at 11, finish yaw gyro |
||
120 | |||
121 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
||
122 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
||
123 | AD_AIRPRESSURE, // at 14, finish air pressure. |
||
124 | |||
125 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
||
126 | AD_GYRO_ROLL, // at 16, finish roll gyro |
||
127 | AD_UBAT // at 17, measure battery. |
||
128 | }; |
||
129 | |||
130 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
||
131 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
||
132 | |||
133 | void analog_init(void) { |
||
134 | uint8_t sreg = SREG; |
||
135 | // disable all interrupts before reconfiguration |
||
136 | cli(); |
||
137 | |||
138 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
||
139 | DDRA = 0x00; |
||
140 | PORTA = 0x00; |
||
141 | // Digital Input Disable Register 0 |
||
142 | // Disable digital input buffer for analog adc_channel pins |
||
143 | DIDR0 = 0xFF; |
||
144 | // external reference, adjust data to the right |
||
145 | ADMUX &= ~((1 << REFS1) | (1 << REFS0) | (1 << ADLAR)); |
||
146 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
||
147 | ADMUX = (ADMUX & 0xE0) | AD_GYRO_PITCH; |
||
148 | //Set ADC Control and Status Register A |
||
149 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
||
150 | ADCSRA = (0 << ADEN) | (0 << ADSC) | (0 << ADATE) | (1 << ADPS2) | (1 |
||
151 | << ADPS1) | (1 << ADPS0) | (0 << ADIE); |
||
152 | //Set ADC Control and Status Register B |
||
153 | //Trigger Source to Free Running Mode |
||
154 | ADCSRB &= ~((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0)); |
||
155 | // Start AD conversion |
||
156 | analog_start(); |
||
157 | // restore global interrupt flags |
||
158 | SREG = sreg; |
||
159 | } |
||
160 | |||
161 | void measureNoise(const int16_t sensor, |
||
162 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
||
163 | if (sensor > (int16_t) (*noiseMeasurement)) { |
||
164 | *noiseMeasurement = sensor; |
||
165 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
||
166 | *noiseMeasurement = -sensor; |
||
167 | } else if (*noiseMeasurement > damping) { |
||
168 | *noiseMeasurement -= damping; |
||
169 | } else { |
||
170 | *noiseMeasurement = 0; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /* |
||
175 | * Min.: 0 |
||
176 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
||
177 | */ |
||
178 | uint16_t getSimplePressure(int advalue) { |
||
179 | return (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
||
180 | } |
||
181 | |||
2022 | - | 182 | /* |
183 | * In the MK coordinate system, nose-down is positive and left-roll is positive. |
||
184 | * If a sensor is used in an orientation where one but not both of the axes has |
||
185 | * an opposite sign, PR_ORIENTATION_REVERSED is set to 1 (true). |
||
186 | */ |
||
1910 | - | 187 | void transformPRGyro(int16_t* result) { |
188 | static const uint8_t tab[] = {1,1,0,0-1,-1,-1,0,1}; |
||
2022 | - | 189 | // Pitch to Pitch part |
190 | int8_t pp = PR_GYROS_ORIENTATION_REVERSED ? tab[(GYRO_QUADRANT+4)%8] : tab[GYRO_QUADRANT]; |
||
191 | // Pitch to Roll part |
||
1910 | - | 192 | int8_t pr = tab[(GYRO_QUADRANT+2)%8]; |
2022 | - | 193 | // Roll to Roll part |
194 | int8_t rp = PR_GYROS_ORIENTATION_REVERSED ? tab[(GYRO_QUADRANT+2)%8] : tab[(GYRO_QUADRANT+6)%8]; |
||
195 | // Roll to Roll part |
||
1910 | - | 196 | int8_t rr = tab[GYRO_QUADRANT]; |
197 | |||
2022 | - | 198 | int16_t pitchIn = result[PITCH]; |
199 | |||
200 | result[PITCH] = pp*result[PITCH] + pr*result[ROLL]; |
||
201 | result[ROLL] = rp*pitchIn + rr*result[ROLL]; |
||
1910 | - | 202 | } |
203 | |||
204 | /***************************************************** |
||
205 | * Interrupt Service Routine for ADC |
||
2022 | - | 206 | * Runs at 312.5 kHz or 3.2 us. When all states are |
1910 | - | 207 | * processed the interrupt is disabled and further |
208 | * AD conversions are stopped. |
||
209 | *****************************************************/ |
||
210 | ISR(ADC_vect) { |
||
211 | static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
||
212 | static uint16_t sensorInputs[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
||
213 | static uint16_t pressureAutorangingWait = 25; |
||
214 | uint16_t rawAirPressure; |
||
215 | uint8_t i, axis; |
||
216 | int16_t newrange; |
||
217 | |||
218 | // for various filters... |
||
219 | int16_t tempOffsetGyro[2]; |
||
220 | |||
221 | sensorInputs[ad_channel] += ADC; |
||
222 | |||
223 | /* |
||
224 | * Actually we don't need this "switch". We could do all the sampling into the |
||
225 | * sensorInputs array first, and all the processing after the last sample. |
||
226 | */ |
||
227 | switch (state++) { |
||
228 | |||
229 | case 8: // Z acc |
||
230 | if (Z_ACC_REVERSED) |
||
231 | acc[Z] = accOffset[Z] - sensorInputs[AD_ACC_Z]; |
||
232 | else |
||
233 | acc[Z] = sensorInputs[AD_ACC_Z] - accOffset[Z]; |
||
234 | |||
235 | /* |
||
236 | stronglyFilteredAcc[Z] = |
||
237 | (stronglyFilteredAcc[Z] * 99 + acc[Z] * 10) / 100; |
||
238 | */ |
||
239 | |||
240 | break; |
||
241 | |||
242 | case 11: // yaw gyro |
||
243 | rawGyroSum[YAW] = sensorInputs[AD_GYRO_YAW]; |
||
2022 | - | 244 | if (YAW_GYRO_REVERSED) |
1910 | - | 245 | tempOffsetGyro[0] = gyroOffset[YAW] - sensorInputs[AD_GYRO_YAW]; |
246 | else |
||
247 | tempOffsetGyro[0] = sensorInputs[AD_GYRO_YAW] - gyroOffset[YAW]; |
||
2022 | - | 248 | gyroD[YAW] = (gyroD[YAW] * (staticParams.DGyroFilter - 1) + (tempOffsetGyro[0] - yawGyro)) / staticParams.DGyroFilter; |
1910 | - | 249 | yawGyro = tempOffsetGyro[0]; |
250 | break; |
||
251 | case 13: // roll axis acc. |
||
252 | |||
253 | // We have no sensor installed... |
||
254 | acc[PITCH] = acc[ROLL] = 0; |
||
255 | |||
256 | for (axis=0; axis<2; axis++) { |
||
257 | filteredAcc[axis] = |
||
2022 | - | 258 | (filteredAcc[axis] * (staticParams.accFilter - 1) + acc[axis]) / staticParams.accFilter; |
1910 | - | 259 | measureNoise(acc[axis], &accNoisePeak[axis], 1); |
260 | } |
||
261 | break; |
||
262 | |||
263 | case 14: // air pressure |
||
264 | if (pressureAutorangingWait) { |
||
265 | //A range switch was done recently. Wait for steadying. |
||
266 | pressureAutorangingWait--; |
||
267 | DebugOut.Analog[27] = (uint16_t) OCR0A; |
||
268 | DebugOut.Analog[31] = simpleAirPressure; |
||
269 | break; |
||
270 | } |
||
271 | |||
272 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
||
273 | if (rawAirPressure < MIN_RAWPRESSURE) { |
||
274 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
||
275 | newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
||
276 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
||
277 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
||
278 | OCR0A = newrange; |
||
279 | } else { |
||
280 | if (OCR0A) { |
||
281 | OCR0A--; |
||
282 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
||
283 | } |
||
284 | } |
||
285 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
||
286 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
||
287 | // If near the end, make a limited increase |
||
288 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
||
289 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
||
290 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
||
291 | OCR0A = newrange; |
||
292 | } else { |
||
293 | if (OCR0A < 254) { |
||
294 | OCR0A++; |
||
295 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
||
296 | } |
||
297 | } |
||
298 | } |
||
299 | |||
300 | // Even if the sample is off-range, use it. |
||
301 | simpleAirPressure = getSimplePressure(rawAirPressure); |
||
302 | DebugOut.Analog[27] = (uint16_t) OCR0A; |
||
303 | DebugOut.Analog[31] = simpleAirPressure; |
||
304 | |||
305 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
||
306 | // Danger: pressure near lower end of range. If the measurement saturates, the |
||
307 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
||
308 | DebugOut.Digital[1] |= DEBUG_SENSORLIMIT; |
||
309 | airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
||
310 | + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
||
311 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
||
312 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
||
313 | // Danger: pressure near upper end of range. If the measurement saturates, the |
||
314 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
||
315 | DebugOut.Digital[1] |= DEBUG_SENSORLIMIT; |
||
316 | airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
||
317 | + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
||
318 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
||
319 | } else { |
||
320 | // normal case. |
||
321 | // If AIRPRESSURE_SUMMATION_FACTOR is an odd number we only want to add half the double sample. |
||
322 | // The 2 cases above (end of range) are ignored for this. |
||
323 | DebugOut.Digital[1] &= ~DEBUG_SENSORLIMIT; |
||
324 | if (pressureMeasurementCount == AIRPRESSURE_SUMMATION_FACTOR - 1) |
||
325 | airPressureSum += simpleAirPressure / 2; |
||
326 | else |
||
327 | airPressureSum += simpleAirPressure; |
||
328 | } |
||
329 | |||
330 | // 2 samples were added. |
||
331 | pressureMeasurementCount += 2; |
||
332 | if (pressureMeasurementCount >= AIRPRESSURE_SUMMATION_FACTOR) { |
||
333 | filteredAirPressure = (filteredAirPressure * (AIRPRESSURE_FILTER - 1) |
||
334 | + airPressureSum + AIRPRESSURE_FILTER / 2) / AIRPRESSURE_FILTER; |
||
335 | pressureMeasurementCount = airPressureSum = 0; |
||
336 | } |
||
337 | |||
338 | break; |
||
339 | |||
340 | case 16: // pitch and roll gyro. |
||
341 | for (axis=0; axis<2; axis++) { |
||
342 | tempOffsetGyro[axis] = rawGyroSum[axis] = sensorInputs[AD_GYRO_PITCH - axis]; |
||
343 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
||
344 | // gyro with a wider range, and helps counter saturation at full control. |
||
345 | |||
346 | if (staticParams.GlobalConfig & CFG_ROTARY_RATE_LIMITER) { |
||
347 | if (tempOffsetGyro[axis] < SENSOR_MIN_PITCHROLL) { |
||
348 | DebugOut.Digital[0] |= DEBUG_SENSORLIMIT; |
||
349 | tempOffsetGyro[axis] = tempOffsetGyro[axis] * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
||
350 | } else if (tempOffsetGyro[axis] > SENSOR_MAX_PITCHROLL) { |
||
351 | DebugOut.Digital[0] |= DEBUG_SENSORLIMIT; |
||
352 | tempOffsetGyro[axis] = (tempOffsetGyro[axis] - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL; |
||
353 | } else { |
||
354 | DebugOut.Digital[0] &= ~DEBUG_SENSORLIMIT; |
||
355 | } |
||
356 | } |
||
357 | |||
358 | // 2) Apply sign and offset, scale before filtering. |
||
359 | tempOffsetGyro[axis] = (tempOffsetGyro[axis] - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL; |
||
360 | } |
||
361 | |||
362 | // 2.1: Transform axis if configured to. |
||
363 | transformPRGyro(tempOffsetGyro); |
||
364 | |||
365 | // 3) Scale and filter. |
||
366 | for (axis=0; axis<2; axis++) { |
||
2022 | - | 367 | tempOffsetGyro[axis] = (gyro_PID[axis] * (staticParams.PIDGyroFilter - 1) + tempOffsetGyro[axis]) / staticParams.PIDGyroFilter; |
1910 | - | 368 | |
369 | // 4) Measure noise. |
||
370 | measureNoise(tempOffsetGyro[axis], &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
||
371 | |||
372 | // 5) Differential measurement. |
||
2022 | - | 373 | gyroD[axis] = (gyroD[axis] * (staticParams.DGyroFilter - 1) + (tempOffsetGyro[axis] - gyro_PID[axis])) / staticParams.DGyroFilter; |
1910 | - | 374 | |
375 | // 6) Done. |
||
376 | gyro_PID[axis] = tempOffsetGyro[axis]; |
||
377 | } |
||
378 | |||
379 | /* |
||
380 | * Now process the data for attitude angles. |
||
381 | */ |
||
382 | for (axis=0; axis<2; axis++) { |
||
383 | tempOffsetGyro[axis] = (rawGyroSum[axis] - gyroOffset[axis]) * GYRO_FACTOR_PITCHROLL; |
||
384 | } |
||
385 | |||
386 | transformPRGyro(tempOffsetGyro); |
||
387 | |||
388 | // 2) Filter. This should really be quite unnecessary. The integration should gobble up any noise anyway and the values are not used for anything else. |
||
2022 | - | 389 | gyro_ATT[PITCH] = (gyro_ATT[PITCH] * (staticParams.attitudeGyroFilter - 1) + tempOffsetGyro[PITCH]) / staticParams.attitudeGyroFilter; |
390 | gyro_ATT[ROLL] = (gyro_ATT[ROLL] * (staticParams.attitudeGyroFilter - 1) + tempOffsetGyro[ROLL]) / staticParams.attitudeGyroFilter; |
||
1910 | - | 391 | break; |
392 | |||
393 | case 17: |
||
394 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
||
395 | // This is divided by 3 --> 10.34 counts per volt. |
||
396 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
||
397 | DebugOut.Analog[20] = UBat; |
||
398 | analogDataReady = 1; // mark |
||
399 | ADCycleCount++; |
||
400 | // Stop the sampling. Cycle is over. |
||
401 | state = 0; |
||
402 | for (i = 0; i < 8; i++) { |
||
403 | sensorInputs[i] = 0; |
||
404 | } |
||
405 | break; |
||
406 | default: { |
||
407 | } // do nothing. |
||
408 | } |
||
409 | |||
410 | // set up for next state. |
||
411 | ad_channel = pgm_read_byte(&channelsForStates[state]); |
||
412 | // ad_channel = channelsForStates[state]; |
||
413 | |||
414 | // set adc muxer to next ad_channel |
||
415 | ADMUX = (ADMUX & 0xE0) | ad_channel; |
||
416 | // after full cycle stop further interrupts |
||
417 | if (state) |
||
418 | analog_start(); |
||
419 | } |
||
420 | |||
421 | void analog_calibrate(void) { |
||
422 | #define GYRO_OFFSET_CYCLES 32 |
||
423 | uint8_t i, axis; |
||
424 | int32_t deltaOffsets[3] = { 0, 0, 0 }; |
||
425 | |||
426 | gyro_calibrate(); |
||
427 | |||
428 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
||
429 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
||
430 | delay_ms_Mess(20); |
||
431 | for (axis = PITCH; axis <= YAW; axis++) { |
||
432 | deltaOffsets[axis] += rawGyroSum[axis]; |
||
433 | } |
||
434 | } |
||
435 | |||
436 | for (axis = PITCH; axis <= YAW; axis++) { |
||
437 | gyroOffset[axis] = (deltaOffsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
||
438 | // DebugOut.Analog[20 + axis] = gyroOffset[axis]; |
||
439 | } |
||
440 | |||
441 | // Noise is relativ to offset. So, reset noise measurements when changing offsets. |
||
442 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
||
443 | |||
444 | accOffset[PITCH] = GetParamWord(PID_ACC_PITCH); |
||
445 | accOffset[ROLL] = GetParamWord(PID_ACC_ROLL); |
||
446 | accOffset[Z] = GetParamWord(PID_ACC_Z); |
||
447 | |||
448 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
||
449 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_SUMMATION_FACTOR/2); |
||
450 | // pressureMeasurementCount = 0; |
||
451 | |||
452 | delay_ms_Mess(100); |
||
453 | } |
||
454 | |||
455 | /* |
||
456 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
||
457 | * Does not (!} update the local variables. This must be done with a |
||
458 | * call to analog_calibrate() - this always (?) is done by the caller |
||
459 | * anyway. There would be nothing wrong with updating the variables |
||
460 | * directly from here, though. |
||
461 | */ |
||
462 | void analog_calibrateAcc(void) { |
||
463 | #define ACC_OFFSET_CYCLES 10 |
||
464 | /* |
||
465 | uint8_t i, axis; |
||
466 | int32_t deltaOffset[3] = { 0, 0, 0 }; |
||
467 | int16_t filteredDelta; |
||
468 | // int16_t pressureDiff, savedRawAirPressure; |
||
469 | |||
470 | for (i = 0; i < ACC_OFFSET_CYCLES; i++) { |
||
471 | delay_ms_Mess(10); |
||
472 | for (axis = PITCH; axis <= YAW; axis++) { |
||
473 | deltaOffset[axis] += acc[axis]; |
||
474 | } |
||
475 | } |
||
476 | |||
477 | for (axis = PITCH; axis <= YAW; axis++) { |
||
478 | filteredDelta = (deltaOffset[axis] + ACC_OFFSET_CYCLES / 2) |
||
479 | / ACC_OFFSET_CYCLES; |
||
480 | accOffset[axis] += ACC_REVERSED[axis] ? -filteredDelta : filteredDelta; |
||
481 | } |
||
482 | |||
483 | // Save ACC neutral settings to eeprom |
||
484 | SetParamWord(PID_ACC_PITCH, accOffset[PITCH]); |
||
485 | SetParamWord(PID_ACC_ROLL, accOffset[ROLL]); |
||
486 | SetParamWord(PID_ACC_Z, accOffset[Z]); |
||
487 | |||
488 | // Noise is relative to offset. So, reset noise measurements when |
||
489 | // changing offsets. |
||
490 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
||
491 | |||
492 | // Setting offset values has an influence in the analog.c ISR |
||
493 | // Therefore run measurement for 100ms to achive stable readings |
||
494 | delay_ms_Mess(100); |
||
495 | |||
496 | */ |
||
497 | // Set the feedback so that air pressure ends up in the middle of the range. |
||
498 | // (raw pressure high --> OCR0A also high...) |
||
499 | /* |
||
500 | OCR0A += ((rawAirPressure - 1024) / rangewidth) - 1; |
||
501 | delay_ms_Mess(1000); |
||
502 | |||
503 | pressureDiff = 0; |
||
504 | // DebugOut.Analog[16] = rawAirPressure; |
||
505 | |||
506 | #define PRESSURE_CAL_CYCLE_COUNT 5 |
||
507 | for (i=0; i<PRESSURE_CAL_CYCLE_COUNT; i++) { |
||
508 | savedRawAirPressure = rawAirPressure; |
||
509 | OCR0A+=2; |
||
510 | delay_ms_Mess(500); |
||
511 | // raw pressure will decrease. |
||
512 | pressureDiff += (savedRawAirPressure - rawAirPressure); |
||
513 | savedRawAirPressure = rawAirPressure; |
||
514 | OCR0A-=2; |
||
515 | delay_ms_Mess(500); |
||
516 | // raw pressure will increase. |
||
517 | pressureDiff += (rawAirPressure - savedRawAirPressure); |
||
518 | } |
||
519 | |||
520 | rangewidth = (pressureDiff + PRESSURE_CAL_CYCLE_COUNT * 2 * 2 - 1) / (PRESSURE_CAL_CYCLE_COUNT * 2 * 2); |
||
521 | DebugOut.Analog[27] = rangewidth; |
||
522 | */ |
||
523 | } |