Motor API
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. | |
GetGeometries | Get all the geometries associated with the motor in its current configuration, in the frame of the motor. | |
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. | |
Close | Safely shut down the resource and prevent further use. |
API
SetPower
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:
- None.
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-1and1. A value of1represents 100% power forwards, while a value of-1represents 100% power backwards.extra(map[string]interface{}): Extra options to pass to the underlying RPC call.
Returns:
- (error): An error, if one occurred.
Example:
myMotorComponent, err := motor.FromProvider(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:
power(number) (required): A value between -1 and 1 where negative values indicate a backwards direction and positive values a forward direction.extra(None) (optional)callOptions(CallOptions) (optional)
Returns:
- (Promise
)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Set the power to 40% forwards
await motor.setPower(0.4);
For more information, see the TypeScript SDK Docs.
Parameters:
Returns:
Example:
// Set the power to 40% forwards.
await myMotor.setPower(0.4);
For more information, see the Flutter SDK Docs.
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.
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:
- None.
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:
- (error): An error, if one occurred.
Example:
// Set the motor's RPM to 50
myMotorComponent.SetRPM(context.Background(), 50, nil)
For more information, see the Go SDK Docs.
Parameters:
rpm(number) (required): Speed in revolutions per minute.extra(None) (optional)callOptions(CallOptions) (optional)
Returns:
- (Promise
)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Spin the motor at 75 RPM
await motor.setRPM(75);
For more information, see the TypeScript 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.
GoFor
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:
- None.
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 is0, this runs the motor atrpmindefinitely. If revolutions not equal to0, 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:
- (error): An error, if one occurred.
Example:
myMotorComponent, err := motor.FromProvider(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.
Parameters:
rpm(number) (required): Speed in revolutions per minute.revolutions(number) (required): Number of revolutions relative to the motor’s starting position. If this value is 0, this will run the motor at the given rpm indefinitely. If this value is nonzero, this will block until the number of revolutions has been completed or another operation comes in.extra(None) (optional)callOptions(CallOptions) (optional)
Returns:
- (Promise
)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Turn the motor 7.2 revolutions at 60 RPM
await motor.goFor(60, 7.2);
For more information, see the TypeScript SDK Docs.
GoTo
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:
- None.
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:
- (error): An error, if one occurred.
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.
Parameters:
rpm(number) (required): Speed in revolutions per minute.positionRevolutions(number) (required): Number of revolutions relative to the motor’s home position.extra(None) (optional)callOptions(CallOptions) (optional)
Returns:
- (Promise
)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Turn the motor to 8.3 revolutions from home at 75 RPM
await motor.goTo(75, 8.3);
For more information, see the TypeScript SDK Docs.
ResetZeroPosition
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:
- None.
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:
- (error): An error, if one occurred.
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:
offset(number) (required): Position from which to offset the current position.extra(None) (optional)callOptions(CallOptions) (optional)
Returns:
- (Promise
)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Set the current position as the new home position with no offset
await motor.resetZeroPosition(0.0);
For more information, see the TypeScript 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.
GetPosition
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:
- (float): : Number of revolutions the motor is away from zero/home.
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:
- (float64): The unit returned is the number of revolutions which is intended to be fed back into calls of
GoFor. - (error): An error, if one occurred.
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:
extra(None) (optional)callOptions(CallOptions) (optional)
Returns:
- (Promise
)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Get the current position of the motor
const position = await motor.getPosition();
console.log('Position:', position);
For more information, see the TypeScript 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.
GetProperties
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:
- (viam.components.motor.motor.Motor.Properties): : Map of feature names to supported status.
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:
- (Properties): A map indicating whether or not the motor supports certain optional features.
- (error): An error, if one occurred.
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:
extra(None) (optional)callOptions(CallOptions) (optional)
Returns:
- (Promise<{ positionReporting: boolean }>)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Report a dictionary mapping optional properties to whether it is supported by
// this motor
const properties = await motor.getProperties();
console.log('Properties:', properties);
For more information, see the TypeScript 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.
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).
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:
- (Tuple[bool, float]): : A tuple containing two values; the first [0] value indicates whether the motor is currently powered, and the second [1] value indicates the current power percentage of the motor.
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:
- (bool):
Trueif the motor is currently running;falseif not. - (float64): The current portion of max power to the motor (between 0 and 1).
- (error): An error, if one occurred.
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:
extra(None) (optional)callOptions(CallOptions) (optional)
Returns:
- (Promise<readonly [boolean, number]>)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Check whether the motor is currently running
const [isPowered, powerPct] = await motor.isPowered();
console.log('Powered:', isPowered);
console.log('Power percentage:', powerPct);
For more information, see the TypeScript 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.
IsMoving
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:
- (bool): : Whether the motor is moving.
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.FromProvider(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:
callOptions(CallOptions) (optional)
Returns:
- (Promise
)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Check whether the motor is currently moving
const moving = await motor.isMoving();
console.log('Moving:', moving);
For more information, see the TypeScript SDK Docs.
Parameters:
Returns:
Example:
// Check whether the motor is moving.
var moving = await myMotor.isMoving();
For more information, see the Flutter SDK Docs.
Stop
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:
- None.
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:
- (error): An error, if one occurred.
Example:
// This example shows using Stop with an arm component.
myArm, err := arm.FromProvider(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:
extra(None) (optional)callOptions(CallOptions) (optional)
Returns:
- (Promise
)
Example:
const motor = new VIAM.MotorClient(machine, 'my_motor');
// Stop the motor
await motor.stop();
For more information, see the TypeScript SDK Docs.
Parameters:
Returns:
Example:
// Stop the motor.
await myMotor.stop();
For more information, see the Flutter SDK Docs.
GetGeometries
Get all the geometries associated with the motor in its current configuration, in the frame of the motor. 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:
- (List[viam.proto.common.Geometry]): : The geometries associated with the Component.
Example:
my_motor = Motor.from_robot(robot=machine, name="my_motor")
geometries = await my_motor.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
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.
Most models do not implement DoCommand.
Any available model-specific commands should be covered in the model’s documentation.
If you are implementing your own motor and want to add features that have no corresponding built-in API method, you can implement them with DoCommand.
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:
- (Mapping[str, viam.utils.ValueTypes]): : Result of the executed command.
Raises:
- (NotImplementedError): Raised if the Resource does not support arbitrary commands.
Example:
my_motor = Motor.from_robot(robot=machine, name="my_motor")
command = {"cmd": "test", "data1": 500}
result = await my_motor.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:
- (map[string]interface{}): The command response.
- (error): An error, if one occurred.
Example:
myMotor, err := motor.FromProvider(machine, "my_motor")
command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myMotor.DoCommand(context.Background(), command)
For more information, see the Go SDK Docs.
Parameters:
command(Struct) (required): The command to execute. Accepts either a Struct or a plain object, which will be converted automatically.callOptions(CallOptions) (optional)
Returns:
- (Promise<JsonValue>)
Example:
// Plain object (recommended)
const result = await resource.doCommand({
myCommand: { key: 'value' },
});
// Struct (still supported)
import { Struct } from '@viamrobotics/sdk';
const result = await resource.doCommand(
Struct.fromJson({ myCommand: { key: 'value' } })
);
For more information, see the TypeScript SDK Docs.
GetResourceName
Get the ResourceName for this motor.
Parameters:
name(str) (required): The name of the Resource.
Returns:
- (viam.proto.common.ResourceName): : The ResourceName of this Resource.
Example:
my_motor_name = Motor.get_resource_name("my_motor")
For more information, see the Python SDK Docs.
Parameters:
- None.
Returns:
Example:
myMotor, err := motor.FromProvider(machine, "my_motor")
err = myMotor.Name()
For more information, see the Go SDK Docs.
Parameters:
- None.
Returns:
- (string): The name of the resource.
Example:
motor.name
For more information, see the TypeScript SDK Docs.
Parameters:
nameString (required)
Returns:
Example:
var name = Motor.getResourceName('myMotor');
For more information, see the Flutter SDK Docs.
Close
Safely shut down the resource and prevent further use.
Parameters:
- None.
Returns:
- None.
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:
- (error): An error, if one occurred.
Example:
myMotor, err := motor.FromProvider(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!