Rev 2133 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2108 | - | 1 | #ifndef _ANALOG_H |
2 | #define _ANALOG_H |
||
3 | #include <inttypes.h> |
||
4 | #include "configuration.h" |
||
5 | |||
6 | /* |
||
7 | About setting constants for different gyros: |
||
8 | Main parameters are positive directions and voltage/angular speed gain. |
||
9 | The "Positive direction" is the rotation direction around an axis where |
||
10 | the corresponding gyro outputs a voltage > the no-rotation voltage. |
||
11 | A gyro is considered, in this code, to be "forward" if its positive |
||
12 | direction is: |
||
13 | - Nose down for pitch |
||
14 | - Left hand side down for roll |
||
15 | - Clockwise seen from above for yaw. |
||
16 | |||
17 | Setting gyro gain correctly: All sensor measurements in analog.c take |
||
18 | place in a cycle, each cycle comprising all sensors. Some sensors are |
||
19 | sampled more than once (oversampled), and the results added. |
||
20 | In the H&I code, the results for pitch and roll are multiplied by 2 (FC1.0) |
||
21 | or 4 (other versions), offset to zero, low pass filtered and then assigned |
||
22 | to the "HiResXXXX" and "AdWertXXXXFilter" variables, where XXXX is nick or |
||
23 | roll. The factor 2 or 4 or whatever is called GYRO_FACTOR_PITCHROLL here. |
||
24 | */ |
||
25 | |||
26 | /* |
||
27 | GYRO_HW_FACTOR is the relation between rotation rate and ADCValue: |
||
28 | ADCValue [units] = |
||
29 | rotational speed [deg/s] * |
||
30 | gyro sensitivity [V / deg/s] * |
||
31 | amplifier gain [units] * |
||
32 | 1024 [units] / |
||
33 | 3V full range [V] |
||
34 | |||
35 | GYRO_HW_FACTOR is: |
||
36 | gyro sensitivity [V / deg/s] * |
||
37 | amplifier gain [units] * |
||
38 | 1024 [units] / |
||
39 | 3V full range [V] |
||
40 | |||
41 | Examples: |
||
42 | FC1.3 has 0.67 mV/deg/s gyros and amplifiers with a gain of 5.7: |
||
43 | GYRO_HW_FACTOR = 0.00067 V / deg / s * 5.7 * 1024 / 3V = 1.304 units/(deg/s). |
||
44 | |||
45 | FC2.0 has 6*(3/5) mV/deg/s gyros (they are ratiometric) and no amplifiers: |
||
46 | GYRO_HW_FACTOR = 0.006 V / deg / s * 1 * 1024 * 3V / (3V * 5V) = 1.2288 units/(deg/s). |
||
47 | |||
48 | My InvenSense copter has 2mV/deg/s gyros and no amplifiers: |
||
49 | GYRO_HW_FACTOR = 0.002 V / deg / s * 1 * 1024 / 3V = 0.6827 units/(deg/s) |
||
50 | (only about half as sensitive as V1.3. But it will take about twice the |
||
51 | rotation rate!) |
||
52 | |||
53 | GYRO_HW_FACTOR is given in the makefile. |
||
54 | */ |
||
55 | |||
56 | /* |
||
57 | * How many samples are added in one ADC loop, for pitch&roll and yaw, |
||
58 | * respectively. This is = the number of occurences of each channel in the |
||
59 | * channelsForStates array in analog.c. |
||
60 | */ |
||
61 | #define GYRO_OVERSAMPLING 4 |
||
62 | |||
63 | /* |
||
64 | * The product of the 3 above constants. This represents the expected change in ADC value sums for 1 deg/s of rotation rate. |
||
65 | */ |
||
66 | #define GYRO_RATE_FACTOR (GYRO_HW_FACTOR * GYRO_OVERSAMPLING) |
||
67 | |||
68 | /* |
||
69 | * The value of gyro[PITCH/ROLL] for one deg/s = The hardware factor H * the number of samples * multiplier factor. |
||
70 | * Will be about 10 or so for InvenSense, and about 33 for ADXRS610. |
||
71 | */ |
||
72 | |||
73 | /* |
||
74 | * Gyro saturation prevention. |
||
75 | */ |
||
76 | // How far from the end of its range a gyro is considered near-saturated. |
||
77 | #define SENSOR_MIN 32 |
||
78 | // Other end of the range (calculated) |
||
79 | #define SENSOR_MAX (GYRO_OVERSAMPLING * 1023 - SENSOR_MIN) |
||
80 | // Max. boost to add "virtually" to gyro signal at total saturation. |
||
81 | #define EXTRAPOLATION_LIMIT 2500 |
||
82 | // Slope of the boost (calculated) |
||
83 | #define EXTRAPOLATION_SLOPE (EXTRAPOLATION_LIMIT/SENSOR_MIN) |
||
84 | |||
85 | /* |
||
86 | * This value is subtracted from the gyro noise measurement in each iteration, |
||
87 | * making it return towards zero. |
||
88 | */ |
||
89 | #define GYRO_NOISE_MEASUREMENT_DAMPING 5 |
||
90 | |||
91 | #define PITCH 0 |
||
92 | #define ROLL 1 |
||
93 | #define YAW 2 |
||
94 | //#define Z 2 |
||
95 | /* |
||
96 | * The values that this module outputs |
||
97 | * These first 2 exported arrays are zero-offset. The "PID" ones are used |
||
98 | * in the attitude control as rotation rates. The "ATT" ones are for |
||
99 | * integration to angles. For the same axis, the PID and ATT variables |
||
100 | * generally have about the same values. There are just some differences |
||
101 | * in filtering, and when a gyro becomes near saturated. |
||
102 | * Maybe this distinction is not really necessary. |
||
103 | */ |
||
104 | extern int16_t gyro_PID[3]; |
||
105 | extern int16_t gyro_ATT[3]; |
||
106 | extern int16_t gyroD[3]; |
||
107 | |||
108 | #define GYRO_D_WINDOW_LENGTH 8 |
||
109 | |||
110 | extern uint16_t UBat; |
||
111 | extern uint16_t airspeedVelocity; |
||
112 | |||
113 | // 1:11 voltage divider, 1024 counts per 3V, and result is divided by 3. |
||
114 | #define UBAT_AT_5V (int16_t)((5.0 * (1.0/11.0)) * 1024 / (3.0 * 3)) |
||
115 | extern sensorOffset_t gyroOffset; |
||
116 | extern uint16_t airpressureOffset; |
||
117 | |||
118 | /* |
||
119 | * This is not really for external use - but the ENC-03 gyro modules needs it. |
||
120 | */ |
||
121 | //extern volatile int16_t rawGyroSum[3]; |
||
122 | |||
123 | /* |
||
124 | * The acceleration values that this module outputs. They are zero based. |
||
125 | */ |
||
126 | //extern int16_t acc[3]; |
||
127 | //extern int16_t filteredAcc[3]; |
||
128 | // extern volatile int32_t stronglyFilteredAcc[3]; |
||
129 | |||
130 | /* |
||
131 | * Diagnostics: Gyro noise level because of motor vibrations. The variables |
||
132 | * only really reflect the noise level when the copter stands still but with |
||
133 | * its motors running. |
||
134 | */ |
||
135 | extern uint16_t gyroNoisePeak[3]; |
||
136 | |||
137 | /* |
||
138 | * Air pressure. |
||
139 | * The sensor has a sensitivity of 45 mV/kPa. |
||
140 | * An approximate p(h) formula is = p(h[m])[kPa] = p_0 - 11.95 * 10^-3 * h |
||
141 | * p(h[m])[kPa] = 101.3 - 11.95 * 10^-3 * h |
||
142 | * 11.95 * 10^-3 * h = 101.3 - p[kPa] |
||
143 | * h = (101.3 - p[kPa])/0.01195 |
||
144 | * That is: dV = -45 mV * 11.95 * 10^-3 dh = -0.53775 mV / m. |
||
145 | * That is, with 38.02 * 1.024 / 3 steps per mV: -7 steps / m |
||
146 | |||
147 | Display pressures |
||
148 | 4165 mV-->1084.7 |
||
149 | 4090 mV-->1602.4 517.7 |
||
150 | 3877 mV-->3107.8 1503.4 |
||
151 | |||
152 | 4165 mV-->1419.1 |
||
153 | 3503 mV-->208.1 |
||
154 | Diff.: 1211.0 |
||
155 | |||
156 | Calculated Vout = 5V(.009P-0.095) --> 5V .009P = Vout + 5V 0.095 --> P = (Vout + 5V 0.095)/(5V 0.009) |
||
157 | 4165 mV = 5V(0.009P-0.095) P = 103.11 kPa h = -151.4m |
||
158 | 4090 mV = 5V(0.009P-0.095) P = 101.44 kPa h = -11.7m 139.7m |
||
159 | 3877 mV = 5V(0.009P-0.095) P = 96.7 kPa h = 385m 396.7m |
||
160 | |||
161 | 4165 mV = 5V(0.009P-0.095) P = 103.11 kPa h = -151.4m |
||
162 | 3503 mV = 5V(0.009P-0.095) P = 88.4 kPa h = 384m Diff: 1079.5m |
||
163 | Pressure at sea level: 101.3 kPa. voltage: 5V * (0.009P-0.095) = 4.0835V |
||
164 | This is OCR2 = 143.15 at 1.5V in --> simple pressure = |
||
165 | */ |
||
166 | |||
167 | #define AIRPRESSURE_WINDOW_LENGTH 32 |
||
168 | extern uint16_t airspeedVelocity; |
||
169 | |||
170 | /* |
||
171 | * Flag: Interrupt handler has done all A/D conversion and processing. |
||
172 | */ |
||
173 | #define ADC_DATA_READY 1 |
||
174 | #define TWI_DATA_READY 2 |
||
175 | #define ALL_DATA_READY (ADC_DATA_READY+TWI_DATA_READY) |
||
176 | |||
177 | extern volatile uint8_t sensorDataReady; |
||
178 | |||
179 | void analog_init(void); |
||
180 | |||
181 | /* |
||
182 | * This is really only for use for the ENC-03 code module, which needs to get the raw value |
||
183 | * for its calibration. The raw value should not be used for anything else. |
||
184 | */ |
||
185 | uint16_t rawGyroValue(uint8_t axis); |
||
186 | |||
187 | /* |
||
188 | * Start the conversion cycle. It will stop automatically. |
||
189 | */ |
||
190 | void startAnalogConversionCycle(void); |
||
191 | |||
192 | /* |
||
193 | * Process the sensor data to update the exported variables. Must be called after each measurement cycle and before the data is used. |
||
194 | */ |
||
195 | void analog_update(void); |
||
196 | |||
197 | /* |
||
198 | * Read gyro and acc.meter calibration from EEPROM. |
||
199 | */ |
||
200 | void analog_setNeutral(void); |
||
201 | |||
202 | /* |
||
203 | * Zero-offset gyros and write the calibration data to EEPROM. |
||
204 | */ |
||
205 | void analog_calibrate(void); |
||
206 | |||
207 | #endif //_ANALOG_H |