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