estimate orientation through inertial sensor fusion -凯发k8网页登录
this example shows how to use 6-axis and 9-axis fusion algorithms to compute orientation. there are several algorithms to compute orientation from inertial measurement units (imus) and magnetic-angular rate-gravity (marg) units. this example covers the basics of orientation and how to use these algorithms.
orientation
the orientation of an object describes its rotation relative to some coordinate system, sometimes called a parent coordinate system, in three dimensions.
for the following algorithms, the fixed, parent coordinate system used is north-east-down (ned). ned is sometimes referred to as the global coordinate system or reference frame. in the ned reference frame, the x-axis points north, the y-axis points east, and the z-axis points downward. the x-y plane of ned is considered to be the local tangent plane of the earth. depending on the algorithm, north may be either magnetic north or true north. the algorithms in this example use magnetic north.
if specified, the following algorithms can estimate orientation relative to east-north-up (enu) parent coordinate system instead of ned.
an object can be thought of as having its own coordinate system, often called the local or child coordinate system. this child coordinate system rotates with the object relative to the parent coordinate system. if there is no translation, the origins of both coordinate systems overlap.
the orientation quantity computed is a rotation that takes quantities from the parent reference frame to the child reference frame. the rotation is represented by a quaternion or rotation matrix.
types of sensors
for orientation estimation, three types of sensors are commonly used: accelerometers, gyroscopes and magnetometers. accelerometers measure proper acceleration. gyroscopes measure angular velocity. magnetometers measure the local magnetic field. different algorithms are used to fuse different combinations of sensors to estimate orientation.
sensor data
through most of this example, the same set of sensor data is used. accelerometer, gyroscope, and magnetometer sensor data was recorded while a device rotated around three different axes: first around its local y-axis, then around its z-axis, and finally around its x-axis. the x-axis of the device was generally pointed southward for the duration of the experiment.
ld = load("rpy_9axis.mat");
acc = ld.sensordata.acceleration;
gyro = ld.sensordata.angularvelocity;
mag = ld.sensordata.magneticfield;
pp = poseplot;
accelerometer-magnetometer fusion
the ecompass
function fuses accelerometer and magnetometer data. this is a memoryless algorithm that requires no parameter tuning, but the algorithm is highly susceptible to sensor noise.
qe = ecompass(acc, mag); for ii=1:size(acc,1) set(pp,"orientation",qe(ii)) drawnow limitrate end
note that the ecompass
algorithm correctly finds the location of north. however, because the function is memoryless, the estimated motion is not smooth. the algorithm could be used as an initialization step in an orientation filter or some of the techniques presented in the could be used to smooth the motion.
accelerometer-gyroscope fusion
the following objects estimate orientation using either an error-state kalman filter or a complementary filter. the error-state kalman filter is the standard estimation filter and allows for many different aspects of the system to be tuned using the corresponding noise parameters. the complementary filter can be used as a substitute for systems with memory constraints, and has minimal tunable parameters, which allows for easier configuration at the cost of finer tuning.
the imufilter
and complementaryfilter
system objects™ fuse accelerometer and gyroscope data. the imufilter
uses an internal error-state kalman filter and the complementaryfilter
uses a complementary filter. the filters are capable of removing the bias noise of the gyroscope, which drifts over time.
ifilt = imufilter(samplerate=ld.fs); for ii=1:size(acc,1) qimu = ifilt(acc(ii,:),gyro(ii,:)); set(pp,"orientation",qimu) drawnow limitrate end
% disable magnetometer input. cfilt = complementaryfilter(samplerate=ld.fs,hasmagnetometer=false); for ii=1:size(acc,1) qimu = cfilt(acc(ii,:),gyro(ii,:)); set(pp,"orientation",qimu) drawnow limitrate end
although the imufilter
and complementaryfilter
algorithms produce significantly smoother estimates of the motion, compared to the ecompass
, they do not correctly estimate the direction of north. the imufilter
does not process magnetometer data, so it simply assumes the x-axis of the device is initially pointing northward. the motion estimate given by imufilter
is relative to the initial estimated orientation. the complementaryfilter
makes the same assumption when the hasmagnetometer
property is set to false
.
accelerometer-gyroscope-magnetometer fusion
an attitude and heading reference system (ahrs) consists of a 9-axis system that uses an accelerometer, gyroscope, and magnetometer to compute orientation. the ahrsfilter
and complementaryfilter
system objects™ combine the best of the previous algorithms to produce a smoothly changing estimate of the device orientation, while correctly estimating the direction of north. the complementaryfilter
uses the same complementary filter algorithm as before, with an extra step to include the magnetometer and improve the orientation estimate. like imufilter
, ahrsfilter
algorithm also uses an error-state kalman filter. in addition to gyroscope bias removal, the ahrsfilter
has some ability to detect and reject mild magnetic jamming.
ifilt = ahrsfilter(samplerate=ld.fs); for ii=1:size(acc,1) qahrs = ifilt(acc(ii,:),gyro(ii,:),mag(ii,:)); set(pp,"orientation",qahrs) drawnow limitrate end
cfilt = complementaryfilter(samplerate=ld.fs); for ii=1:size(acc,1) qahrs = cfilt(acc(ii,:),gyro(ii,:),mag(ii,:)); set(pp,"orientation",qahrs) drawnow limitrate end
tuning filter parameters
the complementaryfilter
, imufilter
, and ahrsfilter
system objects™ all have tunable parameters. tuning the parameters based on the specified sensors being used can improve performance.
the complementaryfilter
parameters accelerometergain
and magnetometergain
can be tuned to change the amount each that the measurements of each sensor impact the orientation estimate. when accelerometergain
is set to 0
, only the gyroscope is used for the x- and y-axis orientation. when accelerometergain
is set to 1
, only the accelerometer is used for the x- and y-axis orientation. when magnetometergain
is set to 0
, only the gyroscope is used for the z-axis orientation. when magnetometergain
is set to 1
, only the magnetometer is used for the z-axis orientation.
the ahrsfilter
and imufilter
system objects™ have more parameters that can allow the filters to more closely match specific hardware sensors. the environment of the sensor is also important to take into account. the imufilter
parameters are a subset of the ahrsfilter
parameters. the accelerometernoise
, gyroscopenoise
, magnetometernoise
, and gyroscopedriftnoise
are measurement noises. the sensors' datasheets help determine those values.
the linearaccelerationnoise
and linearaccelerationdecayfactor
govern the response of the filter to linear (translational) acceleration. shaking a device is a simple example of adding linear acceleration.
consider how an imufilter
with a linearaccelerationnoise
of 9e-3 responds to a shaking trajectory, compared to one with a linearaccelerationnoise
of 9e-4 .
ld = load("shakingdevice.mat"); accel = ld.sensordata.acceleration; gyro = ld.sensordata.angularvelocity; highvarfilt = imufilter(samplerate=ld.fs, ... linearaccelerationnoise=0.009); qhighlanoise = highvarfilt(accel,gyro); lowvarfilt = imufilter(samplerate=ld.fs, ... linearaccelerationnoise=0.0009); qlowlanoise = lowvarfilt(accel,gyro);
one way to see the effect of the linearaccelerationnoise
is to look at the output gravity vector. the gravity vector is simply the third column of the orientation rotation matrix.
rmathigh = rotmat(qhighlanoise,"frame"); rmatlow = rotmat(qlowlanoise,"frame"); gravdisthigh = sqrt(sum((rmathigh(:,3,:)-[0;0;1]).^2, 1)); gravdistlow = sqrt(sum((rmatlow(:,3,:)-[0;0;1]).^2, 1)); figure; plot([squeeze(gravdisthigh),squeeze(gravdistlow)]); title("euclidean distance to gravity"); legend("linearaccelerationnoise = 0.009", ... "linearaccelerationnoise = 0.0009");
the lowvarfilt
has a low linearaccelerationnoise
, so it expects to be in an environment with low linear acceleration. therefore, it is more susceptible to linear acceleration, as illustrated by the large variations earlier in the plot. however, because it expects to be in an environment with a low linear acceleration, higher trust is placed in the accelerometer signal. as such, the orientation estimate converges quickly back to vertical once the shaking has ended. the converse is true for highvarfilt
. the filter is less affected by shaking, but the orientation estimate takes longer to converge to vertical when the shaking has stopped.
the magneticdisturbancenoise
property enables modeling magnetic disturbances (non-geomagnetic noise sources) in much the same way linearaccelerationnoise
models linear acceleration.
the two decay factor properties (magneticdisturbancedecayfactor
and linearaccelerationdecayfactor
) model the rate of variation of the noises. for slowly varying noise sources, set these parameters to a value closer to 1. for quickly varying, uncorrelated noises, set these parameters closer to 0. a lower linearaccelerationdecayfactor
enables the orientation estimate to find "down" more quickly. a lower magneticdisturbancedecayfactor
enables the orientation estimate to find north more quickly.
very large, short magnetic disturbances are rejected almost entirely by the ahrsfilter
. consider a pulse of [0 250 0] ut applied while recording from a stationary sensor. ideally, there should be no change in orientation estimate.
ld = load("magjamming.mat"); hpulse = ahrsfilter(samplerate=ld.fs); len = 1:10000; qpulse = hpulse(ld.sensordata.acceleration(len,:), ... ld.sensordata.angularvelocity(len,:), ... ld.sensordata.magneticfield(len,:)); figure; timevec = 0:ld.fs:(ld.fs*numel(qpulse) - 1); plot( timevec, eulerd(qpulse,"zyx","frame") ); title(["stationary trajectory orientation euler angles" newline ... "magnetic jamming response"]); legend("z-rotation","y-rotation","x-rotation"); ylabel("degrees"); xlabel("seconds");
note that the filter almost totally rejects this magnetic pulse as interference. any magnetic field strength greater than four times the expectedmagneticfieldstrength
is considered a jamming source and the magnetometer signal is ignored for those samples.
conclusion
the algorithms presented here, when properly tuned, enable estimation of orientation and are robust against environmental noise sources. it is important to consider the situations in which the sensors are used and tune the filters accordingly.