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