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