clear
clc
close all
load('data1.mat');
nn = size(imuPosX,1);
%% lightHouse坐标系转换
x =  lightHousePosX * 100;
y = -lightHousePosZ * 100;

% 定义误差函数，即均方根误差
errorFunction = @(params) sqrt(mean((x(1:500) * cos(params(1)) - y(1:500) * sin(params(1)) + params(2) - imuPosX(1:500)).^2 + (x(1:500) * sin(params(1)) + y(1:500) * cos(params(1)) + params(3) - imuPosY(1:500)).^2));

% 使用 fminsearch 优化误差函数，找到使误差最小的旋转角和位移
initialGuess = [0, 10, 10]; % 初始猜测值，[旋转角, 位移]
optimizedParams = fminsearch(errorFunction, initialGuess);

% 输出优化后的旋转角和位移
rotationAngle = optimizedParams(1);
xOffset = optimizedParams(2);
yOffset = optimizedParams(3);
% 旋转坐标
xt = x * cos(rotationAngle) - y * sin(rotationAngle) + xOffset;
yt = x * sin(rotationAngle) + y * cos(rotationAngle) + yOffset;
%%
tagN = 4;
%标签坐标
XN(:,1)=[-300;-300];
XN(:,2)=[-300;300];
XN(:,3)=[300;300];
XN(:,4)=[300;-300];
sim2=6;
Q=diag(repmat(sim2,1,2*tagN));%协方差矩阵
measure_AOA = zeros(4,nn);
measure_d = zeros(4,nn);
measure_AOA(1,:) = aoa1';
measure_AOA(2,:) = aoa2';
measure_AOA(3,:) = aoa3';
measure_AOA(4,:) = aoa4';
measure_d(1,:) = d1';
measure_d(2,:) = d2';
measure_d(3,:) = d3';
measure_d(4,:) = d4';
%% uwb解算
x_uwb(1) = 0;
y_uwb(1) = 0;
theta_uwb=zeros(nn,1);
for i=2:nn
    if measure_d(1,i) == 0
        x_uwb(i) = x_uwb(i-1);
        y_uwb(i) = y_uwb(i-1);        
        continue;
    end
    [t1,theta] = WLS(XN,measure_AOA(:,i),measure_d(:,i),Q);
    x_uwb(i) = t1(1);
    y_uwb(i) = t1(2);
    theta_uwb(i) = 90-theta;
    theta_uwb(i) = mod(theta_uwb(i)+180,360)-180;
    derr_WLS(i)=norm(t1-[xt(i);yt(i)]);
end
thetat=zeros(nn,1);
for i=2:nn
detx=xt(i)-xt(i-1);
dety=yt(i)-yt(i-1);
thetat(i)=atan2d(detx,dety);
end
% Uwb.x=x_uwb;
% Uwb.y=y_uwb;
% Uwb.alpha=theta_uwb;


% 角速度校准
detW = mean(wZ(1:200));
wZ = wZ - detW;

x_imu(1) = 0; 
y_imu(1) = 0;
Z=zeros(3,1);
WW = 0.05;
theta_imu(1) = 0;


% 
% Imu.x=x_imu;
% Imu.y=y_imu;
% Imu.v=v_imu;
% Imu.alpha=alpha_imu;
% Imu.omega=omega_imu;

%%  KF
R = diag([1 1 1]);
% qq = 1;
qq = 0.005;
Q1 = diag([qq qq qq qq qq]);
P0 = diag([0 0 0 0 0]);
H = [1 0 0 0 0;
    0 1 0 0 0;
    0 0 0 1 0];
I = eye(5);
JF = zeros(5,5);
X_pre = zeros(5,nn);
X_kf(:,1) = [imuPosX(1);imuPosY(1);vXY(1)*100;0;wZ(1)];
for i=2:nn
    % 计算IMU和里程计的时间差值
    detImuTime = imuDataRxTime(i) - imuDataRxTime(i-1);
    detOdomTime = odomDataRxTime(i)-odomDataRxTime(i-1);
    % 获得此时Z轴角速度
    w = wZ(i);
    % 获得小车的水平速度
    v = vXY(i)*100;
    % 计算速度增量
    detV=(vXY(i)-vXY(i-1))*100;
    % 计算位置增量
    detD = v*detImuTime; 
    % 计算角度增量
    detTheta = w*180/pi*detImuTime;
    % 计算角速度的变化量
    detW = w-wZ(i-1);
    % 积分计算IMU的航位角
    theta_imu(i) = theta_imu(i-1)+detTheta;
    % 航向约束
    theta_imu(i) = mod(theta_imu(i)+180,360)-180;
    
    % 状态更新方程
    F = [1 0 detImuTime*cosd(X_kf(4,i-1)) 0 0;
         0 1 detImuTime*sind(X_kf(4,i-1)) 0 0;
         0 0 0 0 0;
         0 0 0 0 0;
         0 0 0 0 0;];
    % 获得水平和垂直方向的位置增量
    vtx = detD*cosd(theta_imu(i));
    vty = detD*sind(theta_imu(i));

    x_imu(i) = x_imu(i-1)+vtx; 
    y_imu(i) = y_imu(i-1)+vty;

    X_next = [vtx;vty;detV;detTheta;detW];
    if (x_uwb(i) == x_uwb(i-1))&&(y_uwb(i) == y_uwb(i-1))
        X_kf(:,i) = X_kf(:,i-1)+X_next;
        errKf(i) = norm(X_kf(1:2,i)-[xt(i);yt(i)]);
        theta_kf(i) = X_kf(4,i);
        continue
    end    
    X_pre(:,i) = X_kf(:,i-1)+X_next;
    % 观测矩阵: UWB的位置、UWB的航向角
    Z = [x_uwb(i);y_uwb(i);theta_uwb(i)];
    P = F*P0*F'+Q1;
    Kg_kf = P*H'*inv(H*P*H'+R);
    X_kf(:,i) = X_pre(:,i)+Kg_kf*(Z-H*X_pre(:,i));
    P0 = (I-Kg_kf*H)*P;

    errKf(i) = norm(X_kf(1:2,i)-[xt(i);yt(i)]);
    theta_kf(i) = X_kf(4,i);
end
plot(errKf);
mean(errKf)