Attitude estimation using IMU (3-DoF ver.)
Fork of ATTITUDE_ESTIMATION by
ATTITUDE_ESTIMATION.cpp@9:84fad91d3587, 2016-12-28 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
benson516 | 0:8126c86bac2a | 1 | #include "ATTITUDE_ESTIMATION.h" |
benson516 | 0:8126c86bac2a | 2 | |
benson516 | 6:c362ed165c39 | 3 | //=====================LPF ====================// |
benson516 | 6:c362ed165c39 | 4 | LPF_vector::LPF_vector(size_t dimension, float samplingTime, float cutOff_freq_Hz_in) |
benson516 | 6:c362ed165c39 | 5 | { |
benson516 | 6:c362ed165c39 | 6 | n = dimension; |
benson516 | 6:c362ed165c39 | 7 | Ts = samplingTime; |
benson516 | 6:c362ed165c39 | 8 | cutOff_freq_Hz = cutOff_freq_Hz_in; |
benson516 | 6:c362ed165c39 | 9 | alpha_Ts = (2*3.1415926)*cutOff_freq_Hz*Ts; |
benson516 | 6:c362ed165c39 | 10 | One_alpha_Ts = 1.0 - alpha_Ts; |
benson516 | 6:c362ed165c39 | 11 | |
benson516 | 6:c362ed165c39 | 12 | zeros.assign(n, 0.0); |
benson516 | 6:c362ed165c39 | 13 | |
benson516 | 6:c362ed165c39 | 14 | output = zeros; |
benson516 | 6:c362ed165c39 | 15 | |
benson516 | 6:c362ed165c39 | 16 | // |
benson516 | 6:c362ed165c39 | 17 | Flag_Init = false; |
benson516 | 6:c362ed165c39 | 18 | } |
benson516 | 6:c362ed165c39 | 19 | |
benson516 | 6:c362ed165c39 | 20 | vector<float> LPF_vector::filter(const vector<float> &input) |
benson516 | 6:c362ed165c39 | 21 | { |
benson516 | 6:c362ed165c39 | 22 | // Initialization |
benson516 | 6:c362ed165c39 | 23 | if (!Flag_Init){ |
benson516 | 6:c362ed165c39 | 24 | reset(input); |
benson516 | 6:c362ed165c39 | 25 | Flag_Init = true; |
benson516 | 6:c362ed165c39 | 26 | return output; |
benson516 | 6:c362ed165c39 | 27 | } |
benson516 | 6:c362ed165c39 | 28 | |
benson516 | 6:c362ed165c39 | 29 | for (size_t i = 0; i < n; ++i){ |
benson516 | 6:c362ed165c39 | 30 | // output = One_alpha_Ts*output + alpha_Ts*input; |
benson516 | 6:c362ed165c39 | 31 | output[i] += alpha_Ts*(input[i] - output[i]); |
benson516 | 6:c362ed165c39 | 32 | } |
benson516 | 6:c362ed165c39 | 33 | |
benson516 | 6:c362ed165c39 | 34 | return output; |
benson516 | 6:c362ed165c39 | 35 | } |
benson516 | 6:c362ed165c39 | 36 | void LPF_vector::reset(const vector<float> &input) |
benson516 | 6:c362ed165c39 | 37 | { |
benson516 | 6:c362ed165c39 | 38 | // output = (1.0 - alpha_Ts)*output + alpha_Ts*input; |
benson516 | 6:c362ed165c39 | 39 | output = input; |
benson516 | 6:c362ed165c39 | 40 | return; |
benson516 | 6:c362ed165c39 | 41 | } |
benson516 | 6:c362ed165c39 | 42 | |
benson516 | 6:c362ed165c39 | 43 | //-------------------------------------------------------// |
benson516 | 9:84fad91d3587 | 44 | ATTITUDE::ATTITUDE(float alpha_in, float Ts_in): |
benson516 | 6:c362ed165c39 | 45 | alpha(alpha_in), |
benson516 | 6:c362ed165c39 | 46 | Ts(Ts_in), |
benson516 | 8:3882cb4be9d3 | 47 | lpfv_y_acce(3, Ts_in, 10.0), // Input filter for accelerometers |
benson516 | 8:3882cb4be9d3 | 48 | lpfv_y_mag(3, Ts_in, 200.0), // Input filter for magenetometers |
benson516 | 8:3882cb4be9d3 | 49 | lpfv_w(3, Ts_in, 200.0) // Input filter for gyroscope |
benson516 | 0:8126c86bac2a | 50 | { |
benson516 | 6:c362ed165c39 | 51 | // Dimension |
benson516 | 6:c362ed165c39 | 52 | n = 3; |
benson516 | 6:c362ed165c39 | 53 | // |
benson516 | 6:c362ed165c39 | 54 | init_flag = 0; // Uninitialized |
benson516 | 6:c362ed165c39 | 55 | |
benson516 | 6:c362ed165c39 | 56 | // Default: close the gyro-bias estimation |
benson516 | 6:c362ed165c39 | 57 | enable_biasEst = false; |
benson516 | 9:84fad91d3587 | 58 | one_over_gamma = 0.0; |
benson516 | 9:84fad91d3587 | 59 | // Default: close the estimation for magenetic field |
benson516 | 8:3882cb4be9d3 | 60 | enable_magEst = false; |
benson516 | 6:c362ed165c39 | 61 | |
benson516 | 6:c362ed165c39 | 62 | // Unit transformation |
benson516 | 6:c362ed165c39 | 63 | pi = 3.1415926; |
benson516 | 6:c362ed165c39 | 64 | deg2rad = pi/180.0; |
benson516 | 6:c362ed165c39 | 65 | rad2deg = 180.0/pi; |
benson516 | 7:6fc812e342e6 | 66 | gravity = 9.81; // m/s^2 |
benson516 | 6:c362ed165c39 | 67 | |
benson516 | 6:c362ed165c39 | 68 | // The map from "real" coordinate to "here" coordinate |
benson516 | 6:c362ed165c39 | 69 | // eg. accMap_real2here = [3,-1,-2]; |
benson516 | 6:c362ed165c39 | 70 | // means: real -> here |
benson516 | 6:c362ed165c39 | 71 | // 1 x z 3 |
benson516 | 6:c362ed165c39 | 72 | // 2 y -x -1 |
benson516 | 6:c362ed165c39 | 73 | // 3 z -y -2 |
benson516 | 6:c362ed165c39 | 74 | // int accmap_temp[] = {3,-1,-2}; |
benson516 | 6:c362ed165c39 | 75 | int accmap_temp[] = {-3,1,2}; // Reverse: The direction of accelerometer is defined based on the direction of the acceleration of the sensor, not the g-direction |
benson516 | 6:c362ed165c39 | 76 | // int accmap_temp[] = {1, 2, 3}; |
benson516 | 6:c362ed165c39 | 77 | // |
benson516 | 8:3882cb4be9d3 | 78 | int magMap_temp[] = {3,-1, 2}; // real z-axis is in the reverse direction |
benson516 | 6:c362ed165c39 | 79 | int gyroMap_temp[] = {3,-1,-2}; |
benson516 | 6:c362ed165c39 | 80 | accMap_real2here.assign(accmap_temp, accmap_temp+n); |
benson516 | 8:3882cb4be9d3 | 81 | magMap_real2here.assign(magMap_temp, magMap_temp+n); |
benson516 | 6:c362ed165c39 | 82 | gyroMap_real2here.assign(gyroMap_temp, gyroMap_temp+n); |
benson516 | 6:c362ed165c39 | 83 | |
benson516 | 6:c362ed165c39 | 84 | // zeros |
benson516 | 6:c362ed165c39 | 85 | zeros.assign(n,0.0); |
benson516 | 6:c362ed165c39 | 86 | // unit_nz |
benson516 | 6:c362ed165c39 | 87 | unit_nx = zeros; |
benson516 | 6:c362ed165c39 | 88 | unit_ny = zeros; |
benson516 | 6:c362ed165c39 | 89 | unit_nz = zeros; |
benson516 | 6:c362ed165c39 | 90 | unit_nx[0] = -1; // negative x |
benson516 | 6:c362ed165c39 | 91 | unit_ny[1] = -1; // negative y |
benson516 | 6:c362ed165c39 | 92 | unit_nz[2] = -1; // negative z |
benson516 | 6:c362ed165c39 | 93 | |
benson516 | 6:c362ed165c39 | 94 | // States |
benson516 | 8:3882cb4be9d3 | 95 | xg_est = unit_nx; // g is pointing downward |
benson516 | 8:3882cb4be9d3 | 96 | xm_est = Get_VectorScalarMultiply(unit_nz, -1.0); // m is pointing forward |
benson516 | 6:c362ed165c39 | 97 | gyroBias_est = zeros; |
benson516 | 6:c362ed165c39 | 98 | omega = zeros; |
benson516 | 8:3882cb4be9d3 | 99 | // |
benson516 | 8:3882cb4be9d3 | 100 | y_acce = zeros; // Accelerometer outputs |
benson516 | 8:3882cb4be9d3 | 101 | y_mag = zeros; // Magnetometer outputs |
benson516 | 8:3882cb4be9d3 | 102 | // |
benson516 | 8:3882cb4be9d3 | 103 | // w_cross_ys = zeros; // omega X ys |
benson516 | 6:c362ed165c39 | 104 | ys_cross_x_ys = zeros; // ys X (x_est - ys) |
benson516 | 6:c362ed165c39 | 105 | |
benson516 | 6:c362ed165c39 | 106 | // Eular angles, in rad/s |
benson516 | 6:c362ed165c39 | 107 | pitch = 0.0; |
benson516 | 6:c362ed165c39 | 108 | roll = 0.0; |
benson516 | 6:c362ed165c39 | 109 | yaw = 0.0; |
benson516 | 6:c362ed165c39 | 110 | |
benson516 | 6:c362ed165c39 | 111 | |
benson516 | 6:c362ed165c39 | 112 | // Gain matrix |
benson516 | 1:edc7ccfc5562 | 113 | Set_L1_diag(alpha); |
benson516 | 6:c362ed165c39 | 114 | |
benson516 | 6:c362ed165c39 | 115 | } |
benson516 | 7:6fc812e342e6 | 116 | // Public methods |
benson516 | 8:3882cb4be9d3 | 117 | void ATTITUDE::Vectors_to_EulerAngle(const vector<float> &vg_in, const vector<float> &vm_in){ |
benson516 | 7:6fc812e342e6 | 118 | // |
benson516 | 7:6fc812e342e6 | 119 | // This function should be customized according to the definition of coordinate system |
benson516 | 7:6fc812e342e6 | 120 | // |
benson516 | 6:c362ed165c39 | 121 | |
benson516 | 7:6fc812e342e6 | 122 | /* |
benson516 | 7:6fc812e342e6 | 123 | // Here we follow the definition in bicycle paper |
benson516 | 7:6fc812e342e6 | 124 | yaw = 0.0; // phi, yaw |
benson516 | 8:3882cb4be9d3 | 125 | roll = atan2(-vg_in[1],vg_in[0]); // theta, roll |
benson516 | 8:3882cb4be9d3 | 126 | pitch = atan2(cos(roll)*vg_in[2],vg_in[0]); // psi, pitch |
benson516 | 7:6fc812e342e6 | 127 | */ |
benson516 | 7:6fc812e342e6 | 128 | |
benson516 | 9:84fad91d3587 | 129 | // Eular angle: 1-3-2, zs is pointing forward |
benson516 | 8:3882cb4be9d3 | 130 | // yaw = 0.0; // phi, yaw |
benson516 | 8:3882cb4be9d3 | 131 | pitch = atan2(-vg_in[2],-vg_in[0]); // psi, pitch |
benson516 | 7:6fc812e342e6 | 132 | if (abs(pitch) < 0.7854){ // pi/4 |
benson516 | 8:3882cb4be9d3 | 133 | roll = atan2(cos(pitch)*vg_in[1],-vg_in[0]); // theta, roll |
benson516 | 7:6fc812e342e6 | 134 | }else{ |
benson516 | 7:6fc812e342e6 | 135 | if (pitch >= 0.0) |
benson516 | 8:3882cb4be9d3 | 136 | roll = atan2(sin(pitch)*vg_in[1],-vg_in[2]); // theta, roll |
benson516 | 7:6fc812e342e6 | 137 | else |
benson516 | 8:3882cb4be9d3 | 138 | roll = atan2(-sin(pitch)*vg_in[1],vg_in[2]); // theta, roll |
benson516 | 7:6fc812e342e6 | 139 | } |
benson516 | 7:6fc812e342e6 | 140 | |
benson516 | 8:3882cb4be9d3 | 141 | // Calculate the yaw angle |
benson516 | 8:3882cb4be9d3 | 142 | if (enable_magEst){ |
benson516 | 8:3882cb4be9d3 | 143 | float num = vm_in[1]*cos(pitch); |
benson516 | 8:3882cb4be9d3 | 144 | float den = (vm_in[2]*cos(roll) - vm_in[1]*sin(pitch)*sin(roll)); |
benson516 | 8:3882cb4be9d3 | 145 | yaw = atan2(num, den); |
benson516 | 8:3882cb4be9d3 | 146 | }else{ |
benson516 | 8:3882cb4be9d3 | 147 | yaw = 0.0; // phi, yaw |
benson516 | 8:3882cb4be9d3 | 148 | } |
benson516 | 7:6fc812e342e6 | 149 | |
benson516 | 7:6fc812e342e6 | 150 | } |
benson516 | 7:6fc812e342e6 | 151 | // Setting parameters |
benson516 | 7:6fc812e342e6 | 152 | // Set L1, the diagonal matrix |
benson516 | 7:6fc812e342e6 | 153 | void ATTITUDE::Set_L1_diag(float alpha_in) // set diagnal element of gain matrix |
benson516 | 7:6fc812e342e6 | 154 | { |
benson516 | 7:6fc812e342e6 | 155 | alpha = alpha_in; |
benson516 | 7:6fc812e342e6 | 156 | L1_diag.assign(n,alpha_in); |
benson516 | 7:6fc812e342e6 | 157 | } |
benson516 | 9:84fad91d3587 | 158 | void ATTITUDE::enable_gyroBiasEst(float gamma_in){ // Enable the gyro-bias estimation |
benson516 | 9:84fad91d3587 | 159 | enable_biasEst = true; |
benson516 | 9:84fad91d3587 | 160 | one_over_gamma = 1/gamma_in; |
benson516 | 9:84fad91d3587 | 161 | } |
benson516 | 7:6fc812e342e6 | 162 | // |
benson516 | 8:3882cb4be9d3 | 163 | void ATTITUDE::Init(void) // Let x_est = ys |
benson516 | 7:6fc812e342e6 | 164 | { |
benson516 | 8:3882cb4be9d3 | 165 | // y_acce = y_in; |
benson516 | 8:3882cb4be9d3 | 166 | // Normolization(xg_est,y_in); // xg_est be set as normalized y_in |
benson516 | 8:3882cb4be9d3 | 167 | xg_est = y_acce; |
benson516 | 8:3882cb4be9d3 | 168 | xm_est = y_mag; |
benson516 | 7:6fc812e342e6 | 169 | ++init_flag; |
benson516 | 7:6fc812e342e6 | 170 | } |
benson516 | 8:3882cb4be9d3 | 171 | void ATTITUDE::iterateOnce(const vector<float> &y_acce_in, const vector<float> &omega_in) // Main alogorithm |
benson516 | 7:6fc812e342e6 | 172 | { |
benson516 | 8:3882cb4be9d3 | 173 | enable_magEst = false; // no magenetometers input |
benson516 | 8:3882cb4be9d3 | 174 | // Input mapping |
benson516 | 8:3882cb4be9d3 | 175 | InputMapping(y_acce, y_acce_in, accMap_real2here); |
benson516 | 8:3882cb4be9d3 | 176 | // InputMapping(y_mag, y_mag_in, magMap_real2here); |
benson516 | 7:6fc812e342e6 | 177 | InputMapping(omega, omega_in, gyroMap_real2here); |
benson516 | 7:6fc812e342e6 | 178 | |
benson516 | 7:6fc812e342e6 | 179 | // Input filter |
benson516 | 8:3882cb4be9d3 | 180 | y_acce = lpfv_y_acce.filter(y_acce); |
benson516 | 8:3882cb4be9d3 | 181 | // y_mag = lpfv_y_mag.filter(y_mag); |
benson516 | 7:6fc812e342e6 | 182 | // omega = lpfv_w.filter(omega); |
benson516 | 7:6fc812e342e6 | 183 | |
benson516 | 7:6fc812e342e6 | 184 | // gyro-bias estimation |
benson516 | 7:6fc812e342e6 | 185 | if (enable_biasEst){ |
benson516 | 8:3882cb4be9d3 | 186 | omega = Get_VectorPlus(omega, gyroBias_est, true); // minus, omega - gyroBias_est |
benson516 | 7:6fc812e342e6 | 187 | } |
benson516 | 7:6fc812e342e6 | 188 | |
benson516 | 7:6fc812e342e6 | 189 | // |
benson516 | 7:6fc812e342e6 | 190 | if(init_flag < 3){ |
benson516 | 8:3882cb4be9d3 | 191 | Init(); |
benson516 | 7:6fc812e342e6 | 192 | } |
benson516 | 7:6fc812e342e6 | 193 | else{ |
benson516 | 8:3882cb4be9d3 | 194 | // Estimation kernel process |
benson516 | 8:3882cb4be9d3 | 195 | EstimationKernel(xg_est, y_acce, omega); |
benson516 | 8:3882cb4be9d3 | 196 | // EstimationKernel(xm_est, y_mag, omega); |
benson516 | 7:6fc812e342e6 | 197 | |
benson516 | 7:6fc812e342e6 | 198 | // gyro-bias estimation |
benson516 | 7:6fc812e342e6 | 199 | if (enable_biasEst){ |
benson516 | 7:6fc812e342e6 | 200 | updateGyroBiasEst(); |
benson516 | 7:6fc812e342e6 | 201 | } |
benson516 | 7:6fc812e342e6 | 202 | } |
benson516 | 7:6fc812e342e6 | 203 | // |
benson516 | 8:3882cb4be9d3 | 204 | Vectors_to_EulerAngle(xg_est,xm_est); |
benson516 | 8:3882cb4be9d3 | 205 | } |
benson516 | 8:3882cb4be9d3 | 206 | void ATTITUDE::iterateOnce(const vector<float> &y_acce_in, const vector<float> &y_mag_in, const vector<float> &omega_in) // Main alogorithm |
benson516 | 8:3882cb4be9d3 | 207 | { |
benson516 | 8:3882cb4be9d3 | 208 | enable_magEst = true; // with magenetometers input |
benson516 | 8:3882cb4be9d3 | 209 | // Input mapping |
benson516 | 8:3882cb4be9d3 | 210 | InputMapping(y_acce, y_acce_in, accMap_real2here); |
benson516 | 8:3882cb4be9d3 | 211 | InputMapping(y_mag, y_mag_in, magMap_real2here); |
benson516 | 8:3882cb4be9d3 | 212 | InputMapping(omega, omega_in, gyroMap_real2here); |
benson516 | 8:3882cb4be9d3 | 213 | |
benson516 | 8:3882cb4be9d3 | 214 | // Input filter |
benson516 | 8:3882cb4be9d3 | 215 | y_acce = lpfv_y_acce.filter(y_acce); |
benson516 | 8:3882cb4be9d3 | 216 | // y_mag = lpfv_y_mag.filter(y_mag); |
benson516 | 8:3882cb4be9d3 | 217 | // omega = lpfv_w.filter(omega); |
benson516 | 8:3882cb4be9d3 | 218 | |
benson516 | 8:3882cb4be9d3 | 219 | // gyro-bias estimation |
benson516 | 8:3882cb4be9d3 | 220 | if (enable_biasEst){ |
benson516 | 8:3882cb4be9d3 | 221 | omega = Get_VectorPlus(omega, gyroBias_est, true); // minus, omega - gyroBias_est |
benson516 | 8:3882cb4be9d3 | 222 | } |
benson516 | 8:3882cb4be9d3 | 223 | |
benson516 | 8:3882cb4be9d3 | 224 | // |
benson516 | 8:3882cb4be9d3 | 225 | if(init_flag < 3){ |
benson516 | 8:3882cb4be9d3 | 226 | Init(); |
benson516 | 8:3882cb4be9d3 | 227 | } |
benson516 | 8:3882cb4be9d3 | 228 | else{ |
benson516 | 8:3882cb4be9d3 | 229 | // Estimation kernel process |
benson516 | 8:3882cb4be9d3 | 230 | EstimationKernel(xg_est, y_acce, omega); |
benson516 | 8:3882cb4be9d3 | 231 | EstimationKernel(xm_est, y_mag, omega); |
benson516 | 8:3882cb4be9d3 | 232 | |
benson516 | 8:3882cb4be9d3 | 233 | // gyro-bias estimation |
benson516 | 8:3882cb4be9d3 | 234 | if (enable_biasEst){ |
benson516 | 8:3882cb4be9d3 | 235 | updateGyroBiasEst(); |
benson516 | 8:3882cb4be9d3 | 236 | } |
benson516 | 8:3882cb4be9d3 | 237 | } |
benson516 | 8:3882cb4be9d3 | 238 | // |
benson516 | 8:3882cb4be9d3 | 239 | Vectors_to_EulerAngle(xg_est,xm_est); |
benson516 | 7:6fc812e342e6 | 240 | } |
benson516 | 7:6fc812e342e6 | 241 | // transform the x_est into "real" coordinate |
benson516 | 7:6fc812e342e6 | 242 | void ATTITUDE::getEstimation_realCoordinate(vector<float> &V_out){ |
benson516 | 8:3882cb4be9d3 | 243 | OutputMapping(V_out,xg_est,accMap_real2here); |
benson516 | 7:6fc812e342e6 | 244 | } |
benson516 | 7:6fc812e342e6 | 245 | // Get Eular angles |
benson516 | 7:6fc812e342e6 | 246 | float ATTITUDE::pitch_deg(void){ |
benson516 | 7:6fc812e342e6 | 247 | return (rad2deg*pitch); |
benson516 | 7:6fc812e342e6 | 248 | } |
benson516 | 7:6fc812e342e6 | 249 | float ATTITUDE::roll_deg(void){ |
benson516 | 7:6fc812e342e6 | 250 | return (rad2deg*roll); |
benson516 | 7:6fc812e342e6 | 251 | } |
benson516 | 7:6fc812e342e6 | 252 | float ATTITUDE::yaw_deg(void){ |
benson516 | 7:6fc812e342e6 | 253 | return (rad2deg*yaw); |
benson516 | 7:6fc812e342e6 | 254 | } |
benson516 | 7:6fc812e342e6 | 255 | |
benson516 | 7:6fc812e342e6 | 256 | // Private methods |
benson516 | 7:6fc812e342e6 | 257 | //////////////////////////////////// |
benson516 | 7:6fc812e342e6 | 258 | // Input/output coordinate transformations within the different definitions between the "real" one and the "here" one |
benson516 | 6:c362ed165c39 | 259 | // real -> here |
benson516 | 6:c362ed165c39 | 260 | void ATTITUDE::InputMapping(vector<float> &v_hereDef, const vector<float> &v_realDef, const vector<int> &map_real2here){ |
benson516 | 6:c362ed165c39 | 261 | // The map from "real" coordinate to "here" coordinate |
benson516 | 6:c362ed165c39 | 262 | // eg. accMap_real2here = [3,-1,-2]; |
benson516 | 6:c362ed165c39 | 263 | // means: real -> here |
benson516 | 6:c362ed165c39 | 264 | // 1 x z 3 |
benson516 | 6:c362ed165c39 | 265 | // 2 y -x -1 |
benson516 | 6:c362ed165c39 | 266 | // 3 z -y -2 |
benson516 | 6:c362ed165c39 | 267 | // vector<int> accMap_real2here = {3,-1,-2}; |
benson516 | 6:c362ed165c39 | 268 | // vector<int> gyroMap_real2here = {3,-1,-2}; |
benson516 | 6:c362ed165c39 | 269 | |
benson516 | 6:c362ed165c39 | 270 | // Iterate through "real" coordinates |
benson516 | 6:c362ed165c39 | 271 | int idx_here = 1; |
benson516 | 6:c362ed165c39 | 272 | for (size_t i = 0; i < n; ++i){ |
benson516 | 6:c362ed165c39 | 273 | idx_here = map_real2here[i]; |
benson516 | 6:c362ed165c39 | 274 | if (idx_here > 0){ |
benson516 | 6:c362ed165c39 | 275 | v_hereDef[idx_here-1] = v_realDef[i]; |
benson516 | 6:c362ed165c39 | 276 | }else{ |
benson516 | 6:c362ed165c39 | 277 | v_hereDef[-idx_here-1] = -1*v_realDef[i]; |
benson516 | 6:c362ed165c39 | 278 | } |
benson516 | 6:c362ed165c39 | 279 | } |
benson516 | 0:8126c86bac2a | 280 | } |
benson516 | 6:c362ed165c39 | 281 | // here -> real |
benson516 | 6:c362ed165c39 | 282 | void ATTITUDE::OutputMapping(vector<float> &v_realDef, const vector<float> &v_hereDef, const vector<int> &map_real2here){ |
benson516 | 6:c362ed165c39 | 283 | // This is the inverse mapping of the InputMapping |
benson516 | 6:c362ed165c39 | 284 | |
benson516 | 6:c362ed165c39 | 285 | // The map from "real" coordinate to "here" coordinate |
benson516 | 6:c362ed165c39 | 286 | // eg. accMap_real2here = [3,-1,-2]; |
benson516 | 6:c362ed165c39 | 287 | // means: real -> here |
benson516 | 6:c362ed165c39 | 288 | // 1 x z 3 |
benson516 | 6:c362ed165c39 | 289 | // 2 y -x -1 |
benson516 | 6:c362ed165c39 | 290 | // 3 z -y -2 |
benson516 | 6:c362ed165c39 | 291 | // vector<int> accMap_real2here = {3,-1,-2}; |
benson516 | 6:c362ed165c39 | 292 | // vector<int> gyroMap_real2here = {3,-1,-2}; |
benson516 | 6:c362ed165c39 | 293 | |
benson516 | 6:c362ed165c39 | 294 | // Iterate through "real" coordinates |
benson516 | 6:c362ed165c39 | 295 | int idx_here = 1; |
benson516 | 6:c362ed165c39 | 296 | for (size_t i = 0; i < n; ++i){ |
benson516 | 6:c362ed165c39 | 297 | idx_here = map_real2here[i]; |
benson516 | 6:c362ed165c39 | 298 | if (idx_here > 0){ |
benson516 | 6:c362ed165c39 | 299 | v_realDef[i] = v_hereDef[idx_here-1]; |
benson516 | 6:c362ed165c39 | 300 | }else{ |
benson516 | 6:c362ed165c39 | 301 | v_realDef[i] = -1*v_hereDef[-idx_here-1]; |
benson516 | 6:c362ed165c39 | 302 | } |
benson516 | 6:c362ed165c39 | 303 | } |
benson516 | 6:c362ed165c39 | 304 | } |
benson516 | 7:6fc812e342e6 | 305 | // The kernel of the estimation process |
benson516 | 7:6fc812e342e6 | 306 | //////////////////////////////////////// |
benson516 | 7:6fc812e342e6 | 307 | void ATTITUDE::EstimationKernel(vector<float> &_x_est_, const vector<float> &_ys_, const vector<float> &_omega_){ |
benson516 | 7:6fc812e342e6 | 308 | static vector<float> _w_cross_ys_; |
benson516 | 7:6fc812e342e6 | 309 | Get_CrossProduct3(_w_cross_ys_, _omega_, _ys_); |
benson516 | 8:3882cb4be9d3 | 310 | for(size_t i = 0; i < n; ++i){ |
benson516 | 7:6fc812e342e6 | 311 | // x_est_plus[i] = x_est[i] + Ts*( L1_diag[i]*(ys[i] - x_est[i]) - w_cross_ys[i]); |
benson516 | 7:6fc812e342e6 | 312 | _x_est_[i] += Ts*( L1_diag[i]*(_ys_[i] - _x_est_[i]) - _w_cross_ys_[i]); |
benson516 | 6:c362ed165c39 | 313 | } |
benson516 | 6:c362ed165c39 | 314 | |
benson516 | 7:6fc812e342e6 | 315 | } |
benson516 | 7:6fc812e342e6 | 316 | void ATTITUDE::updateGyroBiasEst(void){ // Update the gyro bias estimation |
benson516 | 7:6fc812e342e6 | 317 | if (one_over_gamma == 0.0){ |
benson516 | 7:6fc812e342e6 | 318 | return; |
benson516 | 7:6fc812e342e6 | 319 | } |
benson516 | 7:6fc812e342e6 | 320 | // |
benson516 | 8:3882cb4be9d3 | 321 | Get_CrossProduct3(ys_cross_x_ys, y_acce, Get_VectorPlus(xg_est,y_acce,true)); |
benson516 | 7:6fc812e342e6 | 322 | // |
benson516 | 7:6fc812e342e6 | 323 | gyroBias_est = Get_VectorPlus(gyroBias_est, Get_VectorScalarMultiply(ys_cross_x_ys, (one_over_gamma)), true); |
benson516 | 7:6fc812e342e6 | 324 | } |
benson516 | 6:c362ed165c39 | 325 | |
benson516 | 7:6fc812e342e6 | 326 | //////////////////////////////////////// end The kernal of the estimation process |
benson516 | 7:6fc812e342e6 | 327 | |
benson516 | 7:6fc812e342e6 | 328 | // Utilities |
benson516 | 7:6fc812e342e6 | 329 | // vector operation |
benson516 | 6:c362ed165c39 | 330 | void ATTITUDE::Get_CrossProduct3(vector<float> &v_c, const vector<float> &v_a, const vector<float> &v_b) // v_a X v_b |
benson516 | 1:edc7ccfc5562 | 331 | { |
benson516 | 7:6fc812e342e6 | 332 | // Check the size |
benson516 | 7:6fc812e342e6 | 333 | if (v_c.size() != n){ |
benson516 | 7:6fc812e342e6 | 334 | v_c.resize(n); |
benson516 | 7:6fc812e342e6 | 335 | } |
benson516 | 1:edc7ccfc5562 | 336 | v_c[0] = (-v_a[2]*v_b[1]) + v_a[1]*v_b[2]; |
benson516 | 1:edc7ccfc5562 | 337 | v_c[1] = v_a[2]*v_b[0] - v_a[0]*v_b[2]; |
benson516 | 6:c362ed165c39 | 338 | v_c[2] = (-v_a[1]*v_b[0]) + v_a[0]*v_b[1]; |
benson516 | 1:edc7ccfc5562 | 339 | } |
benson516 | 6:c362ed165c39 | 340 | vector<float> ATTITUDE::Get_VectorPlus(const vector<float> &v_a, const vector<float> &v_b, bool is_minus) // v_a + (or -) v_b |
benson516 | 6:c362ed165c39 | 341 | { |
benson516 | 6:c362ed165c39 | 342 | static vector<float> v_c(n); |
benson516 | 6:c362ed165c39 | 343 | for (size_t i = 0; i < n; ++i){ |
benson516 | 6:c362ed165c39 | 344 | if (is_minus){ |
benson516 | 6:c362ed165c39 | 345 | v_c[i] = v_a[i] - v_b[i]; |
benson516 | 6:c362ed165c39 | 346 | }else{ |
benson516 | 6:c362ed165c39 | 347 | v_c[i] = v_a[i] + v_b[i]; |
benson516 | 6:c362ed165c39 | 348 | } |
benson516 | 6:c362ed165c39 | 349 | } |
benson516 | 6:c362ed165c39 | 350 | return v_c; |
benson516 | 6:c362ed165c39 | 351 | } |
benson516 | 6:c362ed165c39 | 352 | vector<float> ATTITUDE::Get_VectorScalarMultiply(const vector<float> &v_a, float scale) // scale*v_a |
benson516 | 6:c362ed165c39 | 353 | { |
benson516 | 6:c362ed165c39 | 354 | static vector<float> v_c(n); |
benson516 | 6:c362ed165c39 | 355 | for (size_t i = 0; i < n; ++i){ |
benson516 | 6:c362ed165c39 | 356 | v_c[i] = scale*v_a[i]; |
benson516 | 6:c362ed165c39 | 357 | |
benson516 | 6:c362ed165c39 | 358 | } |
benson516 | 6:c362ed165c39 | 359 | return v_c; |
benson516 | 6:c362ed165c39 | 360 | } |
benson516 | 6:c362ed165c39 | 361 | float ATTITUDE::Get_Vector3Norm(const vector<float> &v_in) |
benson516 | 1:edc7ccfc5562 | 362 | { |
benson516 | 1:edc7ccfc5562 | 363 | float temp = 0.0; |
benson516 | 6:c362ed165c39 | 364 | |
benson516 | 6:c362ed165c39 | 365 | for (size_t i = 0; i < n; ++i) |
benson516 | 1:edc7ccfc5562 | 366 | temp += v_in[i]*v_in[i]; |
benson516 | 6:c362ed165c39 | 367 | return sqrt(temp); // <- Should check if this function is available (?) |
benson516 | 1:edc7ccfc5562 | 368 | } |
benson516 | 6:c362ed165c39 | 369 | void ATTITUDE::Normolization(vector<float> &V_out, const vector<float> &V_in){ |
benson516 | 6:c362ed165c39 | 370 | float norm = Get_Vector3Norm(V_in); |
benson516 | 6:c362ed165c39 | 371 | for (size_t i = 0; i < n; ++i){ |
benson516 | 6:c362ed165c39 | 372 | V_out[i] = V_in[i]/norm; |
benson516 | 6:c362ed165c39 | 373 | } |
benson516 | 6:c362ed165c39 | 374 | } |