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