Previous
Input Controller
The motor API allows you to give commands to your motor components for operating a motor or getting its current status.
The motor component supports the following methods:
Method Name | Description | viam-micro-server Support |
---|---|---|
SetPower | Set the portion of max power to send to the motor (between -1 and 1 ). | |
SetRPM | Spin the motor indefinitely at the specified speed, in revolutions per minute. If rpm is positive, the motor will spin forwards, and if rpm is negative, the motor will spin backwards. | |
GoFor | Spin the motor the specified number of revolutions at specified revolutions per minute. | |
GoTo | Turn the motor to a specified position (in terms of revolutions from home/zero) at a specified speed in revolutions per minute (RPM). | |
ResetZeroPosition | Set the current position (modified by offset ) to be the new zero (home) position. | |
GetPosition | Report the position of the motor based on its encoder. | |
GetProperties | Report a dictionary mapping optional properties to whether it is supported by this motor. | |
IsPowered | Return whether or not the motor is currently running, and the portion of max power (between 0 and 1 ; if the motor is off the power will be 0 ). | |
IsMoving | Return whether the motor is actively moving (or attempting to move) under its own power. | |
Stop | Cut the power to the motor immediately, without any gradual step down. | |
Reconfigure | Reconfigure this resource. | |
DoCommand | Execute model-specific commands that are not otherwise defined by the component API. | |
GetResourceName | Get the ResourceName for this motor 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 motor 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 a motor called "my_motor"
configured as a component of your machine.
If your motor has a different name, change the name
in the code.
Import the motor package for the SDK you are using:
from viam.components.motor import Motor
import (
"go.viam.com/rdk/components/motor"
)
Set the portion of max power to send to the motor (between -1
and 1
).
A value of 1
represents 100% power forwards, while a value of -1
represents 100% power backwards.
Power is expressed as a floating point between -1
and 1
that scales between -100% and 100% power.
Supported by viam-micro-server
.
Parameters:
power
(float) (required): Power between -1 and 1 (negative implies backwards).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_motor = Motor.from_robot(robot=machine, name="my_motor")
# Set the power to 40% forwards.
await my_motor.set_power(power=0.4)
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.powerPct
(float64): Portion of full power to send to the motor expressed as a floating point between -1
and 1
. A value of 1
represents 100% power forwards, while a value of -1
represents 100% power backwards.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
Example:
myMotorComponent, err := motor.FromRobot(machine, "my_motor")
// Set the motor power to 40% forwards.
myMotorComponent.SetPower(context.Background(), 0.4, nil)
For more information, see the Go SDK Docs.
Parameters:
Returns:
Example:
// Set the power to 40% forwards.
await myMotor.setPower(0.4);
For more information, see the Flutter SDK Docs.
Spin the motor indefinitely at the specified speed, in revolutions per minute. If rpm
is positive, the motor will spin forwards, and if rpm
is negative, the motor will spin backwards.
Parameters:
rpm
(float) (required): Speed at which the motor should rotate.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_motor = Motor.from_robot(robot=machine, name="my_motor")
# Spin the motor at 75 RPM.
await my_motor.set_rpm(rpm=75)
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.rpm
(float64): The speed in RPM for the motor to move at.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
Example:
// Set the motor's RPM to 50
myMotorComponent.SetRPM(context.Background(), 50, nil)
For more information, see the Go SDK Docs.
Parameters:
Returns:
Example:
// Set the motor to turn backwards at 120.5 RPM.
await myMotor.setRPM(-120.5);
For more information, see the Flutter SDK Docs.
Spin the motor the specified number of revolutions at specified revolutions per minute.
When rpm
or revolutions
is a negative value, the motor spins in the backward direction.
If both rpm
and revolutions
are negative, the motor spins in the forward direction.
Parameters:
rpm
(float) (required): Speed at which the motor should move in rotations per minute (negative implies backwards).revolutions
(float) (required): Number of revolutions the motor should run for (negative implies backwards).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_motor = Motor.from_robot(robot=machine, name="my_motor")
# Turn the motor 7.2 revolutions at 60 RPM.
await my_motor.go_for(rpm=60, revolutions=7.2)
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.rpm
(float64): Speed at which the motor should move in revolutions per minute (negative implies backwards).revolutions
(float64): Number of revolutions the motor should run for (negative implies backwards). If revolutions is 0
, this runs the motor at rpm
indefinitely. If revolutions not equal to 0
, this blocks until the number of revolutions has been completed or another operation comes in.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
Example:
myMotorComponent, err := motor.FromRobot(machine, "my_motor")
// Turn the motor 7.2 revolutions at 60 RPM.
myMotorComponent.GoFor(context.Background(), 60, 7.2, nil)
For more information, see the Go SDK Docs.
Turn the motor to a specified position (in terms of revolutions from home/zero) at a specified speed in revolutions per minute (RPM).
Regardless of the directionality of the rpm
, the motor will move towards the specified target position.
This blocks until the position has been reached.
Parameters:
rpm
(float) (required): Speed at which the motor should rotate (absolute value).position_revolutions
(float) (required): Target position relative to home/zero, in revolutions.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_motor = Motor.from_robot(robot=machine, name="my_motor")
# Turn the motor to 8.3 revolutions from home at 75 RPM.
await my_motor.go_to(rpm=75, revolutions=8.3)
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.rpm
(float64): Speed at which the motor should move in revolutions per minute (absolute value).positionRevolutions
(float64): Target position relative to home/zero, in revolutions.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
Example:
// Turn the motor to 8.3 revolutions from home at 75 RPM.
myMotorComponent.GoTo(context.Background(), 75, 8.3, nil)
For more information, see the Go SDK Docs.
Set the current position (modified by offset
) to be the new zero (home) position.
Parameters:
offset
(float) (required): The offset from the current position to new home/zero 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_motor = Motor.from_robot(robot=machine, name="my_motor")
# Set the current position as the new home position with no offset.
await my_motor.reset_zero_position(offset=0.0)
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.offset
(float64): The offset from the current position to the new home (zero) position.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
Example:
// Set the current position as the new home position with no offset.
myMotorComponent.ResetZeroPosition(context.Background(), 0.0, nil)
For more information, see the Go SDK Docs.
Parameters:
Returns:
Example:
// Set the current position as the new home position with no offset.
await myMotor.resetZeroPosition(0.0);
For more information, see the Flutter SDK Docs.
Report the position of the motor based on its encoder.
The value returned is the number of revolutions relative to its zero position.
This method raises an exception if position reporting is not supported by the motor.
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_motor = Motor.from_robot(robot=machine, name="my_motor")
# Get the current position of the motor.
position = await my_motor.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:
GoFor
.Example:
// Get the current position of an encoded motor.
position, err := myMotorComponent.Position(context.Background(), nil)
// Log the position
logger.Info("Position:")
logger.Info(position)
For more information, see the Go SDK Docs.
Parameters:
Returns:
Example:
// Get the current position of an encoded motor.
var position = await myMotor.position();
For more information, see the Flutter SDK Docs.
Report a dictionary mapping optional properties to whether it is supported by this motor.
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_motor = Motor.from_robot(robot=machine, name="my_motor")
# Report a dictionary mapping optional properties to whether it is supported by
# this motor.
properties = await my_motor.get_properties()
# Print out the properties.
print(f'Properties: {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:
// Return whether or not the motor supports certain optional features.
properties, err := myMotorComponent.Properties(context.Background(), nil)
// Log the properties.
logger.Info("Properties:")
logger.Info(properties)
For more information, see the Go SDK Docs.
Parameters:
Returns:
Example:
// Return whether the motor supports certain optional features
var properties = await myMotor.properties();
For more information, see the Flutter SDK Docs.
Return whether or not the motor is currently running, and the portion of max power (between 0
and 1
; if the motor is off the power will be 0
).
Stepper motors will report true
if they are being powered while holding a position, as well as when they are turning.
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_motor = Motor.from_robot(robot=machine, name="my_motor")
# Check whether the motor is currently running.
powered = await my_motor.is_powered()
print('Powered: ', powered)
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:
True
if the motor is currently running; false
if not.Example:
// Check whether the motor is currently running.
powered, pct, err := myMotorComponent.IsPowered(context.Background(), nil)
logger.Info("Is powered?")
logger.Info(powered)
logger.Info("Power percent:")
logger.Info(pct)
For more information, see the Go SDK Docs.
Parameters:
Returns:
Example:
// Check whether the motor is currently powered and
// check the percentage of max power to the motor.
var powerState = await myMotor.powerState();
var powered = powerState.isOn;
var pct = powerState.powerPct;
For more information, see the Flutter SDK Docs.
Return whether the motor is actively moving (or attempting to move) under its own power.
Supported by viam-micro-server
.
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:
Example:
my_motor = Motor.from_robot(robot=machine, name="my_motor")
# Check whether the motor is currently moving.
moving = await my_motor.is_moving()
print('Moving: ', 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:
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:
Returns:
Example:
// Check whether the motor is moving.
var moving = await myMotor.isMoving();
For more information, see the Flutter SDK Docs.
Cut the power to the motor immediately, without any gradual step down.
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_motor = Motor.from_robot(robot=machine, name="my_motor")
# Stop the motor.
await my_motor.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:
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:
// Stop the motor.
await myMotor.stop();
For more information, see the Flutter 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.
For built-in models, model-specific commands are covered with each model’s documentation.
If you are implementing your own motor and add features that have no built-in API method, you can access them with DoCommand
.
Get the ResourceName
for this motor with the given name.
Parameters:
name
(str) (required): The name of the Resource.Returns:
Example:
my_motor_name = Motor.get_resource_name("my_motor")
For more information, see the Python SDK Docs.
Parameters:
name
String (required)Returns:
Example:
var name = Motor.getResourceName('myMotor');
For more information, see the Flutter SDK Docs.
Safely shut down the resource and prevent further use.
Parameters:
Returns:
Example:
my_motor = Motor.from_robot(robot=machine, name="my_motor")
await my_motor.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:
myMotor, err := motor.FromRobot(machine, "my_motor")
err = myMotor.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!