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 NameDescriptionviam-micro-server Support
SetPowerSet the portion of max power to send to the motor (between -1 and 1).

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

GoForSpin the motor the specified number of revolutions at specified revolutions per minute.

GoToTurn the motor to a specified position (in terms of revolutions from home/zero) at a specified speed in revolutions per minute (RPM).

ResetZeroPositionSet the current position (modified by offset) to be the new zero (home) position.

GetPositionReport the position of the motor based on its encoder.

GetPropertiesReport a dictionary mapping optional properties to whether it is supported by this motor.

IsPoweredReturn 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).

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

StopCut the power to the motor immediately, without any gradual step down.

ReconfigureReconfigure this resource.

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

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.

Be sure to import the motor package for the SDK you are using:

from viam.components.motor import Motor
import (
  "go.viam.com/rdk/components/motor"
)

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=robot, 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:

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

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.

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=robot, 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)

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.

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=robot, 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:

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

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.

Parameters:

Returns:

Example:

// Turn the motor 7.2 revolutions forward at 60 RPM.
await myMotor.goFor(60, 7.2);

For more information, see the Flutter 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=robot, 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:

Returns:

Example:

// Turn the motor to 8.3 revolutions from home at 75 RPM.
await myMotor.goTo(75, 8.3);

For more information, see the Flutter 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=robot, 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:

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=robot, 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:

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:

Example:

my_motor = Motor.from_robot(robot=robot, 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:

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, andthe second [1] value indicates the current power percentage of the motor. .

Example:

my_motor = Motor.from_robot(robot=robot, 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): True if the motor is currently running; false if 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:

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=robot, 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:

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

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=robot, 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.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

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

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:

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.FromRobot(machine, "my_motor")

err = myMotor.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.