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