Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1645 - 1
/*********************************************************************************/
2
/* Attitude sense system (processing of gyro, accelerometer and altimeter data)  */
3
/*********************************************************************************/
1612 dongfang 4
 
5
#ifndef _ATTITUDE_H
6
#define _ATTITUDE_H
7
 
8
#include <inttypes.h>
9
 
10
#include "analog.h"
2052 - 11
#include "timer0.h"
1612 dongfang 12
 
13
/*
14
 * If you have no acc. sensor or do not want to use it, remove this define. This will cause the
15
 * acc. sensor to be ignored at attitude calibration.
16
 */
17
#define ATTITUDE_USE_ACC_SENSORS yes
18
 
19
/*
20
 * Default hysteresis to use for the -180 degree to 180 degree wrap.
21
 */
22
#define PITCHOVER_HYSTERESIS 0L
23
#define ROLLOVER_HYSTERESIS 0L
24
 
25
/*
26
 * The frequency at which numerical integration takes place. 488 in original code.
27
 */
2052 - 28
#define INTEGRATION_FREQUENCY F_MAINLOOP
1612 dongfang 29
 
30
/*
31
 * Gyro readings are divided by this before being used in attitude control. This will scale them
32
 * to match the scale of the stick control etc. variables. This is just a rough non-precision
33
 * scaling - the below definitions make precise conversion factors.
34
 */
1874 - 35
#define HIRES_GYRO_INTEGRATION_FACTOR 1
36
// (int)((GYRO_RATE_FACTOR_PITCHROLL * INTEGRATION_FREQUENCY * GYRO_PITCHROLL_CORRECTION) / 1250)
1612 dongfang 37
 
38
/*
2049 - 39
 Gyro integration:
40
 
41
 The control loop executes at INTEGRATION_FREQUENCY Hz, and for each iteration
42
 gyro_ATT[PITCH/ROLL] is added to gyroIntegral[PITCH/ROLL].
43
 Assuming a constant rotation rate v and a zero initial gyroIntegral
44
 (for this explanation), we get:
45
 
46
 gyroIntegral =
47
 t * INTEGRATION_FREQUENCY * v * GYRO_RATE_FACTOR_PITCHROLL / HIRES_GYRO_INTEGRATION_FACTOR
48
 
49
 For one degree of rotation: t*v = 1:
50
 
51
 gyroIntegral = INTEGRATION_FREQUENCY * v * GYRO_RATE_FACTOR_PITCHROLL / HIRES_GYRO_INTEGRATION_FACTOR
52
 
53
 This number (INTEGRATION_FREQUENCY * v * GYRO_RATE_FACTOR_PITCHROLL / HIRES_GYRO_INTEGRATION_FACTOR) is the integral-to-degree factor.
54
 
55
 Examples:
56
 FC1.3:                 GYRO_DEG_FACTOR_PITCHROLL = 2545
57
 FC2.0:                 GYRO_DEG_FACTOR_PITCHROLL = 2399
58
 My InvenSense copter:  GYRO_DEG_FACTOR_PITCHROLL = 1333
59
 */
2052 - 60
//#define GYRO_PITCHROLL_CORRECTION GYRO_PITCHROLL_CORRECTION_should_be_overridden_with_a_-D_at_compile_time
2049 - 61
#define GYRO_DEG_FACTOR_PITCHROLL (uint16_t)(GYRO_RATE_FACTOR_PITCHROLL * INTEGRATION_FREQUENCY * GYRO_PITCHROLL_CORRECTION / HIRES_GYRO_INTEGRATION_FACTOR)
62
#define GYRO_DEG_FACTOR_YAW (uint16_t)(GYRO_RATE_FACTOR_YAW * INTEGRATION_FREQUENCY * GYRO_YAW_CORRECTION)
63
 
64
/*
1612 dongfang 65
 * Constant for deriving an attitude angle from acceleration measurement.
66
 *
67
 * The value is derived from the datasheet of the ACC sensor where 5g are scaled to VRef.
68
 * 1g is (3V * 1024) / (5 * 3V) = 205 counts. The ADC ISR sums 2 acc. samples to each
69
 * [pitch/roll]AxisAcc and thus reads about acc = 410 counts / g.
70
 * We approximate a small pitch/roll angle v by assuming that the copter does not accelerate:
71
 * In this explanation it is assumed that the ADC values are 0 based, and gravity is included.
72
 * The sine of v is the far side / the hypothenusis:
73
 * sin v = acc / sqrt(acc^2 + acc_z^2)
74
 * Using that v is a small angle, and the near side is about equal to the the hypothenusis:
75
 * sin v ~= acc / acc_z
2160 - 76
 * Assuming that the multicopter is hovering at small pitch and roll angles, acc_z is about 410,
1612 dongfang 77
 * and sin v ~= v (small angles, in radians):
78
 * sin v ~= acc / 410
79
 * v / 57.3 ~= acc / 410
80
 * v ~= acc * 57.3 / 410
81
 * acc / v ~= 410 / 57.3 ~= 7, that is: There are about 7 counts per degree.
82
 *
83
 * Summary: DEG_ACC_FACTOR = (2 * 1024 * [sensitivity of acc. meter in V/g]) / (3V * 57.3)
84
 */
85
#define DEG_ACC_FACTOR 7
86
 
87
 
88
/*
89
 * This is ([gyro integral value] / degree) / (degree / acc. sensor value) = gyro integral value / acc.sensor value
90
 * = the factor an acc. sensor should be multiplied by to get the gyro integral
91
 * value for the same (small) angle.
92
 * The value is about 200.
93
 */
94
#define GYRO_ACC_FACTOR ((GYRO_DEG_FACTOR_PITCHROLL) / (DEG_ACC_FACTOR))
95
 
2048 - 96
#define PITCHROLLOVER180 (GYRO_DEG_FACTOR_PITCHROLL * 180L)
97
#define PITCHROLLOVER360 (GYRO_DEG_FACTOR_PITCHROLL * 360L)
98
#define YAWOVER180       (GYRO_DEG_FACTOR_YAW * 180L)
99
#define YAWOVER360       (GYRO_DEG_FACTOR_YAW * 360L)
100
 
1612 dongfang 101
/*
102
 * Rotation rates
103
 */
1775 - 104
extern int16_t rate_PID[2], rate_ATT[2], yawRate;
1645 - 105
extern int16_t differential[2];
1612 dongfang 106
 
107
/*
1645 - 108
 * Attitudes calculated by numerical integration of gyro rates
1612 dongfang 109
 */
2048 - 110
extern int32_t attitude[2];
1612 dongfang 111
 
2048 - 112
// This is really a flight module thing, but it should be corrected along
113
// when the yaw angle is corrected from the compass, and that happens here.
114
// extern int32_t yawAngleDiff;
1612 dongfang 115
 
116
/*
117
 * Compass navigation
118
 */
2048 - 119
extern int32_t heading;
1805 - 120
extern uint16_t ignoreCompassTimer;
1980 - 121
extern uint16_t accVector;
1612 dongfang 122
 
2051 - 123
extern int32_t headingError;
1775 - 124
 
2048 - 125
 
1612 dongfang 126
/*
127
 * Dynamic gyro offsets. These are signed values that are subtracted from the gyro measurements,
1872 - 128
 * to help canceling out drift and vibration noise effects. The dynamic offsets themselves
1612 dongfang 129
 * can be updated in flight by different ways, for example:
130
 * - Just taking them from parameters, so the pilot can trim manually in a PC or mobile tool
131
 * - Summing up how much acc. meter correction was done to the gyro integrals over the last n
132
 *   integration, and then adding the sum / n to the dynamic offset
133
 * - Detect which way the pilot pulls the stick to keep the copter steady, and correct by that
134
 * - Invent your own...
135
 */
1645 - 136
extern int16_t dynamicOffset[2], dynamicOffsetYaw;
1612 dongfang 137
 
138
/*
1775 - 139
 * For NaviCtrl use.
140
 */
141
extern int16_t averageAcc[2], averageAccCount;
142
 
143
/*
1612 dongfang 144
 * Re-init flight attitude, setting all angles to 0 (or to whatever can be derived from acc. sensor).
145
 * To be called when the pilot commands gyro calibration (eg. by moving the left stick up-left or up-right).
146
 */
147
void attitude_setNeutral(void);
148
 
149
/*
150
 * Experiment.
151
 */
152
// void attitude_startDynamicCalibration(void);
153
// void attitude_continueDynamicCalibration(void);
154
 
1775 - 155
int32_t getAngleEstimateFromAcc(uint8_t axis);
156
 
1612 dongfang 157
/*
158
 * Main routine, called from the flight loop.
159
 */
160
void calculateFlightAttitude(void);
2035 - 161
 
2048 - 162
void attitude_resetHeadingToMagnetic(void);
163
 
1612 dongfang 164
#endif //_ATTITUDE_H