Servo Component
The servo component supports “RC” or “hobby” servo motors. These are small motors with built-in potentiometer position sensors, enabling you to control the angular position of the servo precisely.
As servos can use a lot of power, drawing voltage away from a board, you should power your servo with its own power supply in most cases.
The following shows an example wiring diagram for a hobby servo wired to a pi
board:
The colors of the servo wires in this diagram may not match your servo. Refer to your servo’s data sheet for wiring specifications.
Most robots with a servo need at least the following hardware:
- A board component that can run
viam-server
- A servo
- A power supply for the board
- A power supply for the servo
Related Services
Tip
The Viam servo component supports hobby servos.
If your motor is coupled with an encoder, not a potentiometer, for position feedback, you should not configure it as a servo. Check your device’s data sheet and configure that type of servo as an encoded motor.
Supported Models
To use your servo with Viam, check whether one of the following built-in models supports your servo.
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. |
gpio | A hobby servo wired to any model of board besides pi . |
pi | A hobby servo wired to a Raspberry Pi board. |
If none of these models fit your use case, you can create a modular resource to add support for it.
Control your servo 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 adding API method calls as shown in the following examples.
These examples assume you have a servo called "my_servo"
configured as a component of your robot.
If your servo has a different name, change the name
in the code.
Be sure to import the servo package for the SDK you are using:
from viam.components.servo import Servo
import (
"go.viam.com/rdk/components/servo"
)
API
The servo component supports the following methods:
Method Name | Description |
---|---|
Move | Move the servo to the desired angle. |
GetPosition | Get the current angle of the servo. |
Stop | Stop the servo. |
GetGeometries | Get all the geometries associated with the servo in its current configuration, in the frame of the servo. |
DoCommand | Send or receive model-specific commands. |
Close | Safely shut down the resource and prevent further use. |
Move
Move the servo to the desired angle in degrees.
Stability Notice
Support for continuous servos with the GPIO servo model is experimental. Stability is not guaranteed. Breaking changes are likely to occur, and occur often.
If you are using a continuous rotation servo, you can use the Move
command, but instead of moving to a given position, the servo will start moving at a set speed.
The speed will be related to the “angle” you pass in as a linear approximation. 90 degrees represents stop, 91 to 180 represents counter-clockwise rotation from slowest to fastest, and 89 to 1 represents clockwise from slowest to fastest. It is recommended that you test your servo to determine the desired speed.
Parameters:
angle
(int): The desired angle of the servo in degrees.extra
(Optional[Mapping[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:
- None
For more information, see the Python SDK Docs.
my_servo = Servo.from_robot(robot=robot, name="my_servo")
# Move the servo from its origin to the desired angle of 10 degrees.
await my_servo.move(10)
# Move the servo from its origin to the desired angle of 90 degrees.
await my_servo.move(90)
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.angleDeg
(uint32): The desired angle of the servo in degrees.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.
Returns:
- (error): An error, if one occurred.
For more information, see the Go SDK Docs.
myServo, err := servo.FromRobot(robot, "my_servo")
// Move the servo from its origin to the desired angle of 10 degrees.
myServo.Move(context.Background(), 10, nil)
// Move the servo from its origin to the desired angle of 90 degrees.
myServo.Move(context.Background(), 90, nil)
GetPosition
Get the current set angle of the servo in degrees.
Parameters:
extra
(Optional[Mapping[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:
- (int): The current set angle of the servo in degrees.
For more information, see the Python SDK Docs.
my_servo = Servo.from_robot(robot=robot, name="my_servo")
# Move the servo from its origin to the desired angle of 10 degrees.
await my_servo.move(10)
# Get the current set angle of the servo.
pos1 = await my_servo.get_position()
# Move the servo from its origin to the desired angle of 20 degrees.
await my_servo.move(20)
# Get the current set angle of the servo.
pos2 = await my_servo.get_position()
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.
myServo, err := servo.FromRobot(robot, "my_servo")
// Move the servo from its origin to the desired angle of 10 degrees.
myServo.Move(context.Background(), 10, nil)
// Get the current set angle of the servo.
pos1, err = myServo.Position(context.Background(), nil)
// Move the servo from its origin to the desired angle of 20 degrees.
myServo.Move(context.Background(), 20, nil)
// Get the current set angle of the servo.
pos2, err = myServo.Position(context.Background(), nil)
Stop
Stop the servo from moving.
Parameters:
extra
(Optional[Mapping[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:
- None.
For more information, see the Python SDK Docs.
my_servo = Servo.from_robot(robot=robot, name="my_servo")
# Move the servo from its origin to the desired angle of 10 degrees.
await my_servo.move(10)
# Stop the servo. It is assumed that the servo stops moving immediately.
await my_servo.stop()
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:
- (error): An error, if one occurred.
For more information, see the Go SDK Docs.
myServo, err := servo.FromRobot(robot, "my_servo")
// Move the servo from its origin to the desired angle of 10 degrees.
myServo.Move(context.Background(), 10, nil)
// Stop the servo. It is assumed that the servo stops moving immediately.
myServo.Stop(context.Background(), nil)
GetGeometries
Get all the geometries associated with the servo in its current configuration, in the frame of the servo. 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 servo, in any order.
For more information, see the Python SDK Docs.
my_servo = Servo.from_robot(robot=robot, name="my_servo")
geometries = await my_servo.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 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 servo 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_servo = Servo.from_robot(robot, "my_servo")
command = {"cmd": "test", "data1": 500}
result = my_servo.do(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:
- (map[string]interface{}): Result of the executed command.
- (error): An error, if one occurred.
myServo, err := servo.FromRobot(robot, "my_servo")
command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myServo.DoCommand(context.Background(), command)
For more information, see the Go SDK Code.
Close
Safely shut down the resource and prevent further use.
Parameters:
- None
Returns:
- None
my_servo = Servo.from_robot(robot, "my_servo")
await my_servo.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.
myServo, err := servo.FromRobot(robot, "my_servo")
err := myServo.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.
Next Steps
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!