Control your generic component with the generic API

The generic API allows you to give commands to your generic components for running model-specific commands using DoCommand.

The generic component supports the following method:

Method NameDescriptionviam-micro-server Support
DoCommandExecute model-specific commands.

GetGeometriesGet all the geometries associated with the generic component in its current configuration, in the frame of the generic component.

GetResourceNameGet the ResourceName for this generic component 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 will create a connection to your machine as a client. Then control your machine programmatically by getting your generic component from the machine with FromRobot and adding API method calls, as shown in the following examples.

The following examples assume you have a board called “my_board” configured as a component of your machine. If your board has a different name, change the name in the code.

Be sure to import the generic component package for the SDK you are using:

from viam.components.generic import Generic
import (
  "go.viam.com/rdk/components/generic"
)
#include <viam/sdk/components/generic/generic.hpp>

API

DoCommand

Execute model-specific commands. If you are implementing your own generic component 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, Any]): 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_generic_component.do_command(command)

For more information, see the Python SDK Docs.

Parameters:

Returns:

Example:

myGenericcomponent, err := generic_component.FromRobot(machine, "my_generic_component")

command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myGenericcomponent.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.

Parameters:

Returns:

auto my_generic = robot->resource_by_name<GenericComponent>("my_generic_component");
auto example = std::make_shared<ProtoType>(std::string("example"));
AttributeMap command =
    std::make_shared<std::unordered_map<std::string, std::shared_ptr<ProtoType>>>();
command->insert({{std::string("command"), example}});
auto resp = my_generic->do_command(command);

For more information, see the C++ SDK Docs

GetGeometries

Get all the geometries associated with the generic component in its current configuration, in the frame of the generic component. 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 my_generic_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.

GetResourceName

Get the ResourceName for this generic component with the given name.

Parameters:

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

Returns:

Example:

my_generic_component_name = Generic.get_resource_name("my_generic_component")

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 my_generic_component.close()

For more information, see the Python SDK Docs.

There is no need to explicitly close a generic component’s resource in C++, as resource destruction is handled automatically by the generic component’s class destructor when variables exit scope.

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.