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