Rev 2110 | Rev 2119 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2110 | Rev 2116 | ||
---|---|---|---|
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" |
- | |
9 | #include "printf_P.h" |
8 | #include "printf_P.h" |
10 | #include "isqrt.h" |
9 | #include "isqrt.h" |
11 | 10 | ||
12 | // for Delay functions |
11 | // for Delay functions |
13 | #include "timer0.h" |
12 | #include "timer0.h" |
14 | 13 | ||
15 | // For reading and writing acc. meter offsets. |
14 | // For reading and writing acc. meter offsets. |
16 | #include "eeprom.h" |
15 | #include "eeprom.h" |
17 | 16 | ||
18 | // For debugOut |
17 | // For debugOut |
19 | #include "output.h" |
18 | #include "output.h" |
20 | 19 | ||
21 | // set ADC enable & ADC Start Conversion & ADC Interrupt Enable bit |
20 | // set ADC enable & ADC Start Conversion & ADC Interrupt Enable bit |
22 | #define startADC() (ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE)) |
21 | #define startADC() (ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE)) |
23 | 22 | ||
24 | const char* recal = ", recalibration needed."; |
23 | const char* recal = ", recalibration needed."; |
25 | 24 | ||
26 | /* |
25 | /* |
27 | * 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 |
28 | * (see array channelsForStates), and the results for each channel are summed. |
27 | * (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. |
28 | * 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 |
29 | * 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 |
30 | * reason for the export is that the ENC-03_FC1.3 modules needs them for calibrating |
32 | * the offsets with the DAC. |
31 | * the offsets with the DAC. |
33 | */ |
32 | */ |
34 | volatile uint16_t sensorInputs[8]; |
33 | volatile uint16_t sensorInputs[8]; |
35 | 34 | ||
36 | 35 | ||
37 | /* |
36 | /* |
38 | * These 4 exported variables are zero-offset. The "PID" ones are used |
37 | * These 4 exported variables are zero-offset. The "PID" ones are used |
39 | * in the attitude control as rotation rates. The "ATT" ones are for |
38 | * in the attitude control as rotation rates. The "ATT" ones are for |
40 | * integration to angles. |
39 | * integration to angles. |
41 | */ |
40 | */ |
42 | int16_t gyro_PID[3]; |
41 | int16_t gyro_PID[3]; |
43 | int16_t gyro_ATT[3]; |
42 | int16_t gyro_ATT[3]; |
44 | int16_t gyroD[3]; |
43 | int16_t gyroD[3]; |
45 | int16_t gyroDWindow[3][GYRO_D_WINDOW_LENGTH]; |
44 | int16_t gyroDWindow[3][GYRO_D_WINDOW_LENGTH]; |
46 | uint8_t gyroDWindowIdx = 0; |
45 | uint8_t gyroDWindowIdx = 0; |
47 | 46 | ||
48 | /* |
47 | /* |
49 | * Airspeed |
48 | * Airspeed |
50 | */ |
49 | */ |
51 | int16_t airpressure; |
50 | int16_t airpressure; |
52 | uint16_t airspeedVelocity = 0; |
51 | uint16_t airspeedVelocity = 0; |
53 | //int16_t airpressureWindow[AIRPRESSURE_WINDOW_LENGTH]; |
52 | //int16_t airpressureWindow[AIRPRESSURE_WINDOW_LENGTH]; |
54 | //uint8_t airpressureWindowIdx = 0; |
53 | //uint8_t airpressureWindowIdx = 0; |
55 | 54 | ||
56 | /* |
55 | /* |
57 | * Offset values. These are the raw gyro and acc. meter sums when the copter is |
56 | * 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 |
57 | * standing still. They are used for adjusting the gyro and acc. meter values |
59 | * to be centered on zero. |
58 | * to be centered on zero. |
60 | */ |
59 | */ |
61 | sensorOffset_t gyroOffset; |
60 | sensorOffset_t gyroOffset; |
62 | uint16_t airpressureOffset; |
61 | uint16_t airpressureOffset; |
63 | 62 | ||
64 | /* |
63 | /* |
65 | * In the MK coordinate system, nose-down is positive and left-roll is positive. |
64 | * In the MK coordinate system, nose-down is positive and left-roll is positive. |
66 | * If a sensor is used in an orientation where one but not both of the axes has |
65 | * If a sensor is used in an orientation where one but not both of the axes has |
67 | * an opposite sign, PR_ORIENTATION_REVERSED is set to 1 (true). |
66 | * an opposite sign, PR_ORIENTATION_REVERSED is set to 1 (true). |
68 | * Transform: |
67 | * Transform: |
69 | * pitch <- pp*pitch + pr*roll |
68 | * pitch <- pp*pitch + pr*roll |
70 | * roll <- rp*pitch + rr*roll |
69 | * roll <- rp*pitch + rr*roll |
71 | * Not reversed, GYRO_QUADRANT: |
70 | * Not reversed, GYRO_QUADRANT: |
72 | * 0: pp=1, pr=0, rp=0, rr=1 // 0 degrees |
71 | * 0: pp=1, pr=0, rp=0, rr=1 // 0 degrees |
73 | * 1: pp=1, pr=-1,rp=1, rr=1 // +45 degrees |
72 | * 1: pp=1, pr=-1,rp=1, rr=1 // +45 degrees |
74 | * 2: pp=0, pr=-1,rp=1, rr=0 // +90 degrees |
73 | * 2: pp=0, pr=-1,rp=1, rr=0 // +90 degrees |
75 | * 3: pp=-1,pr=-1,rp=1, rr=1 // +135 degrees |
74 | * 3: pp=-1,pr=-1,rp=1, rr=1 // +135 degrees |
76 | * 4: pp=-1,pr=0, rp=0, rr=-1 // +180 degrees |
75 | * 4: pp=-1,pr=0, rp=0, rr=-1 // +180 degrees |
77 | * 5: pp=-1,pr=1, rp=-1,rr=-1 // +225 degrees |
76 | * 5: pp=-1,pr=1, rp=-1,rr=-1 // +225 degrees |
78 | * 6: pp=0, pr=1, rp=-1,rr=0 // +270 degrees |
77 | * 6: pp=0, pr=1, rp=-1,rr=0 // +270 degrees |
79 | * 7: pp=1, pr=1, rp=-1,rr=1 // +315 degrees |
78 | * 7: pp=1, pr=1, rp=-1,rr=1 // +315 degrees |
80 | * Reversed, GYRO_QUADRANT: |
79 | * Reversed, GYRO_QUADRANT: |
81 | * 0: pp=-1,pr=0, rp=0, rr=1 // 0 degrees with pitch reversed |
80 | * 0: pp=-1,pr=0, rp=0, rr=1 // 0 degrees with pitch reversed |
82 | * 1: pp=-1,pr=-1,rp=-1,rr=1 // +45 degrees with pitch reversed |
81 | * 1: pp=-1,pr=-1,rp=-1,rr=1 // +45 degrees with pitch reversed |
83 | * 2: pp=0, pr=-1,rp=-1,rr=0 // +90 degrees with pitch reversed |
82 | * 2: pp=0, pr=-1,rp=-1,rr=0 // +90 degrees with pitch reversed |
84 | * 3: pp=1, pr=-1,rp=-1,rr=1 // +135 degrees with pitch reversed |
83 | * 3: pp=1, pr=-1,rp=-1,rr=1 // +135 degrees with pitch reversed |
85 | * 4: pp=1, pr=0, rp=0, rr=-1 // +180 degrees with pitch reversed |
84 | * 4: pp=1, pr=0, rp=0, rr=-1 // +180 degrees with pitch reversed |
86 | * 5: pp=1, pr=1, rp=1, rr=-1 // +225 degrees with pitch reversed |
85 | * 5: pp=1, pr=1, rp=1, rr=-1 // +225 degrees with pitch reversed |
87 | * 6: pp=0, pr=1, rp=1, rr=0 // +270 degrees with pitch reversed |
86 | * 6: pp=0, pr=1, rp=1, rr=0 // +270 degrees with pitch reversed |
88 | * 7: pp=-1,pr=1, rp=1, rr=1 // +315 degrees with pitch reversed |
87 | * 7: pp=-1,pr=1, rp=1, rr=1 // +315 degrees with pitch reversed |
89 | */ |
88 | */ |
90 | 89 | ||
91 | void rotate(int16_t* result, uint8_t quadrant, uint8_t reversePR, uint8_t reverseYaw) { |
90 | void rotate(int16_t* result, uint8_t quadrant, uint8_t reversePR, uint8_t reverseYaw) { |
92 | static const int8_t rotationTab[] = {1,1,0,-1,-1,-1,0,1}; |
91 | static const int8_t rotationTab[] = {1,1,0,-1,-1,-1,0,1}; |
93 | // Pitch to Pitch part |
92 | // Pitch to Pitch part |
94 | int8_t xx = reversePR ? rotationTab[(quadrant+4)%8] : rotationTab[quadrant]; |
93 | int8_t xx = reversePR ? rotationTab[(quadrant+4)%8] : rotationTab[quadrant]; |
95 | // Roll to Pitch part |
94 | // Roll to Pitch part |
96 | int8_t xy = rotationTab[(quadrant+2)%8]; |
95 | int8_t xy = rotationTab[(quadrant+2)%8]; |
97 | // Pitch to Roll part |
96 | // Pitch to Roll part |
98 | int8_t yx = reversePR ? rotationTab[(quadrant+2)%8] : rotationTab[(quadrant+6)%8]; |
97 | int8_t yx = reversePR ? rotationTab[(quadrant+2)%8] : rotationTab[(quadrant+6)%8]; |
99 | // Roll to Roll part |
98 | // Roll to Roll part |
100 | int8_t yy = rotationTab[quadrant]; |
99 | int8_t yy = rotationTab[quadrant]; |
101 | 100 | ||
102 | int16_t xIn = result[0]; |
101 | int16_t xIn = result[0]; |
103 | result[0] = xx*xIn + xy*result[1]; |
102 | result[0] = xx*xIn + xy*result[1]; |
104 | result[1] = yx*xIn + yy*result[1]; |
103 | result[1] = yx*xIn + yy*result[1]; |
105 | 104 | ||
106 | if (quadrant & 1) { |
105 | if (quadrant & 1) { |
107 | // A rotation was used above, where the factors were too large by sqrt(2). |
106 | // A rotation was used above, where the factors were too large by sqrt(2). |
108 | // So, we multiply by 2^n/sqt(2) and right shift n bits, as to divide by sqrt(2). |
107 | // So, we multiply by 2^n/sqt(2) and right shift n bits, as to divide by sqrt(2). |
109 | // A suitable value for n: Sample is 11 bits. After transformation it is the sum |
108 | // A suitable value for n: Sample is 11 bits. After transformation it is the sum |
110 | // of 2 11 bit numbers, so 12 bits. We have 4 bits left... |
109 | // of 2 11 bit numbers, so 12 bits. We have 4 bits left... |
111 | result[0] = (result[0]*11) >> 4; |
110 | result[0] = (result[0]*11) >> 4; |
112 | result[1] = (result[1]*11) >> 4; |
111 | result[1] = (result[1]*11) >> 4; |
113 | } |
112 | } |
114 | 113 | ||
115 | if (reverseYaw) |
114 | if (reverseYaw) |
116 | result[3] =-result[3]; |
115 | result[3] =-result[3]; |
117 | } |
116 | } |
118 | 117 | ||
119 | /* |
118 | /* |
120 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
119 | * Battery voltage, in units of: 1k/11k / 3V * 1024 = 31.03 per volt. |
121 | * That is divided by 3 below, for a final 10.34 per volt. |
120 | * That is divided by 3 below, for a final 10.34 per volt. |
122 | * So the initial value of 100 is for 9.7 volts. |
121 | * So the initial value of 100 is for 9.7 volts. |
123 | */ |
122 | */ |
124 | uint16_t UBat = 100; |
123 | uint16_t UBat = 100; |
125 | 124 | ||
126 | /* |
125 | /* |
127 | * Control and status. |
126 | * Control and status. |
128 | */ |
127 | */ |
129 | volatile uint8_t analogDataReady = 1; |
128 | volatile uint8_t analogDataReady = 1; |
130 | 129 | ||
131 | /* |
130 | /* |
132 | * Experiment: Measuring vibration-induced sensor noise. |
131 | * Experiment: Measuring vibration-induced sensor noise. |
133 | */ |
132 | */ |
134 | uint16_t gyroNoisePeak[3]; |
133 | uint16_t gyroNoisePeak[3]; |
135 | 134 | ||
136 | volatile uint8_t adState; |
135 | volatile uint8_t adState; |
137 | volatile uint8_t adChannel; |
136 | volatile uint8_t adChannel; |
138 | 137 | ||
139 | // ADC channels |
138 | // ADC channels |
140 | #define AD_GYRO_YAW 0 |
139 | #define AD_GYRO_YAW 0 |
141 | #define AD_GYRO_ROLL 1 |
140 | #define AD_GYRO_ROLL 1 |
142 | #define AD_GYRO_PITCH 2 |
141 | #define AD_GYRO_PITCH 2 |
143 | #define AD_AIRPRESSURE 3 |
142 | #define AD_AIRPRESSURE 3 |
144 | #define AD_UBAT 4 |
143 | #define AD_UBAT 4 |
145 | #define AD_ACC_Z 5 |
144 | #define AD_ACC_Z 5 |
146 | #define AD_ACC_ROLL 6 |
145 | #define AD_ACC_ROLL 6 |
147 | #define AD_ACC_PITCH 7 |
146 | #define AD_ACC_PITCH 7 |
148 | 147 | ||
149 | /* |
148 | /* |
150 | * Table of AD converter inputs for each state. |
149 | * Table of AD converter inputs for each state. |
151 | * The number of samples summed for each channel is equal to |
150 | * The number of samples summed for each channel is equal to |
152 | * the number of times the channel appears in the array. |
151 | * the number of times the channel appears in the array. |
153 | * The max. number of samples that can be taken in 2 ms is: |
152 | * The max. number of samples that can be taken in 2 ms is: |
154 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
153 | * 20e6 / 128 / 13 / (1/2e-3) = 24. Since the main control |
155 | * loop needs a little time between reading AD values and |
154 | * loop needs a little time between reading AD values and |
156 | * re-enabling ADC, the real limit is (how much?) lower. |
155 | * re-enabling ADC, the real limit is (how much?) lower. |
157 | * The acc. sensor is sampled even if not used - or installed |
156 | * The acc. sensor is sampled even if not used - or installed |
158 | * at all. The cost is not significant. |
157 | * at all. The cost is not significant. |
159 | */ |
158 | */ |
160 | 159 | ||
161 | const uint8_t channelsForStates[] PROGMEM = { |
160 | const uint8_t channelsForStates[] PROGMEM = { |
162 | AD_GYRO_PITCH, |
161 | AD_GYRO_PITCH, |
163 | AD_GYRO_ROLL, |
162 | AD_GYRO_ROLL, |
164 | AD_GYRO_YAW, |
163 | AD_GYRO_YAW, |
165 | 164 | ||
166 | AD_AIRPRESSURE, |
165 | AD_AIRPRESSURE, |
167 | 166 | ||
168 | AD_GYRO_PITCH, |
167 | AD_GYRO_PITCH, |
169 | AD_GYRO_ROLL, |
168 | AD_GYRO_ROLL, |
170 | AD_GYRO_YAW, |
169 | AD_GYRO_YAW, |
171 | 170 | ||
172 | AD_UBAT, |
171 | AD_UBAT, |
173 | 172 | ||
174 | AD_GYRO_PITCH, |
173 | AD_GYRO_PITCH, |
175 | AD_GYRO_ROLL, |
174 | AD_GYRO_ROLL, |
176 | AD_GYRO_YAW, |
175 | AD_GYRO_YAW, |
177 | 176 | ||
178 | AD_AIRPRESSURE, |
177 | AD_AIRPRESSURE, |
179 | 178 | ||
180 | AD_GYRO_PITCH, |
179 | AD_GYRO_PITCH, |
181 | AD_GYRO_ROLL, |
180 | AD_GYRO_ROLL, |
182 | AD_GYRO_YAW |
181 | AD_GYRO_YAW |
183 | }; |
182 | }; |
184 | 183 | ||
185 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
184 | // Feature removed. Could be reintroduced later - but should work for all gyro types then. |
186 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
185 | // uint8_t GyroDefectPitch = 0, GyroDefectRoll = 0, GyroDefectYaw = 0; |
187 | 186 | ||
188 | void analog_init(void) { |
187 | void analog_init(void) { |
189 | uint8_t sreg = SREG; |
188 | uint8_t sreg = SREG; |
190 | // disable all interrupts before reconfiguration |
189 | // disable all interrupts before reconfiguration |
191 | cli(); |
190 | cli(); |
192 | 191 | ||
193 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
192 | //ADC0 ... ADC7 is connected to PortA pin 0 ... 7 |
194 | DDRA = 0x00; |
193 | DDRA = 0x00; |
195 | PORTA = 0x00; |
194 | PORTA = 0x00; |
196 | // Digital Input Disable Register 0 |
195 | // Digital Input Disable Register 0 |
197 | // Disable digital input buffer for analog adc_channel pins |
196 | // Disable digital input buffer for analog adc_channel pins |
198 | DIDR0 = 0xFF; |
197 | DIDR0 = 0xFF; |
199 | // external reference, adjust data to the right |
198 | // external reference, adjust data to the right |
200 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
199 | ADMUX &= ~((1<<REFS1)|(1<<REFS0)|(1<<ADLAR)); |
201 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
200 | // set muxer to ADC adc_channel 0 (0 to 7 is a valid choice) |
202 | ADMUX = (ADMUX & 0xE0); |
201 | ADMUX = (ADMUX & 0xE0); |
203 | //Set ADC Control and Status Register A |
202 | //Set ADC Control and Status Register A |
204 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
203 | //Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz |
205 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
204 | ADCSRA = (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); |
206 | //Set ADC Control and Status Register B |
205 | //Set ADC Control and Status Register B |
207 | //Trigger Source to Free Running Mode |
206 | //Trigger Source to Free Running Mode |
208 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
207 | ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); |
209 | 208 | ||
210 | startAnalogConversionCycle(); |
209 | startAnalogConversionCycle(); |
211 | 210 | ||
212 | // restore global interrupt flags |
211 | // restore global interrupt flags |
213 | SREG = sreg; |
212 | SREG = sreg; |
214 | } |
213 | } |
215 | 214 | ||
216 | uint16_t rawGyroValue(uint8_t axis) { |
215 | uint16_t rawGyroValue(uint8_t axis) { |
217 | return sensorInputs[AD_GYRO_PITCH-axis]; |
216 | return sensorInputs[AD_GYRO_PITCH-axis]; |
218 | } |
217 | } |
219 | 218 | ||
220 | /* |
219 | /* |
221 | uint16_t rawAccValue(uint8_t axis) { |
220 | uint16_t rawAccValue(uint8_t axis) { |
222 | return sensorInputs[AD_ACC_PITCH-axis]; |
221 | return sensorInputs[AD_ACC_PITCH-axis]; |
223 | } |
222 | } |
224 | */ |
223 | */ |
225 | 224 | ||
226 | void measureNoise(const int16_t sensor, |
225 | void measureNoise(const int16_t sensor, |
227 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
226 | volatile uint16_t* const noiseMeasurement, const uint8_t damping) { |
228 | if (sensor > (int16_t) (*noiseMeasurement)) { |
227 | if (sensor > (int16_t) (*noiseMeasurement)) { |
229 | *noiseMeasurement = sensor; |
228 | *noiseMeasurement = sensor; |
230 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
229 | } else if (-sensor > (int16_t) (*noiseMeasurement)) { |
231 | *noiseMeasurement = -sensor; |
230 | *noiseMeasurement = -sensor; |
232 | } else if (*noiseMeasurement > damping) { |
231 | } else if (*noiseMeasurement > damping) { |
233 | *noiseMeasurement -= damping; |
232 | *noiseMeasurement -= damping; |
234 | } else { |
233 | } else { |
235 | *noiseMeasurement = 0; |
234 | *noiseMeasurement = 0; |
236 | } |
235 | } |
237 | } |
236 | } |
238 | 237 | ||
239 | void startAnalogConversionCycle(void) { |
238 | void startAnalogConversionCycle(void) { |
240 | analogDataReady = 0; |
239 | analogDataReady = 0; |
241 | 240 | ||
242 | // Stop the sampling. Cycle is over. |
241 | // Stop the sampling. Cycle is over. |
243 | for (uint8_t i = 0; i < 8; i++) { |
242 | for (uint8_t i = 0; i < 8; i++) { |
244 | sensorInputs[i] = 0; |
243 | sensorInputs[i] = 0; |
245 | } |
244 | } |
246 | adState = 0; |
245 | adState = 0; |
247 | adChannel = AD_GYRO_PITCH; |
246 | adChannel = AD_GYRO_PITCH; |
248 | ADMUX = (ADMUX & 0xE0) | adChannel; |
247 | ADMUX = (ADMUX & 0xE0) | adChannel; |
249 | startADC(); |
248 | startADC(); |
250 | } |
249 | } |
251 | 250 | ||
252 | /***************************************************** |
251 | /***************************************************** |
253 | * Interrupt Service Routine for ADC |
252 | * Interrupt Service Routine for ADC |
254 | * Runs at 312.5 kHz or 3.2 �s. When all states are |
253 | * Runs at 312.5 kHz or 3.2 �s. When all states are |
255 | * processed further conversions are stopped. |
254 | * processed further conversions are stopped. |
256 | *****************************************************/ |
255 | *****************************************************/ |
257 | ISR(ADC_vect) { |
256 | ISR(ADC_vect) { |
258 | sensorInputs[adChannel] += ADC; |
257 | sensorInputs[adChannel] += ADC; |
259 | // set up for next state. |
258 | // set up for next state. |
260 | adState++; |
259 | adState++; |
261 | if (adState < sizeof(channelsForStates)) { |
260 | if (adState < sizeof(channelsForStates)) { |
262 | adChannel = pgm_read_byte(&channelsForStates[adState]); |
261 | adChannel = pgm_read_byte(&channelsForStates[adState]); |
263 | // set adc muxer to next adChannel |
262 | // set adc muxer to next adChannel |
264 | ADMUX = (ADMUX & 0xE0) | adChannel; |
263 | ADMUX = (ADMUX & 0xE0) | adChannel; |
265 | // after full cycle stop further interrupts |
264 | // after full cycle stop further interrupts |
266 | startADC(); |
265 | startADC(); |
267 | } else { |
266 | } else { |
268 | analogDataReady = 1; |
267 | analogDataReady = 1; |
269 | // do not restart ADC converter. |
268 | // do not restart ADC converter. |
270 | } |
269 | } |
271 | } |
270 | } |
272 | 271 | ||
273 | void analog_updateGyros(void) { |
272 | void analog_updateGyros(void) { |
274 | // for various filters... |
273 | // for various filters... |
275 | int16_t tempOffsetGyro[3], tempGyro; |
274 | int16_t tempOffsetGyro[3], tempGyro; |
276 | 275 | ||
277 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
276 | debugOut.digital[0] &= ~DEBUG_SENSORLIMIT; |
278 | 277 | ||
279 | for (uint8_t axis=0; axis<3; axis++) { |
278 | for (uint8_t axis=0; axis<3; axis++) { |
280 | tempGyro = rawGyroValue(axis); |
279 | tempGyro = rawGyroValue(axis); |
281 | /* |
280 | /* |
282 | * Process the gyro data for the PID controller. |
281 | * Process the gyro data for the PID controller. |
283 | */ |
282 | */ |
284 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
283 | // 1) Extrapolate: Near the ends of the range, we boost the input significantly. This simulates a |
285 | // gyro with a wider range, and helps counter saturation at full control. |
284 | // gyro with a wider range, and helps counter saturation at full control. |
286 | 285 | ||
287 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
286 | if (staticParams.bitConfig & CFG_GYRO_SATURATION_PREVENTION) { |
288 | if (tempGyro < SENSOR_MIN) { |
287 | if (tempGyro < SENSOR_MIN) { |
289 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
288 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
290 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
289 | tempGyro = tempGyro * EXTRAPOLATION_SLOPE - EXTRAPOLATION_LIMIT; |
291 | } else if (tempGyro > SENSOR_MAX) { |
290 | } else if (tempGyro > SENSOR_MAX) { |
292 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
291 | debugOut.digital[0] |= DEBUG_SENSORLIMIT; |
293 | tempGyro = (tempGyro - SENSOR_MAX) * EXTRAPOLATION_SLOPE + SENSOR_MAX; |
292 | tempGyro = (tempGyro - SENSOR_MAX) * EXTRAPOLATION_SLOPE + SENSOR_MAX; |
294 | } |
293 | } |
295 | } |
294 | } |
296 | 295 | ||
297 | // 2) Apply sign and offset, scale before filtering. |
296 | // 2) Apply sign and offset, scale before filtering. |
298 | tempOffsetGyro[axis] = (tempGyro - gyroOffset.offsets[axis]); |
297 | tempOffsetGyro[axis] = (tempGyro - gyroOffset.offsets[axis]); |
299 | } |
298 | } |
300 | 299 | ||
301 | // 2.1: Transform axes. |
300 | // 2.1: Transform axes. |
302 | rotate(tempOffsetGyro, IMUConfig.gyroQuadrant, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_PR, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_YAW); |
301 | rotate(tempOffsetGyro, IMUConfig.gyroQuadrant, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_PR, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_YAW); |
303 | 302 | ||
304 | for (uint8_t axis=0; axis<3; axis++) { |
303 | for (uint8_t axis=0; axis<3; axis++) { |
305 | // 3) Filter. |
304 | // 3) Filter. |
306 | tempOffsetGyro[axis] = (gyro_PID[axis] * (IMUConfig.gyroPIDFilterConstant - 1) + tempOffsetGyro[axis]) / IMUConfig.gyroPIDFilterConstant; |
305 | tempOffsetGyro[axis] = (gyro_PID[axis] * (IMUConfig.gyroPIDFilterConstant - 1) + tempOffsetGyro[axis]) / IMUConfig.gyroPIDFilterConstant; |
307 | 306 | ||
308 | // 4) Measure noise. |
307 | // 4) Measure noise. |
309 | measureNoise(tempOffsetGyro[axis], &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
308 | measureNoise(tempOffsetGyro[axis], &gyroNoisePeak[axis], GYRO_NOISE_MEASUREMENT_DAMPING); |
310 | 309 | ||
311 | // 5) Differential measurement. |
310 | // 5) Differential measurement. |
312 | // gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro[axis] - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
311 | // gyroD[axis] = (gyroD[axis] * (staticParams.gyroDFilterConstant - 1) + (tempOffsetGyro[axis] - gyro_PID[axis])) / staticParams.gyroDFilterConstant; |
313 | int16_t diff = tempOffsetGyro[axis] - gyro_PID[axis]; |
312 | int16_t diff = tempOffsetGyro[axis] - gyro_PID[axis]; |
314 | gyroD[axis] -= gyroDWindow[axis][gyroDWindowIdx]; |
313 | gyroD[axis] -= gyroDWindow[axis][gyroDWindowIdx]; |
315 | gyroD[axis] += diff; |
314 | gyroD[axis] += diff; |
316 | gyroDWindow[axis][gyroDWindowIdx] = diff; |
315 | gyroDWindow[axis][gyroDWindowIdx] = diff; |
317 | 316 | ||
318 | // 6) Done. |
317 | // 6) Done. |
319 | gyro_PID[axis] = tempOffsetGyro[axis]; |
318 | gyro_PID[axis] = tempOffsetGyro[axis]; |
320 | 319 | ||
321 | // Prepare tempOffsetGyro for next calculation below... |
320 | // Prepare tempOffsetGyro for next calculation below... |
322 | tempOffsetGyro[axis] = (rawGyroValue(axis) - gyroOffset.offsets[axis]); |
321 | tempOffsetGyro[axis] = (rawGyroValue(axis) - gyroOffset.offsets[axis]); |
323 | } |
322 | } |
324 | 323 | ||
325 | /* |
324 | /* |
326 | * Now process the data for attitude angles. |
325 | * Now process the data for attitude angles. |
327 | */ |
326 | */ |
328 | rotate(tempOffsetGyro, IMUConfig.gyroQuadrant, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_PR, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_YAW); |
327 | rotate(tempOffsetGyro, IMUConfig.gyroQuadrant, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_PR, IMUConfig.imuReversedFlags & IMU_REVERSE_GYRO_YAW); |
329 | 328 | ||
330 | // dampenGyroActivity(); |
329 | // dampenGyroActivity(); |
331 | gyro_ATT[PITCH] = tempOffsetGyro[PITCH]; |
330 | gyro_ATT[PITCH] = tempOffsetGyro[PITCH]; |
332 | gyro_ATT[ROLL] = tempOffsetGyro[ROLL]; |
331 | gyro_ATT[ROLL] = tempOffsetGyro[ROLL]; |
333 | gyro_ATT[YAW] = tempOffsetGyro[YAW]; |
332 | gyro_ATT[YAW] = tempOffsetGyro[YAW]; |
334 | 333 | ||
335 | if (++gyroDWindowIdx >= IMUConfig.gyroDWindowLength) { |
334 | if (++gyroDWindowIdx >= IMUConfig.gyroDWindowLength) { |
336 | gyroDWindowIdx = 0; |
335 | gyroDWindowIdx = 0; |
337 | } |
336 | } |
338 | } |
337 | } |
339 | 338 | ||
340 | // probably wanna aim at 1/10 m/s/unit. |
339 | // probably wanna aim at 1/10 m/s/unit. |
341 | #define LOG_AIRSPEED_FACTOR 0 |
340 | #define LOG_AIRSPEED_FACTOR 0 |
342 | 341 | ||
343 | void analog_updateAirspeed(void) { |
342 | void analog_updateAirspeed(void) { |
344 | uint16_t rawAirpressure = sensorInputs[AD_AIRPRESSURE]; |
343 | uint16_t rawAirpressure = sensorInputs[AD_AIRPRESSURE]; |
345 | int16_t temp = airpressureOffset - rawAirpressure; |
344 | int16_t temp = airpressureOffset - rawAirpressure; |
346 | //airpressure -= airpressureWindow[airpressureWindowIdx]; |
345 | //airpressure -= airpressureWindow[airpressureWindowIdx]; |
347 | //airpressure += temp; |
346 | //airpressure += temp; |
348 | //airpressureWindow[airpressureWindowIdx] = temp; |
347 | //airpressureWindow[airpressureWindowIdx] = temp; |
349 | //airpressureWindowIdx++; |
348 | //airpressureWindowIdx++; |
350 | //if (airpressureWindowIdx == AIRPRESSURE_WINDOW_LENGTH) { |
349 | //if (airpressureWindowIdx == AIRPRESSURE_WINDOW_LENGTH) { |
351 | // airpressureWindowIdx = 0; |
350 | // airpressureWindowIdx = 0; |
352 | //} |
351 | //} |
353 | 352 | ||
354 | #define AIRPRESSURE_FILTER 16 |
353 | #define AIRPRESSURE_FILTER 16 |
355 | airpressure = ((int32_t)airpressure * (AIRPRESSURE_FILTER-1) + (AIRPRESSURE_FILTER/2) + temp) / AIRPRESSURE_FILTER; |
354 | airpressure = ((int32_t)airpressure * (AIRPRESSURE_FILTER-1) + (AIRPRESSURE_FILTER/2) + temp) / AIRPRESSURE_FILTER; |
356 | 355 | ||
357 | uint16_t p2 = (airpressure<0) ? 0 : airpressure; |
356 | uint16_t p2 = (airpressure<0) ? 0 : airpressure; |
358 | airspeedVelocity = (staticParams.airspeedCorrection * isqrt16(p2)) >> LOG_AIRSPEED_FACTOR; |
357 | airspeedVelocity = (staticParams.airspeedCorrection * isqrt16(p2)) >> LOG_AIRSPEED_FACTOR; |
359 | 358 | ||
360 | debugOut.analog[17] = airpressure; |
359 | debugOut.analog[17] = airpressure; |
361 | debugOut.analog[18] = airpressureOffset; |
360 | debugOut.analog[18] = airpressureOffset; |
362 | debugOut.analog[19] = airspeedVelocity; |
361 | debugOut.analog[19] = airspeedVelocity; |
363 | 362 | ||
364 | isFlying = 0; //(airspeedVelocity >= staticParams.isFlyingThreshold); |
363 | isFlying = 0; //(airspeedVelocity >= staticParams.isFlyingThreshold); |
365 | } |
364 | } |
366 | 365 | ||
367 | void analog_updateBatteryVoltage(void) { |
366 | void analog_updateBatteryVoltage(void) { |
368 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
367 | // Battery. The measured value is: (V * 1k/11k)/3v * 1024 = 31.03 counts per volt (max. measurable is 33v). |
369 | // This is divided by 3 --> 10.34 counts per volt. |
368 | // This is divided by 3 --> 10.34 counts per volt. |
370 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
369 | UBat = (3 * UBat + sensorInputs[AD_UBAT] / 3) / 4; |
371 | } |
370 | } |
372 | 371 | ||
373 | void analog_update(void) { |
372 | void analog_update(void) { |
374 | analog_updateGyros(); |
373 | analog_updateGyros(); |
375 | // analog_updateAccelerometers(); |
374 | // analog_updateAccelerometers(); |
376 | analog_updateAirspeed(); |
375 | analog_updateAirspeed(); |
377 | analog_updateBatteryVoltage(); |
376 | analog_updateBatteryVoltage(); |
378 | #ifdef USE_MK3MAG |
377 | #ifdef USE_MK3MAG |
379 | magneticHeading = volatileMagneticHeading; |
378 | magneticHeading = volatileMagneticHeading; |
380 | #endif |
379 | #endif |
381 | } |
380 | } |
382 | 381 | ||
383 | void analog_setNeutral() { |
382 | void analog_setNeutral() { |
384 | gyro_init(); |
383 | gyro_init(); |
385 | 384 | ||
386 | if (gyroOffset_readFromEEProm()) { |
385 | if (gyroOffset_readFromEEProm()) { |
387 | printf("gyro offsets invalid%s",recal); |
386 | printf("gyro offsets invalid%s",recal); |
388 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_OVERSAMPLING; |
387 | gyroOffset.offsets[PITCH] = gyroOffset.offsets[ROLL] = 512 * GYRO_OVERSAMPLING; |
389 | gyroOffset.offsets[YAW] = 512 * GYRO_OVERSAMPLING; |
388 | gyroOffset.offsets[YAW] = 512 * GYRO_OVERSAMPLING; |
390 | } |
389 | } |
391 | 390 | ||
392 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
391 | // Noise is relative to offset. So, reset noise measurements when changing offsets. |
393 | for (uint8_t i=PITCH; i<=YAW; i++) { |
392 | for (uint8_t i=PITCH; i<=YAW; i++) { |
394 | gyroNoisePeak[i] = 0; |
393 | gyroNoisePeak[i] = 0; |
395 | gyroD[i] = 0; |
394 | gyroD[i] = 0; |
396 | for (uint8_t j=0; j<GYRO_D_WINDOW_LENGTH; j++) { |
395 | for (uint8_t j=0; j<GYRO_D_WINDOW_LENGTH; j++) { |
397 | gyroDWindow[i][j] = 0; |
396 | gyroDWindow[i][j] = 0; |
398 | } |
397 | } |
399 | } |
398 | } |
400 | // Setting offset values has an influence in the analog.c ISR |
399 | // Setting offset values has an influence in the analog.c ISR |
401 | // Therefore run measurement for 100ms to achive stable readings |
400 | // Therefore run measurement for 100ms to achive stable readings |
402 | delay_ms_with_adc_measurement(100, 0); |
401 | delay_ms_with_adc_measurement(100, 0); |
403 | 402 | ||
404 | // gyroActivity = 0; |
403 | // gyroActivity = 0; |
405 | } |
404 | } |
406 | 405 | ||
407 | void analog_calibrate(void) { |
406 | void analog_calibrate(void) { |
408 | #define OFFSET_CYCLES 64 |
407 | #define OFFSET_CYCLES 64 |
409 | uint8_t i, axis; |
408 | uint8_t i, axis; |
410 | int32_t offsets[4] = { 0, 0, 0, 0}; |
409 | int32_t offsets[4] = { 0, 0, 0, 0}; |
411 | gyro_calibrate(); |
410 | gyro_calibrate(); |
412 | 411 | ||
413 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
412 | // determine gyro bias by averaging (requires that the copter does not rotate around any axis!) |
414 | for (i = 0; i < OFFSET_CYCLES; i++) { |
413 | for (i = 0; i < OFFSET_CYCLES; i++) { |
415 | delay_ms_with_adc_measurement(10, 1); |
414 | delay_ms_with_adc_measurement(10, 1); |
416 | for (axis = PITCH; axis <= YAW; axis++) { |
415 | for (axis = PITCH; axis <= YAW; axis++) { |
417 | offsets[axis] += rawGyroValue(axis); |
416 | offsets[axis] += rawGyroValue(axis); |
418 | } |
417 | } |
419 | offsets[3] += sensorInputs[AD_AIRPRESSURE]; |
418 | offsets[3] += sensorInputs[AD_AIRPRESSURE]; |
420 | } |
419 | } |
421 | 420 | ||
422 | for (axis = PITCH; axis <= YAW; axis++) { |
421 | for (axis = PITCH; axis <= YAW; axis++) { |
423 | gyroOffset.offsets[axis] = (offsets[axis] + OFFSET_CYCLES / 2) / OFFSET_CYCLES; |
422 | gyroOffset.offsets[axis] = (offsets[axis] + OFFSET_CYCLES / 2) / OFFSET_CYCLES; |
424 | int16_t min = (512-200) * GYRO_OVERSAMPLING; |
423 | int16_t min = (512-200) * GYRO_OVERSAMPLING; |
425 | int16_t max = (512+200) * GYRO_OVERSAMPLING; |
424 | int16_t max = (512+200) * GYRO_OVERSAMPLING; |
426 | if(gyroOffset.offsets[axis] < min || gyroOffset.offsets[axis] > max) |
425 | if(gyroOffset.offsets[axis] < min || gyroOffset.offsets[axis] > max) |
427 | versionInfo.hardwareErrors[0] |= FC_ERROR0_GYRO_PITCH << axis; |
426 | versionInfo.hardwareErrors[0] |= FC_ERROR0_GYRO_PITCH << axis; |
428 | } |
427 | } |
429 | 428 | ||
430 | airpressureOffset = (offsets[3] + OFFSET_CYCLES / 2) / OFFSET_CYCLES; |
429 | airpressureOffset = (offsets[3] + OFFSET_CYCLES / 2) / OFFSET_CYCLES; |
431 | int16_t min = 200; |
430 | int16_t min = 200; |
432 | int16_t max = 1024-200; |
431 | int16_t max = 1024-200; |
433 | if(airpressureOffset < min || airpressureOffset > max) |
432 | if(airpressureOffset < min || airpressureOffset > max) |
434 | versionInfo.hardwareErrors[0] |= FC_ERROR0_PRESSURE; |
433 | versionInfo.hardwareErrors[0] |= FC_ERROR0_PRESSURE; |
435 | 434 | ||
436 | gyroOffset_writeToEEProm(); |
435 | gyroOffset_writeToEEProm(); |
437 | 436 | ||
438 | startAnalogConversionCycle(); |
437 | startAnalogConversionCycle(); |
439 | } |
438 | } |
440 | 439 |