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