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