Lab 9: Sensor Fusion
In the previous few labs you have built nodes that used various sensors to measure environmental conditions. In this lab you will combine the output of two sensors, a 3-D accelerometer and a 3-D magnetometer, to measure 3-D sensor orientation (heading, pitch, and roll). Pitch and roll (defined below) can be determined with an accelerometer, and the compass heading can be determined with a magnetometer – assuming that it is level – but the measurements from the two sensors must be combined to accurately compute heading. This combination of sensor data to compute an environmental value that neither can independently compute is called sensor fusion.
While early accelerometers “simply” measured acceleration, modern accelerometers frequently have some built-in higher level functionality such a detecting taps or double-taps (useful for user interfaces on portable devices), orientation (frequently used for determining the correct screen orientation), activity detection, free-fall detection (used to protect laptop hard drives), and even step detection. Most of these functions were previously computed in software using a stream of acceleration data, but can be computed at much lower energy cost with dedicated hardware. In this lab you will use one of these functions – activity detection – to determine when it is necessary to provide data updates to a MQTT broker.
Overview of Lab
In this assignment you will be using the LIS2MDL 3D magnetometer and
LIS2DW12 3D accelerometer on the X-NUCLEO-IKS01A3 to build a combination
compass and activity detector. The lab is structured as a series
of smaller steps to develop the various sensor processing techniques. As with the previous lab, you will be given several working programs
and asked to extend and combine these to build an integrated solution.
You will also be asked integrate your solution into an MQTT system with a node-red dashboard displaying the data as appropriate.
The skeleton directory for this lab has the following form:
.
├── cfg
│ └── ... elided ...
├── config.mk
├── lis2dw12-example1
│ └── ... elided ...
├── lis2dw12-example2
│ └── ... elided ...
├── lis2mdl-example
│ └── ... elided ...
└── make.mk
The assignment consists of several parts:
- Build and test the example programs
- Extend the lis2mdl (magnetometer) example to compute compass heading.
- Extend the lis2dw12 (accelerometer) the first to compute pitch and roll angles.
- Combine your two programs into one that computes pitch/roll/yaw(heading).
- Add an activity detection component (based upon lis2dw12-example2) to your code.
- Integrate your system into node-red
Deliverables
As before, your report should document your efforts and describe any code that you created.
The grading rubric for this lab is
- Completion of tasks 60%
- Demonstrate example programs running: 10pts
- Program computing pitch/roll: 10pts
- Program computing pitch/roll/yaw: 10pts
- Program computing pitch/roll/yaw + activity detection: 10pts
- Working program + MQTT communication + Dashboard: 20pts
- Lab report 40%
- Overview: 10 pts
- Description of your code + screen shots: 25 pts
- Issues: 5pts
2 Background (part 1 – accelerometer)
An accelerometer is a device that measures the acceleration (in units of gravity) of a body. Most accelerometers measure acceleration along three axes – typically labelled x, y, and z. Because the earth exerts a constant
$$ 9.8 m/s^2 $$
in the z direction, it is possible to determine the orientation of an accelerometer at rest relative to the surface of the earth. The following figure illustrates the axes of the lis2dw12 relative to the chip. On the X-NUCLEO-IKS01A3 board, the side labeled 1 points towards the edge with the rectangular cutout.Assuming with have three acceleration components Gx,Gy, and Gz, then we can compute roll (rotation about the y axis) and pitch (rotation around the x axis) relative to the accelerometer lying flat as
$$ roll (\theta) = arctan(\frac{-G_x}{\sqrt{G_y^2 + G_z^2}}) \times 180/\pi $$
$$ pitch (\phi) = arctan(\frac{-G_y}{\sqrt{G_x^2 + G_z^2}}) \times 180/\pi $$
The derivation of these equations is presented in tilt sensing.
These equations are only valid if the rate of change of acceleration is small and, only for ranges of +- 90 degrees. Additional care is required to handle full rotation. The requirement for small change rate is handled, in the accelerometer, with a low-pass filter. A low pass filter removes (filters) rapidly changing portions of a measurement.
Modern accelerometer often support relatively complex filtering to enable data to be used both for orientation (low-pass) and for detecting ephemeral events (high-pass) such as tap detection. The lis2dw12 has a relatively complex filtering chain consisting of
- The ADC converter that translates the analog sensor data
- An anti-aliasing filter (not relevant to this lab)
- Two low-pass filters
- One high-pass filter
In addition to providing various filters, the LIS2DW12 has a number of embedded functions to detect:
- Free-fall
- Wake-up
- 6D/4D orientation detection
- Single-tap and double-tap sensing
- Activity/Inactivity detection
Thes various function blocks utilize the output of the various filters (as appropriate):
In addition to using the accelerometer for computing orientation, we will use the activity detection function in this lab. The following figure illustrates this function. Suppose the accelerometer output (high pass filtered) in one dimension is illustrated in red. So long as the instantaneous acceleration remains within the range += WK Threshold, the device is inactive. If magnitude of the instantaneous accelerated exceeds the threshold for an appropriate time (WU_DUR) the device is “active”. This is described in more detail in AN5038
3. Background (part 2 – magnetometer)
The LIS2MDL measures magnetic fields in three dimensions. The orientation of the X,Y, and Z axes on the X-NUCLEO-IKS01A3 board are the same as those of the LIS2DW12. This is a considerably simpler component – consisting of the sensors and an ADC chain:
The following figures from AN2272 illustrate the issues
Assuming the magnetometer is parallel to the earth, the heading (direction) is given by:
$$ heading = arctan(\frac{H_y}{H_x}) $$
In practice, the device is not parallel to the earth, but is at some roll and pitch angle:
To compensate using the roll and pitch computed with the accelerometer, we use the following equations
$$H’_x = H_xcos(\phi) + H_ysin(\theta)sin(\phi) + H_zcos(\theta)sin(\phi)$$
$$H’_y = H_ycos(\theta) - H_zsin(\theta)$$
and finally,
$$ heading = arctan(\frac{H’_y}{H’_x}) $$
4 Compiling and Executing the Examples
Each of the examples can be compiled and down-loaded as with the previous lab.
- cd to the example directory
make download
Each of the examples generates text output that can be viewd with the goshell
../../../lab-instructions/bin/goshell --port /dev/ttyxxx
explain the examples
5. Computing Pitch and Roll
6. Computing Compass Heading
7. Adding activity detection
8. Building an MQTT application
Acknowledgement
- Information and figures on tilt sensing drawn from – “Tilt Sensing Using a Three-Axis Accelerometer” by Mark Pedley, Freescale seminar.
- Sensing- Magnetic compass with tilt compensation Cypress AN2272