Movement sensor API
The movement sensor API allows you to give commands to your movement sensor components for getting a GPS location, linear velocity and acceleration, angular velocity and acceleration and heading.
Different movement sensors provide different data, so be aware that not all of the methods below are supported by all movement sensors.
Tip
You can run GetProperties
on your sensor for a list of its supported methods.
Method Name | Description | Models That Support This Method | viam-micro-server Support |
---|---|---|---|
GetPosition | Get the current latitude, longitude and altitude. | GPS models, wheeled-odometry | |
GetLinearVelocity | Get the current linear velocity as a 3D vector. | GPS models, wheeled-odometry | |
GetAngularVelocity | Get the current angular velocity as a 3D vector. | IMU models, gyro-mpu6050 , and wheeled-odometry | |
GetLinearAcceleration | Get the current linear acceleration as a 3D vector. | IMU models, accel-adxl345 , and gyro-mpu6050 | |
GetCompassHeading | Get the current compass heading in degrees. | GPS models | |
GetOrientation | Get the current orientation. | IMU models, wheeled-odometry | |
GetProperties | Get the supported properties of this sensor. | all models | |
GetAccuracy | Get the accuracy of the various sensors. | GPS models | |
GetReadings | Obtain the measurements/data specific to this sensor. | all models | |
DoCommand | Send or receive model-specific commands. | all models | |
Close | Safely shut down the resource and prevent further use. | all models |
Establish a connection
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.
API key and API key ID
By default, the sample code does not include your machine API key and API key ID. We strongly recommend that you add your API key and API key ID as an environment variable and import this variable into your development environment as needed.
To show your machine’s API key and API key ID in the sample code, toggle Include API key on the CONNECT tab’s Code sample page.
Caution
Do not share your API key or machine address publicly. Sharing this information could compromise your system security by allowing unauthorized access to your machine, or to the computer running your machine.
When executed, this sample code creates a connection to your machine as a client.
The following 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
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 viam-micro-server
.
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:
- (viam.components.movement_sensor.Vector3): The linear velocity in m/sec.
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 viam-micro-server
.
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:
- (viam.components.movement_sensor.Vector3): The angular velocity in degrees/sec.
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 viam-micro-server
.
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:
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:
- (viam.components.movement_sensor.Orientation): The orientation.
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
, andtheta
, 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.OX)
logger.Info("The y component of the orientation vector: ", orientation.OY)
logger.Info("The z component of the orientation vector: ", orientation.OZ)
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 viam-micro-server
.
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:
- (*geo.Point): Contains the current latitude and longitude as floats.
- (float64): The altitude in meters.
- (error): An error, if one occurred.
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();
var altitude = position.altitude;
var coordinates = position.coordinates;
For more information, see the Flutter SDK Docs.
GetProperties
Get the supported properties of this sensor.
Supported by viam-micro-server
.
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:
- (viam.components.movement_sensor.movement_sensor.MovementSensor.Properties): The properties.
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:
- (*Properties): The supported properties of the movement sensor.
- (error): An error, if one occurred.
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:
- (MovementSensor.Accuracy): The accuracies of the movement sensor.
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 viam-micro-server
.
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:
- (viam.components.movement_sensor.Vector3): The linear acceleration in m/sec^2.
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.
linAcc, err := myMovementSensor.LinearAcceleration(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.
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 viam-micro-server
.
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.
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 viam-micro-server
.
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 = await my_movement_sensor.do_command(command)
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.cmd
(map[string]interface{}): The command to execute.
Returns:
- (map[string]interface{}): The command response.
- (error): An error, if one occurred.
Example:
myMovementSensor, err := movement_sensor.FromRobot(machine, "my_movement_sensor")
command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myMovementSensor.DoCommand(context.Background(), command)
For more information, see the Go SDK Docs.
GetResourceName
Get the ResourceName
for this movement sensor with the given name.
Parameters:
name
(str) (required): The name of the Resource.
Returns:
- (viam.proto.common.ResourceName): The ResourceName of this Resource.
Example:
my_movement_sensor_name = MovementSensor.get_resource_name("my_movement_sensor")
For more information, see the Python SDK Docs.
Close
Safely shut down the resource and prevent further use.
Parameters:
- None.
Returns:
- None.
Example:
await my_movement_sensor.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:
myMovementSensor, err := movementsensor.FromRobot(machine, "my_movement_sensor")
err = myMovementSensor.Close(context.Background())
For more information, see the Go SDK Docs.
Have questions, or want to meet other people working on robots? Join our Community Discord.
If you notice any issues with the documentation, feel free to file an issue or edit this file.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!