Stream Sensor Data with mobiledev.logging
Property
Stream data to MATLAB® running on the MathWorks® Cloud with a WiFi or cellular connection. To communicate with the
Android™ device and acquire data from the sensors, you create the
mobiledev
object. You cannot log the sensor data until you create
the object.
Stream Sensor Data
Sign in to your MathWorks Account, if prompted.
Place the device where you want to acquire the sensor data.
In MATLAB, create a
mobiledev
object,m
.m = mobiledev
mobiledev with properties: Connected: 1 Available Cameras: {'back' 'front'} Logging: 0 InitialTimestamp: '' AccelerationSensorEnabled: 0 AngularVelocitySensorEnabled: 0 MagneticSensorEnabled: 0 OrientationSensorEnabled: 0 PositionSensorEnabled: 0 Supported functions
In the display that is shown when you create the object:
0
- Not enabled, off, or the sensor is not available on this device.1
- Enabled or on.
In this example, you can see that the device and the
mobiledev
object are connected, but the sensors are not enabled (all sensor values are0
) and data has not been logged yet (InitialTimestamp
property is empty). If your device does not have a particular sensor, that sensor always shows a0
in the display.InitialTimestamp
is the timestamp of the first data point received bymobiledev
. All other timestamps for all sensors are relative toInitialTimestamp
.Enable one or more sensors.
m.MagneticSensorEnabled=1
If you are using the position sensor, turn on Location access for MATLAB in Settings on your device.
You can begin logging data from the selected sensors by enabling the
Logging
property.m.Logging = 1
This action starts transmitting data from all selected sensors.
After you start logging data, a display of the object shows current values:
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 functions
In this display, you can see that the device and the Cloud are connected, and data is now being logged. Also, the
InitialTimestamp
property value and the sensor values are displayed, indicating the current measurement value when you created the object.Use
mobiledev
properties to control Android sensor acquisition.To stop streaming sensor data, enter:
m.Logging=0
Use Properties to Control Android Sensor Acquisition
MATLAB Mobile™ includes many properties that display current sensor values, enable and disable sensors from MATLAB, turn logging on and off, set sample rate, and show connection and logging status.
You can use the following properties with the mobiledev
object.
Property | Description and Values |
---|---|
Connected | Shows the status of the connection between MATLAB
Mobile and the
|
Logging | Shows and controls the status of data transfer from the
device to MATLAB. Possible values are
To turn logging on or off:
|
InitialTimestamp | Time in milliseconds, in
|
SampleRate | Sample rate of the acquisition from your device, in Hz. The
value is a double and must be in the range of 0 to 100 Hz. The
default sample rate is 10 Hz. In addition to setting a specific
numeric value, you can also use the character vector values
Note that due to hardware limitations on Android devices, the sample rate does not always exactly match what you set it to, especially on the low end. |
Acceleration | Current acceleration reading in Acceleration
is defined in relation to the |
AngularVelocity | Current angular velocity reading in Angular Velocity is defined in
relation to the |
Orientation
| Current orientation reading in Orientation is defined in relation to the
Azimuth is the angle between the positive Y-axis and magnetic north, and its range is between -180 and 180 degrees. Positive Roll is defined when the phone is lying flat on a surface and the positive Z-axis tilts towards the positive X-axis, and its range is between -180 and 180 degrees. Positive Pitch is defined when the phone is lying flat on a surface and the positive Z-axis tilts towards the positive Y-axis, and its range is between -90 and 90 degrees. |
MagneticField | Current magnetic field reading in Magnetic field is defined in relation
to the |
Latitude
| Current latitude reading, in degrees relative to the equator, with positive values indicating latitudes north of the equator. |
Longitude
| Current longitude reading, in degrees relative to the zero meridian, with positive values extending east of the meridian. |
HorizontalAccuracy
| Current horizontal accuracy, in meters defined by a circle around the latitude and longitude. |
Altitude
| Current altitude reading, in meters above sea level. |
Course | Current course reading, in degrees relative to true north. |
Speed | Current speed reading, in m/s (meters per second). |
AccelerationSensorEnabled | Turns Acceleration sensor on or off. To turn sensor on or off:
|
AngularVelocitySensorEnabled | Turns Angular Velocity sensor on or off. To turn sensor on or off:
|
MagneticSensorEnabled | Turns Magnetic Field sensor on or off. To turn sensor on or off:
|
OrientationSensorEnabled | Turns Orientation sensor on or off. To turn sensor on or off:
|
PositionSensorEnabled | Turns Position sensor on or off. To turn sensor on or off:
|
Object Properties
The Connected
property is read-only and shows the status of the
connection between MATLAB
Mobile and the mobiledev
object in MATLAB. Possible values
are 0
for not connected and 1
for
connected.
To display the status of the connection, for a mobiledev
object
m
:
m.Connected
The Logging
property shows the status of data transfer from the
device to MATLAB. Possible values are 0
for not logging and
1
for logging.
To display the status of logging, for a mobiledev
object
m
:
m.Logging
The Logging
property is also used to enable and disable
logging. To begin logging data from the selected sensors, enable the
property.
m.Logging = 1
This action starts the transmitting of data from all selected sensors. You can also start transmission by tapping the Start button in MATLAB Mobile on the device.
You stop logging data from the selected sensors by disabling the
Logging
property.
m.Logging = 0
This action stops the transmitting of data from all selected sensors. You can also stop transmission by tapping the Stop button in MATLAB Mobile on the device.
Sensor Reading Properties
Use the sensor reading properties to display the current reading of the sensor. At
any time you can display the current reading of any of the sensors that are being
logged. The Acceleration
, AngularVelocity
,
Orientation
, and MagneticField
properties
display the current readings from their respective sensors. If the Position sensor
is logging, you can get individual position readings using the
Latitude
, Longitude
,
Speed
, Course
,
Altitude
, and HorizontalAccuracy
properties.
To get the current value from a sensor, use
<objectname>.<propertyname>
. For example, to get
the acceleration reading, for object m
:
m.Acceleration
To get the longitude reading from the Position sensor:
m.Longitude
See Device Orientation
for a diagram showing the X
, Y
, and
Z
axes relative to the device.
Sensor Control Properties
MATLAB
Mobile has five properties to turn the five sensors on and off from MATLAB.
You can perform the same actions by selecting or deselecting the sensor buttons in
MATLAB
Mobile on the device. Each control property has two possible values:
1
for on or enabled, and 0
for off or
disabled.
For example, to turn the Acceleration sensor on:
m.AccelerationSensorEnabled = 1
To turn the Acceleration sensor off:
m.AccelerationSensorEnabled = 0
Sample Rate Property
This property is the sample rate of the data acquisition from your device, in Hz. The value is a double and must be a positive number in the range of 0 to 100 Hz. The default sample rate is 10 Hz.
In addition to setting a specific numeric value, you can also use the character
vector values 'high'
, 'medium'
, and
'low'
. These settings use the following values:
high
– 100 Hzmedium
– 10 Hzlow
– 1 Hz
You can change the sample rate even after you start logging data. For example, to
change the sample rate to a specific value in Hz for mobiledev
object m
:
m.SampleRate = 100
To change the sample rate to one of the character vector values:
m.SampleRate = 'low'
Note
Note that due to hardware limitations on Android devices, the sample rate does not always exactly match what you set it to, especially on the low end.