In modeling the quadcopter for simulation we have full knowledge of the vehicle attitude, however for the real hardware we have limited observability with our sensors, and must deal with the errors that come with those sensors. In this post I will go over how we can estimate the quadcopter’s orientation in Euler angles from the available sensors. None of the sensors directly measure an Euler angle, so instead we must calculate estimates for the angles based on the information the sensors provide us. I will show how we can derive orientation information from the gyroscopes, magnetometer, and accelerometers; all of which are included on the APM board. This post will be limited to the individual measurements the sensors provide; how we can combine them through sensor fusion will be covered in another post.
The first sensor is the primary sensor for attitude determination. Gyroscopes measure angular rates which must be integrated over time to estimate angular orientation. Because it must be integrated means that we need some initial condition which we can then add on to.
The initial condition can either be assumed based on operating procedure (for example, vehicle is only started up on level ground) or from other sensors which provide absolute orientation measurements (as we’ll see the magnetometer and accelerometers can provide).
Transforming Measured Angular Rates to Euler Angle Rates
Once we have an initial condition of vehicle attitude identified, we can integrate the measured angular rates to propagate the orientation over time. One caveat though, is that the measured rates from the gyros represent the angular velocity about the body-fixed frame, which is not the same as the rate of change of the Euler angles.
This is because the Euler angles represent a sequence of rotations each in their own intermediate frame of reference, whereas the angular velocity is the instantaneous angular rate about each of the body-fixed axes. We can use coordinate transformations to express the angular velocity as a function of the Euler angle derivatives by transforming them all into the body frame.
Measured angular velocity , Euler angle rates
To help make this clear I’ve labeled the intermediate reference frames in the sequence of Euler rotations below as b” and b’. So if we want to know how would be represented in the body frame (b), we must transform it from its intermediate frame. Here we see is the rotation rate about unit vector in the b” frame, so we will refer to it as . However in the final body-fixed frame, b, that unit vector has been rotated by the second and third Euler angles in the sequence.
Therefore, the rotation rates measured in the body-fixed frame due to the rate of change of the first Euler angle will equal the rotation transformed by the and rotations. (Since its rotation angle is also parallel with the inertial axis , you could get the same answer by multiplying it by all three Euler angle rotations with the inertial-to-body coordinate transformation derived in Modeling Vehicle Dynamics – Euler Angles).
The second Euler rotation, is treated similarly, and needs no rotation as the unit vector it rotates around, , is a unit vector in the body frame, b.
The relationship between measured gyro rates and Euler angle rates can be expressed in scalar form multiplied by unit vectors as:
If we express it in matrix form however, we can take advantage of our rotation matrices to handle the disparate reference frames:
To make things a little easier to read and fit on the page, I’m going to use a shorthand for the trigonometric functions: and . The above expression can be simplified by multiplying the matrices.
So finally we arrive at the set of equations:
These equations can also be rewritten for the Euler angle derivatives:
Estimating Euler Angles from Rate Measurements
These equations can be used to propagate the attitude of the vehicle for each successive sampling frame using numerical integration techniques. If your sampling time is small enough, or the rates are small, a first order approximation may be sufficient. The estimated orientation for new frame would be a function of the estimated orientation and measured angular velocity at time frame where :
An example of this in Python code would be:
# Derivative function def xdot(x,u): x_dot = [0,0,0] # phidot = p + (q*sin(phi) + r*cos(phi))*tan(theta) x_dot = u + (u*sin(x) + u*cos(x))*tan(x) # thetadot = q*cos(phi) - r*sin(phi) x_dot = u*cos(x) - u*sin(x) # psidot = (q*sin(phi) + r*cos(phi))*sec(theta) x_dot = (u*sin(x) + u*cos(x))*sec(x) return x_dot # Initial condition setup eulerAngles = [0,0,0] #Assume start up with phi = theta = psi = 0 radians deltaT = 0.01 #Sampling time (sec) # While loop while True: omegaRead = gyros.getAngularVelocity() eulerAngles += xdot(eulerAngles,omegaRead) * deltaT
Gyroscopes will have errors you may need to account for in your attitude estimation. These include measurement bias as well as scaling and cross-coupling errors. These errors can be accounted for through calibration during initial startup or estimated by a Kalman filter while operating.
The primary concern with any estimate achieved through integration is drifting orientation estimates due to errors being accumulated over time through the integration. A way to counteract this is to perform sensor fusion (through techniques such as a complimentary filter or a Kalman filter) with sensors that measure absolute orientation.
With a magnetometer we can receive a 3-axis reading of the magnetic field of the Earth. However, while we read a 3-dimensional vector from the sensor, it is generally only used to gather an estimate of the heading angle of a vehicle. This is because the geometry of the magnetic field is predominantly parallel to the ground in most areas away from the poles, and thus is not conducive to estimating pitch or roll.
Magnetic north does not directly align with true north. If you wish to resolve the vehicle’s orientation in a reference frame for navigation along cardinal directions or latitude/longitude, you will need to correct your heading measurements to true north. In order to do this, one needs the declination angle () for the vehicle’s location about Earth. One way to do this is to simply store a lookup table that can be queried with a known starting position or measured/estimated location. The magnetic field in the inertial frame is equal to the following expression with declination angle, inclination angle (), and magnetic field magnitude :
In some instances where navigation is not as important, the magnetic heading may be all that is needed to correct for gyro drift in attitude estimation. Either way, by measuring an absolute orientation, the magnetometer can be used to initialize the heading of the vehicle before propagating orientation through use of the gyros.
Calculating Heading () from Magnetometer Readings
The magnetometer is fixed to the vehicle frame, and thus will produce magnetic flux readings in the body frame. To find magnetic heading, we first need to reorient the magnetic reading into a frame level with the ground. To do this I will define a new reference frame called the “level frame” (L) which is simply only one Euler angle () rotation from the inertial reference frame. This means that the Level frame is aligned with the heading of the vehicle, but is before the pitch and roll Euler angle rotations. We can transform our body frame magnetometer reading, , into the level frame with the rotation matrices:
This transformation provides us with the magnetic field strength in the x and y components of the level frame as a function of the body frame measurements:
To determine the magnetic heading, we take the arctangent of the x and y components in the level frame:
And if we know the declination angle, , then the true heading is given by
An example Python code calculating magnetic heading, , given magnetometer reading and estimated pitch and roll angles
def magHdg(magRead, phi, theta): # Takes magnetometer reading and estimated phi and theta Euler angles, # and calculates a measured psi Euler angle (magnetic heading) mx_b = magRead my_b = magRead mz_b = magRead # Rotate by phi and theta estimates to Level (L) frame mx_L = mx_b*cos(theta) + my_b*sin(theta)*sin(phi) + mz_b*sin(theta)*cos(phi) my_L = my_b*cos(phi) - mz_b*sin(phi) #Calculate psi psi_meas = arctan2(-my_L,mx_L) return psi_meas
The magnetometer is sensitive to sources of magnetism other than the Earth’s magnetic field. Calibration is normally done to account for soft-iron and hard-iron magnetism from the vehicle itself. Other magnetic fields nearby the magnetometer may also influence its readings.
While accelerometers are generally used to measure accelerations to integrate into velocity and position, they can also be used to aid attitude estimation through accelerometer leveling. When the vehicle is stationary the 3-axis accelerometer will only detect the specific force due to gravity. Using the magnitude of the gravity measurement in each component, an estimate of pitch and roll orientation can be made.
For accelerometer leveling the only prior information needed is the magnitude of gravity, , which should be stored as a constant. Accelerometer leveling may also be used to help define the initial and orientation of the vehicle during startup.
Transforming Inertial Gravity Vector to Body Coordinates
Gravity is assumed to be a constant acceleration always oriented towards the center of the Earth. Since the accelerometers are attached to the vehicle frame, they will measure acceleration in the body fixed reference frame. We can use the coordinate transformation matrix, , from the definition of our Euler angles to transform the inertial gravity vector into body-fixed specific force measurements, .
Calculating Pitch () and Roll () Attitude from Accelerometer Reading
By using the separate components of the accelerometer measurements we can see that
And therefore can solve for the Euler angles by
A sample excerpt of Python code illustrates how this estimate can be calculated:
def accelLevel(accelRead): # Takes accelerometer specific force reading when stationary and estimates # phi and theta from the gravity vector fx_b = accelRead fy_b = accelRead fz_b = accelRead phi_est = arctan2(-fy_b,-fz_b) theta_est = arctan2(fx_b, sqrt(fy_b*fy_b + fz_b*fz_b)) return phi_est, theta_est
Just as with the gyroscopes, accelerometers suffer from bias, scaling, and cross-coupling errors. These can be calibrated out during initial startup or estimated by a Kalman filter during operation.
Accelerometer leveling is only accurate when the vehicle is stationary. If there are accelerations then the specific force components are no longer measuring only gravity. One way to mitigate this is by using a threshold limit in acceleration readings to identify when an accelerometer leveling attitude estimate is acceptable. Additionally known states of rest could trigger an accelerometer leveling update.
Now that we have identified how to extract Euler angle information from three independent sensor sources, we can next look at how to combine them all into a single, accurate estimate of orientation.
 Groves, Paul D. “Principles of GNSS, Inertial, and Multisensor Integrated Navigation Systems”