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