I'm trying to determine the attitude of a rigid body in R3 fusing the data coming from an accelerometer, a gyroscope and a magnetometer.
I've taken the UKF algorithm as written in the book "Optimal State Estimation: Kalman, H∞, and Nonlinear Approaches" and used some manipulation in order to work with quaternions. The goal is to estimate the components of a quaternion rotating the measurements taken in an inertial frame (eg: N.E.D.) in the sensor frame. I choose to use the gyroscope measurements as input and a vector composed by the acceleration and magnetometer measurements as output. The idea is to use the rotated measurements to compute the estimate of the output and then to compute the innovation vector. The rotated measurements are obtained using the sigma points to deviate the quaternion and then each quaternion (corresponding to a sigma point) is used to rotate the vector in the N.E.D. frame in a vector in the sensor frame.
x_hat_k_i = x_k_priori + x_tilde_priori; %% Previous state + sigma points
q_out = quaternion(x_hat_k_i'); % q_k|k
acc_body = rotatepoint(q_out, [0 0 1]*g); %accel
mag_body = rotatepoint(q_out, href); %magnetometer
y_hat_k_i = [acc_body, ...
mag_body ]' ; % " sigma-outputs"
%% Estimated output:
y_hat_k = sum(y_hat_k_i, 2)/(2*nx);
%% Output - Input/Output cov:
for i = 1:2*nx
x_hat_k_i(:, i) = x_hat_k_i(:, i)/norm(x_hat_k_i(:, i));
Cov_uscita(:, :, i) = (y_hat_k_i(:,i) - y_hat_k)*(y_hat_k_i(:,i) - y_hat_k)';
Cov_xy(:, :, i) = (x_hat_k_i(:,i) - x_k_priori)*(y_hat_k_i(:,i) - y_hat_k)';
end
P_y = sum(Cov_uscita, 3)/(2*nx) + V_out;
P_xy = sum(Cov_xy, 3)/(2*nx);
It looks like that this is not a good approach. It mimics the dynamics of the sensors, but it doesn't follow it. Any suggestion?
Thanks in advance
P.S. This approach seems to be okay, I've tried changing the weight of the measurement in the uncertainty matrix and the algorithm works. I've also tried to implement the (more) general formulation defined in: "The Unscented Kalman Filter for Nonlinear Estimation, Eric A. Wan and Rudolph van der Menve" and it works slightly better.