Movement Sensor Component

A movement sensor component is a sensor that gives data on where a machine is and how fast it is moving. Examples of movement sensors include global positioning systems (GPS), inertial measurement units (IMUs), accelerometers and gyroscopes.

Supported models

To use your movement sensor component, check whether one of the following models supports it.

For configuration information, click on the model name:

Model
Description
ModelDescription
accel-adxl345The Analog Devices ADXL345 digital accelerometer
gyro-mpu6050A gyroscope/accelerometer manufactured by TDK InvenSense

Control your movement sensor with Viam’s client SDK libraries

To get started using Viam’s SDKs to connect to and control your machine, go to your machine’s page on the Viam app, navigate to the CONNECT tab’s Code sample page, select your preferred programming language, and copy the sample code generated.

When executed, this sample code will create a connection to your machine as a client. Then control your machine programmatically by adding API method calls as shown in the following examples.

These examples assume you have a movement sensor called "my_movement_sensor" configured as a component of your machine. If your movement sensor has a different name, change the name in the code.

Be sure to import the movement sensor package for the SDK you are using:

from viam.components.movement_sensor import MovementSensor
import (
  "go.viam.com/rdk/components/movementsensor"
)

API

Different movement sensors provide different data, so be aware that not all of the methods below are supported by all movement sensors.

Method NameDescriptionModels That Support This Methodmicro-RDK Support
GetPositionGet the current latitude, longitude and altitude.GPS models, wheeled-odometry

GetLinearVelocityGet the current linear velocity as a 3D vector.GPS models, wheeled-odometry

GetAngularVelocityGet the current angular velocity as a 3D vector.IMU models, gyro-mpu6050, and wheeled-odometry

GetLinearAccelerationGet the current linear acceleration as a 3D vector.IMU models, accel-adxl345, and gyro-mpu6050

GetCompassHeadingGet the current compass heading in degrees.GPS models

GetOrientationGet the current orientation.IMU models, wheeled-odometry

GetPropertiesGet the supported properties of this sensor.all models

GetAccuracyGet the accuracy of the various sensors.GPS models

GetReadingsObtain the measurements/data specific to this sensor.all models

GetGeometriesGet all the geometries associated with the movement sensor in its current configuration, in the frame of the movement sensor.all models

DoCommandSend or receive model-specific commands.all models

CloseSafely shut down the resource and prevent further use.all models

GetLinearVelocity

Report the current linear velocity in the x, y and z directions (as a 3D vector) in meters per second.

Supported by GPS models. Supported by the micro-RDK.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

Example:

my_movement_sensor = MovementSensor.from_robot(
    robot=robot, name="my_movement_sensor")

# Get the current linear velocity of the movement sensor.
lin_vel = await my_movement_sensor.get_linear_velocity()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (r3.Vector): A 3D vector containing three floats representing the linear velocity in the x, y and z directions in meters per second.
  • (error): An error, if one occurred.

Example:

// Get the current linear velocity of the movement sensor.
linVel, err := myMovementSensor.LinearVelocity(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var linVel = await myMovementSensor.linearVelocity();

For more information, see the Flutter SDK Docs.

GetAngularVelocity

Report the current angular velocity about the x, y and z axes (as a 3D vector) in degrees per second.

Supported by IMU models and by gyro-mpu6050. Supported by the micro-RDK.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

Example:

my_movement_sensor = MovementSensor.from_robot(
    robot=robot, name="my_movement_sensor")

# Get the current angular velocity of the movement sensor.
ang_vel = await my_movement_sensor.get_angular_velocity()

# Get the y component of angular velocity.
y_ang_vel = ang_vel.y

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (spatialmath.AngularVelocity): A 3D vector containing three floats representing the angular velocity about the x, y and z axes in degrees per second.
  • (error): An error, if one occurred.

Example:

// Get the current angular velocity of the movement sensor.
angVel, err := myMovementSensor.AngularVelocity(context.Background(), nil)

// Get the y component of angular velocity.
yAngVel := angVel.Y

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var angVel = await myMovementSensor.angularVelocity();

For more information, see the Flutter SDK Docs.

GetCompassHeading

Report the current compass heading in degrees.

Supported by GPS models. Supported by the micro-RDK.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (float): The compass heading in degrees.

Example:

my_movement_sensor = MovementSensor.from_robot(
    robot=robot, name="my_movement_sensor")

# Get the current compass heading of the movement sensor.
heading = await my_movement_sensor.get_compass_heading()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (float64): The compass heading in degrees (between 0 and 360).
  • (error): An error, if one occurred.

Example:

// Get the current compass heading of the movement sensor.
heading, err := myMovementSensor.CompassHeading(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var compassHeading = await myMovementSensor.compassHeading();

For more information, see the Flutter SDK Docs.

GetOrientation

Report the current orientation of the sensor.

Supported by IMU models.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

Example:

my_movement_sensor = MovementSensor.from_robot(
    robot=robot, name="my_movement_sensor")

# Get the current orientation vector of the movement sensor.
orientation = await my_movement_sensor.get_orientation()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (spatialmath.Orientation): Abstract base class for protocol messages, containing o_x, o_y, o_z, and theta, which together represent a vector pointing in the direction that the sensor is pointing, and the angle (theta) in degrees of the sensor’s rotation about that axis.
  • (error): An error, if one occurred.

Example:

// Get the current orientation of the movement sensor.
sensorOrientation, err := myMovementSensor.Orientation(context.Background(), nil)

// Get the orientation vector.
orientation := sensorOrientation.OrientationVectorDegrees()

// Print out the orientation vector.
logger.Info("The x component of the orientation vector: ", orientation.0X)
logger.Info("The y component of the orientation vector: ", orientation.0Y)
logger.Info("The z component of the orientation vector: ", orientation.0Z)
logger.Info("The number of degrees that the movement sensor is rotated about the vector: ", orientation.Theta)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var orientation = await myMovementSensor.orientation();

For more information, see the Flutter SDK Docs.

GetPosition

Report the current GeoPoint (latitude, longitude) and altitude (in meters).

Supported by GPS models. Supported by the micro-RDK.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Tuple[viam.components.movement_sensor.GeoPoint, float]): The current lat/long, along with the altitude in m.

Example:

my_movement_sensor = MovementSensor.from_robot(
    robot=robot,
    name="my_movement_sensor")

# Get the current position of the movement sensor.
position = await my_movement_sensor.get_position()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

Example:

// Get the current position of the movement sensor above sea level in meters.
position, altitude, err := myMovementSensor.Position(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var position = await myMovementSensor.position();

For more information, see the Flutter SDK Docs.

GetProperties

Get the supported properties of this sensor. Supported by the micro-RDK.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

Example:

my_movement_sensor = MovementSensor.from_robot(
    robot=robot, name="my_movement_sensor")

# Get the supported properties of the movement sensor.
properties = await my_movement_sensor.get_properties()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

Example:

// Get the supported properties of the movement sensor.
properties, err := myMovementSensor.Properties(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var props = await myMovementSensor.properties();

For more information, see the Flutter SDK Docs.

GetAccuracy

Get the reliability metrics of the movement sensor, including various parameters to assess the sensor’s accuracy and precision in different dimensions.

Supported by GPS models and imu-wit.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

Example:

my_movement_sensor = MovementSensor.from_robot(
    robot=robot, name="my_movement_sensor")

# Get the accuracy of the movement sensor.
accuracy = await my_movement_sensor.get_accuracy()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (*Accuracy): The precision and reliability metrics of the movement sensor, which vary depending on model. This type contains the following fields:

    • AccuracyMap (map[string]float32): A mapping of specific measurement parameters to their accuracy values. The keys are string identifiers for each measurement (for example, “Hdop”, “Vdop”), and the values are their corresponding accuracy levels as float32.
    • Hdop (float32): Horizontal Dilution of Precision (HDOP) value. It indicates the level of accuracy of horizontal measurements. Lower values indicate improved reliability of positional measurements.
    • Vdop (float32): Vertical Dilution of Precision (VDOP) value. Similar to HDOP, it denotes the accuracy level of vertical measurements. Lower values indicate improved reliability of positional measurements.
    • NmeaFix (int32): An integer value representing the quality of the NMEA fix. See Novatel documentation for the meaning of each fix value.
    • CompassDegreeError (float32): The estimated error in compass readings, measured in degrees. This signifies the deviation or uncertainty in the sensor’s compass measurements. A lower value implies a more accurate compass direction.
  • (error): An error, if one occurred.

Example:

// Get the accuracy of the movement sensor.
accuracy, err := myMovementSensor.Accuracy(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var accuracy = await myMovementSensor.accuracy();

For more information, see the Flutter SDK Docs.

GetLinearAcceleration

Report the current linear acceleration in the x, y and z directions (as a 3D vector) in meters per second per second.

Supported by IMU models, accel-adxl345, and gyro-mpu6050. Supported by the micro-RDK.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

Example:

my_movement_sensor = MovementSensor.from_robot(
    robot=robot, name="my_movement_sensor")

# Get the current linear acceleration of the movement sensor.
lin_accel = await my_movement_sensor.get_linear_acceleration()

# Get the x component of linear acceleration.
x_lin_accel = lin_accel.x

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (r3.Vector): A 3D vector containing three floats representing the linear acceleration in the x, y and z directions in meters per second per second (m/s2).
  • (error): An error, if one occurred.

Example:

// Get the current linear acceleration of the movement sensor.
linVel, err := myMovementSensor.LinearVelocity(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var linAccel = await myMovementSensor.linearAcceleration();

For more information, see the Flutter SDK Docs.

GetGeometries

Get all the geometries associated with the movement sensor in its current configuration, in the frame of the movement sensor. The motion and navigation services use the relative position of inherent geometries to configured geometries representing obstacles for collision detection and obstacle avoidance while motion planning.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

Example:

geometries = await component.get_geometries()

if geometries:
    # Get the center of the first geometry
    print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")

For more information, see the Python SDK Docs.

GetReadings

Get all the measurements/data from the sensor. Results depend on the sensor model and can be of any type. If a sensor is not configured to take a certain measurement or fails to read a piece of data, that data will not appear in the readings dictionary. Supported by the micro-RDK.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Mapping[str, viam.utils.SensorReading]): The readings for the MovementSensor. Can be of any type.

Example:

my_movement_sensor = MovementSensor.from_robot(
    robot=robot, name="my_movement_sensor")

# Get the latest readings from the movement sensor.
readings = await my_movement_sensor.get_readings()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (map[string]interface{}): A map containing the measurements from the sensor. Contents depend on sensor model and can be of any type.
  • (error): An error, if one occurred.

Example:

// Get the readings provided by the sensor.
readings, err := mySensor.Readings(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var readings = await myMovementSensor.readings();

For more information, see the Flutter SDK Docs.

Reconfigure

Reconfigure this resource. Reconfigure must reconfigure the resource atomically and in place.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • deps (Dependencies): The resource dependencies.
  • conf (Config): The resource configuration.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

DoCommand

Execute model-specific commands that are not otherwise defined by the component API. If you are implementing your own movement sensor and add features that have no built-in API method, you can access them with DoCommand. Supported by the micro-RDK.

Parameters:

  • command (Mapping[str, ValueTypes]) (required): The command to execute.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Mapping[str, viam.utils.ValueTypes]): Result of the executed command.

Raises:

  • (NotImplementedError): Raised if the Resource does not support arbitrary commands.

Example:

command = {"cmd": "test", "data1": 500}
result = component.do(command)

For more information, see the Python SDK Docs.

Parameters:

Returns:

Example:

// This example shows using DoCommand with an arm component.
myArm, err := arm.FromRobot(machine, "my_arm")

command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myArm.DoCommand(context.Background(), command)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

// Example using doCommand with an arm component
const command = {'cmd': 'test', 'data1': 500};
var result = myArm.doCommand(command);

For more information, see the Flutter SDK Docs.

FromRobot

Get the resource from the provided robot with the given name.

Parameters:

Returns:

For more information, see the Flutter SDK Docs.

GetResourceName

Get the ResourceName for this movement sensor with the given name.

Parameters:

  • name (str) (required): The name of the Resource.

Returns:

Example:

# Can be used with any resource, using an arm as an example
my_arm_name = my_arm.get_resource_name("my_arm")

For more information, see the Python SDK Docs.

Parameters:

Returns:

For more information, see the Flutter SDK Docs.

Close

Safely shut down the resource and prevent further use.

Parameters:

  • None.

Returns:

  • None.

Example:

await component.close()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.

Returns:

  • (error): An error, if one occurred.

Example:

// This example shows using Close with an arm component.
myArm, err := arm.FromRobot(machine, "my_arm")

err = myArm.Close(ctx)

For more information, see the Go SDK Docs.

Troubleshooting

You can find additional assistance in the Troubleshooting section.

You can also ask questions on the Viam Community Slack and we will be happy to help.

Next steps

Try adding a movement sensor to your mobile robot and writing some code with our SDKs to implement closed-loop movement control for your machine.

Or, try configuring data capture on your movement sensor.

You can also ask questions in the Community Discord and we will be happy to help.