Subversion Repositories FlightCtrl

Rev

Rev 2099 | Rev 2104 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1910 - 1
#ifndef _ANALOG_H
2
#define _ANALOG_H
3
#include <inttypes.h>
2096 - 4
#include "configuration.h"
1910 - 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.
2096 - 16
 
1910 - 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
2096 - 19
 sampled more than once (oversampled), and the results added.
1910 - 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
2096 - 23
 roll. The factor 2 or 4 or whatever is called GYRO_FACTOR_PITCHROLL here.
24
*/
1910 - 25
 
2096 - 26
/*
27
 GYRO_HW_FACTOR is the relation between rotation rate and ADCValue:
1910 - 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
 
2096 - 35
 GYRO_HW_FACTOR is:
1910 - 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:
2096 - 43
 GYRO_HW_FACTOR = 0.00067 V / deg / s * 5.7 * 1024 / 3V = 1.304 units/(deg/s).
44
 
1910 - 45
 FC2.0 has 6*(3/5) mV/deg/s gyros (they are ratiometric) and no amplifiers:
2096 - 46
 GYRO_HW_FACTOR = 0.006 V / deg / s * 1 * 1024 * 3V / (3V * 5V) = 1.2288 units/(deg/s).
47
 
1910 - 48
 My InvenSense copter has 2mV/deg/s gyros and no amplifiers:
2096 - 49
 GYRO_HW_FACTOR = 0.002 V / deg / s * 1 * 1024 / 3V = 0.6827 units/(deg/s)
1910 - 50
 (only about half as sensitive as V1.3. But it will take about twice the
51
 rotation rate!)
52
 
2096 - 53
 GYRO_HW_FACTOR is given in the makefile.
54
*/
1910 - 55
 
56
/*
2096 - 57
 * How many samples are added in one ADC loop, for pitch&roll and yaw,
1910 - 58
 * respectively. This is = the number of occurences of each channel in the
59
 * channelsForStates array in analog.c.
60
 */
2099 - 61
#define GYRO_OVERSAMPLING 4
1910 - 62
 
2099 - 63
//#define ACC_OVERSAMPLING_XY 2
64
//#define ACC_OVERSAMPLING_Z 1
1910 - 65
 
66
/*
2096 - 67
 * The product of the 3 above constants. This represents the expected change in ADC value sums for 1 deg/s of rotation rate.
1910 - 68
 */
2099 - 69
#define GYRO_RATE_FACTOR (GYRO_HW_FACTOR * GYRO_OVERSAMPLING)
1910 - 70
 
71
/*
72
 * The value of gyro[PITCH/ROLL] for one deg/s = The hardware factor H * the number of samples * multiplier factor.
73
 * Will be about 10 or so for InvenSense, and about 33 for ADXRS610.
74
 */
75
 
76
/*
77
 * Gyro saturation prevention.
78
 */
79
// How far from the end of its range a gyro is considered near-saturated.
2099 - 80
#define SENSOR_MIN 32
1910 - 81
// Other end of the range (calculated)
2099 - 82
#define SENSOR_MAX (GYRO_OVERSAMPLING * 1023 - SENSOR_MIN)
1910 - 83
// Max. boost to add "virtually" to gyro signal at total saturation.
84
#define EXTRAPOLATION_LIMIT 2500
85
// Slope of the boost (calculated)
2099 - 86
#define EXTRAPOLATION_SLOPE (EXTRAPOLATION_LIMIT/SENSOR_MIN)
1910 - 87
 
88
/*
89
 * This value is subtracted from the gyro noise measurement in each iteration,
90
 * making it return towards zero.
91
 */
92
#define GYRO_NOISE_MEASUREMENT_DAMPING 5
93
 
94
#define PITCH 0
95
#define ROLL 1
96
#define YAW 2
2099 - 97
//#define Z 2
1910 - 98
/*
99
 * The values that this module outputs
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.
106
 */
2099 - 107
extern int16_t gyro_PID[3];
108
extern int16_t gyro_ATT[3];
2096 - 109
#define GYRO_D_WINDOW_LENGTH 8
2099 - 110
 
2096 - 111
extern int16_t gyroD[3];
112
extern int16_t UBat;
1910 - 113
 
114
// 1:11 voltage divider, 1024 counts per 3V, and result is divided by 3.
115
#define UBAT_AT_5V (int16_t)((5.0 * (1.0/11.0)) * 1024 / (3.0 * 3))
116
 
2096 - 117
extern sensorOffset_t gyroOffset;
2099 - 118
//extern sensorOffset_t accOffset;
2102 - 119
//extern sensorOffset_t gyroAmplifierOffset;
2096 - 120
 
1910 - 121
/*
122
 * This is not really for external use - but the ENC-03 gyro modules needs it.
123
 */
2096 - 124
//extern volatile int16_t rawGyroSum[3];
1910 - 125
 
126
/*
127
 * The acceleration values that this module outputs. They are zero based.
128
 */
2099 - 129
//extern int16_t acc[3];
130
//extern int16_t filteredAcc[3];
1910 - 131
// extern volatile int32_t stronglyFilteredAcc[3];
132
 
133
/*
134
 * Diagnostics: Gyro noise level because of motor vibrations. The variables
135
 * only really reflect the noise level when the copter stands still but with
136
 * its motors running.
137
 */
2096 - 138
extern uint16_t gyroNoisePeak[3];
2102 - 139
//extern uint16_t accNoisePeak[3];
1910 - 140
 
141
/*
142
 * Air pressure.
2096 - 143
 * The sensor has a sensitivity of 45 mV/kPa.
144
 * An approximate p(h) formula is = p(h[m])[kPa] = p_0 - 11.95 * 10^-3 * h
145
 * p(h[m])[kPa] = 101.3 - 11.95 * 10^-3 * h
146
 * 11.95 * 10^-3 * h = 101.3 - p[kPa]
147
 * h = (101.3 - p[kPa])/0.01195
148
 * That is: dV = -45 mV * 11.95 * 10^-3 dh = -0.53775 mV / m.
149
 * That is, with 38.02 * 1.024 / 3 steps per mV: -7 steps / m
150
 
151
Display pressures
152
4165 mV-->1084.7
153
4090 mV-->1602.4   517.7
154
3877 mV-->3107.8  1503.4
155
 
156
4165 mV-->1419.1
157
3503 mV-->208.1
158
Diff.:   1211.0
159
 
160
Calculated  Vout = 5V(.009P-0.095) --> 5V .009P = Vout + 5V 0.095 --> P = (Vout + 5V 0.095)/(5V 0.009)
161
4165 mV = 5V(0.009P-0.095)  P = 103.11 kPa  h = -151.4m
162
4090 mV = 5V(0.009P-0.095)  P = 101.44 kPa  h = -11.7m   139.7m
163
3877 mV = 5V(0.009P-0.095)  P = 96.7   kPa  h = 385m     396.7m
164
 
165
4165 mV = 5V(0.009P-0.095)  P = 103.11 kPa  h = -151.4m
166
3503 mV = 5V(0.009P-0.095)  P = 88.4   kPa  h = 384m  Diff: 1079.5m
167
Pressure at sea level: 101.3 kPa. voltage: 5V * (0.009P-0.095) = 4.0835V
168
This is OCR2 = 143.15 at 1.5V in --> simple pressure =
169
*/
170
 
171
extern uint16_t simpleAirPressure;
2102 - 172
extern uint16_t airspeedVelocity;
1910 - 173
 
174
/*
175
 * Flag: Interrupt handler has done all A/D conversion and processing.
176
 */
177
extern volatile uint8_t analogDataReady;
178
 
2096 - 179
 
1910 - 180
void analog_init(void);
181
 
2096 - 182
/*
183
 * This is really only for use for the ENC-03 code module, which needs to get the raw value
184
 * for its calibration. The raw value should not be used for anything else.
185
 */
186
uint16_t rawGyroValue(uint8_t axis);
1910 - 187
 
2096 - 188
/*
189
 * Start the conversion cycle. It will stop automatically.
190
 */
191
void startAnalogConversionCycle(void);
1910 - 192
 
193
/*
2096 - 194
 * Process the sensor data to update the exported variables. Must be called after each measurement cycle and before the data is used.
1910 - 195
 */
2096 - 196
void analog_update(void);
1910 - 197
 
198
/*
2096 - 199
 * Read gyro and acc.meter calibration from EEPROM.
1910 - 200
 */
2096 - 201
void analog_setNeutral(void);
202
 
203
/*
204
 * Zero-offset gyros and write the calibration data to EEPROM.
205
 */
206
void analog_calibrateGyros(void);
207
 
208
/*
209
 * Zero-offset accelerometers and write the calibration data to EEPROM.
210
 */
2099 - 211
//void analog_calibrateAcc(void);
2096 - 212
 
1910 - 213
#endif //_ANALOG_H