Rev 1612 | Rev 1635 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1612 | Rev 1634 | ||
---|---|---|---|
Line 122... | Line 122... | ||
122 | * That is divided by 3 below, for a final 10.34 per volt. |
122 | * That is divided by 3 below, for a final 10.34 per volt. |
123 | * So the initial value of 100 is for 9.7 volts. |
123 | * So the initial value of 100 is for 9.7 volts. |
124 | */ |
124 | */ |
125 | volatile int16_t UBat = 100; |
125 | volatile int16_t UBat = 100; |
Line -... | Line 126... | ||
- | 126 | ||
- | 127 | volatile int16_t filteredAirPressure; |
|
126 | 128 | ||
127 | /* |
129 | /* |
128 | * Control and status. |
130 | * Control and status. |
129 | */ |
131 | */ |
130 | volatile uint16_t ADCycleCount = 0; |
132 | volatile uint16_t ADCycleCount = 0; |
Line 135... | Line 137... | ||
135 | */ |
137 | */ |
136 | volatile uint16_t pitchGyroNoisePeak, rollGyroNoisePeak; |
138 | volatile uint16_t pitchGyroNoisePeak, rollGyroNoisePeak; |
137 | volatile uint16_t pitchAccNoisePeak, rollAccNoisePeak; |
139 | volatile uint16_t pitchAccNoisePeak, rollAccNoisePeak; |
Line 138... | Line 140... | ||
138 | 140 | ||
139 | // ADC channels |
141 | // ADC channels |
140 | #define AD_GYRO_YAW 0 |
142 | #define AD_GYRO_YAW 0 |
141 | #define AD_GYRO_ROLL 1 |
143 | #define AD_GYRO_ROLL 1 |
142 | #define AD_GYRO_PITCH 2 |
144 | #define AD_GYRO_PITCH 2 |
143 | #define AD_AIRPRESSURE 3 |
145 | #define AD_AIRPRESSURE 3 |
144 | #define AD_UBAT 4 |
146 | #define AD_UBAT 4 |
145 | #define AD_ACC_Z 5 |
147 | #define AD_ACC_Z 5 |
146 | #define AD_ACC_ROLL 6 |
148 | #define AD_ACC_ROLL 6 |
Line 147... | Line 149... | ||
147 | #define AD_ACC_PITCH 7 |
149 | #define AD_ACC_PITCH 7 |
148 | 150 | ||
149 | /* |
151 | /* |
150 | * Table of AD converter inputs for each state. |
152 | * Table of AD converter inputs for each state. |
Line 161... | Line 163... | ||
161 | const uint8_t channelsForStates[] PROGMEM = { |
163 | const uint8_t channelsForStates[] PROGMEM = { |
162 | AD_GYRO_PITCH, |
164 | AD_GYRO_PITCH, |
163 | AD_GYRO_ROLL, |
165 | AD_GYRO_ROLL, |
164 | AD_GYRO_YAW, |
166 | AD_GYRO_YAW, |
Line 165... | Line -... | ||
165 | - | ||
166 | AD_ACC_ROLL, |
167 | |
- | 168 | AD_ACC_PITCH, |
|
- | 169 | AD_ACC_ROLL, |
|
Line 167... | Line 170... | ||
167 | AD_ACC_PITCH, |
170 | // AD_AIRPRESSURE, |
168 | 171 | ||
169 | AD_GYRO_PITCH, |
- | |
170 | AD_GYRO_ROLL, |
172 | AD_GYRO_PITCH, |
Line 171... | Line 173... | ||
171 | 173 | AD_GYRO_ROLL, |
|
172 | AD_ACC_Z, // at 7, finish Z acc. |
174 | AD_ACC_Z, // at 7, measure Z acc. |
173 | 175 | ||
Line 174... | Line 176... | ||
174 | AD_GYRO_PITCH, |
176 | AD_GYRO_PITCH, |
175 | AD_GYRO_ROLL, |
177 | AD_GYRO_ROLL, |
- | 178 | AD_GYRO_YAW, // at 10, finish yaw gyro |
|
Line 176... | Line 179... | ||
176 | AD_GYRO_YAW, // at 10, finish yaw gyro |
179 | |
177 | 180 | AD_ACC_PITCH, // at 11, finish pitch axis acc. |
|
178 | AD_ACC_PITCH, // at 11, finish pitch axis acc. |
- | |
179 | AD_ACC_ROLL, // at 12, finish roll axis acc. |
181 | AD_ACC_ROLL, // at 12, finish roll axis acc. |
180 | 182 | AD_AIRPRESSURE, // at 13, finish air pressure. |
|
Line 181... | Line 183... | ||
181 | AD_GYRO_PITCH, // at 13, finish pitch gyro |
183 | |
182 | AD_GYRO_ROLL, // at 14, finish roll gyro |
184 | AD_GYRO_PITCH, // at 14, finish pitch gyro |
Line 224... | Line 226... | ||
224 | } else { |
226 | } else { |
225 | *noiseMeasurement = 0; |
227 | *noiseMeasurement = 0; |
226 | } |
228 | } |
227 | } |
229 | } |
Line -... | Line 230... | ||
- | 230 | ||
- | 231 | ||
- | 232 | #define ADCENTER (1023/2) |
|
- | 233 | #define HALFRANGE 400 |
|
- | 234 | uint8_t stepsize = 53; |
|
- | 235 | ||
- | 236 | uint16_t getAbsPressure(int advalue) { |
|
- | 237 | return (uint16_t)OCR0A * (uint16_t)stepsize + advalue; |
|
- | 238 | } |
|
- | 239 | ||
- | 240 | uint16_t filterAirPressure(uint16_t rawpressure) { |
|
- | 241 | return rawpressure; |
|
- | 242 | } |
|
228 | 243 | ||
229 | /*****************************************************/ |
244 | /*****************************************************/ |
230 | /* Interrupt Service Routine for ADC */ |
245 | /* Interrupt Service Routine for ADC */ |
231 | /*****************************************************/ |
246 | /*****************************************************/ |
232 | // Runs at 312.5 kHz or 3.2 µs |
247 | // Runs at 312.5 kHz or 3.2 µs |
Line 236... | Line 251... | ||
236 | ISR(ADC_vect) { |
251 | ISR(ADC_vect) { |
237 | static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
252 | static uint8_t ad_channel = AD_GYRO_PITCH, state = 0; |
238 | static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0}; |
253 | static uint16_t sensorInputs[8] = {0,0,0,0,0,0,0,0}; |
Line 239... | Line 254... | ||
239 | 254 | ||
- | 255 | uint8_t i; |
|
240 | uint8_t i; |
256 | int16_t step = OCR0A; |
241 | 257 | ||
242 | // for various filters... |
258 | // for various filters... |
Line 243... | Line 259... | ||
243 | static int16_t pitchGyroFilter, rollGyroFilter, tempOffsetGyro; |
259 | static int16_t pitchGyroFilter, rollGyroFilter, tempOffsetGyro; |
Line 285... | Line 301... | ||
285 | #endif |
301 | #endif |
286 | filteredRollAxisAcc = (filteredRollAxisAcc * (ACC_FILTER-1) + rollAxisAcc) / ACC_FILTER; |
302 | filteredRollAxisAcc = (filteredRollAxisAcc * (ACC_FILTER-1) + rollAxisAcc) / ACC_FILTER; |
287 | measureNoise(rollAxisAcc, &rollAccNoisePeak, 1); |
303 | measureNoise(rollAxisAcc, &rollAccNoisePeak, 1); |
288 | break; |
304 | break; |
Line -... | Line 305... | ||
- | 305 | ||
- | 306 | case 13: // air pressure |
|
- | 307 | if (sensorInputs[AD_AIRPRESSURE] < ADCENTER-HALFRANGE) { |
|
- | 308 | // value is too low, so decrease voltage on the op amp minus input, making the value higher. |
|
- | 309 | step -= ((HALFRANGE-sensorInputs[AD_AIRPRESSURE]) / stepsize + 1); |
|
- | 310 | if (step<0) step = 0; |
|
- | 311 | OCR0A = step; |
|
- | 312 | // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off) |
|
- | 313 | } else if (sensorInputs[AD_AIRPRESSURE] > ADCENTER+HALFRANGE) { |
|
- | 314 | // value is too high, so increase voltage on the op amp minus input, making the value lower. |
|
- | 315 | step += ((sensorInputs[AD_AIRPRESSURE] - HALFRANGE)/stepsize + 1); |
|
- | 316 | if (step>254) step = 254; |
|
- | 317 | OCR0A = step; |
|
- | 318 | // wait = ... (calculate something here .. calculate at what time the R/C filter is to within one sample off) |
|
- | 319 | } else { |
|
- | 320 | filteredAirPressure = filterAirPressure(getAbsPressure(sensorInputs[AD_AIRPRESSURE])); |
|
- | 321 | } |
|
- | 322 | break; |
|
289 | 323 | ||
290 | case 13: // pitch gyro |
324 | case 14: // pitch gyro |
291 | rawPitchGyroSum = sensorInputs[AD_GYRO_PITCH]; |
325 | rawPitchGyroSum = sensorInputs[AD_GYRO_PITCH]; |
292 | // Filter already before offsetting. The offsetting resolution improvement obtained by divding by |
326 | // Filter already before offsetting. The offsetting resolution improvement obtained by divding by |
293 | // GYROS_FIRSTORDERFILTER _after_ offsetting is too small to be worth pursuing. |
327 | // GYROS_FIRSTORDERFILTER _after_ offsetting is too small to be worth pursuing. |
294 | pitchGyroFilter = (pitchGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawPitchGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
328 | pitchGyroFilter = (pitchGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawPitchGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
Line 305... | Line 339... | ||
305 | // Filter a little more. This value is used in integration to angles. |
339 | // Filter a little more. This value is used in integration to angles. |
306 | filteredHiResPitchGyro = (filteredHiResPitchGyro * (GYROS_SECONDORDERFILTER-1) + hiResPitchGyro) / GYROS_SECONDORDERFILTER; |
340 | filteredHiResPitchGyro = (filteredHiResPitchGyro * (GYROS_SECONDORDERFILTER-1) + hiResPitchGyro) / GYROS_SECONDORDERFILTER; |
307 | measureNoise(hiResPitchGyro, &pitchGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING); |
341 | measureNoise(hiResPitchGyro, &pitchGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING); |
308 | break; |
342 | break; |
Line 309... | Line 343... | ||
309 | 343 | ||
310 | case 14: // Roll gyro. Works the same as pitch. |
344 | case 15: // Roll gyro. Works the same as pitch. |
311 | rawRollGyroSum = sensorInputs[AD_GYRO_ROLL]; |
345 | rawRollGyroSum = sensorInputs[AD_GYRO_ROLL]; |
312 | rollGyroFilter = (rollGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawRollGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
346 | rollGyroFilter = (rollGyroFilter * (GYROS_FIRSTORDERFILTER-1) + rawRollGyroSum * GYRO_FACTOR_PITCHROLL) / GYROS_FIRSTORDERFILTER; |
313 | #ifdef GYRO_REVERSE_ROLL |
347 | #ifdef GYRO_REVERSE_ROLL |
314 | tempOffsetGyro = rollOffset - rollGyroFilter; |
348 | tempOffsetGyro = rollOffset - rollGyroFilter; |
Line 319... | Line 353... | ||
319 | hiResRollGyro = tempOffsetGyro; |
353 | hiResRollGyro = tempOffsetGyro; |
320 | filteredHiResRollGyro = (filteredHiResRollGyro * (GYROS_SECONDORDERFILTER-1) + hiResRollGyro) / GYROS_SECONDORDERFILTER; |
354 | filteredHiResRollGyro = (filteredHiResRollGyro * (GYROS_SECONDORDERFILTER-1) + hiResRollGyro) / GYROS_SECONDORDERFILTER; |
321 | measureNoise(hiResRollGyro, &rollGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING); |
355 | measureNoise(hiResRollGyro, &rollGyroNoisePeak, GYRO_NOISE_MEASUREMENT_DAMPING); |
322 | break; |
356 | break; |
Line 323... | Line 357... | ||
323 | 357 | ||
324 | case 15: |
358 | case 16: |
325 | // battery |
359 | // battery |
326 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
360 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
327 | analogDataReady = 1; // mark |
361 | analogDataReady = 1; // mark |
328 | ADCycleCount++; |
362 | ADCycleCount++; |