imu and gps fusion for inertial navigation -凯发k8网页登录
this example shows how you might build an imu gps fusion algorithm suitable for unmanned aerial vehicles (uavs) or quadcopters.
this example uses accelerometers, gyroscopes, magnetometers, and gps to determine orientation and position of a uav.
simulation setup
set the sampling rates. in a typical system, the accelerometer and gyroscope run at relatively high sample rates. the complexity of processing data from those sensors in the fusion algorithm is relatively low. conversely, the gps, and in some cases the magnetometer, run at relatively low sample rates, and the complexity associated with processing them is high. in this fusion algorithm, the magnetometer and gps samples are processed together at the same low rate, and the accelerometer and gyroscope samples are processed together at the same high rate.
to simulate this configuration, the imu (accelerometer, gyroscope, and magnetometer) are sampled at 160 hz, and the gps is sampled at 1 hz. only one out of every 160 samples of the magnetometer is given to the fusion algorithm, so in a real system the magnetometer could be sampled at a much lower rate.
imufs = 160; gpsfs = 1; % define where on the earth this simulated scenario takes place using the % latitude, longitude and altitude. refloc = [42.2825 -72.3430 53.0352]; % validate that the |gpsfs| divides |imufs|. this allows the sensor sample % rates to be simulated using a nested for loop without complex sample rate % matching. imusamplespergps = (imufs/gpsfs); assert(imusamplespergps == fix(imusamplespergps), ... 'gps sampling rate must be an integer factor of imu sampling rate.');
fusion filter
create the filter to fuse imu gps measurements. the fusion filter uses an extended kalman filter to track orientation (as a quaternion), velocity, position, sensor biases, and the geomagnetic vector.
this insfiltermarg
has a few methods to process sensor data, including predict
, fusemag
and fusegps
. the predict
method takes the accelerometer and gyroscope samples from the imu as inputs. call the predict
method each time the accelerometer and gyroscope are sampled. this method predicts the states one time step ahead based on the accelerometer and gyroscope. the error covariance of the extended kalman filter is updated here.
the fusegps
method takes gps samples as input. this method updates the filter states based on gps samples by computing a kalman gain that weights the various sensor inputs according to their uncertainty. an error covariance is also updated here, this time using the kalman gain as well.
the fusemag
method is similar but updates the states, kalman gain, and error covariance based on the magnetometer samples.
though the insfiltermarg
takes accelerometer and gyroscope samples as inputs, these are integrated to compute delta velocities and delta angles, respectively. the filter tracks the bias of the magnetometer and these integrated signals.
fusionfilt = insfiltermarg; fusionfilt.imusamplerate = imufs; fusionfilt.referencelocation = refloc;
uav trajectory
this example uses a saved trajectory recorded from a uav as the ground truth. this trajectory is fed to several sensor simulators to compute simulated accelerometer, gyroscope, magnetometer, and gps data streams.
% load the "ground truth" uav trajectory. load loggedquadcopter.mat trajdata; trajorient = trajdata.orientation; trajvel = trajdata.velocity; trajpos = trajdata.position; trajacc = trajdata.acceleration; trajangvel = trajdata.angularvelocity; % initialize the random number generator used in the simulation of sensor % noise. rng(1)
gps sensor
set up the gps at the specified sample rate and reference location. the other parameters control the nature of the noise in the output signal.
gps = gpssensor('updaterate', gpsfs); gps.referencelocation = refloc; gps.decayfactor = 0.5; % random walk noise parameter gps.horizontalpositionaccuracy = 1.6; gps.verticalpositionaccuracy = 1.6; gps.velocityaccuracy = 0.1;
imu sensors
typically, a uav uses an integrated marg sensor (magnetic, angular rate, gravity) for pose estimation. to model a marg sensor, define an imu sensor model containing an accelerometer, gyroscope, and magnetometer. in a real-world application the three sensors could come from a single integrated circuit or separate ones. the property values set here are typical for low-cost mems sensors.
imu = imusensor('accel-gyro-mag', 'samplerate', imufs); imu.magneticfield = [19.5281 -5.0741 48.0067]; % accelerometer imu.accelerometer.measurementrange = 19.6133; imu.accelerometer.resolution = 0.0023928; imu.accelerometer.constantbias = 0.19; imu.accelerometer.noisedensity = 0.0012356; % gyroscope imu.gyroscope.measurementrange = deg2rad(250); imu.gyroscope.resolution = deg2rad(0.0625); imu.gyroscope.constantbias = deg2rad(3.125); imu.gyroscope.axesmisalignment = 1.5; imu.gyroscope.noisedensity = deg2rad(0.025); % magnetometer imu.magnetometer.measurementrange = 1000; imu.magnetometer.resolution = 0.1; imu.magnetometer.constantbias = 100; imu.magnetometer.noisedensity = 0.3/ sqrt(50);
initialize the state vector of the insfiltermarg
the insfiltermarg
tracks the pose states in a 22-element vector. the states are:
state units state vector index orientation as a quaternion 1:4 position (ned) m 5:7 velocity (ned) m/s 8:10 delta angle bias (xyz) rad 11:13 delta velocity bias (xyz) m/s 14:16 geomagnetic field vector (ned) ut 17:19 magnetometer bias (xyz) ut 20:22
ground truth is used to help initialize the filter states, so the filter converges to good answers quickly.
% initialize the states of the filter
initstate = zeros(22,1);
initstate(1:4) = compact( meanrot(trajorient(1:100)));
initstate(5:7) = mean( trajpos(1:100,:), 1);
initstate(8:10) = mean( trajvel(1:100,:), 1);
initstate(11:13) = imu.gyroscope.constantbias./imufs;
initstate(14:16) = imu.accelerometer.constantbias./imufs;
initstate(17:19) = imu.magneticfield;
initstate(20:22) = imu.magnetometer.constantbias;
fusionfilt.state = initstate;
initialize the variances of the insfiltermarg
the insfiltermarg
measurement noises describe how much noise is corrupting the sensor reading. these values are based on the imusensor
and gpssensor
parameters.
the process noises describe how well the filter equations describe the state evolution. process noises are determined empirically using parameter sweeping to jointly optimize position and orientation estimates from the filter.
% measurement noises rmag = 0.0862; % magnetometer measurement noise rvel = 0.0051; % gps velocity measurement noise rpos = 5.169; % gps position measurement noise % process noises fusionfilt.accelerometerbiasnoise = 0.010716; fusionfilt.accelerometernoise = 9.7785; fusionfilt.gyroscopebiasnoise = 1.3436e-14; fusionfilt.gyroscopenoise = 0.00016528; fusionfilt.magnetometerbiasnoise = 2.189e-11; fusionfilt.geomagneticvectornoise = 7.67e-13; % initial error covariance fusionfilt.statecovariance = 1e-9*eye(22);
initialize scopes
the helperscrollingplotter
scope enables plotting of variables over time. it is used here to track errors in pose. the helperposeviewer
scope allows 3-d visualization of the filter estimate and ground truth pose. the scopes can slow the simulation. to disable a scope, set the corresponding logical variable to false
.
useerrscope = true; % turn on the streaming error plot useposeview = true; % turn on the 3-d pose viewer if useerrscope errscope = helperscrollingplotter(... 'numinputs', 4, ... 'timespan', 10, ... 'samplerate', imufs, ... 'ylabel', {'degrees', ... 'meters', ... 'meters', ... 'meters'}, ... 'title', {'quaternion distance', ... 'position x error', ... 'position y error', ... 'position z error'}, ... 'ylimits', ... [ -3, 3 -2, 2 -2 2 -2 2]); end if useposeview posescope = helperposeviewer(... 'xpositionlimits', [-15 15], ... 'ypositionlimits', [-15, 15], ... 'zpositionlimits', [-10 10]); end
simulation loop
the main simulation loop is a while loop with a nested for loop. the while loop executes at gpsfs
, which is the gps sample rate. the nested for loop executes at imufs
, which is the imu sample rate. the scopes are updated at the imu sample rate.
% loop setup - |trajdata| has about 142 seconds of recorded data. secondstosimulate = 50; % simulate about 50 seconds numsamples = secondstosimulate*imufs; loopbound = floor(numsamples); loopbound = floor(loopbound/imufs)*imufs; % ensure enough imu samples % log data for final metric computation. pqorient = quaternion.zeros(loopbound,1); pqpos = zeros(loopbound,3); fcnt = 1; while(fcnt <=loopbound) % |predict| loop at imu update frequency. for ff=1:imusamplespergps % simulate the imu data from the current pose. [accel, gyro, mag] = imu(trajacc(fcnt,:), trajangvel(fcnt, :), ... trajorient(fcnt)); % use the |predict| method to estimate the filter state based % on the simulated accelerometer and gyroscope signals. predict(fusionfilt, accel, gyro); % acquire the current estimate of the filter states. [fusedpos, fusedorient] = pose(fusionfilt); % save the position and orientation for post processing. pqorient(fcnt) = fusedorient; pqpos(fcnt,:) = fusedpos; % compute the errors and plot. if useerrscope orienterr = rad2deg(dist(fusedorient, ... trajorient(fcnt) )); poserr = fusedpos - trajpos(fcnt,:); errscope(orienterr, poserr(1), poserr(2), poserr(3)); end % update the pose viewer. if useposeview posescope(pqpos(fcnt,:), pqorient(fcnt), trajpos(fcnt,:), ... trajorient(fcnt,:) ); end fcnt = fcnt 1; end % this next step happens at the gps sample rate. % simulate the gps output based on the current pose. [lla, gpsvel] = gps( trajpos(fcnt,:), trajvel(fcnt,:) ); % correct the filter states based on the gps data and magnetic % field measurements. fusegps(fusionfilt, lla, rpos, gpsvel, rvel); fusemag(fusionfilt, mag, rmag); end
error metric computation
position and orientation estimates were logged throughout the simulation. now compute an end-to-end root mean squared error for both position and orientation.
posd = pqpos(1:loopbound,:) - trajpos( 1:loopbound, :); % for orientation, quaternion distance is a much better alternative to % subtracting euler angles, which have discontinuities. the quaternion % distance can be computed with the |dist| function, which gives the % angular difference in orientation in radians. convert to degrees % for display in the command window. quatd = rad2deg(dist(pqorient(1:loopbound), trajorient(1:loopbound)) ); % display rms errors in the command window. fprintf('\n\nend-to-end simulation position rms error\n');
end-to-end simulation position rms error
msep = sqrt(mean(posd.^2)); fprintf('\tx: %.2f , y: %.2f, z: %.2f (meters)\n\n',msep(1), ... msep(2), msep(3));
x: 0.50 , y: 0.79, z: 0.65 (meters)
fprintf('end-to-end quaternion distance rms error (degrees) \n');
end-to-end quaternion distance rms error (degrees)
fprintf('\t%.2f (degrees)\n\n', sqrt(mean(quatd.^2)));
1.45 (degrees)