33 void CpiV2::feed_IMU(
double t_0,
double t_1, Eigen::Matrix<double, 3, 1> w_m_0, Eigen::Matrix<double, 3, 1> a_m_0,
34 Eigen::Matrix<double, 3, 1> w_m_1, Eigen::Matrix<double, 3, 1> a_m_1) {
37 double delta_t = t_1 - t_0;
46 Eigen::Matrix<double, 3, 1> w_hat = w_m_0 -
b_w_lin;
57 Eigen::Matrix<double, 3, 1> w_hatdt = w_hat * delta_t;
60 double w_1 = w_hat(0, 0);
61 double w_2 = w_hat(1, 0);
62 double w_3 = w_hat(2, 0);
65 double mag_w = w_hat.norm();
66 double w_dt = mag_w * delta_t;
69 bool small_w = (mag_w < 0.008726646);
72 double dt_2 = pow(delta_t, 2);
73 double cos_wt = cos(w_dt);
74 double sin_wt = sin(w_dt);
76 Eigen::Matrix<double, 3, 3> w_x =
skew_x(w_hat);
77 Eigen::Matrix<double, 3, 3> w_tx =
skew_x(w_hatdt);
78 Eigen::Matrix<double, 3, 3> w_x_2 = w_x * w_x;
85 Eigen::Matrix<double, 3, 3> R_tau2tau1 = small_w ?
eye3 - delta_t * w_x + (pow(delta_t, 2) / 2) * w_x_2
86 :
eye3 - (sin_wt / mag_w) * w_x + ((1.0 - cos_wt) / (pow(mag_w, 2.0))) * w_x_2;
89 Eigen::Matrix<double, 3, 3> R_k2tau1 = R_tau2tau1 *
R_k2tau;
90 Eigen::Matrix<double, 3, 3> R_tau12k = R_k2tau1.transpose();
97 Eigen::Matrix<double, 3, 3> a_x =
skew_x(a_hat);
106 f_1 = -(pow(delta_t, 3) / 3);
107 f_2 = (pow(delta_t, 4) / 8);
108 f_3 = -(pow(delta_t, 2) / 2);
109 f_4 = (pow(delta_t, 3) / 6);
111 f_1 = (w_dt * cos_wt - sin_wt) / (pow(mag_w, 3));
112 f_2 = (pow(w_dt, 2) - 2 * cos_wt - 2 * w_dt * sin_wt + 2) / (2 * pow(mag_w, 4));
113 f_3 = -(1 - cos_wt) / pow(mag_w, 2);
114 f_4 = (w_dt - sin_wt) / pow(mag_w, 3);
118 Eigen::Matrix<double, 3, 3> alpha_arg = ((dt_2 / 2.0) *
eye3 + f_1 * w_x + f_2 * w_x_2);
119 Eigen::Matrix<double, 3, 3> Beta_arg = (delta_t *
eye3 + f_3 * w_x + f_4 * w_x_2);
122 Eigen::Matrix<double, 3, 3> H_al = R_tau12k * alpha_arg;
123 Eigen::Matrix<double, 3, 3> H_be = R_tau12k * Beta_arg;
134 Eigen::Matrix<double, 3, 3> J_r_tau1 =
135 small_w ?
eye3 - .5 * w_tx + (1.0 / 6.0) * w_tx * w_tx
136 :
eye3 - ((1 - cos_wt) / (pow((w_dt), 2.0))) * w_tx + ((w_dt - sin_wt) / (pow(w_dt, 3.0))) * w_tx * w_tx;
139 Eigen::Matrix<double, 3, 3> J_save =
J_q;
140 J_q = R_tau2tau1 *
J_q + J_r_tau1 * delta_t;
154 Eigen::MatrixXd d_R_bw_1 = -R_tau12k *
skew_x(
J_q *
e_1);
155 Eigen::MatrixXd d_R_bw_2 = -R_tau12k *
skew_x(
J_q *
e_2);
156 Eigen::MatrixXd d_R_bw_3 = -R_tau12k *
skew_x(
J_q *
e_3);
176 double df_1_dw_mag = -(pow(delta_t, 5) / 15);
177 df_1_dbw_1 = w_1 * df_1_dw_mag;
178 df_1_dbw_2 = w_2 * df_1_dw_mag;
179 df_1_dbw_3 = w_3 * df_1_dw_mag;
181 double df_2_dw_mag = (pow(delta_t, 6) / 72);
182 df_2_dbw_1 = w_1 * df_2_dw_mag;
183 df_2_dbw_2 = w_2 * df_2_dw_mag;
184 df_2_dbw_3 = w_3 * df_2_dw_mag;
186 double df_3_dw_mag = -(pow(delta_t, 4) / 12);
187 df_3_dbw_1 = w_1 * df_3_dw_mag;
188 df_3_dbw_2 = w_2 * df_3_dw_mag;
189 df_3_dbw_3 = w_3 * df_3_dw_mag;
191 double df_4_dw_mag = (pow(delta_t, 5) / 60);
192 df_4_dbw_1 = w_1 * df_4_dw_mag;
193 df_4_dbw_2 = w_2 * df_4_dw_mag;
194 df_4_dbw_3 = w_3 * df_4_dw_mag;
196 double df_1_dw_mag = (pow(w_dt, 2) * sin_wt - 3 * sin_wt + 3 * w_dt * cos_wt) / pow(mag_w, 5);
197 df_1_dbw_1 = w_1 * df_1_dw_mag;
198 df_1_dbw_2 = w_2 * df_1_dw_mag;
199 df_1_dbw_3 = w_3 * df_1_dw_mag;
201 double df_2_dw_mag = (pow(w_dt, 2) - 4 * cos_wt - 4 * w_dt * sin_wt + pow(w_dt, 2) * cos_wt + 4) / (pow(mag_w, 6));
202 df_2_dbw_1 = w_1 * df_2_dw_mag;
203 df_2_dbw_2 = w_2 * df_2_dw_mag;
204 df_2_dbw_3 = w_3 * df_2_dw_mag;
206 double df_3_dw_mag = (2 * (cos_wt - 1) + w_dt * sin_wt) / (pow(mag_w, 4));
207 df_3_dbw_1 = w_1 * df_3_dw_mag;
208 df_3_dbw_2 = w_2 * df_3_dw_mag;
209 df_3_dbw_3 = w_3 * df_3_dw_mag;
211 double df_4_dw_mag = (2 * w_dt + w_dt * cos_wt - 3 * sin_wt) / (pow(mag_w, 5));
212 df_4_dbw_1 = w_1 * df_4_dw_mag;
213 df_4_dbw_2 = w_2 * df_4_dw_mag;
214 df_4_dbw_3 = w_3 * df_4_dw_mag;
222 J_a.block(0, 0, 3, 1) +=
223 (d_R_bw_1 * alpha_arg + R_tau12k * (df_1_dbw_1 * w_x - f_1 *
e_1x + df_2_dbw_1 * w_x_2 - f_2 * (
e_1x * w_x + w_x *
e_1x))) * a_hat -
225 J_a.block(0, 1, 3, 1) +=
226 (d_R_bw_2 * alpha_arg + R_tau12k * (df_1_dbw_2 * w_x - f_1 *
e_2x + df_2_dbw_2 * w_x_2 - f_2 * (
e_2x * w_x + w_x *
e_2x))) * a_hat -
228 J_a.block(0, 2, 3, 1) +=
229 (d_R_bw_3 * alpha_arg + R_tau12k * (df_1_dbw_3 * w_x - f_1 *
e_3x + df_2_dbw_3 * w_x_2 - f_2 * (
e_3x * w_x + w_x *
e_3x))) * a_hat -
231 J_b.block(0, 0, 3, 1) +=
232 (d_R_bw_1 * Beta_arg + R_tau12k * (df_3_dbw_1 * w_x - f_3 *
e_1x + df_4_dbw_1 * w_x_2 - f_4 * (
e_1x * w_x + w_x *
e_1x))) * a_hat -
234 J_b.block(0, 1, 3, 1) +=
235 (d_R_bw_2 * Beta_arg + R_tau12k * (df_3_dbw_2 * w_x - f_3 *
e_2x + df_4_dbw_2 * w_x_2 - f_4 * (
e_2x * w_x + w_x *
e_2x))) * a_hat -
237 J_b.block(0, 2, 3, 1) +=
238 (d_R_bw_3 * Beta_arg + R_tau12k * (df_3_dbw_3 * w_x - f_3 *
e_3x + df_4_dbw_3 * w_x_2 - f_4 * (
e_3x * w_x + w_x *
e_3x))) * a_hat -
247 double dt_mid = delta_t / 2.0;
248 double w_dt_mid = mag_w * dt_mid;
249 Eigen::Matrix<double, 3, 3> R_mid;
252 R_mid = small_w ?
eye3 - dt_mid * w_x + (pow(dt_mid, 2) / 2) * w_x_2
253 :
eye3 - (sin(w_dt_mid) / mag_w) * w_x + ((1.0 - cos(w_dt_mid)) / (pow(mag_w, 2.0))) * w_x_2;
260 Eigen::Matrix<double, 21, 21> F_k1 = Eigen::Matrix<double, 21, 21>::Zero();
261 F_k1.block(0, 0, 3, 3) = -w_x;
262 F_k1.block(0, 3, 3, 3) = -
eye3;
263 F_k1.block(6, 0, 3, 3) = -
R_k2tau.transpose() * a_x;
264 F_k1.block(6, 9, 3, 3) = -
R_k2tau.transpose();
267 F_k1.block(12, 6, 3, 3) =
eye3;
270 Eigen::Matrix<double, 21, 12> G_k1 = Eigen::Matrix<double, 21, 12>::Zero();
271 G_k1.block(0, 0, 3, 3) = -
eye3;
272 G_k1.block(3, 3, 3, 3) =
eye3;
273 G_k1.block(6, 6, 3, 3) = -
R_k2tau.transpose();
274 G_k1.block(9, 9, 3, 3) =
eye3;
277 Eigen::Matrix<double, 21, 21> Phi_dot_k1 = F_k1;
278 Eigen::Matrix<double, 21, 21> P_dot_k1 = F_k1 *
P_big +
P_big * F_k1.transpose() + G_k1 *
Q_c * G_k1.transpose();
283 Eigen::Matrix<double, 21, 21> F_k2 = Eigen::Matrix<double, 21, 21>::Zero();
284 F_k2.block(0, 0, 3, 3) = -w_x;
285 F_k2.block(0, 3, 3, 3) = -
eye3;
286 F_k2.block(6, 0, 3, 3) = -R_mid.transpose() * a_x;
287 F_k2.block(6, 9, 3, 3) = -R_mid.transpose();
288 F_k2.block(6, 15, 3, 3) = -R_mid.transpose() *
skew_x(
R_k2tau * R_G_to_k *
grav);
289 F_k2.block(6, 18, 3, 3) = -R_mid.transpose() *
R_k2tau *
skew_x(R_G_to_k *
grav);
290 F_k2.block(12, 6, 3, 3) =
eye3;
293 Eigen::Matrix<double, 21, 12> G_k2 = Eigen::Matrix<double, 21, 12>::Zero();
294 G_k2.block(0, 0, 3, 3) = -
eye3;
295 G_k2.block(3, 3, 3, 3) =
eye3;
296 G_k2.block(6, 6, 3, 3) = -R_mid.transpose();
297 G_k2.block(9, 9, 3, 3) =
eye3;
300 Eigen::Matrix<double, 21, 21> Phi_k2 = Eigen::Matrix<double, 21, 21>::Identity() + Phi_dot_k1 * dt_mid;
301 Eigen::Matrix<double, 21, 21> P_k2 =
P_big + P_dot_k1 * dt_mid;
302 Eigen::Matrix<double, 21, 21> Phi_dot_k2 = F_k2 * Phi_k2;
303 Eigen::Matrix<double, 21, 21> P_dot_k2 = F_k2 * P_k2 + P_k2 * F_k2.transpose() + G_k2 *
Q_c * G_k2.transpose();
309 Eigen::Matrix<double, 21, 21> F_k3 = F_k2;
310 Eigen::Matrix<double, 21, 12> G_k3 = G_k2;
313 Eigen::Matrix<double, 21, 21> Phi_k3 = Eigen::Matrix<double, 21, 21>::Identity() + Phi_dot_k2 * dt_mid;
314 Eigen::Matrix<double, 21, 21> P_k3 =
P_big + P_dot_k2 * dt_mid;
315 Eigen::Matrix<double, 21, 21> Phi_dot_k3 = F_k3 * Phi_k3;
316 Eigen::Matrix<double, 21, 21> P_dot_k3 = F_k3 * P_k3 + P_k3 * F_k3.transpose() + G_k3 *
Q_c * G_k3.transpose();
321 Eigen::Matrix<double, 21, 21> F_k4 = Eigen::Matrix<double, 21, 21>::Zero();
322 F_k4.block(0, 0, 3, 3) = -w_x;
323 F_k4.block(0, 3, 3, 3) = -
eye3;
324 F_k4.block(6, 0, 3, 3) = -R_k2tau1.transpose() * a_x;
325 F_k4.block(6, 9, 3, 3) = -R_k2tau1.transpose();
326 F_k4.block(6, 15, 3, 3) = -R_k2tau1.transpose() *
skew_x(
R_k2tau * R_G_to_k *
grav);
327 F_k4.block(6, 18, 3, 3) = -R_k2tau1.transpose() *
R_k2tau *
skew_x(R_G_to_k *
grav);
328 F_k4.block(12, 6, 3, 3) =
eye3;
331 Eigen::Matrix<double, 21, 12> G_k4 = Eigen::Matrix<double, 21, 12>::Zero();
332 G_k4.block(0, 0, 3, 3) = -
eye3;
333 G_k4.block(3, 3, 3, 3) =
eye3;
334 G_k4.block(6, 6, 3, 3) = -R_k2tau1.transpose();
335 G_k4.block(9, 9, 3, 3) =
eye3;
338 Eigen::Matrix<double, 21, 21> Phi_k4 = Eigen::Matrix<double, 21, 21>::Identity() + Phi_dot_k3 * delta_t;
339 Eigen::Matrix<double, 21, 21> P_k4 =
P_big + P_dot_k3 * delta_t;
340 Eigen::Matrix<double, 21, 21> Phi_dot_k4 = F_k4 * Phi_k4;
341 Eigen::Matrix<double, 21, 21> P_dot_k4 = F_k4 * P_k4 + P_k4 * F_k4.transpose() + G_k4 *
Q_c * G_k4.transpose();
347 P_big += (delta_t / 6.0) * (P_dot_k1 + 2.0 * P_dot_k2 + 2.0 * P_dot_k3 + P_dot_k4);
351 Eigen::Matrix<double, 21, 21> Phi =
352 Eigen::Matrix<double, 21, 21>::Identity() + (delta_t / 6.0) * (Phi_dot_k1 + 2.0 * Phi_dot_k2 + 2.0 * Phi_dot_k3 + Phi_dot_k4);
359 Eigen::Matrix<double, 21, 21> B_k = Eigen::Matrix<double, 21, 21>::Identity();
360 B_k.block(15, 15, 3, 3).setZero();
361 B_k.block(15, 0, 3, 3) = Eigen::Matrix<double, 3, 3>::Identity();