estimate position and orientation of a ground vehicle -凯发k8网页登录
this example shows how to estimate the position and orientation of ground vehicles by fusing data from an inertial measurement unit (imu) and a global positioning system (gps) receiver.
simulation setup
set the sampling rates. in a typical system, the accelerometer and gyroscope in the imu run at relatively high sample rates. the complexity of processing data from those sensors in the fusion algorithm is relatively low. conversely, the gps runs at a relatively low sample rate and the complexity associated with processing it is high. in this fusion algorithm the gps samples are processed at a low rate, and the accelerometer and gyroscope samples are processed together at the same high rate.
to simulate this configuration, the imu (accelerometer and gyroscope) is sampled at 100 hz, and the gps is sampled at 10 hz.
imufs = 100; gpsfs = 10; % define where on the earth this simulation takes place using latitude, % longitude, and altitude (lla) coordinates. localorigin = [42.2825 -71.343 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), position, velocity, and sensor biases.
the insfilternonholonomic
object has two main methods: predict
and fusegps
. the predict
method takes the accelerometer and gyroscope samples from the imu as input. call the predict
method each time the accelerometer and gyroscope are sampled. this method predicts the states forward one time step based on the accelerometer and gyroscope. the error covariance of the extended kalman filter is updated in this step.
the fusegps
method takes the gps samples as input. this method updates the filter states based on the gps sample by computing a kalman gain that weights the various sensor inputs according to their uncertainty. an error covariance is also updated in this step, this time using the kalman gain as well.
the insfilternonholonomic
object has two main properties: imusamplerate
and decimationfactor
. the ground vehicle has two velocity constraints that assume it does not bounce off the ground or slide on the ground. these constraints are applied using the extended kalman filter update equations. these updates are applied to the filter states at a rate of imusamplerate/decimationfactor
hz.
gndfusion = insfilternonholonomic('referenceframe', 'enu', ... 'imusamplerate', imufs, ... 'referencelocation', localorigin, ... 'decimationfactor', 2);
create ground vehicle trajectory
the waypointtrajectory
object calculates pose based on specified sampling rate, waypoints, times of arrival, and orientation. specify the parameters of a circular trajectory for the ground vehicle.
% trajectory parameters r = 8.42; % (m) speed = 2.50; % (m/s) center = [0, 0]; % (m) initialyaw = 90; % (degrees) numrevs = 2; % define angles theta and corresponding times of arrival t. revtime = 2*pi*r / speed; theta = (0:pi/2:2*pi*numrevs).'; t = linspace(0, revtime*numrevs, numel(theta)).'; % define position. x = r .* cos(theta) center(1); y = r .* sin(theta) center(2); z = zeros(size(x)); position = [x, y, z]; % define orientation. yaw = theta deg2rad(initialyaw); yaw = mod(yaw, 2*pi); pitch = zeros(size(yaw)); roll = zeros(size(yaw)); orientation = quaternion([yaw, pitch, roll], 'euler', ... 'zyx', 'frame'); % generate trajectory. groundtruth = waypointtrajectory('samplerate', imufs, ... 'waypoints', position, ... 'timeofarrival', t, ... 'orientation', orientation); % initialize the random number generator used to simulate sensor noise. rng('default');
gps receiver
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, 'referenceframe', 'enu'); gps.referencelocation = localorigin; gps.decayfactor = 0.5; % random walk noise parameter gps.horizontalpositionaccuracy = 1.0; gps.verticalpositionaccuracy = 1.0; gps.velocityaccuracy = 0.1;
imu sensors
typically, ground vehicles use a 6-axis imu sensor for pose estimation. to model an imu sensor, define an imu sensor model containing an accelerometer and gyroscope. in a real-world application, the two 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', ... 'referenceframe', 'enu', 'samplerate', imufs); % accelerometer imu.accelerometer.measurementrange = 19.6133; imu.accelerometer.resolution = 0.0023928; imu.accelerometer.noisedensity = 0.0012356; % gyroscope imu.gyroscope.measurementrange = deg2rad(250); imu.gyroscope.resolution = deg2rad(0.0625); imu.gyroscope.noisedensity = deg2rad(0.025);
initialize the states of the insfilternonholonomic
the states are:
states units index orientation (quaternion parts) 1:4 gyroscope bias (xyz) rad/s 5:7 position (ned) m 8:10 velocity (ned) m/s 11:13 accelerometer bias (xyz) m/s^2 14:16
ground truth is used to help initialize the filter states, so the filter converges to good answers quickly.
% get the initial ground truth pose from the first sample of the trajectory % and release the ground truth trajectory to ensure the first sample is not % skipped during simulation. [initialpos, initialatt, initialvel] = groundtruth(); reset(groundtruth); % initialize the states of the filter gndfusion.state(1:4) = compact(initialatt).'; gndfusion.state(5:7) = imu.gyroscope.constantbias; gndfusion.state(8:10) = initialpos.'; gndfusion.state(11:13) = initialvel.'; gndfusion.state(14:16) = imu.accelerometer.constantbias;
initialize the variances of the insfilternonholonomic
the measurement noises describe how much noise is corrupting the gps reading based on the gpssensor
parameters and how much uncertainty is in the vehicle dynamic model.
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 rvel = gps.velocityaccuracy.^2; rpos = gps.horizontalpositionaccuracy.^2; % the dynamic model of the ground vehicle for this filter assumes there is % no side slip or skid during movement. this means that the velocity is % constrained to only the forward body axis. the other two velocity axis % readings are corrected with a zero measurement weighted by the % |zerovelocityconstraintnoise| parameter. gndfusion.zerovelocityconstraintnoise = 1e-2; % process noises gndfusion.gyroscopenoise = 4e-6; gndfusion.gyroscopebiasnoise = 4e-14; gndfusion.accelerometernoise = 4.8e-2; gndfusion.accelerometerbiasnoise = 4e-14; % initial error covariance gndfusion.statecovariance = 1e-9*eye(16);
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 3d 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', ... [-1, 1 -1, 1 -1, 1 -1, 1]); end if useposeview viewer = helperposeviewer( ... 'xpositionlimits', [-15, 15], ... 'ypositionlimits', [-15, 15], ... 'zpositionlimits', [-5, 5], ... 'referenceframe', 'enu'); end
simulation loop
the main simulation loop is a while loop with a nested for loop. the while loop executes at the gpsfs
, which is the gps measurement rate. the nested for loop executes at the imufs
, which is the imu sample rate. the scopes are updated at the imu sample rate.
totalsimtime = 30; % seconds % log data for final metric computation. numsamples = floor(min(t(end), totalsimtime) * gpsfs); trueposition = zeros(numsamples,3); trueorientation = quaternion.zeros(numsamples,1); estposition = zeros(numsamples,3); estorientation = quaternion.zeros(numsamples,1); idx = 0; for sampleidx = 1:numsamples % predict loop at imu update frequency. for i = 1:imusamplespergps if ~isdone(groundtruth) idx = idx 1; % simulate the imu data from the current pose. [trueposition(idx,:), trueorientation(idx,:), ... truevel, trueacc, trueangvel] = groundtruth(); [acceldata, gyrodata] = imu(trueacc, trueangvel, ... trueorientation(idx,:)); % use the predict method to estimate the filter state based % on the acceldata and gyrodata arrays. predict(gndfusion, acceldata, gyrodata); % log the estimated orientation and position. [estposition(idx,:), estorientation(idx,:)] = pose(gndfusion); % compute the errors and plot. if useerrscope orienterr = rad2deg( ... dist(estorientation(idx,:), trueorientation(idx,:))); poserr = estposition(idx,:) - trueposition(idx,:); errscope(orienterr, poserr(1), poserr(2), poserr(3)); end % update the pose viewer. if useposeview viewer(estposition(idx,:), estorientation(idx,:), ... trueposition(idx,:), estorientation(idx,:)); end end end if ~isdone(groundtruth) % this next step happens at the gps sample rate. % simulate the gps output based on the current pose. [lla, gpsvel] = gps(trueposition(idx,:), truevel); % update the filter states based on the gps data. fusegps(gndfusion, lla, rpos, gpsvel, rvel); end end
error metric computation
position and orientation were logged throughout the simulation. now compute an end-to-end root mean squared error for both position and orientation.
posd = estposition - trueposition; % 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(estorientation, trueorientation)); % 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: 1.16 , y: 0.98, z: 0.03 (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)));
0.11 (degrees)