RunRealData.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. %% Set initial conditions
  2. clear all;
  3. load('fltTest.mat');
  4. startDelayTime = 100; % number of seconds to delay filter start (used to simulate in-flight restart)
  5. dt = 1/50;
  6. startTime = 0.001*(IMU(1,2));
  7. stopTime = 0.001*(IMU(length(IMU),2));
  8. indexLimit = length(IMU);
  9. magIndexlimit = length(MAG);
  10. statesLog = zeros(11,indexLimit);
  11. eulLog = zeros(4,indexLimit);
  12. velInnovLog = zeros(4,indexLimit);
  13. angErrLog = velInnovLog;
  14. decInnovLog = zeros(2,magIndexlimit);
  15. velInnovVarLog = velInnovLog;
  16. decInnovVarLog = decInnovLog;
  17. % initialise the filter to level
  18. quat = [1;0;0;0];
  19. states = zeros(10,1);
  20. Tbn = Quat2Tbn(quat);
  21. % Set the expected declination
  22. measDec = 0.18;
  23. % define the state covariances with the exception of the quaternion covariances
  24. Sigma_velNED = 0.5; % 1 sigma uncertainty in horizontal velocity components
  25. Sigma_dAngBias = 5*pi/180*dt; % 1 Sigma uncertainty in delta angle bias
  26. Sigma_quatErr = 1; % 1 Sigma uncertainty in angular misalignment (rad)
  27. covariance = single(diag([Sigma_quatErr*[1;1;1;1];Sigma_velNED*[1;1;1];Sigma_dAngBias*[1;1;1]].^2));
  28. %% Main Loop
  29. magIndex = 1;
  30. time = 0;
  31. tiltError = 0;
  32. headingAligned = 0;
  33. angErrVec = [0;0;0];
  34. startIndex = max(11,ceil(startDelayTime/dt));
  35. for index = startIndex:indexLimit
  36. time=time+dt + startIndex*dt;
  37. % read IMU measurements and correct rates using estimated bias
  38. angRate = IMU(index,3:5)' - states(7:9)./dt;
  39. accel = IMU(index,6:8)';
  40. % predict states
  41. [quat, states, Tbn, delAng, delVel] = PredictStates(quat,states,angRate,accel,dt);
  42. statesLog(1,index) = time;
  43. statesLog(2:11,index) = states;
  44. eulLog(1,index) = time;
  45. eulLog(2:4,index) = QuatToEul(quat);
  46. % predict covariance matrix
  47. covariance = PredictCovariance(delAng,delVel,quat,states,covariance,dt);
  48. % read magnetometer measurements
  49. while ((MAG(magIndex,1) < IMU(index,1)) && (magIndex < magIndexlimit))
  50. magIndex = magIndex + 1;
  51. % fuse magnetometer measurements if new data available and when tilt has settled
  52. if ((MAG(magIndex,1) >= IMU(index,1)) && ((angErrVec(1)^2 + angErrVec(2)^2) < 0.05^2) && (index > 50))
  53. magBody = 0.001*MAG(magIndex,3:5)';
  54. [states,covariance,decInnov,decInnovVar] = FuseMagnetometer(states,covariance,magBody,measDec,Tbn);
  55. decInnovLog(1,magIndex) = time;
  56. decInnovLog(2,magIndex) = decInnov;
  57. decInnovVarLog(1,magIndex) = time;
  58. decInnovVarLog(2,magIndex) = decInnovVar;
  59. end
  60. end
  61. % fuse velocity measurements - use synthetic measurements
  62. measVel = [0;0;0];
  63. [states,covariance,velInnov,velInnovVar] = FuseVelocity(states,covariance,measVel);
  64. velInnovLog(1,index) = time;
  65. velInnovLog(2:4,index) = velInnov;
  66. velInnovVarLog(1,index) = time;
  67. velInnovVarLog(2:4,index) = velInnovVar;
  68. end
  69. %% Generate Plots
  70. PlotData;