Documentation

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

  1. Connect to the MathWorks Cloud in Settings.

    1. In MATLAB Mobile™ on your device, go to Settings > Connect to MathWorks Cloud.

    2. If you have previously connected from MATLAB Mobile:

      1. Tap your user ID.

      2. Tap Connect.

    3. If you have not previously connected from MATLAB Mobile:

      1. Tap Log In.

      2. Sign in to your MathWorks Account when prompted.

  2. Place the device where you want to acquire the sensor data.

  3. 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 are 0) and data has not been logged yet (InitialTimestamp property is empty). If your device does not have a particular sensor, that sensor always shows a 0 in the display.

    InitialTimestamp is the timestamp of the first data point received by mobiledev. All other timestamps for all sensors are relative to InitialTimestamp.

  4. 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.

  5. 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
  6. 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.

  7. Use mobiledev properties to control Android sensor acquisition.

  8. 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.

PropertyDescription and Values
Connected

Shows the status of the connection between MATLAB Mobile and the mobiledev object in MATLAB. Read-only. Possible values are 0 for not connected and 1 for connected. To display the status of connection, for a mobiledev object m:

m.Connected

Logging

Shows and controls 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

To turn logging on or off:

m.Logging = 1 or m.Logging = 0

InitialTimestamp

Time in milliseconds, in dd-mm-yyyy HH:MM:SS.FFF format, when the first data point was transferred from the device to the mobiledev object. To display the initial timestamp for a mobiledev object m:

m.InitialTimestamp

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 'high', 'medium', and 'low', which have the following values:

high 100 Hz

medium 10 Hz

low 1 Hz

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 X, Y, and Z coordinates, in m/s^2 (meters per second squared).

Acceleration is defined in relation to the X, Y and Z axes. If you place the phone face-up on a table, the positive X-axis extends out of the right side of the phone, positive Y-axis extends out of the top side, and the positive Z-axis extends out of the front face of the phone. This is independent of the orientation of the phone.

AngularVelocity

Current angular velocity reading in X, Y, and Z rotations, in radians per second.

Angular Velocity is defined in relation to the X, Y and Z axes and in standard right-hand rotational vector notation. If you place the phone face-up on a table, the positive X-axis extends out of the right side of the phone, positive Y-axis extends out of the top side, and the positive Z-axis extends out of the front face of the phone. This is independent of the orientation of the phone.

Orientation

Current orientation reading in X, Y, and Z coordinates, in degrees, for azimuth, pitch, and roll.

Orientation is defined in relation to the X, Y and Z axes. If you place the phone face-up on a table, the positive X-axis extends out of the right side of the phone, positive Y-axis extends out of the top side, and the positive Z-axis extends out of the front face of the phone. This is independent of the orientation of the phone.

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 X, Y, and Z coordinates, in microtesla.

Magnetic field is defined in relation to the X, Y and Z axes. If you place the phone face-up on a table, the positive X-axis extends out of the right side of the phone, positive Y-axis extends out of the top side, and the positive Z-axis extends out of the front face of the phone. This is independent of the orientation of the phone.

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 meters per second.

AccelerationSensorEnabled

Turns Acceleration sensor on or off.

To turn sensor on or off:

m.AccelerationSensorEnabled = 1

m.AccelerationSensorEnabled = 0

AngularVelocitySensorEnabled

Turns Angular Velocity sensor on or off.

To turn sensor on or off:

m.AngularVelocitySensorEnabled = 1

m.AngularVelocitySensorEnabled = 0

MagneticSensorEnabled

Turns Magnetic Field sensor on or off.

To turn sensor on or off:

m.MagneticSensorEnabled = 1

m.MagneticSensorEnabled = 0

OrientationSensorEnabled

Turns Orientation sensor on or off.

To turn sensor on or off:

m.OrientationSensorEnabled = 1

m.OrientationSensorEnabled = 0

PositionSensorEnabled

Turns Position sensor on or off.

To turn sensor on or off:

m.PositionSensorEnabled = 1

m.PositionSensorEnabled = 0

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 (MATLAB Mobile for Android) 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 Hz

  • medium – 10 Hz

  • low – 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.

See Also

Related Topics