Sensors Service
The sensors service is a built-in service that provides a central interface to all of your robot’s sensors, regardless of the sensor model. With it you can obtain readings from multiple sensors on your robot at once.
API
The sensors service supports the following methods:
Method Name | Description |
---|---|
GetSensors | Return a list of names of the available sensors. |
GetReadings | Return a list of readings from a given list of sensors. |
Tip
The following code examples assume that you have a robot configured, and that you add the required code to connect to your robot and import any required packages at the top of your code file. Go to your robot’s Code sample tab on the Viam app for boilerplate code to connect to your robot.
GetSensors
Returns a list containing the name
of each available sensor.
Parameters:
extra
(Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
Returns:
- (List[ResourceName]): Names of the available sensors of the robot.
For more information, see the Python SDK Docs.
# Access the sensors service
sensors_svc = SensorsClient.from_robot(robot=robot, name="builtin")
# Get available sensors
sensors = await sensors_svc.get_sensors()
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:
- ([]resource.Name): An array of sensor names.
- (error): An error, if one occurred.
For more information, see the Go SDK Docs.
// Access the sensors service
sensorsService, err := sensors.FromRobot(robot, "builtin")
if err != nil {
logger.Fatal(err)
}
// Get available sensors
sensor_names, err := sensorsService.Sensors(context.Background(), nil)
GetReadings
Returns a list of sensor readings from a given list of sensors.
Parameters:
sensors
(ResourceName): An array of sensor names for which to return readings.extra
(Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
Returns:
- ([Mapping[ResourceName, Any]]): A list of readings from the requested sensors.
For more information, see the Python SDK Docs.
# Access the sensors service
sensors_svc = SensorsClient.from_robot(robot=robot, name="builtin")
# Get available sensors
sensors = await sensors_svc.get_sensors()
# Get readings for sensors
readings = await sensors_svc.get_readings(sensors)
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.sensorNames
([]resource.Name): An array of sensor names for which to return readings.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.
Returns:
- ([]Readings): A list of readings from the requested sensors.
- (error): An error, if one occurred.
For more information, see the Go SDK Docs.
// Access the sensors service
sensorsService, err := sensors.FromRobot(robot, "builtin")
if err != nil {
logger.Fatal(err)
}
// Get readings from all available sensors
sensor_names, err := sensorsService.Sensors(context.Background(), nil)
readings, err := sensorsService.Readings(context.Background(), sensor_names, nil)
Have questions, or want to meet other people working on robots? Join our Community Discord.
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!