Rev 2035 | Rev 2051 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2035 | Rev 2049 | ||
---|---|---|---|
Line 2... | Line 2... | ||
2 | #define _ANALOG_H |
2 | #define _ANALOG_H |
3 | #include <inttypes.h> |
3 | #include <inttypes.h> |
4 | #include "configuration.h" |
4 | #include "configuration.h" |
Line 5... | Line 5... | ||
5 | 5 | ||
6 | /* |
- | |
7 | * How much low pass filtering to apply for gyro_PID. |
- | |
8 | * 0=illegal, 1=no filtering, 2=50% last value + 50% new value, 3=67% last value + 33 % new value etc... |
- | |
9 | * Temporarily replaced by userparam-configurable variable. |
- | |
10 | */ |
- | |
11 | // #define GYROS_PID_FILTER 1 |
- | |
12 | - | ||
13 | /* |
- | |
14 | * How much low pass filtering to apply for gyro_ATT. |
- | |
15 | * 0=illegal, 1=no filtering, 2=50% last value + 50% new value, 3=67% last value + 33 % new value etc... |
- | |
16 | * Temporarily replaced by userparam-configurable variable. |
- | |
17 | */ |
- | |
18 | // #define GYROS_ATT_FILTER 1 |
- | |
19 | // #define ACC_FILTER 4 |
- | |
20 | - | ||
21 | /* |
6 | /* |
22 | About setting constants for different gyros: |
7 | About setting constants for different gyros: |
23 | Main parameters are positive directions and voltage/angular speed gain. |
8 | Main parameters are positive directions and voltage/angular speed gain. |
24 | The "Positive direction" is the rotation direction around an axis where |
9 | The "Positive direction" is the rotation direction around an axis where |
25 | the corresponding gyro outputs a voltage > the no-rotation voltage. |
10 | the corresponding gyro outputs a voltage > the no-rotation voltage. |
Line 29... | Line 14... | ||
29 | - Left hand side down for roll |
14 | - Left hand side down for roll |
30 | - Clockwise seen from above for yaw. |
15 | - Clockwise seen from above for yaw. |
Line 31... | Line 16... | ||
31 | |
16 | |
32 | Setting gyro gain correctly: All sensor measurements in analog.c take |
17 | Setting gyro gain correctly: All sensor measurements in analog.c take |
33 | 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 |
34 | sampled more than ones, and the results added. The pitch and roll gyros |
- | |
35 | are sampled 4 times and the yaw gyro 2 times in the original H&I V0.74 |
- | |
36 | code. |
19 | sampled more than once (oversampled), and the results added. |
37 | 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) |
38 | 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 |
39 | to the "HiResXXXX" and "AdWertXXXXFilter" variables, where XXXX is nick or |
- | |
40 | roll. |
- | |
41 | So: |
- | |
42 | 22 | to the "HiResXXXX" and "AdWertXXXXFilter" variables, where XXXX is nick or |
|
43 | gyro = V * (ADCValue1 + ADCValue2 + ADCValue3 + ADCValue4), |
- | |
44 | where V is 2 for FC1.0 and 4 for all others. |
23 | roll. The factor 2 or 4 or whatever is called GYRO_FACTOR_PITCHROLL here. |
45 | 24 | */ |
|
46 | Assuming constant ADCValue, in the H&I code: |
- | |
47 | |
- | |
48 | gyro = I * ADCValue. |
- | |
49 | - | ||
Line -... | Line 25... | ||
- | 25 | #define GYRO_FACTOR_PITCHROLL 1 |
|
50 | where I is 8 for FC1.0 and 16 for all others. |
26 | |
51 | 27 | /* |
|
52 | The relation between rotation rate and ADCValue: |
28 | GYRO_HW_FACTOR is the relation between rotation rate and ADCValue: |
53 | ADCValue [units] = |
29 | ADCValue [units] = |
54 | rotational speed [deg/s] * |
30 | rotational speed [deg/s] * |
55 | gyro sensitivity [V / deg/s] * |
31 | gyro sensitivity [V / deg/s] * |
56 | amplifier gain [units] * |
32 | amplifier gain [units] * |
Line 57... | Line -... | ||
57 | 1024 [units] / |
- | |
58 | 3V full range [V] |
33 | 1024 [units] / |
59 | - | ||
60 | or: H is the number of steps the ADC value changes with, |
34 | 3V full range [V] |
61 | for a 1 deg/s change in rotational velocity: |
35 | |
62 | H = ADCValue [units] / rotation rate [deg/s] = |
36 | GYRO_HW_FACTOR is: |
63 | gyro sensitivity [V / deg/s] * |
37 | gyro sensitivity [V / deg/s] * |
Line 64... | Line 38... | ||
64 | amplifier gain [units] * |
38 | amplifier gain [units] * |
65 | 1024 [units] / |
39 | 1024 [units] / |
66 | 3V full range [V] |
40 | 3V full range [V] |
- | 41 | ||
67 | 42 | Examples: |
|
68 | Examples: |
43 | 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). |
|
69 | FC1.3 has 0.67 mV/deg/s gyros and amplifiers with a gain of 5.7: |
45 | |
70 | H = 0.00067 V / deg / s * 5.7 * 1024 / 3V = 1.304 units/(deg/s). |
46 | FC2.0 has 6*(3/5) mV/deg/s gyros (they are ratiometric) and no amplifiers: |
71 | 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). |
72 | H = 0.006 V / deg / s * 1 * 1024 * 3V / (3V * 5V) = 1.2288 units/(deg/s). |
48 | |
Line 73... | Line 49... | ||
73 | My InvenSense copter has 2mV/deg/s gyros and no amplifiers: |
49 | My InvenSense copter has 2mV/deg/s gyros and no amplifiers: |
74 | H = 0.002 V / deg / s * 1 * 1024 / 3V = 0.6827 units/(deg/s) |
50 | GYRO_HW_FACTOR = 0.002 V / deg / s * 1 * 1024 / 3V = 0.6827 units/(deg/s) |
Line 75... | Line 51... | ||
75 | (only about half as sensitive as V1.3. But it will take about twice the |
51 | (only about half as sensitive as V1.3. But it will take about twice the |
76 | rotation rate!) |
- | |
77 | - | ||
78 | All together: gyro = I * H * rotation rate [units / (deg/s)]. |
- | |
79 | */ |
- | |
80 | - | ||
81 | /* |
- | |
82 | * A factor that the raw gyro values are multiplied by, |
- | |
83 | * before being filtered and passed to the attitude module. |
- | |
84 | * A value of 1 would cause a little bit of loss of precision in the |
- | |
85 | * filtering (on the other hand the values are so noisy in flight that |
- | |
86 | * it will not really matter - but when testing on the desk it might be |
- | |
87 | * noticeable). 4 is fine for the default filtering. |
52 | rotation rate!) |
88 | * Experiment: Set it to 1. |
53 | |
89 | */ |
54 | GYRO_HW_FACTOR is given in the makefile. |
90 | #define GYRO_FACTOR_PITCHROLL 1 |
55 | */ |
91 | 56 | ||
92 | /* |
57 | /* |
Line 93... | Line 58... | ||
93 | * How many samples are summed in one ADC loop, for pitch&roll and yaw, |
58 | * How many samples are added in one ADC loop, for pitch&roll and yaw, |
94 | * respectively. This is = the number of occurences of each channel in the |
59 | * respectively. This is = the number of occurences of each channel in the |
Line 95... | Line 60... | ||
95 | * channelsForStates array in analog.c. |
60 | * channelsForStates array in analog.c. |
96 | */ |
- | |
97 | #define GYRO_OVERSAMPLING_PITCHROLL 4 |
- | |
98 | #define GYRO_OVERSAMPLING_YAW 2 |
- | |
99 | - | ||
100 | #define ACC_OVERSAMPLING_XY 2 |
- | |
101 | #define ACC_OVERSAMPLING_Z 1 |
- | |
102 | 61 | */ |
|
103 | /* |
- | |
104 | Integration: |
- | |
105 | The HiResXXXX values are divided by 8 (in H&I firmware) before integration. |
- | |
106 | In the Killagreg rewrite of the H&I firmware, the factor 8 is called |
- | |
107 | HIRES_GYRO_AMPLIFY. In this code, it is called HIRES_GYRO_INTEGRATION_FACTOR, |
- | |
108 | and care has been taken that all other constants (gyro to degree factor, and |
- | |
109 | 180 degree flip-over detection limits) are corrected to it. Because the |
- | |
110 | division by the constant takes place in the flight attitude code, the |
- | |
111 | constant is there. |
- | |
112 | - | ||
113 | The control loop executes at 488Hz, and for each iteration |
- | |
114 | gyro_ATT[PITCH/ROLL] is added to gyroIntegral[PITCH/ROLL]. |
- | |
115 | Assuming a constant rotation rate v and a zero initial gyroIntegral |
- | |
116 | (for this explanation), we get: |
- | |
117 | - | ||
118 | gyroIntegral = |
- | |
119 | N * gyro / HIRES_GYRO_INTEGRATION_FACTOR = |
- | |
120 | N * I * H * v / HIRES_GYRO_INTEGRATION_FACTOR |
- | |
121 | - | ||
122 | where N is the number of summations; N = t*488. |
- | |
123 | - | ||
124 | For one degree of rotation: t*v = 1: |
- | |
125 | - | ||
126 | gyroIntegralXXXX = t * 488 * I * H * 1/t = INTEGRATION_FREQUENCY * I * H / HIRES_GYRO_INTEGRATION_FACTOR. |
62 | #define GYRO_OVERSAMPLING_PITCHROLL 4 |
- | 63 | #define GYRO_OVERSAMPLING_YAW 2 |
|
- | 64 | ||
Line 127... | Line 65... | ||
127 | 65 | #define ACC_OVERSAMPLING_XY 2 |
|
128 | This number (INTEGRATION_FREQUENCY * I * H) is the integral-to-degree factor. |
66 | #define ACC_OVERSAMPLING_Z 1 |
129 | 67 | ||
130 | Examples: |
68 | /* |
131 | FC1.3: I=4, H=1.304, HIRES_GYRO_INTEGRATION_FACTOR=1 --> integralDegreeFactor = 2545 |
- | |
132 | FC2.0: I=4, H=1.2288, HIRES_GYRO_INTEGRATION_FACTOR=1 --> integralDegreeFactor = 2399 |
- | |
Line 133... | Line 69... | ||
133 | My InvenSense copter: HIRES_GYRO_INTEGRATION_FACTOR=4, H=0.6827 --> integralDegreeFactor = 1333 |
69 | * The product of the 3 above constants. This represents the expected change in ADC value sums for 1 deg/s of rotation rate. |
134 | */ |
70 | */ |
135 | 71 | #define GYRO_RATE_FACTOR_PITCHROLL (GYRO_HW_FACTOR * GYRO_OVERSAMPLING_PITCHROLL * GYRO_FACTOR_PITCHROLL) |
|
136 | /* |
72 | #define GYRO_RATE_FACTOR_YAW (GYRO_HW_FACTOR * GYRO_OVERSAMPLING_YAW) |