Previous
Gantry
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 Name | Description | viam-micro-server Support |
---|---|---|
DoCommand | Execute model-specific commands. | |
GetResourceName | Get the ResourceName for this generic component with the given name. | |
Close | Safely shut down the resource and prevent further use. |
To get started using Viam’s SDKs to connect to and control your generic component and the rest of 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.
To show your machine’s API key in the sample code, toggle Include API key.
We strongly recommend that you add your API key and machine address as an environment variable. Anyone with these secrets can access your machine, and the computer running your machine.
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.
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>
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:
Raises:
Example:
my_generic_component = Generic.from_robot(robot=machine, name="my_generic_component")
command = {"cmd": "test", "data1": 500}
result = await my_generic_component.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:
Example:
myGenericComponent, err := generic.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.
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.
Safely shut down the resource and prevent further use.
Parameters:
Returns:
Example:
my_generic_component = Generic.from_robot(robot=machine, name="my_generic_component")
await my_generic_component.close()
For more information, see the Python SDK Docs.
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!