SLAM service API

The SLAM service API allows you to get a machine’s position within a map.

The SLAM service supports the following methods:

Method NameDescription
GetPositionGet the current position of the component the SLAM service is configured to source point cloud data from in the SLAM map as a Pose.
GetPointCloudMapGet the point cloud map.
GetInternalStateGet the internal state of the SLAM algorithm required to continue mapping/localization.
GetPropertiesGet information about the current SLAM session.
InternalStateFullInternalStateFull concatenates the streaming responses from InternalState into the internal serialized state of the SLAM algorithm.
PointCloudMapFullPointCloudMapFull concatenates the streaming responses from PointCloudMap into a full point cloud.
ReconfigureReconfigure this resource.
DoCommandExecute model-specific commands that are not otherwise defined by the service API.
GetResourceNameGet the ResourceName for this instance of the SLAM service with the given name.
CloseSafely shut down the resource and prevent further use.

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.

When executed, this sample code creates a connection to your machine as a client.

The following code examples assume that you have a machine configured with a SLAM service called "my_slam_service".

from viam.services.slam import SLAMClient
import (
  "go.viam.com/rdk/services/slam"
)

API

GetPosition

Get the current position of the component the SLAM service is configured to source point cloud data from in the SLAM map as a Pose.

Parameters:

  • 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:

slam_svc = SLAMClient.from_robot(robot=robot, name="my_slam_service")

# Get the current position of the specified source component in the SLAM map as a Pose.
pose = await slam.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.

Returns:

Example:

// Get the current position of the specified source component
// in the SLAM map as a Pose.
pos, name, err := mySLAMService.Position(context.Background())

For more information, see the Go SDK Docs.

GetPointCloudMap

Get the point cloud map.

Parameters:

  • return_edited_map (bool) (required): signal to the SLAM service to return an edited map, if the map package contains one and if the SLAM service supports the feature.
  • 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:

  • (List[bytes]): Complete pointcloud in standard PCD format. Chunks of the PointCloud, concatenating all GetPointCloudMapResponse.point_cloud_pcd_chunk values.

Example:

slam_svc = SLAMClient.from_robot(robot=robot, name="my_slam_service")

# Get the point cloud map in standard PCD format.
pcd_map = await slam_svc.get_point_cloud_map()

For more information, see the Python SDK Docs.

GetInternalState

Get the internal state of the SLAM algorithm required to continue mapping/localization.

Parameters:

  • 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:

  • (List[bytes]): Chunks of the internal state of the SLAM algorithm.

Example:

slam = SLAMClient.from_robot(robot=robot, name="my_slam_service")

# Get the internal state of the SLAM algorithm required to continue mapping/localization.
internal_state = await slam.get_internal_state()

For more information, see the Python SDK Docs.

GetProperties

Get information about the current SLAM session.

Parameters:

  • 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.services.slam.slam.SLAM.Properties): The properties of SLAM.

Example:

slam_svc = SLAMClient.from_robot(robot=robot, name="my_slam_service")

# Get the properties of your current SLAM session.
slam_properties = await slam_svc.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.

Returns:

  • (Properties): Information about the current SLAM session. An object containing four fields:

  • SensorInfo (SensorInfo[]): Information about the sensors (camera and movement sensor) configured for your SLAM service, including the name and type of sensor.

  • CloudSlam (bool): A boolean which indicates whether the session is being run in the cloud.

  • MappingMode (MappingMode): Represents the form of mapping and localizing the current session is performing. This includes creating a new map, localizing on an existing map and updating an existing map.

  • InternalStateFileType (string): The file type the service’s internal state algorithm is stored in.

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

Example:

// Get the properties of your current SLAM session
properties, err := mySLAMService.Properties(context.Background())

For more information, see the Go SDK Docs.

InternalStateFull

InternalStateFull concatenates the streaming responses from InternalState into the internal serialized state of the SLAM algorithm.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • slamSvc (Service): The SLAM service name to fetch the internal state for.

Returns:

  • ([]byte): A byte value representing the internal serialized state of the SLAM algorithm.
  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

PointCloudMapFull

PointCloudMapFull concatenates the streaming responses from PointCloudMap into a full point cloud.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • slamSvc (Service): The SLAM service name to fetch the point cloud map for.
  • returnEditedMap (bool): A boolean representing whether to return the edited map (true) or not (false).

Returns:

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 service API. For built-in service models, any model-specific commands available are covered with each model’s documentation. If you are implementing your own SLAM service and add features that have no built-in API method, you can access them with DoCommand.

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.

Example:

service = SLAMClient.from_robot(robot, "my_slam_svc")

my_command = {
  "cmnd": "dosomething",
  "someparameter": 52
}

# Can be used with any resource, using the motion service as an example
await service.do_command(command=my_command)

For more information, see the Python SDK Docs.

Parameters:

Returns:

Example:

mySlam, err := slam.FromRobot(machine, "my_slam")

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

For more information, see the Go SDK Docs.

GetResourceName

Get the ResourceName for this instance of the SLAM service with the given name.

Parameters:

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

Returns:

Example:

my_slam_svc_name = SLAMClient.get_resource_name("my_slam_svc")

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_slam_svc.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:

mySlamSvc, err := slam.FromRobot(machine, "my_slam_svc")

err = mySlamSvc.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.