Rev 2097 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1612 | dongfang | 1 | #ifndef _ANALOG_H |
2 | #define _ANALOG_H |
||
3 | #include <inttypes.h> |
||
1965 | - | 4 | #include "configuration.h" |
2189 | - | 5 | #include "Vector3.h" |
1612 | dongfang | 6 | |
7 | /* |
||
2049 | - | 8 | GYRO_HW_FACTOR is the relation between rotation rate and ADCValue: |
1821 | - | 9 | ADCValue [units] = |
2189 | - | 10 | rotational speed [rad/s] * |
11 | gyro sensitivity [V/rad/s] * |
||
1821 | - | 12 | amplifier gain [units] * |
13 | 1024 [units] / |
||
14 | 3V full range [V] |
||
1612 | dongfang | 15 | |
2049 | - | 16 | GYRO_HW_FACTOR is: |
2189 | - | 17 | gyro sensitivity [V/rad/s] * |
1821 | - | 18 | amplifier gain [units] * |
19 | 1024 [units] / |
||
20 | 3V full range [V] |
||
1612 | dongfang | 21 | |
1821 | - | 22 | Examples: |
2189 | - | 23 | FC1.3 has 38.39 mV/rad/s gyros and amplifiers with a gain of 5.7: |
24 | GYRO_HW_FACTOR = 0.03839 V/deg/s*5.7*1024/3V = 74.688 units/rad/s. |
||
2049 | - | 25 | |
2189 | - | 26 | FC2.0 has 6*(3/5) mV/rad/s gyros (they are ratiometric) and no amplifiers: |
27 | GYRO_HW_FACTOR = 0.006 V/rad/s*1*1024*3V/5V/3V = 70.405 units/(rad/s). |
||
2049 | - | 28 | |
1821 | - | 29 | My InvenSense copter has 2mV/deg/s gyros and no amplifiers: |
2189 | - | 30 | GYRO_HW_FACTOR = 0.002 V/rad/s*1*1024/3V = 39.1139 units/(rad/s) |
31 | (only about half as sensitive as V1.3. But it will take about twice the rotation rate!) |
||
1612 | dongfang | 32 | |
2049 | - | 33 | GYRO_HW_FACTOR is given in the makefile. |
34 | */ |
||
1612 | dongfang | 35 | |
2189 | - | 36 | #define ACCEL_HW_FACTOR 204.8f |
37 | |||
38 | // If defined, acceleration is scaled to m/s^2. Else it is scaled to g's. |
||
39 | #define USE_MSSQUARED 1 |
||
40 | |||
1612 | dongfang | 41 | /* |
2049 | - | 42 | * How many samples are added in one ADC loop, for pitch&roll and yaw, |
1612 | dongfang | 43 | * respectively. This is = the number of occurences of each channel in the |
44 | * channelsForStates array in analog.c. |
||
45 | */ |
||
2189 | - | 46 | #define GYRO_OVERSAMPLING_XY 8 |
47 | #define GYRO_RATE_FACTOR_XY (GYRO_HW_FACTOR * GYRO_OVERSAMPLING_XY * GYRO_XY_CORRECTION) |
||
48 | #define GYRO_RATE_FACTOR_XY_MS (GYRO_RATE_FACTOR_XY * 1000.0) |
||
1612 | dongfang | 49 | |
2189 | - | 50 | #define GYRO_OVERSAMPLING_Z 4 |
51 | #define GYRO_RATE_FACTOR_Z (GYRO_HW_FACTOR * GYRO_OVERSAMPLING_Z * GYRO_Z_CORRECTION) |
||
52 | #define GYRO_RATE_FACTOR_Z_MS (GYRO_RATE_FACTOR_Z * 1000.0) |
||
1646 | - | 53 | |
2189 | - | 54 | #define ACCEL_OVERSAMPLING_XY 4 |
55 | #define ACCEL_G_FACTOR_XY (ACCEL_HW_FACTOR * ACCEL_OVERSAMPLING_XY) |
||
56 | #define ACCEL_M_SSQUARED_FACTOR_XY (ACCEL_G_FACTOR_XY / 9.82f) |
||
1821 | - | 57 | |
2189 | - | 58 | #define ACCEL_OVERSAMPLING_Z 2 |
1612 | dongfang | 59 | |
2189 | - | 60 | // number of counts per g |
61 | #define ACCEL_G_FACTOR_Z (ACCEL_HW_FACTOR * ACCEL_OVERSAMPLING_Z) |
||
62 | // number of counts per m/s^2 |
||
63 | #define ACCEL_M_SSQUARED_FACTOR_Z (ACCEL_G_FACTOR_Z / 9.82f) |
||
64 | |||
65 | #ifdef USE_MSSQUARED |
||
66 | #define ACCEL_FACTOR_XY ACCEL_M_SSQUARED_FACTOR_XY |
||
67 | #define ACCEL_FACTOR_Z ACCEL_M_SSQUARED_FACTOR_Z |
||
68 | #else |
||
69 | #define ACCEL_FACTOR_XY ACCEL_G_FACTOR_XY |
||
70 | #define ACCEL_FACTOR_Z ACCEL_G_FACTOR_Z |
||
71 | #endif |
||
1612 | dongfang | 72 | /* |
1645 | - | 73 | * Gyro saturation prevention. |
74 | */ |
||
75 | // How far from the end of its range a gyro is considered near-saturated. |
||
2189 | - | 76 | #define SENSOR_MIN_XY (32 * GYRO_OVERSAMPLING_XY) |
1645 | - | 77 | // Other end of the range (calculated) |
2189 | - | 78 | #define SENSOR_MAX_XY (GYRO_OVERSAMPLING_XY * 1023 - SENSOR_MIN_XY) |
1645 | - | 79 | // Max. boost to add "virtually" to gyro signal at total saturation. |
80 | #define EXTRAPOLATION_LIMIT 2500 |
||
81 | // Slope of the boost (calculated) |
||
2189 | - | 82 | #define EXTRAPOLATION_SLOPE (EXTRAPOLATION_LIMIT/SENSOR_MIN_XY) |
1645 | - | 83 | |
2189 | - | 84 | #define X 0 |
85 | #define Y 1 |
||
86 | #define Z 2 |
||
1612 | dongfang | 87 | |
2189 | - | 88 | // ADC channels |
89 | #define AD_GYRO_Z 0 |
||
90 | #define AD_GYRO_X 1 |
||
91 | #define AD_GYRO_Y 2 |
||
92 | #define AD_AIRPRESSURE 3 |
||
93 | #define AD_UBAT 4 |
||
94 | #define AD_ACCEL_Z 5 |
||
95 | #define AD_ACCEL_Y 6 |
||
96 | #define AD_ACCEL_X 7 |
||
97 | |||
1612 | dongfang | 98 | /* |
99 | * The values that this module outputs |
||
1645 | - | 100 | * These first 2 exported arrays are zero-offset. The "PID" ones are used |
101 | * in the attitude control as rotation rates. The "ATT" ones are for |
||
102 | * integration to angles. For the same axis, the PID and ATT variables |
||
103 | * generally have about the same values. There are just some differences |
||
104 | * in filtering, and when a gyro becomes near saturated. |
||
105 | * Maybe this distinction is not really necessary. |
||
1612 | dongfang | 106 | */ |
2189 | - | 107 | extern Vector3f gyro_attitude; |
108 | |||
109 | /* |
||
110 | * The acceleration values that this module outputs. They are zero based. |
||
111 | */ |
||
112 | extern Vector3f accel; |
||
113 | |||
2095 | - | 114 | #define GYRO_D_WINDOW_LENGTH 8 |
2189 | - | 115 | |
116 | extern int16_t gyro_control[3]; |
||
2015 | - | 117 | extern int16_t gyroD[2]; |
2189 | - | 118 | |
2015 | - | 119 | extern int16_t UBat; |
1612 | dongfang | 120 | |
1775 | - | 121 | // 1:11 voltage divider, 1024 counts per 3V, and result is divided by 3. |
1869 | - | 122 | #define UBAT_AT_5V (int16_t)((5.0 * (1.0/11.0)) * 1024 / (3.0 * 3)) |
1775 | - | 123 | |
1969 | - | 124 | extern sensorOffset_t gyroOffset; |
2189 | - | 125 | extern sensorOffset_t accelOffset; |
1969 | - | 126 | extern sensorOffset_t gyroAmplifierOffset; |
1960 | - | 127 | |
2189 | - | 128 | typedef enum { |
129 | CONTROL_SENSOR_SAMPLING_DATA, |
||
130 | CONTROL_SENSOR_DATA_READY, |
||
131 | } ControlSensorDataStatus; |
||
1612 | dongfang | 132 | |
2189 | - | 133 | typedef enum { |
134 | ATTITUDE_SENSOR_NO_DATA, |
||
135 | ATTITUDE_SENSOR_DATA_READY, |
||
136 | ATTITUDE_SENSOR_READING_DATA |
||
137 | } AttitudeSensorDataStatus; |
||
1612 | dongfang | 138 | |
2189 | - | 139 | extern volatile uint8_t analog_controlDataStatus; |
140 | extern volatile uint8_t analog_attitudeDataStatus; |
||
1775 | - | 141 | |
142 | /* |
||
143 | * Air pressure. |
||
1961 | - | 144 | * The sensor has a sensitivity of 45 mV/kPa. |
1970 | - | 145 | * An approximate p(h) formula is = p(h[m])[kPa] = p_0 - 11.95 * 10^-3 * h |
146 | * p(h[m])[kPa] = 101.3 - 11.95 * 10^-3 * h |
||
147 | * 11.95 * 10^-3 * h = 101.3 - p[kPa] |
||
148 | * h = (101.3 - p[kPa])/0.01195 |
||
149 | * That is: dV = -45 mV * 11.95 * 10^-3 dh = -0.53775 mV / m. |
||
150 | * That is, with 38.02 * 1.024 / 3 steps per mV: -7 steps / m |
||
151 | |||
152 | Display pressures |
||
153 | 4165 mV-->1084.7 |
||
154 | 4090 mV-->1602.4 517.7 |
||
155 | 3877 mV-->3107.8 1503.4 |
||
156 | |||
157 | 4165 mV-->1419.1 |
||
158 | 3503 mV-->208.1 |
||
159 | Diff.: 1211.0 |
||
160 | |||
161 | Calculated Vout = 5V(.009P-0.095) --> 5V .009P = Vout + 5V 0.095 --> P = (Vout + 5V 0.095)/(5V 0.009) |
||
162 | 4165 mV = 5V(0.009P-0.095) P = 103.11 kPa h = -151.4m |
||
163 | 4090 mV = 5V(0.009P-0.095) P = 101.44 kPa h = -11.7m 139.7m |
||
164 | 3877 mV = 5V(0.009P-0.095) P = 96.7 kPa h = 385m 396.7m |
||
165 | |||
166 | 4165 mV = 5V(0.009P-0.095) P = 103.11 kPa h = -151.4m |
||
167 | 3503 mV = 5V(0.009P-0.095) P = 88.4 kPa h = 384m Diff: 1079.5m |
||
168 | Pressure at sea level: 101.3 kPa. voltage: 5V * (0.009P-0.095) = 4.0835V |
||
169 | This is OCR2 = 143.15 at 1.5V in --> simple pressure = |
||
170 | */ |
||
171 | |||
2189 | - | 172 | #define AIRPRESSURE_OVERSAMPLING 28 |
1775 | - | 173 | #define AIRPRESSURE_FILTER 8 |
174 | // Minimum A/D value before a range change is performed. |
||
175 | #define MIN_RAWPRESSURE (200 * 2) |
||
176 | // Maximum A/D value before a range change is performed. |
||
177 | #define MAX_RAWPRESSURE (1023 * 2 - MIN_RAWPRESSURE) |
||
178 | |||
1796 | - | 179 | #define MIN_RANGES_EXTRAPOLATION 15 |
180 | #define MAX_RANGES_EXTRAPOLATION 240 |
||
1775 | - | 181 | |
182 | #define PRESSURE_EXTRAPOLATION_COEFF 25L |
||
183 | #define AUTORANGE_WAIT_FACTOR 1 |
||
184 | |||
1970 | - | 185 | #define ABS_ALTITUDE_OFFSET 108205 |
186 | |||
1612 | dongfang | 187 | void analog_init(void); |
188 | |||
1952 | - | 189 | /* |
2015 | - | 190 | * This is really only for use for the ENC-03 code module, which needs to get the raw value |
191 | * for its calibration. The raw value should not be used for anything else. |
||
192 | */ |
||
2189 | - | 193 | uint16_t gyroValueForFC13DACCalibration(uint8_t axis); |
2015 | - | 194 | |
195 | /* |
||
1952 | - | 196 | * Start the conversion cycle. It will stop automatically. |
197 | */ |
||
2189 | - | 198 | void startADCCycle(void); |
199 | void waitADCCycle(uint16_t delay); |
||
1612 | dongfang | 200 | |
1952 | - | 201 | /* |
202 | */ |
||
2189 | - | 203 | void analog_updateControlData(void); |
204 | void analog_sumAttitudeData(void); |
||
205 | void analog_updateAttitudeData(void); |
||
1612 | dongfang | 206 | |
207 | /* |
||
1961 | - | 208 | * Read gyro and acc.meter calibration from EEPROM. |
1612 | dongfang | 209 | */ |
1961 | - | 210 | void analog_setNeutral(void); |
1612 | dongfang | 211 | |
212 | /* |
||
1961 | - | 213 | * Zero-offset gyros and write the calibration data to EEPROM. |
1612 | dongfang | 214 | */ |
1961 | - | 215 | void analog_calibrateGyros(void); |
216 | |||
217 | /* |
||
218 | * Zero-offset accelerometers and write the calibration data to EEPROM. |
||
219 | */ |
||
1612 | dongfang | 220 | void analog_calibrateAcc(void); |
2033 | - | 221 | |
222 | |||
2035 | - | 223 | void analog_setGround(void); |
2033 | - | 224 | int32_t analog_getHeight(void); |
225 | int16_t analog_getDHeight(void); |
||
226 | |||
1612 | dongfang | 227 | #endif //_ANALOG_H |