Counting Steps using BeagleBone Blue Hardware

This example demonstrates a pedometer that counts the number of steps a person walked while holding a BeagleBone Blue® hardware.

Introduction

A pedometer is an electronic device that estimates the distance traveled by a person by recording the number of steps walked. Pedometers use an accelerometer to count the number of steps.

In this example, a Beaglebone Blue hardware records acceleration along X, Y, and Z axes. This data is recorded over a duration of time using the MAT-file logging feature. You can then use MATLAB® to analyze the imported MAT-files to count the number of steps.

To use the MAT-file logging feature with the Simulink® Coder™ Support Package for BeagleBone Blue Hardware, you must have a Simulink Coder license.

Prerequisites

Before you start with this example, we recommend you to complete the Getting Started with Simulink Coder Support Package for BeagleBone Blue Hardware example and Log Signals on a Hardware.

Required Hardware

To run this example, you must have the BeagleBone Blue hardware.

Step 1: Create a Simulink model for BeagleBone Blue Hardware

1. Open the Count Steps Using MPU9250 Sensor of BeagleboneBlue Hardware model.

2. In your Simulink model, click Simulation > Model Configuration Parameters to open the Configuration Parameters dialog box.

3. Under the Hardware Implementation pane, select BeagleBone Blue in the Hardware board list. Do not change any other settings.

4. Click Apply to save your changes, and then click OK.

Step 2: Enable MAT File Logging

This task explains how to enable MAT-file logging to save acceleration data as MAT-files.

1. To open the Model Configuration Parameters dialog box, click the gear icon on the Simulink model toolbar.

2. Browse to Code Generation > Interface > Advanced Parameters, or type MAT-file logging in the search box.

3. Select the MAT-file logging option and click Apply to save the changes.

4. Click OK to close the dialog box.

5. In the Simulink model, double-click the Scope block, and click the gear icon to open the Configuration Properties dialog box.

6. In the Logging tab, select the Log data to workspace option, and click Apply to save the changes.

7. On the Simulink model toolbar, set the Simulation stop time parameter. This parameter specifies the duration for which the signals are logged. After the simulation stop time elapses, the logging of signals stops. However, your model continues to run. For example, if the Simulation stop time parameter is specified as 10.0 seconds, the signals are logged for 10.0 seconds, and then the logging stops. However, the model continues to run for indefinite time.

Step 3: Deploy the Model on BeagleBone Blue Hardware

1. On the Simulink model toolbar, click the Deploy To Hardware button. This action builds, downloads, and runs the model on the BeagleBone Blue hardware.

2. Walk a few steps while holding the hardware. Make sure that you walk at least for the duration specified in the Simulation stop time parameter.

Step 4: Import and Analyze Data

This task explains how to import the generated MAT-files from the hardware to your computer after the logging is completed.

1. In the MATLAB command window, use the following command to create a beagleboneblue object. The parameters specified in this command must match the board parameters specified in Simulation > Model Configuration Parameters > Target hardware resources > Board Parameters.

bbb = beagleboneblue(<IP address>, <username>, <password>);

2. Use the getFile function to copy the MAT-files from the hardware to your computer.

getFile(bbb,<filename>)

Here, bbb specifies the beagleboneblue object and filename specifies the path and name of the file created. After importing the MAT-files, you can use it like a regular MAT-file for any further analysis in MATLAB®.

3. Load the MAT files into workspace variables.

load('beagleboneblue_countstep_1_1.mat');
a(:,:) =  rt_ScopeData.signals.values(1,:,:) * 9.8;
a = a';
t =  rt_tout;

4. Plot raw sensor data.

plot(t, a);
legend('X', 'Y', 'Z');
xlabel('Relative time (s)');
ylabel('Acceleration (m/s^2)');

5. Process raw acceleration data.

To convert the XYZ acceleration vectors at each point in time into scalar values, calculate the magnitude of each vector. This way, you can detect large changes in overall acceleration, such as steps taken while walking, regardless of device orientation.

x = a(:,1);
y = a(:,2);
z = a(:,3);
mag = sqrt(sum(x.^2 + y.^2 + z.^2, 2));

Plot the magnitude to visualize the general changes in acceleration.

plot(t, mag);
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');

The plot shows that the acceleration magnitude is not zero mean. Subtract the mean from the data to remove any constant effects, such as gravity.

magNoG = mag - mean(mag);
plot(t, magNoG);
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');

The plotted data is now centered about zero and clearly shows peaks in acceleration magnitude. Each peak corresponds to a step being taken while walking.

6. Count the number of steps taken.

Use findpeaks, a function from the Signal Processing Toolbox™, to find the local maxima of the acceleration magnitude data. Only peaks with a minimum height above one standard deviation are treated as a step. This threshold must be tuned experimentally to match a person's level of movement while walking, hardness of floor surfaces, and other variables.

minPeakHeight = std(magNoG);
[pks, locs] = findpeaks(magNoG, 'MINPEAKHEIGHT', minPeakHeight);

The number of steps taken is simply the number of peaks found.

numSteps = numel(pks)

Visualize the peak locations with the acceleration magnitude data.

hold on;
plot(t(locs), pks, 'r', 'Marker', 'v', 'LineStyle', 'none');
title('Counting Steps');
xlabel('Time (s)');
ylabel('Acceleration Magnitude, No Gravity (m/s^2)');
hold off;

See Also

MAT-file logging on Beaglebone Blue™ Hardware