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