Attitude estimation using IMU (3-DoF ver.)

Fork of ATTITUDE_ESTIMATION by LDSC_Robotics_TAs

Committer:
benson516
Date:
Wed Dec 28 16:53:58 2016 +0000
Revision:
9:84fad91d3587
Parent:
8:3882cb4be9d3
Child:
10:166006e89252
Remove the one-over-gamma from constructor's argument list

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benson516 0:8126c86bac2a 1 #ifndef _ATTITUDE_ESTIMATION_H_
benson516 0:8126c86bac2a 2 #define _ATTITUDE_ESTIMATION_H_
benson516 6:c362ed165c39 3
benson516 0:8126c86bac2a 4 #include "mbed.h"
benson516 6:c362ed165c39 5 #include <vector>
benson516 6:c362ed165c39 6
benson516 6:c362ed165c39 7 using std::vector;
benson516 6:c362ed165c39 8
benson516 6:c362ed165c39 9
benson516 6:c362ed165c39 10 class LPF_vector
benson516 6:c362ed165c39 11 {public:
benson516 6:c362ed165c39 12 vector<float> output;
benson516 6:c362ed165c39 13
benson516 6:c362ed165c39 14 LPF_vector(size_t dimension, float samplingTime, float cutOff_freq_Hz_in); // cutOff_freq_Hz_in is in "Hz"
benson516 6:c362ed165c39 15 vector<float> filter(const vector<float> &v_in);
benson516 6:c362ed165c39 16 void reset(const vector<float> &v_in);
benson516 6:c362ed165c39 17
benson516 6:c362ed165c39 18 private:
benson516 6:c362ed165c39 19 size_t n;
benson516 6:c362ed165c39 20 float Ts;
benson516 6:c362ed165c39 21 float cutOff_freq_Hz; // Hz
benson516 6:c362ed165c39 22 float alpha_Ts;
benson516 6:c362ed165c39 23 float One_alpha_Ts;
benson516 6:c362ed165c39 24
benson516 6:c362ed165c39 25 // Flag
benson516 6:c362ed165c39 26 bool Flag_Init;
benson516 6:c362ed165c39 27
benson516 6:c362ed165c39 28 //
benson516 6:c362ed165c39 29 vector<float> zeros; // Zero vector [0;0;0]
benson516 6:c362ed165c39 30 };
benson516 6:c362ed165c39 31
benson516 0:8126c86bac2a 32
benson516 0:8126c86bac2a 33 // Class
benson516 0:8126c86bac2a 34 class ATTITUDE{
benson516 0:8126c86bac2a 35 public:
benson516 6:c362ed165c39 36
benson516 0:8126c86bac2a 37 // Variables
benson516 1:edc7ccfc5562 38 float alpha; // Convergent rate, rad/sec.
benson516 6:c362ed165c39 39 float one_over_gamma; // 1/gamma, one_over_gamma == 0 means no estimation on gyro bias
benson516 1:edc7ccfc5562 40 float Ts; // Sampling time, sec.
benson516 6:c362ed165c39 41 bool enable_biasEst; // Enable the gyro-bias estimation capability
benson516 8:3882cb4be9d3 42 bool enable_magEst; // Enable the estimation of magenetic field
benson516 6:c362ed165c39 43
benson516 6:c362ed165c39 44 // The map from "real" coordinate to "here" coordinate
benson516 6:c362ed165c39 45 // eg. accMap_real2here = [3,-1,-2];
benson516 6:c362ed165c39 46 // means: real -> here
benson516 6:c362ed165c39 47 // 1 x z 3
benson516 6:c362ed165c39 48 // 2 y -x -1
benson516 6:c362ed165c39 49 // 3 z -y -2
benson516 6:c362ed165c39 50 vector<int> accMap_real2here;
benson516 8:3882cb4be9d3 51 vector<int> magMap_real2here;
benson516 6:c362ed165c39 52 vector<int> gyroMap_real2here;
benson516 6:c362ed165c39 53
benson516 8:3882cb4be9d3 54 vector<float> xg_est; // Estimated g-vector
benson516 8:3882cb4be9d3 55 vector<float> xm_est; // Estimated m-vector (magnetic field)
benson516 8:3882cb4be9d3 56 // vector<float> x_est; // Estimated state
benson516 6:c362ed165c39 57 vector<float> gyroBias_est; // The estimated gyro bias in each channel
benson516 6:c362ed165c39 58 vector<float> omega; // Rotation speed in body-fixed frame
benson516 8:3882cb4be9d3 59 //
benson516 8:3882cb4be9d3 60 vector<float> y_acce; // Accelerometer outputs
benson516 8:3882cb4be9d3 61 vector<float> y_mag; // Magnetometer outputs
benson516 8:3882cb4be9d3 62
benson516 8:3882cb4be9d3 63 // No use, may be deleted
benson516 8:3882cb4be9d3 64 // vector<float> w_cross_ys; // omega X ys
benson516 6:c362ed165c39 65 vector<float> ys_cross_x_ys; // ys X (x_est - ys)
benson516 6:c362ed165c39 66
benson516 6:c362ed165c39 67 // Eular angles, in rad/s
benson516 6:c362ed165c39 68 float pitch;
benson516 6:c362ed165c39 69 float roll;
benson516 6:c362ed165c39 70 float yaw;
benson516 6:c362ed165c39 71
benson516 6:c362ed165c39 72
benson516 0:8126c86bac2a 73 // Constructor:
benson516 1:edc7ccfc5562 74 // Initialize the estimator
benson516 9:84fad91d3587 75 ATTITUDE(float alpha_in, float Ts_in); // alpha in rad/sec., Ts in sec.
benson516 6:c362ed165c39 76
benson516 0:8126c86bac2a 77 // Methods
benson516 7:6fc812e342e6 78 // Get the estimation results in Eular angle
benson516 7:6fc812e342e6 79 // vector <--> Eular angle
benson516 8:3882cb4be9d3 80 void Vectors_to_EulerAngle(const vector<float> &vg_in, const vector<float> &vm_in);
benson516 6:c362ed165c39 81 //float* EulerAngle_to_gVector(float Eu_in[]);
benson516 6:c362ed165c39 82
benson516 7:6fc812e342e6 83 // Setting parameters
benson516 7:6fc812e342e6 84 // Set L1, the diagonal matrix
benson516 7:6fc812e342e6 85 void Set_L1_diag(float alpha_in); // set diagnal element of gain matrix
benson516 9:84fad91d3587 86 void enable_gyroBiasEst(float gamma_in); // Enable the gyro-bias estimation
benson516 6:c362ed165c39 87
benson516 0:8126c86bac2a 88 // Estimator
benson516 8:3882cb4be9d3 89 void Init(void); // Let x_est = ys
benson516 8:3882cb4be9d3 90 // Two version of estimator, with/without magenetic field estimation
benson516 8:3882cb4be9d3 91 void iterateOnce(const vector<float> &y_acce_in, const vector<float> &omega_in); // Main alogorithm
benson516 8:3882cb4be9d3 92 void iterateOnce(const vector<float> &y_acce_in, const vector<float> &y_mag_in, const vector<float> &omega_in); // Main alogorithm
benson516 7:6fc812e342e6 93 // Get the results
benson516 6:c362ed165c39 94 void getEstimation_realCoordinate(vector<float> &V_out);
benson516 6:c362ed165c39 95 float pitch_deg(void);
benson516 6:c362ed165c39 96 float roll_deg(void);
benson516 6:c362ed165c39 97 float yaw_deg(void);
benson516 6:c362ed165c39 98
benson516 6:c362ed165c39 99 // Unit transformation
benson516 6:c362ed165c39 100 float pi; // pi = 3.1415926
benson516 6:c362ed165c39 101 float deg2rad; // = 3.1415926/180.0;
benson516 6:c362ed165c39 102 float rad2deg; // = 180.0/3.1415926;
benson516 7:6fc812e342e6 103 float gravity; // = 9.81 m/s^2
benson516 0:8126c86bac2a 104
benson516 0:8126c86bac2a 105 private:
benson516 6:c362ed165c39 106 // Dimension
benson516 6:c362ed165c39 107 size_t n;
benson516 6:c362ed165c39 108
benson516 5:01e322f4158f 109 // Variables
benson516 6:c362ed165c39 110 vector<float> unit_nx; // (-x) direction [-1;0;0]
benson516 6:c362ed165c39 111 vector<float> unit_ny; // (-y) direction [0;-1;0]
benson516 6:c362ed165c39 112 vector<float> unit_nz; // (-z) direction [0;0;-1]
benson516 6:c362ed165c39 113 vector<float> zeros; // Zero vector [0;0;0]
benson516 1:edc7ccfc5562 114 //
benson516 6:c362ed165c39 115 size_t init_flag; // Flag for displaying initialization status
benson516 1:edc7ccfc5562 116 // float _omega_x[3][3]; // Skew symmetric matrix of omega
benson516 6:c362ed165c39 117
benson516 6:c362ed165c39 118 vector<float> L1_diag; // Diagonal vector of gain matrix L1
benson516 6:c362ed165c39 119
benson516 7:6fc812e342e6 120 //
benson516 7:6fc812e342e6 121 // Input/output coordinate transformations within the different definitions between the "real" one and the "here" one
benson516 7:6fc812e342e6 122 void InputMapping(vector<float> &v_hereDef, const vector<float> &v_realDef, const vector<int> &map_real2here);
benson516 7:6fc812e342e6 123 void OutputMapping(vector<float> &v_realDef, const vector<float> &v_hereDef, const vector<int> &map_real2here);
benson516 7:6fc812e342e6 124
benson516 7:6fc812e342e6 125
benson516 7:6fc812e342e6 126 // The kernel of the estimation process
benson516 7:6fc812e342e6 127 void EstimationKernel(vector<float> &_x_est_, const vector<float> &_ys_, const vector<float> &_omega_);
benson516 7:6fc812e342e6 128 void updateGyroBiasEst(void);
benson516 0:8126c86bac2a 129
benson516 7:6fc812e342e6 130 // Utilities
benson516 7:6fc812e342e6 131 // vector operation
benson516 7:6fc812e342e6 132 //float* Vector_to_SkewSymmetry(float v_in[]);
benson516 7:6fc812e342e6 133 //float* SkewSymmetry_to_Vector(float M_in[3][]);
benson516 7:6fc812e342e6 134 void Get_CrossProduct3(vector<float> &v_c, const vector<float> &v_a, const vector<float> &v_b); // v_a X v_b
benson516 7:6fc812e342e6 135 vector<float> Get_VectorPlus(const vector<float> &v_a, const vector<float> &v_b, bool is_minus); // v_a + (or -) v_b
benson516 7:6fc812e342e6 136 vector<float> Get_VectorScalarMultiply(const vector<float> &v_a, float scale); // scale*v_a
benson516 7:6fc812e342e6 137 float Get_Vector3Norm(const vector<float> &v_in);
benson516 7:6fc812e342e6 138 void Normolization(vector<float> &V_out, const vector<float> &V_in);
benson516 7:6fc812e342e6 139
benson516 7:6fc812e342e6 140 // Low-pass filter, vector version
benson516 8:3882cb4be9d3 141 LPF_vector lpfv_y_acce;
benson516 8:3882cb4be9d3 142 LPF_vector lpfv_y_mag;
benson516 6:c362ed165c39 143 LPF_vector lpfv_w;
benson516 0:8126c86bac2a 144 };
benson516 6:c362ed165c39 145
benson516 6:c362ed165c39 146 #endif // _ATTITUDE_ESTIMATION_H_