Sensor Data Streaming
Control Sensors and Acquire Data
This example shows a simple workflow of controlling mobile device sensors and acquiring data from them.
This example assumes that you have already installed and set up MATLAB® Mobile™ on your mobile device and connected it to the MathWorks Cloud. If you have not, see Install MATLAB Mobile on Your Device.
Start MATLAB Mobile on your mobile device.
Sign in to the Cloud, if prompted.
On the Sensors screen, turn on the sensors that you want to send data from using the toggle switches.
In the sensor settings, tap Stream to and then select MATLAB.
Place the device where you want to acquire the sensor data.
In MATLAB, create a
mobiledevobject,m.m = mobiledev
m = mobiledev with properties: Connected: 1 Available Cameras: {'back' 'front'} Logging: 0 InitialTimestamp: '' AccelerationSensorEnabled: 1 AngularVelocitySensorEnabled: 1 MagneticSensorEnabled: 1 OrientationSensorEnabled: 1 PositionSensorEnabled: 1 Supported functionsIn the display, a value of
1means enabled or on, and0means not enabled or off. In this example, you can see that the device and the Cloud are connected, and all the sensors are enabled (from the Sensors screen), but data is not being logged yet. This device contains all five sensors, but your device might not. If your device does not have a particular sensor, that sensor always show a0in the display. The timestamp is empty because no data has been logged yet.InitialTimestampis the timestamp of the first data point received bymobiledev. All other timestamps for all sensors are relative toInitialTimestamp.Begin logging data from the selected sensors by setting the
Loggingproperty.m.Logging = 1
This action starts the transmission of data from all selected sensors. You can also start transmission by tapping the Start button in MATLAB Mobile on the device.
Now that you have started logging data, view the
mobiledevobject with thedispfunction.disp(m)
mobiledev with properties: Connected: 1 Available Cameras: {'back' 'front'} Logging: 1 InitialTimestamp: '06-08-2014 13:45:56.529' AccelerationSensorEnabled: 1 AngularVelocitySensorEnabled: 1 MagneticSensorEnabled: 1 OrientationSensorEnabled: 1 PositionSensorEnabled: 1 Current Sensor Values: Acceleration: [0.27 0.23 -10.19] (m/s^2) AngularVelocity: [-0.22 0.07 0.06] (rad/s) MagneticField: [3.56 1.56 -48.19] (microtesla) Orientation: [85.91 -27.1 0.35] (degrees) Position Data: Latitude: 41.29 (degrees) Longitude: -72.35 (degrees) Speed: 25 (m/s) Course: 83.6 (degrees) Altitude: 200.1 (m) HorizontalAccuracy: 9.0 (m) Supported functionsIn this display, you can see that the device and the Cloud are connected, and data is now being logged. You can also now see the
InitialTimestampproperty value, and the sensor values are displayed, indicating the current measurement value when you created the object.While you are logging data, you can display the current value of any sensor using the sensor reading properties. The
Acceleration,AngularVelocity,Orientation, andMagneticFieldproperties display the current readings from their respective sensors. If the Position sensor is logging, you can get individual position readings using theLatitude,Longitude,HorizontalAccuracy,Altitude,Course, andSpeedproperties.For example, to get the Acceleration sensor reading for object
m:m.Acceleration
ans = 0.6945 -0.2579 9.9338To get the longitude reading from the Position sensor:
m.Longitude
ans = -71.3517You can turn the sensors on and off using the sensor control properties in MATLAB. Using the control properties is the same as toggling the sensor buttons in MATLAB Mobile. Each control property has two possible values:
1for on or enabled, and0for off or disabled. For example, to turn off the Acceleration sensor from MATLAB:m.AccelerationSensorEnabled = 0
To turn back on the Acceleration sensor:
m.AccelerationSensorEnabled = 1
Stop logging sensor data.
m.Logging = 0
You can use the sensor reading properties to get the current value of a sensor while you are logging data, as shown in step 7. If you want to see the entire log of all readings, use the log functions. You can use these functions while you are still logging data, or after you stop. Each sensor type has a log function, for example,
accellogreturns the logged acceleration data from the Acceleration sensor.To get the logged acceleration data from object
m, assign the variableafor the logged acceleration data andtfor the timestamps.[a, t] = accellog(m);
You can then plot the data or do other data processing.
When you are done with the session, delete the object.
clear m