Servo API

The servo API allows you to give commands to your servo components for controlling the angular position of a servo precisely or getting its current status.

The servo component supports the following methods:

Method NameDescriptionviam-micro-server Support
MoveMove the servo to the desired angle in degrees.

GetPositionGet the current set angle of the servo in degrees.

IsMovingReturns whether the servo is actively moving (or attempting to move) under its own power.

StopStop the servo from moving.

ReconfigureReconfigure this resource.

DoCommandExecute model-specific commands that are not otherwise defined by the component API.
GetResourceNameGet the ResourceName for this servo 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 servo 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.

When executed, this sample code creates a connection to your machine as a client.

The following examples assume you have a servo called "my_servo" configured as a component of your machine. If your servo has a different name, change the name in the code.

Import the servo package for the SDK you are using:

from viam.components.servo import Servo
import (
  "go.viam.com/rdk/components/servo"
)

API

Move

Move the servo to the desired angle in degrees.

Supported by viam-micro-server.

Parameters:

  • angle (int) (required): The desired angle of the servo in degrees.
  • 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:

  • None.

Example:

my_servo = Servo.from_robot(robot=machine, 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)

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.
  • 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.

Example:

// Move the servo from its origin to the desired angle of 30 degrees.
myServoComponent.Move(context.Background(), 30, nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

await myServo.move(30);

For more information, see the Flutter SDK Docs.

GetPosition

Get the current set angle of the servo in degrees. Supported by viam-micro-server.

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:

  • (int): The current angle of the servo in degrees.

Example:

my_servo = Servo.from_robot(robot=machine, 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()

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.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (uint32): The current set angle of the servo in degrees.
  • (error): An error, if one occurred.

Example:

// Get the current set angle of the servo.
pos1, err := myServoComponent.Position(context.Background(), nil)

// Move the servo from its origin to the desired angle of 20 degrees.
myServoComponent.Move(context.Background(), 20, nil)

// Get the current set angle of the servo.
pos2, err := myServoComponent.Position(context.Background(), nil)

logger.Info("Position 1: ", pos1)
logger.Info("Position 2: ", pos2)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var angle = await myServo.position();

For more information, see the Flutter SDK Docs.

IsMoving

Returns whether the servo is actively moving (or attempting to move) under its own power.

Parameters:

  • 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:

  • (bool): Whether the servo is moving.

Example:

my_servo = Servo.from_robot(robot=machine, name="my_servo")

print(await my_servo.is_moving())

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:

  • (bool): Whether this resource is moving (true) or not (false).
  • (error): An error, if one occurred.

Example:

// This example shows using IsMoving with an arm component.
myArm, err := arm.FromRobot(machine, "my_arm")

// Stop all motion of the arm. It is assumed that the arm stops immediately.
myArm.Stop(context.Background(), nil)

// Log if the arm is currently moving.
is_moving, err := myArm.IsMoving(context.Background())
logger.Info(is_moving)

For more information, see the Go SDK Docs.

Parameters:

  • None.

Returns:

Example:

var isItMoving = await myServo.isMoving();

For more information, see the Flutter SDK Docs.

Stop

Stop the servo from moving. Supported by viam-micro-server.

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:

  • None.

Example:

my_servo = Servo.from_robot(robot=machine, 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()

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.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (error): An error, if one occurred.

Example:

// This example shows using Stop with an arm component.
myArm, err := arm.FromRobot(machine, "my_arm")

// Stop all motion of the arm. It is assumed that the arm stops immediately.
err = myArm.Stop(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

await myServo.stop();

For more information, see the Flutter SDK Docs.

Reconfigure

Reconfigure this resource. Reconfigure must reconfigure the resource atomically and in place.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • deps (Dependencies): The resource dependencies.
  • conf (Config): The resource configuration.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

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:

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.

GetResourceName

Get the ResourceName for this servo with the given name.

Parameters:

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

Returns:

Example:

my_servo_name = Servo.get_resource_name("my_servo")

For more information, see the Python SDK Docs.

Parameters:

Returns:

Example:

// Example:
var name = Servo.getResourceName('myServo');

For more information, see the Flutter SDK Docs.

Close

Safely shut down the resource and prevent further use.

Parameters:

  • None.

Returns:

  • None.

Example:

my_servo = Servo.from_robot(robot=machine, name="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.

Example:

myServo, err := servo.FromRobot(machine, "my_servo")

err = myServo.Close(context.Background())

For more information, see the Go SDK Docs.

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.