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