Previous
Servo
The switch API allows you to give commands to your switch components for reading the state of a physical switch that has multiple discrete positions. A simple switch has two positions, and a knob could have any number of positions.
The switch component supports the following methods:
Method Name | Description | viam-micro-server Support |
---|---|---|
SetPosition | Set the position of the switch. | |
GetPosition | Return the current position of the switch. | |
GetNumberOfPositions | Return the number of valid positions for this switch. | |
DoCommand | Execute model-specific commands that are not otherwise defined by the component API. | |
Close | Safely shut down the resource and prevent further use. |
To get started using Viam’s SDKs to connect to and control your switch 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 creates a connection to your machine as a client.
The following examples assume you have an switch called "my_switch"
configured as a component of your machine.
If your switch has a different name, change the name
in the code.
Import the switch package for the SDK you are using:
import (
"go.viam.com/rdk/components/switch"
)
Set the position of the switch. Position must be within the valid range for the switch type.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.position
(uint32)extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
For more information, see the Go SDK Docs.
Return the current position of the switch.
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:
For more information, see the Go SDK Docs.
Return the number of valid positions for this switch.
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:
For more information, see the Go SDK Docs.
Execute model-specific commands that are not otherwise defined by the component API.
For built-in models, model-specific commands are covered with each model’s documentation.
If you are implementing your own switch and add features that have no built-in API method, you can access them with DoCommand
.
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:
mySwitch, err := switch.FromRobot(machine, "my_switch")
command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := mySwitch.DoCommand(context.Background(), command)
For more information, see the Go SDK Docs.
Safely shut down the resource and prevent further use.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.Returns:
Example:
mySwitch, err := switch.FromRobot(machine, "my_switch")
err = mySwitch.Close(context.Background())
For more information, see the Go 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!