Previous
Camera
The encoder API allows you to give commands to your encoder components for getting the position of a motor or a joint in ticks or degrees.
The encoder component supports the following methods:
Method Name | Description | viam-micro-server Support |
---|---|---|
GetPosition | Get the current position of the encoder in ticks or degrees. | |
ResetPosition | Set the current position of the encoder to be the new zero position. | |
GetProperties | Get a list of all the position types that are supported by a given encoder. | |
GetGeometries | Get all the geometries associated with the encoder in its current configuration, in the frame of the encoder. | |
Reconfigure | Reconfigure this resource. | |
DoCommand | Execute model-specific commands that are not otherwise defined by the component API. | |
GetResourceName | Get the ResourceName for this encoder 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 encoder 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 encoder called "my_encoder"
configured as a component of your machine.
If your encoder has a different name, change the name
in the code.
Import the encoder package for the SDK you are using:
from viam.components.encoder import Encoder
import (
"go.viam.com/rdk/components/encoder"
)
Get the current position of the encoder in ticks or degrees.
Relative encoders return ticks since last zeroing.
Absolute encoders return degrees.
Supported by viam-micro-server
.
Parameters:
position_type
(viam.proto.component.encoder.PositionType.ValueType) (optional): The desired output type of the position.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:
Example:
my_encoder = Encoder.from_robot(robot=machine, name='my_encoder')
# Get the position of the encoder in ticks
position = await my_encoder.get_position(PositionType.POSITION_TYPE_TICKS_COUNT)
print("The encoder position is currently ", position[0], position[1])
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.positionType
(PositionType): Specify whether to get the current position in ticks (encoder.PositionTypeTicks) or in degrees (encoder.PositionTypeDegrees
). If you are not sure which position type your encoder supports but it is a built-in Viam-supported model, you can leave this parameter unspecified (encoder.PositionTypeUnspecified
) and it will default to the correct position type.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
Example:
myEncoder, err := encoder.FromRobot(machine, "my_encoder")
if err != nil {
logger.Fatalf("cannot get encoder: %v", err)
}
// Get the position of the encoder in ticks
position, posType, err := myEncoder.Position(context.Background(), encoder.PositionTypeTicks, nil)
For more information, see the Go SDK Docs.
Set the current position of the encoder to be the new zero position.
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:
Example:
my_encoder = Encoder.from_robot(robot=machine, name='my_encoder')
# Reset the zero position of the encoder.
await my_encoder.reset_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:
Example:
myEncoder, err := encoder.FromRobot(machine, "my_encoder")
if err != nil {
logger.Fatalf("cannot get encoder: %v", err)
}
err = myEncoder.ResetPosition(context.Background(), nil)
For more information, see the Go SDK Docs.
Get a list of all the position types that are supported by a given encoder.
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:
Example:
my_encoder = Encoder.from_robot(robot=machine, name='my_encoder')
# Get whether the encoder returns position in ticks or degrees.
properties = await my_encoder.get_properties()
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:
Example:
myEncoder, err := encoder.FromRobot(machine, "my_encoder")
// Get whether the encoder returns position in ticks or degrees.
properties, err := myEncoder.Properties(context.Background(), nil)
For more information, see the Go SDK Docs.
Get all the geometries associated with the encoder in its current configuration, in the frame of the encoder. 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
(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:
Example:
my_encoder = Encoder.from_robot(robot=machine, name="my_encoder")
geometries = await my_encoder.get_geometries()
if geometries:
# Get the center of the first geometry
print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")
For more information, see the Python SDK Docs.
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:
For more information, see the Go SDK Docs.
Execute model-specific commands that are not otherwise defined by the component API.
If you are implementing your own encoder as a modular resource and are adding 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_encoder = Encoder.from_robot(robot=machine, name="my_encoder")
command = {"cmd": "test", "data1": 500}
result = await my_encoder.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:
myEncoder, err := encoder.FromRobot(machine, "my_encoder")
command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myEncoder.DoCommand(context.Background(), command)
For more information, see the Go SDK Docs.
Get the ResourceName
for this encoder with the given name.
Parameters:
name
(str) (required): The name of the Resource.Returns:
Example:
my_encoder_name = Encoder.get_resource_name("my_encoder")
For more information, see the Python SDK Docs.
Safely shut down the resource and prevent further use.
Parameters:
Returns:
Example:
my_encoder = Encoder.from_robot(robot=machine, name="my_encoder")
await my_encoder.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:
Example:
myEncoder, err := encoder.FromRobot(machine, "my_encoder")
err = myEncoder.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!