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. A Raspberry Pi SenseHAT records acceleration along X, Y, and Z axes. You can use Simulink to record this data 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 Support Package for Raspberry Pi hardware, you must have a Simulink Coder license.

For those who are not familiar with Simulink, I would recommend you complete the Getting Started with Raspberry Pi Hardware and MAT-file logging on Raspberry Pi Hardware examples that are available on MathWorks website.

Required Hardware

To recreate this project, you must have the following hardware:

Create a Simulink model for Raspberry Pi Hardware

1. Open the Log Accelerometer data using Raspberry Pi Hardware model by typing raspberrypi_countstep in MATLAB Command Window. You will see a block diagram that looks like the image shown here.

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

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

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

Enable MAT file logging

Here are step-by-step instructions on 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.0seconds, the signals are logged for 10.0 seconds, and then the logging stops. However, the model continues to run for indefinite time.

Deploy the Model on Raspberry Pi Hardware

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

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

Import and Analyze Data

To import the generated MAT-files from the hardware to your computer after the logging is completed, follow these steps -

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

r = raspberrypi(<IP address>, <username>, <password>);

2. Use the getFile function to copy the MAT-files from the Raspberry Pi™ board to your computer.


Here, r specifies the raspberrypi 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.


a(:,:) = rt_simout.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;

This shows how you can make use of the IMU sensor on Raspberry Pi Sense HAT to count the number of steps a person walked.