Sensor Fusion App: Examples

This page contains examples of how to use the Sensor Fusion App:

Stream Data to MATLAB

This is a short example of how to stream data to MATLAB from the Sensor Fusion App. The original page also linked to a separate lab handout, orientation.pdf. Its published download is currently unavailable; the connection examples and MATLAB support files are preserved here.

  1. Download the ZIP archive with the support functions and unzip the files to your MATLAB path (for example, the current directory).
  2. Start MATLAB and run startup.m.
  3. Execute the following code to start a small server to receive measurements from the Sensor Fusion App in MATLAB and process data as it arrives:
import('se.hendeby.sensordata.*');
try
  %% Create data link
  server = StreamSensorDataReader(3400);
  % Makes sure to resources are returned.
  sentinel = onCleanup(@() server.stop());

  server.start();  % Start data reception.
catch e
  fprintf(['Unsuccessful connecting to client!\n' ...
    'Make sure to start streaming from the phone *after* '...
           'running this function!']);
  return;
end

while server.status()  % Repeat while data is available
  % Get the next measurement set, assume all measurements
  % within the next 5 ms are concurrent (suitable for sampling
  % in 100Hz).
  data = server.getNext(5);

  if isnan(data(1))  % No new data received
    continue;
  end

  % Process data
  % data(1)         Time [ms]
  % data(1, 2:4)    Accelerometer; x, y, z [m/s^2]
  % data(1, 5:7     Gyroscope; x, y, z [rad/s]
  % data(1, 8:10)   Magnetometer; x, y, z [uT]
  % data(1, 11:13)  GPS [deg North, deg East, alt m]
  % data(1, 14)     Pressure [hPa]
  % data(1, 15)     Light [lux]
  % data(1, 16)     Proximity [cm]
  % data(1, 17)     Temperature [deg C]
  % data(1, 18:21)  Orientation [normalized quaternion]
  % data(1, 22:27)  Uncalibrated gyroscope; x, y, z, bx, by, bz [rad/s]
  % data(1, 28:33)  Uncalibrated magnetometer; x, y, z, bx, by, bz [uT]
  %
  % Nan values indicate missing data.
end
  1. Set the app to stream data to your computer’s IP address and the same port as the server, then press Start in the app.

Start the server before pressing Start in the Sensor Fusion App.

Read a Log File Offline in MATLAB

The following code reads a log file produced by the Sensor Fusion App into a large matrix. A similar method can also read a log file in an online-like manner; see the Java API reference for details.

import('se.hendeby.sensordata.*');
dataFile = FileSensorDataReader('sensorLog.txt');
dataFile.start();

% Normal measurement data
dataFile.reset();
data = dataFile.getAll(5); % Typically group things separated less than half a period

% Process data
% One row per time stamp

Re-Stream a Log File

The provided JAR file contains a program to stream a given log file to a server. It simulates measurements from a Sensor Fusion App running on a smartphone, based on measurements collected in a log file from the same app. Run the program instead of pressing Start in the Sensor Fusion App.

Usage:

java -classpath sensordata.jar se.hendeby.sensordata.StreamSensorDataFile [--ip <127.0.0.1>] [--port <3400>] [--] logfile