Sensor Fusion app Examples
This page contains examples of how to use the Sensor Fusion app:
This is a short example of how to streamdata to MATLAB from the Sensor Fusion app, more detailed instructions and a complete example application is available as part of these lab instructions.
- Download the zip archive with the support functions and unzip the files to your MATLAB path (eg, the current directory).
- Start MATLAB and run script startup.m.
- Execute the following code snippet starts a small server to receive measurements from the Sensor Fusion app in MATLAB and starts processing 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
- Make sure the Sensor Fusion app tires to connect to the correct IP and is set to stream data and press start to begin processing data.NB: It is essential to start the server before pressing start in the Sensor Fusion app.
Read a Log File offline in MATLAB
The following code snippet illustrates how to read in a log file produced by the Sensor Fusion app into a large matrix. A similar method can also be used to read the log file in an online-like manner. (See the jar-documentation for how.)
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. The program 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