Subversion Repositories FlightCtrl

Rev

Rev 2096 | Rev 2102 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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