viam.components.motor
Submodules
Classes
Motor represents a physical motor. |
Package Contents
- class viam.components.motor.Motor(name: str, *, logger: logging.Logger | None = None)[source]
Bases:
viam.components.component_base.ComponentBase
Motor represents a physical motor.
This acts as an abstract base class for any drivers representing specific motor implementations. This cannot be used on its own. If the
__init__()
function is overridden, it must call thesuper().__init__()
function.from viam.components.motor import Motor
For more information, see Motor component.
- SUBTYPE: Final
The Subtype of the Resource
- abstract set_power(power: float, *, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs)[source]
- Async:
Sets the “percentage” of power the motor should employ between -1 and 1. When
power
is negative, the rotation will be in the backward direction.my_motor = Motor.from_robot(robot=machine, name="my_motor") # Set the power to 40% forwards. await my_motor.set_power(power=0.4)
- Parameters:
power (float) – Power between -1 and 1 (negative implies backwards).
For more information, see Motor component.
- abstract go_for(rpm: float, revolutions: float, *, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs)[source]
- Async:
Spin the motor the specified number of
revolutions
at specifiedrpm
. Whenrpm
orrevolutions
is a negative value, the rotation will be in the backward direction. Note: if bothrpm
andrevolutions
are negative, the motor will spin in the forward direction.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)
- Parameters:
rpm (float) – Speed at which the motor should move in rotations per minute (negative implies backwards).
revolutions (float) – Number of revolutions the motor should run for (negative implies backwards).
For more information, see Motor component.
- abstract go_to(rpm: float, position_revolutions: float, *, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs)[source]
- Async:
Spin the motor to the specified position (provided in revolutions from home/zero), at the specified speed, in revolutions per minute. Regardless of the directionality of the
rpm
this function will move the motor towards the specified position.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)
- Parameters:
rpm (float) – Speed at which the motor should rotate (absolute value).
position_revolutions (float) – Target position relative to home/zero, in revolutions.
For more information, see Motor component.
- abstract set_rpm(rpm: float, *, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs)[source]
- Async:
Spin the motor indefinitely at the specified speed, in revolutions per minute. Positive
rpm
will result in forward movement and negativerpm
will result in backwards movementmy_motor = Motor.from_robot(robot=machine, name="my_motor") # Spin the motor at 75 RPM. await my_motor.set_rpm(rpm=75)
- Parameters:
rpm (float) – Speed at which the motor should rotate.
For more information, see Motor component.
- abstract reset_zero_position(offset: float, *, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs)[source]
- Async:
Set the current position (modified by
offset
) to be the new zero (home) position.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)
- Parameters:
offset (float) – The offset from the current position to new home/zero position.
For more information, see Motor component.
- abstract get_position(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs) float [source]
- Async:
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 will raise an exception if position reporting is not supported by the motor.
my_motor = Motor.from_robot(robot=machine, name="my_motor") # Get the current position of the motor. position = await my_motor.get_position()
- Returns:
Number of revolutions the motor is away from zero/home.
- Return type:
float
For more information, see Motor component.
- abstract get_properties(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs) Properties [source]
- Async:
Report a dictionary mapping optional properties to whether it is supported by this motor.
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}')
- Returns:
Map of feature names to supported status.
- Return type:
For more information, see Motor component.
- abstract stop(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs)[source]
- Async:
Stop the motor immediately, without any gradual step down.
my_motor = Motor.from_robot(robot=machine, name="my_motor") # Stop the motor. await my_motor.stop()
For more information, see Motor component.
- abstract is_powered(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs) Tuple[bool, float] [source]
- Async:
Returns whether or not the motor is currently running.
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)
- Returns:
- 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.
- Return type:
Tuple[bool, float]
For more information, see Motor component.
- abstract is_moving() bool [source]
- Async:
Get if the motor is currently moving.
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)
- Returns:
Whether the motor is moving.
- Return type:
bool
For more information, see Motor component.
- classmethod from_robot(robot: viam.robot.client.RobotClient, name: str) typing_extensions.Self
Get the component named
name
from the provided robot.- Parameters:
robot (RobotClient) – The robot
name (str) – The name of the component
- Returns:
The component, if it exists on the robot
- Return type:
Self
- abstract do_command(command: Mapping[str, ValueTypes], *, timeout: float | None = None, **kwargs) Mapping[str, ValueTypes]
- Async:
Send/Receive arbitrary commands to the Resource
command = {"cmd": "test", "data1": 500} result = await component.do_command(command)
- Parameters:
command (Mapping[str, ValueTypes]) – The command to execute
- Raises:
NotImplementedError – Raised if the Resource does not support arbitrary commands
- Returns:
Result of the executed command
- Return type:
Mapping[str, ValueTypes]
- async get_geometries(*, extra: Dict[str, Any] | None = None, timeout: float | None = None) List[viam.proto.common.Geometry]
Get all geometries associated with the component, in their current configuration, in the frame of the component.
geometries = await component.get_geometries() if geometries: # Get the center of the first geometry print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")
- Returns:
The geometries associated with the Component.
- Return type:
List[Geometry]
- classmethod get_resource_name(name: str) viam.proto.common.ResourceName
Get the ResourceName for this Resource with the given name
# Can be used with any resource, using an arm as an example my_arm_name = Arm.get_resource_name("my_arm")
- Parameters:
name (str) – The name of the Resource
- Returns:
The ResourceName of this Resource
- Return type:
- get_operation(kwargs: Mapping[str, Any]) viam.operations.Operation
Get the
Operation
associated with the currently running function.When writing custom resources, you should get the
Operation
by calling this function and check to see if it’s cancelled. If theOperation
is cancelled, then you can perform any necessary (terminating long running tasks, cleaning up connections, etc. ).- Parameters:
kwargs (Mapping[str, Any]) – The kwargs object containing the operation
- Returns:
The operation associated with this function
- Return type:
- async close()
Safely shut down the resource and prevent further use.
Close must be idempotent. Later configuration may allow a resource to be “open” again. If a resource does not want or need a close function, it is assumed that the resource does not need to return errors when future non-Close methods are called.
await component.close()