Rev 2035 | Rev 2051 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1612 | dongfang | 1 | #include <avr/io.h> |
2 | #include <avr/interrupt.h> |
||
3 | #include <avr/pgmspace.h> |
||
1864 | - | 4 | |
1612 | dongfang | 5 | #include "analog.h" |
1864 | - | 6 | #include "attitude.h" |
1612 | dongfang | 7 | #include "sensors.h" |
1964 | - | 8 | #include "printf_P.h" |
1612 | dongfang | 9 | |
10 | // for Delay functions |
||
11 | #include "timer0.h" |
||
12 | |||
1955 | - | 13 | // For debugOut |
1612 | dongfang | 14 | #include "uart0.h" |
15 | |||
16 | // For reading and writing acc. meter offsets. |
||
17 | #include "eeprom.h" |
||
18 | |||
1955 | - | 19 | // For debugOut.digital |
1796 | - | 20 | #include "output.h" |
21 | |||
1952 | - | 22 | // set ADC enable & ADC Start Conversion & ADC Interrupt Enable bit |
23 | #define startADC() (ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE)) |
||
24 | |||
1969 | - | 25 | const char* recal = ", recalibration needed."; |
26 | |||
1854 | - | 27 | /* |
28 | * For each A/D conversion cycle, each analog channel is sampled a number of times |
||
29 | * (see array channelsForStates), and the results for each channel are summed. |
||
1645 | - | 30 | * Here are those for the gyros and the acc. meters. They are not zero-offset. |
1612 | dongfang | 31 | * They are exported in the analog.h file - but please do not use them! The only |
32 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
||
33 | * the offsets with the DAC. |
||
34 | */ |
||
1952 | - | 35 | volatile uint16_t sensorInputs[8]; |
2015 | - | 36 | int16_t acc[3]; |
37 | int16_t filteredAcc[3] = { 0,0,0 }; |
||
1612 | dongfang | 38 | |
39 | /* |
||
1645 | - | 40 | * These 4 exported variables are zero-offset. The "PID" ones are used |
41 | * in the attitude control as rotation rates. The "ATT" ones are for |
||
1854 | - | 42 | * integration to angles. |
1612 | dongfang | 43 | */ |
2015 | - | 44 | int16_t gyro_PID[2]; |
45 | int16_t gyro_ATT[2]; |
||
46 | int16_t gyroD[2]; |
||
47 | int16_t yawGyro; |
||
1612 | dongfang | 48 | |
2033 | - | 49 | int32_t groundPressure; |
50 | |||
1612 | dongfang | 51 | /* |
52 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
||
53 | * standing still. They are used for adjusting the gyro and acc. meter values |
||
1645 | - | 54 | * to be centered on zero. |
1612 | dongfang | 55 | */ |
56 | |||
1969 | - | 57 | sensorOffset_t gyroOffset; |
58 | sensorOffset_t accOffset; |
||
59 | sensorOffset_t gyroAmplifierOffset; |
||
1960 | - | 60 | |
1612 | dongfang | 61 | /* |
2015 | - | 62 | * In the MK coordinate system, nose-down is positive and left-roll is positive. |
63 | * If a sensor is used in an orientation where one but not both of the axes has |
||
64 | * an opposite sign, PR_ORIENTATION_REVERSED is set to 1 (true). |
||
65 | * Transform: |
||
66 | * pitch <- pp*pitch + pr*roll |
||
67 | * roll <- rp*pitch + rr*roll |
||
68 | * Not reversed, GYRO_QUADRANT: |
||
69 | * 0: pp=1, pr=0, rp=0, rr=1 // 0 degrees |
||
70 | * 1: pp=1, pr=-1,rp=1, rr=1 // +45 degrees |
||
71 | * 2: pp=0, pr=-1,rp=1, rr=0 // +90 degrees |
||
72 | * 3: pp=-1,pr=-1,rp=1, rr=1 // +135 degrees |
||
73 | * 4: pp=-1,pr=0, rp=0, rr=-1 // +180 degrees |
||
74 | * 5: pp=-1,pr=1, rp=-1,rr=-1 // +225 degrees |
||
75 | * 6: pp=0, pr=1, rp=-1,rr=0 // +270 degrees |
||
76 | * 7: pp=1, pr=1, rp=-1,rr=1 // +315 degrees |
||
77 | * Reversed, GYRO_QUADRANT: |
||
78 | * 0: pp=-1,pr=0, rp=0, rr=1 // 0 degrees with pitch reversed |
||
79 | * 1: pp=-1,pr=-1,rp=-1,rr=1 // +45 degrees with pitch reversed |
||
80 | * 2: pp=0, pr=-1,rp=-1,rr=0 // +90 degrees with pitch reversed |
||
81 | * 3: pp=1, pr=-1,rp=-1,rr=1 // +135 degrees with pitch reversed |
||
82 | * 4: pp=1, pr=0, rp=0, rr=-1 // +180 degrees with pitch reversed |
||
83 | * 5: pp=1, pr=1, rp=1, rr=-1 // +225 degrees with pitch reversed |
||
84 | * 6: pp=0, pr=1, rp=1, rr=0 // +270 degrees with pitch reversed |
||
85 | * 7: pp=-1,pr=1, rp=1, rr=1 // +315 degrees with pitch reversed |
||
1612 | dongfang | 86 | */ |
87 | |||
2015 | - | 88 | void rotate(int16_t* result, uint8_t quadrant, uint8_t reverse) { |
89 | static const int8_t rotationTab[] = {1,1,0,-1,-1,-1,0,1}; |
||
90 | // Pitch to Pitch part |
||
2020 | - | 91 | int8_t xx = reverse ? rotationTab[(quadrant+4)%8] : rotationTab[quadrant]; |
2015 | - | 92 | // Roll to Pitch part |
93 | int8_t xy = rotationTab[(quadrant+2)%8]; |
||
94 | // Pitch to Roll part |
||
95 | int8_t yx = reverse ? rotationTab[(quadrant+2)%8] : rotationTab[(quadrant+6)%8]; |
||
96 | // Roll to Roll part |
||
97 | int8_t yy = rotationTab[quadrant]; |
||
98 | |||
99 | int16_t xIn = result[0]; |
||
2019 | - | 100 | result[0] = xx*xIn + xy*result[1]; |
2015 | - | 101 | result[1] = yx*xIn + yy*result[1]; |
102 | |||
103 | if (quadrant & 1) { |
||
104 | // A rotation was used above, where the factors were too large by sqrt(2). |
||
105 | // So, we multiply by 2^n/sqt(2) and right shift n bits, as to divide by sqrt(2). |
||
106 | // A suitable value for n: Sample is 11 bits. After transformation it is the sum |
||
107 | // of 2 11 bit numbers, so 12 bits. We have 4 bits left... |
||
108 | result[0] = (result[0]*11) >> 4; |
||
109 | result[1] = (result[1]*11) >> 4; |
||
110 | } |
||
111 | } |
||
2019 | - | 112 | |
1645 | - | 113 | /* |
1775 | - | 114 | * Air pressure |
1645 | - | 115 | */ |
1970 | - | 116 | volatile uint8_t rangewidth = 105; |
1612 | dongfang | 117 | |
1775 | - | 118 | // Direct from sensor, irrespective of range. |
119 | // volatile uint16_t rawAirPressure; |
||
120 | |||
121 | // Value of 2 samples, with range. |
||
2015 | - | 122 | uint16_t simpleAirPressure; |
1775 | - | 123 | |
2019 | - | 124 | // Value of AIRPRESSURE_OVERSAMPLING samples, with range, filtered. |
2015 | - | 125 | int32_t filteredAirPressure; |
2033 | - | 126 | int32_t lastFilteredAirPressure; |
1775 | - | 127 | |
2036 | - | 128 | #define MAX_AIRPRESSURE_WINDOW_LENGTH 32 |
2026 | - | 129 | int16_t airPressureWindow[MAX_AIRPRESSURE_WINDOW_LENGTH]; |
130 | int32_t windowedAirPressure; |
||
131 | uint8_t windowPtr; |
||
132 | |||
1775 | - | 133 | // Partial sum of AIRPRESSURE_SUMMATION_FACTOR samples. |
2015 | - | 134 | int32_t airPressureSum; |
1775 | - | 135 | |
136 | // The number of samples summed into airPressureSum so far. |
||
2015 | - | 137 | uint8_t pressureMeasurementCount; |
1775 | - | 138 | |
1612 | dongfang | 139 | /* |
1854 | - | 140 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
1612 | dongfang | 141 | * That is divided by 3 below, for a final 10.34 per volt. |
142 | * So the initial value of 100 is for 9.7 volts. |
||
143 | */ |
||
2015 | - | 144 | int16_t UBat = 100; |
1612 | dongfang | 145 | |
146 | /* |
||
147 | * Control and status. |
||
148 | */ |
||
149 | volatile uint16_t ADCycleCount = 0; |
||
150 | volatile uint8_t analogDataReady = 1; |
||
151 | |||
152 | /* |
||
153 | * Experiment: Measuring vibration-induced sensor noise. |
||
154 | */ |
||
2015 | - | 155 | uint16_t gyroNoisePeak[3]; |
156 | uint16_t accNoisePeak[3]; |
||
1612 | dongfang | 157 | |
1986 | - | 158 | volatile uint8_t adState; |
1987 | - | 159 | volatile uint8_t adChannel; |
1986 | - | 160 | |
1612 | dongfang | 161 | // ADC channels |
1645 | - | 162 | #define AD_GYRO_YAW 0 |
163 | #define AD_GYRO_ROLL 1 |
||
1634 | - | 164 | #define AD_GYRO_PITCH 2 |
165 | #define AD_AIRPRESSURE 3 |
||
1645 | - | 166 | #define AD_UBAT 4 |
167 | #define AD_ACC_Z 5 |
||
168 | #define AD_ACC_ROLL 6 |
||
169 | #define AD_ACC_PITCH 7 |
||
1612 | dongfang | 170 | |
171 | /* |
||
172 | * Table of AD converter inputs for each state. |
||
1854 | - | 173 | * The number of samples summed for each channel is equal to |
1612 | dongfang | 174 | * the number of times the channel appears in the array. |
175 | * The max. number of samples that can be taken in 2 ms is: |
||
1854 | - | 176 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
177 | * loop needs a little time between reading AD values and |
||
1612 | dongfang | 178 | * re-enabling ADC, the real limit is (how much?) lower. |
179 | * The acc. sensor is sampled even if not used - or installed |
||
180 | * at all. The cost is not significant. |
||
181 | */ |
||
182 | |||
1870 | - | 183 | const uint8_t channelsForStates[] PROGMEM = { |
184 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, |
||
185 | AD_ACC_PITCH, AD_ACC_ROLL, AD_AIRPRESSURE, |
||
1612 | dongfang | 186 | |
1870 | - | 187 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_ACC_Z, // at 8, measure Z acc. |
188 | AD_GYRO_PITCH, AD_GYRO_ROLL, AD_GYRO_YAW, // at 11, finish yaw gyro |
||
189 | |||
190 | AD_ACC_PITCH, // at 12, finish pitch axis acc. |
||
191 | AD_ACC_ROLL, // at 13, finish roll axis acc. |
||
192 | AD_AIRPRESSURE, // at 14, finish air pressure. |
||
193 | |||
194 | AD_GYRO_PITCH, // at 15, finish pitch gyro |
||
195 | AD_GYRO_ROLL, // at 16, finish roll gyro |
||
196 | AD_UBAT // at 17, measure battery. |
||
197 | }; |
||
1612 | dongfang | 198 | |
199 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
||
200 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
||
201 | |||
202 | void analog_init(void) { |
||
1821 | - | 203 | uint8_t sreg = SREG; |
204 | // disable all interrupts before reconfiguration |
||
205 | cli(); |
||
1612 | dongfang | 206 | |
1821 | - | 207 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
208 | DDRA = 0x00; |
||
209 | PORTA = 0x00; |
||
210 | // Digital Input Disable Register 0 |
||
211 | // Disable digital input buffer for analog adc_channel pins |
||
212 | DIDR0 = 0xFF; |
||
213 | // external reference, adjust data to the right |
||
1952 | - | 214 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
1821 | - | 215 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
1987 | - | 216 | ADMUX = (ADMUX & 0xE0); |
1821 | - | 217 | //Set ADC Control and Status Register A |
218 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
||
1952 | - | 219 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
1821 | - | 220 | //Set ADC Control and Status Register B |
221 | //Trigger Source to Free Running Mode |
||
1952 | - | 222 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
223 | |||
2026 | - | 224 | for (uint8_t i=0; i<MAX_AIRPRESSURE_WINDOW_LENGTH; i++) { |
225 | airPressureWindow[i] = 0; |
||
226 | } |
||
2036 | - | 227 | windowedAirPressure = 0; |
2026 | - | 228 | |
1952 | - | 229 | startAnalogConversionCycle(); |
230 | |||
1821 | - | 231 | // restore global interrupt flags |
232 | SREG = sreg; |
||
1612 | dongfang | 233 | } |
234 | |||
2015 | - | 235 | uint16_t rawGyroValue(uint8_t axis) { |
236 | return sensorInputs[AD_GYRO_PITCH-axis]; |
||
237 | } |
||
238 | |||
239 | uint16_t rawAccValue(uint8_t axis) { |
||
240 | return sensorInputs[AD_ACC_PITCH-axis]; |
||
241 | } |
||
242 | |||
1821 | - | 243 | void measureNoise(const int16_t sensor, |
244 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
||
245 | if (sensor > (int16_t) (*noiseMeasurement)) { |
||
246 | *noiseMeasurement = sensor; |
||
247 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
||
248 | *noiseMeasurement = -sensor; |
||
249 | } else if (*noiseMeasurement > damping) { |
||
250 | *noiseMeasurement -= damping; |
||
251 | } else { |
||
252 | *noiseMeasurement = 0; |
||
253 | } |
||
1612 | dongfang | 254 | } |
255 | |||
1796 | - | 256 | /* |
257 | * Min.: 0 |
||
258 | * Max: About 106 * 240 + 2047 = 27487; it is OK with just a 16 bit type. |
||
259 | */ |
||
1775 | - | 260 | uint16_t getSimplePressure(int advalue) { |
2026 | - | 261 | uint16_t result = (uint16_t) OCR0A * (uint16_t) rangewidth + advalue; |
262 | result += (acc[Z] * (staticParams.airpressureAccZCorrection-128)) >> 10; |
||
263 | return result; |
||
1634 | - | 264 | } |
265 | |||
1952 | - | 266 | void startAnalogConversionCycle(void) { |
1960 | - | 267 | analogDataReady = 0; |
2017 | - | 268 | |
1952 | - | 269 | // Stop the sampling. Cycle is over. |
270 | for (uint8_t i = 0; i < 8; i++) { |
||
271 | sensorInputs[i] = 0; |
||
272 | } |
||
1986 | - | 273 | adState = 0; |
1987 | - | 274 | adChannel = AD_GYRO_PITCH; |
275 | ADMUX = (ADMUX & 0xE0) | adChannel; |
||
1952 | - | 276 | startADC(); |
277 | } |
||
278 | |||
1645 | - | 279 | /***************************************************** |
1854 | - | 280 | * Interrupt Service Routine for ADC |
1963 | - | 281 | * Runs at 312.5 kHz or 3.2 �s. When all states are |
1952 | - | 282 | * processed further conversions are stopped. |
1645 | - | 283 | *****************************************************/ |
1870 | - | 284 | ISR(ADC_vect) { |
1986 | - | 285 | sensorInputs[adChannel] += ADC; |
1952 | - | 286 | // set up for next state. |
1986 | - | 287 | adState++; |
288 | if (adState < sizeof(channelsForStates)) { |
||
289 | adChannel = pgm_read_byte(&channelsForStates[adState]); |
||
290 | // set adc muxer to next adChannel |
||
291 | ADMUX = (ADMUX & 0xE0) | adChannel; |
||
1952 | - | 292 | // after full cycle stop further interrupts |
293 | startADC(); |
||
294 | } else { |
||
295 | ADCycleCount++; |
||
296 | analogDataReady = 1; |
||
297 | // do not restart ADC converter. |
||
298 | } |
||
299 | } |
||
1612 | dongfang | 300 | |
1952 | - | 301 | void analog_updateGyros(void) { |
302 | // for various filters... |
||
2015 | - | 303 | int16_t tempOffsetGyro[2], tempGyro; |
1952 | - | 304 | |
1991 | - | 305 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
1952 | - | 306 | for (uint8_t axis=0; axis<2; axis++) { |
2015 | - | 307 | tempGyro = rawGyroValue(axis); |
1952 | - | 308 | /* |
309 | * Process the gyro data for the PID controller. |
||
310 | */ |
||
311 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
||
312 | // gyro with a wider range, and helps counter saturation at full control. |
||
313 | |||
1960 | - | 314 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
1952 | - | 315 | if (tempGyro < SENSOR_MIN_PITCHROLL) { |
2015 | - | 316 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
317 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
||
1952 | - | 318 | } else if (tempGyro > SENSOR_MAX_PITCHROLL) { |
2015 | - | 319 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
320 | tempGyro = (tempGyro - SENSOR_MAX_PITCHROLL) * EXTRAPOLATION_SLOPE + SENSOR_MAX_PITCHROLL; |
||
1952 | - | 321 | } |
322 | } |
||
2015 | - | 323 | |
1952 | - | 324 | // 2) Apply sign and offset, scale before filtering. |
2015 | - | 325 | tempOffsetGyro[axis] = (tempGyro - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
326 | } |
||
327 | |||
328 | // 2.1: Transform axes. |
||
2020 | - | 329 | rotate(tempOffsetGyro, staticParams.gyroQuadrant, staticParams.imuReversedFlags & IMU_REVERSE_GYRO_PR); |
2015 | - | 330 | |
331 | for (uint8_t axis=0; axis<2; axis++) { |
||
332 | // 3) Filter. |
||
333 | tempOffsetGyro[axis] = (gyro_PID[axis] * (staticParams.gyroPIDFilterConstant - 1) + tempOffsetGyro[axis]) / staticParams.gyroPIDFilterConstant; |
||
334 | |||
1952 | - | 335 | // 4) Measure noise. |
2015 | - | 336 | measureNoise(tempOffsetGyro[axis], &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
337 | |||
1952 | - | 338 | // 5) Differential measurement. |
2015 | - | 339 | gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro[axis] - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
340 | |||
1952 | - | 341 | // 6) Done. |
2015 | - | 342 | gyro_PID[axis] = tempOffsetGyro[axis]; |
343 | |||
344 | // Prepare tempOffsetGyro for next calculation below... |
||
345 | tempOffsetGyro[axis] = (rawGyroValue(axis) - gyroOffset.offsets[axis]) * GYRO_FACTOR_PITCHROLL; |
||
1952 | - | 346 | } |
347 | |||
2015 | - | 348 | /* |
349 | * Now process the data for attitude angles. |
||
350 | */ |
||
2020 | - | 351 | rotate(tempOffsetGyro, staticParams.gyroQuadrant, staticParams.imuReversedFlags & IMU_REVERSE_GYRO_PR); |
2015 | - | 352 | |
2017 | - | 353 | gyro_ATT[PITCH] = tempOffsetGyro[PITCH]; |
354 | gyro_ATT[ROLL] = tempOffsetGyro[ROLL]; |
||
355 | |||
356 | debugOut.analog[22 + 0] = gyro_PID[0]; |
||
357 | debugOut.analog[22 + 1] = gyro_PID[1]; |
||
358 | |||
359 | debugOut.analog[24 + 0] = gyro_ATT[0]; |
||
360 | debugOut.analog[24 + 1] = gyro_ATT[1]; |
||
361 | |||
2015 | - | 362 | // 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. |
363 | // gyro_ATT[PITCH] = (gyro_ATT[PITCH] * (staticParams.attitudeGyroFilter - 1) + tempOffsetGyro[PITCH]) / staticParams.attitudeGyroFilter; |
||
364 | // gyro_ATT[ROLL] = (gyro_ATT[ROLL] * (staticParams.attitudeGyroFilter - 1) + tempOffsetGyro[ROLL]) / staticParams.attitudeGyroFilter; |
||
365 | |||
1952 | - | 366 | // Yaw gyro. |
2020 | - | 367 | if (staticParams.imuReversedFlags & IMU_REVERSE_GYRO_YAW) |
1960 | - | 368 | yawGyro = gyroOffset.offsets[YAW] - sensorInputs[AD_GYRO_YAW]; |
1952 | - | 369 | else |
1960 | - | 370 | yawGyro = sensorInputs[AD_GYRO_YAW] - gyroOffset.offsets[YAW]; |
1952 | - | 371 | } |
1775 | - | 372 | |
1952 | - | 373 | void analog_updateAccelerometers(void) { |
374 | // Pitch and roll axis accelerations. |
||
375 | for (uint8_t axis=0; axis<2; axis++) { |
||
2015 | - | 376 | acc[axis] = rawAccValue(axis) - accOffset.offsets[axis]; |
1979 | - | 377 | } |
2015 | - | 378 | |
2020 | - | 379 | rotate(acc, staticParams.accQuadrant, staticParams.imuReversedFlags & IMU_REVERSE_ACC_XY); |
2015 | - | 380 | for(uint8_t axis=0; axis<3; axis++) { |
1960 | - | 381 | filteredAcc[axis] = (filteredAcc[axis] * (staticParams.accFilterConstant - 1) + acc[axis]) / staticParams.accFilterConstant; |
1952 | - | 382 | measureNoise(acc[axis], &accNoisePeak[axis], 1); |
383 | } |
||
2015 | - | 384 | |
385 | // Z acc. |
||
386 | if (staticParams.imuReversedFlags & 8) |
||
387 | acc[Z] = accOffset.offsets[Z] - sensorInputs[AD_ACC_Z]; |
||
388 | else |
||
389 | acc[Z] = sensorInputs[AD_ACC_Z] - accOffset.offsets[Z]; |
||
1952 | - | 390 | } |
1645 | - | 391 | |
1952 | - | 392 | void analog_updateAirPressure(void) { |
393 | static uint16_t pressureAutorangingWait = 25; |
||
394 | uint16_t rawAirPressure; |
||
395 | int16_t newrange; |
||
396 | // air pressure |
||
397 | if (pressureAutorangingWait) { |
||
398 | //A range switch was done recently. Wait for steadying. |
||
399 | pressureAutorangingWait--; |
||
400 | } else { |
||
401 | rawAirPressure = sensorInputs[AD_AIRPRESSURE]; |
||
402 | if (rawAirPressure < MIN_RAWPRESSURE) { |
||
403 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
||
404 | newrange = OCR0A - (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (MAX_RAWPRESSURE - rawAirPressure) / (rangewidth * 2) + 1; |
||
405 | if (newrange > MIN_RANGES_EXTRAPOLATION) { |
||
406 | pressureAutorangingWait = (OCR0A - newrange) * AUTORANGE_WAIT_FACTOR; // = OCRA0 - OCRA0 + |
||
407 | OCR0A = newrange; |
||
408 | } else { |
||
409 | if (OCR0A) { |
||
410 | OCR0A--; |
||
411 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
||
1821 | - | 412 | } |
1952 | - | 413 | } |
414 | } else if (rawAirPressure > MAX_RAWPRESSURE) { |
||
415 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
||
416 | // If near the end, make a limited increase |
||
417 | newrange = OCR0A + (MAX_RAWPRESSURE - MIN_RAWPRESSURE) / (rangewidth * 4); // 4; // (rawAirPressure - MIN_RAWPRESSURE) / (rangewidth * 2) - 1; |
||
418 | if (newrange < MAX_RANGES_EXTRAPOLATION) { |
||
419 | pressureAutorangingWait = (newrange - OCR0A) * AUTORANGE_WAIT_FACTOR; |
||
420 | OCR0A = newrange; |
||
421 | } else { |
||
422 | if (OCR0A < 254) { |
||
423 | OCR0A++; |
||
424 | pressureAutorangingWait = AUTORANGE_WAIT_FACTOR; |
||
425 | } |
||
426 | } |
||
427 | } |
||
428 | |||
429 | // Even if the sample is off-range, use it. |
||
430 | simpleAirPressure = getSimplePressure(rawAirPressure); |
||
431 | |||
432 | if (simpleAirPressure < MIN_RANGES_EXTRAPOLATION * rangewidth) { |
||
433 | // Danger: pressure near lower end of range. If the measurement saturates, the |
||
434 | // copter may climb uncontrolledly... Simulate a drastic reduction in pressure. |
||
1955 | - | 435 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
1952 | - | 436 | airPressureSum += (int16_t) MIN_RANGES_EXTRAPOLATION * rangewidth |
437 | + (simpleAirPressure - (int16_t) MIN_RANGES_EXTRAPOLATION |
||
438 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
||
439 | } else if (simpleAirPressure > MAX_RANGES_EXTRAPOLATION * rangewidth) { |
||
440 | // Danger: pressure near upper end of range. If the measurement saturates, the |
||
441 | // copter may descend uncontrolledly... Simulate a drastic increase in pressure. |
||
1955 | - | 442 | debugOut.digital[1] |= DEBUG_SENSORLIMIT; |
1952 | - | 443 | airPressureSum += (int16_t) MAX_RANGES_EXTRAPOLATION * rangewidth |
444 | + (simpleAirPressure - (int16_t) MAX_RANGES_EXTRAPOLATION |
||
445 | * rangewidth) * PRESSURE_EXTRAPOLATION_COEFF; |
||
446 | } else { |
||
447 | // normal case. |
||
2026 | - | 448 | // If AIRPRESSURE_OVERSAMPLING is an odd number we only want to add half the double sample. |
1952 | - | 449 | // The 2 cases above (end of range) are ignored for this. |
1955 | - | 450 | debugOut.digital[1] &= ~DEBUG_SENSORLIMIT; |
2035 | - | 451 | airPressureSum += simpleAirPressure; |
1952 | - | 452 | } |
453 | |||
454 | // 2 samples were added. |
||
455 | pressureMeasurementCount += 2; |
||
2035 | - | 456 | // Assumption here: AIRPRESSURE_OVERSAMPLING is even (well we all know it's 14 haha...) |
457 | if (pressureMeasurementCount == AIRPRESSURE_OVERSAMPLING) { |
||
458 | |||
459 | // The best oversampling count is 14.5. We add a quarter of the double ADC value to get the final half. |
||
460 | airPressureSum += simpleAirPressure >> 2; |
||
461 | |||
2033 | - | 462 | lastFilteredAirPressure = filteredAirPressure; |
2035 | - | 463 | |
2036 | - | 464 | |
2035 | - | 465 | if (!staticParams.airpressureWindowLength) { |
466 | filteredAirPressure = (filteredAirPressure * (staticParams.airpressureFilterConstant - 1) |
||
467 | + airPressureSum + staticParams.airpressureFilterConstant / 2) / staticParams.airpressureFilterConstant; |
||
468 | } else { |
||
469 | // use windowed. |
||
2036 | - | 470 | windowedAirPressure += simpleAirPressure; |
471 | windowedAirPressure -= airPressureWindow[windowPtr]; |
||
472 | airPressureWindow[windowPtr] = simpleAirPressure; |
||
473 | windowPtr = (windowPtr+1) % staticParams.airpressureWindowLength; |
||
474 | filteredAirPressure = windowedAirPressure / staticParams.airpressureWindowLength; |
||
2035 | - | 475 | } |
2036 | - | 476 | |
1952 | - | 477 | pressureMeasurementCount = airPressureSum = 0; |
478 | } |
||
479 | } |
||
480 | } |
||
1821 | - | 481 | |
1952 | - | 482 | void analog_updateBatteryVoltage(void) { |
483 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
||
484 | // This is divided by 3 --> 10.34 counts per volt. |
||
485 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
||
486 | } |
||
1821 | - | 487 | |
1952 | - | 488 | void analog_update(void) { |
489 | analog_updateGyros(); |
||
490 | analog_updateAccelerometers(); |
||
491 | analog_updateAirPressure(); |
||
492 | analog_updateBatteryVoltage(); |
||
1612 | dongfang | 493 | } |
494 | |||
1961 | - | 495 | void analog_setNeutral() { |
2018 | - | 496 | gyro_init(); |
497 | |||
1961 | - | 498 | if (gyroOffset_readFromEEProm()) { |
1969 | - | 499 | printf("gyro offsets invalid%s",recal); |
2019 | - | 500 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_OVERSAMPLING_PITCHROLL; |
501 | gyroOffset.offsets[YAW] = 512 * GYRO_OVERSAMPLING_YAW; |
||
1961 | - | 502 | } |
1964 | - | 503 | |
1961 | - | 504 | if (accOffset_readFromEEProm()) { |
1969 | - | 505 | printf("acc. meter offsets invalid%s",recal); |
2019 | - | 506 | accOffset.offsets[PITCH] = accOffset.offsets[ROLL] = 512 * ACC_OVERSAMPLING_XY; |
507 | accOffset.offsets[Z] = 717 * ACC_OVERSAMPLING_Z; |
||
1961 | - | 508 | } |
509 | |||
510 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
||
511 | gyroNoisePeak[PITCH] = gyroNoisePeak[ROLL] = 0; |
||
512 | accNoisePeak[PITCH] = accNoisePeak[ROLL] = 0; |
||
513 | |||
514 | // Setting offset values has an influence in the analog.c ISR |
||
515 | // Therefore run measurement for 100ms to achive stable readings |
||
2015 | - | 516 | delay_ms_with_adc_measurement(100, 0); |
1961 | - | 517 | |
518 | // Rough estimate. Hmm no nothing happens at calibration anyway. |
||
2019 | - | 519 | // airPressureSum = simpleAirPressure * (AIRPRESSURE_OVERSAMPLING/2); |
1961 | - | 520 | // pressureMeasurementCount = 0; |
521 | } |
||
522 | |||
523 | void analog_calibrateGyros(void) { |
||
1612 | dongfang | 524 | #define GYRO_OFFSET_CYCLES 32 |
1952 | - | 525 | uint8_t i, axis; |
1963 | - | 526 | int32_t offsets[3] = { 0, 0, 0 }; |
1952 | - | 527 | gyro_calibrate(); |
528 | |||
529 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
||
530 | for (i = 0; i < GYRO_OFFSET_CYCLES; i++) { |
||
2015 | - | 531 | delay_ms_with_adc_measurement(10, 1); |
1952 | - | 532 | for (axis = PITCH; axis <= YAW; axis++) { |
2015 | - | 533 | offsets[axis] += rawGyroValue(axis); |
1952 | - | 534 | } |
535 | } |
||
536 | |||
537 | for (axis = PITCH; axis <= YAW; axis++) { |
||
1963 | - | 538 | gyroOffset.offsets[axis] = (offsets[axis] + GYRO_OFFSET_CYCLES / 2) / GYRO_OFFSET_CYCLES; |
2018 | - | 539 | |
2019 | - | 540 | int16_t min = (512-200) * (axis==YAW) ? GYRO_OVERSAMPLING_YAW : GYRO_OVERSAMPLING_PITCHROLL; |
541 | int16_t max = (512+200) * (axis==YAW) ? GYRO_OVERSAMPLING_YAW : GYRO_OVERSAMPLING_PITCHROLL; |
||
2018 | - | 542 | if(gyroOffset.offsets[axis] < min || gyroOffset.offsets[axis] > max) |
543 | versionInfo.hardwareErrors[0] |= FC_ERROR0_GYRO_PITCH << axis; |
||
1952 | - | 544 | } |
1961 | - | 545 | |
546 | gyroOffset_writeToEEProm(); |
||
2015 | - | 547 | startAnalogConversionCycle(); |
1612 | dongfang | 548 | } |
549 | |||
550 | /* |
||
551 | * Find acc. offsets for a neutral reading, and write them to EEPROM. |
||
552 | * Does not (!} update the local variables. This must be done with a |
||
553 | * call to analog_calibrate() - this always (?) is done by the caller |
||
554 | * anyway. There would be nothing wrong with updating the variables |
||
555 | * directly from here, though. |
||
556 | */ |
||
557 | void analog_calibrateAcc(void) { |
||
2015 | - | 558 | #define ACC_OFFSET_CYCLES 32 |
1960 | - | 559 | uint8_t i, axis; |
2015 | - | 560 | int32_t offsets[3] = { 0, 0, 0 }; |
561 | |||
1960 | - | 562 | for (i = 0; i < ACC_OFFSET_CYCLES; i++) { |
2015 | - | 563 | delay_ms_with_adc_measurement(10, 1); |
1960 | - | 564 | for (axis = PITCH; axis <= YAW; axis++) { |
2015 | - | 565 | offsets[axis] += rawAccValue(axis); |
1960 | - | 566 | } |
567 | } |
||
2015 | - | 568 | |
1960 | - | 569 | for (axis = PITCH; axis <= YAW; axis++) { |
2015 | - | 570 | accOffset.offsets[axis] = (offsets[axis] + ACC_OFFSET_CYCLES / 2) / ACC_OFFSET_CYCLES; |
2018 | - | 571 | int16_t min,max; |
572 | if (axis==Z) { |
||
2020 | - | 573 | if (staticParams.imuReversedFlags & IMU_REVERSE_ACC_Z) { |
2018 | - | 574 | // TODO: This assumes a sensitivity of +/- 2g. |
2019 | - | 575 | min = (256-200) * ACC_OVERSAMPLING_Z; |
576 | max = (256+200) * ACC_OVERSAMPLING_Z; |
||
2018 | - | 577 | } else { |
578 | // TODO: This assumes a sensitivity of +/- 2g. |
||
2019 | - | 579 | min = (768-200) * ACC_OVERSAMPLING_Z; |
580 | max = (768+200) * ACC_OVERSAMPLING_Z; |
||
2018 | - | 581 | } |
582 | } else { |
||
2019 | - | 583 | min = (512-200) * ACC_OVERSAMPLING_XY; |
584 | max = (512+200) * ACC_OVERSAMPLING_XY; |
||
2018 | - | 585 | } |
586 | if(gyroOffset.offsets[axis] < min || gyroOffset.offsets[axis] > max) { |
||
587 | versionInfo.hardwareErrors[0] |= FC_ERROR0_ACC_X << axis; |
||
588 | } |
||
1960 | - | 589 | } |
1961 | - | 590 | |
2015 | - | 591 | accOffset_writeToEEProm(); |
592 | startAnalogConversionCycle(); |
||
1612 | dongfang | 593 | } |
2033 | - | 594 | |
595 | void analog_setGround() { |
||
596 | groundPressure = filteredAirPressure; |
||
597 | } |
||
598 | |||
599 | int32_t analog_getHeight(void) { |
||
600 | return groundPressure - filteredAirPressure; |
||
601 | } |
||
602 | |||
603 | int16_t analog_getDHeight(void) { |
||
604 | // dHeight = -dPressure, so here it is the old pressure minus the current, not opposite. |
||
605 | return lastFilteredAirPressure - filteredAirPressure; |
||
606 | } |