Generic Component
The generic component subtype enables you to add support for unique types of hardware that do not already have an appropriate API defined for them.
For example, when using an arm component, it makes sense to use the arm API, which provides specific functionality an arm component needs, such as moving to position or stopping movement. If you want to use an LED display, you need very different functionality that isn’t currently exposed in any API. Instead, you can use the generic component API to add support for your unique type of hardware, like LED displays, to your machine.
There are no built-in generic component models (other than fake
).
Use generic for a modular resource model that represents a unique type of hardware.
Important
The generic component API only supports the DoCommand
method.
If you use the generic subtype, your module needs to define any and all component functionality and pass it through DoCommand
.
Whenever possible, it is best to use an existing component API instead of generic so that you do not have to replicate code.
If you want to use most of an existing API but need just a few other functions, try using the DoCommand
endpoint and extra parameters to add custom functionality to an existing subtype, instead of using generic.
Supported Models
Before creating a new generic component, check whether one of the following modular resources supports your component.
Add support for other models
If none of the existing models fit your use case, you can create a modular resource to add support for it.
Built-in models
For configuration information, click on the model name:
Model | Description |
---|---|
fake | A model used for testing, with no physical hardware. |
Modular Resources
Search for additional generic models that you can add from the Viam Registry:
For configuration information, click on the model name:
Control your board with Viam’s client SDK libraries
To get started using Viam’s SDKs to connect to and control your robot, go to your robot’s page on the Viam app, navigate to the Code sample tab, select your preferred programming language, and copy the sample code generated.
API key and API key id
By default, the sample code does not include your robot 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 robot’s API key and API key id in the sample code, toggle Include secret on the Code sample tab. You can also see your API key and API key id on your robot’s Security tab.
Caution
Do not share your API key or robot address publicly. Sharing this information could compromise your system security by allowing unauthorized access to your robot, or to the computer running your robot.
When executed, this sample code will create a connection to your robot as a client.
Then control your robot programmatically by getting your generic
component from the robot with FromRobot
and adding API method calls, as shown in the following examples.
These examples assume you have a board called “my_board” configured as a component of your robot.
If your board has a different name, change the name
in the code.
Be sure to import the generic package for the SDK you are using:
from viam.components.generic import Generic
import (
"go.viam.com/rdk/components/generic"
)
API
The generic component supports the following method:
Method Name | Description |
---|---|
GetGeometries | Get all the geometries associated with the generic component in its current configuration, in the frame of the generic component. |
DoCommand | Send or receive model-specific commands. |
Close | Safely shut down the resource and prevent further use. |
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
(Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.timeout
(Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.
Returns:
- (List[Geometry]): The geometries associated with the generic component, in any order.
For more information, see the Python SDK Docs.
my_generic = Generic.from_robot(robot=robot, name="my_generic_component")
geometries = await my_generic.get_geometries()
if geometries:
# Get the center of the first geometry
print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")
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
.
Parameters:
command
(Dict[str, Any]): The command to execute.
Returns:
- (Dict[str, Any]): Result of the executed command.
my_generic = Generic.from_robot(robot=robot, name="my_generic_component")
raw_dict = {
"command": "raw",
"raw_input": "home"
}
await my_generic.do_command(raw_dict)
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{}): Result of the executed command.
- (error): An error, if one occurred.
myGeneric, err := generic.FromRobot(robot, "my_generic_component")
resp, err := myGeneric.DoCommand(ctx, map[string]interface{}{"command": "example"})
For more information, see the Go SDK Code.
Close
Safely shut down the resource and prevent further use.
Parameters:
- None
Returns:
- None
my_generic = Generic.from_robot(robot, "my_generic")
await my_generic.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. Close will never return an error for a generic resource.
myGeneric, err := generic.FromRobot(robot, "my_generic")
err := myGeneric.Close(ctx)
For more information, see the Go SDK Docs.
Troubleshooting
You can find additional assistance in the Troubleshooting section.
You can also ask questions in the Community Discord and we will be happy to help.
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!