Repository navigation
Api Documentation ‐ Motor
carl wheezer with dreads edited this page Sep 24, 2025
·
1 revision
The motor module provides functions and constants for controlling motors connected to the hub.
To use the motor module, you can require it as follows:
local motor = require("src.modules.motor")
local Port = require("src.modules.hub.ports") -- For accessing port constantsThe motor module defines several constants for movement status, stop behaviors, and directions.
| Constant | Value | Description |
|---|---|---|
motor.READY |
0 |
Movement is ready. |
motor.RUNNING |
1 |
Movement is in progress. |
motor.STALLED |
2 |
Movement has stalled. |
motor.CANCELED |
3 |
Movement was canceled. |
motor.ERROR |
4 |
An error occurred. |
motor.DISCONNECTED |
5 |
The motor was disconnected. |
| Constant | Value | Description |
|---|---|---|
motor.COAST |
10 |
Motor will coast freely. |
motor.BRAKE |
11 |
Motor will actively brake. |
motor.HOLD |
12 |
Motor will hold its current position. |
motor.CONTINUE |
13 |
Motor will continue running. |
motor.SMART_COAST |
14 |
Brake then coast and compensate. |
motor.SMART_BRAKE |
15 |
Brake and compensate. |
| Constant | Value | Description |
|---|---|---|
motor.CLOCKWISE |
20 |
Turn clockwise. |
motor.COUNTERCLOCKWISE |
21 |
Turn counter-clockwise. |
motor.SHORTEST_PATH |
22 |
Turn via the shortest path. |
motor.LONGEST_PATH |
23 |
Turn via the longest path. |
Gets the absolute position of a Motor.
-
port(number): A port from thePortmodule (e.g.,Port.A). -
Returns (
number): The absolute position in degrees.
local absPos = motor.absolute_position(Port.A)
print("Motor A absolute position:", absPos)Gets the current PWM duty cycle of a Motor.
-
port(number): A port from thePortmodule. -
Returns (
number): The PWM value.
local pwm = motor.get_duty_cycle(Port.B)
print("Motor B PWM duty cycle:", pwm)Gets the relative position of a Motor.
-
port(number): A port from thePortmodule. -
Returns (
number): The relative position in degrees.
local relPos = motor.relative_position(Port.C)
print("Motor C relative position:", relPos)Resets the motor's relative position counter.
-
port(number): A port from thePortmodule. -
position(number): The degree to set as the new zero position. -
Returns (
void)
motor.reset_relative_position(Port.A, 0) -- Set current position of Motor A as 0 degreesStarts a Motor at a constant speed.
-
port(number): A port from thePortmodule. -
velocity(number): The velocity in degrees/sec. -
acceleration?(number): The acceleration (deg/sec²) from 1-10000 (optional). -
Returns (
void)
motor.run(Port.A, 360, 5000) -- Run Motor A at 360 deg/sec with 5000 deg/sec² accelerationTurns a motor for a specific number of degrees.
-
port(number): A port from thePortmodule. -
degrees(number): The number of degrees to turn. -
velocity(number): The velocity in degrees/sec. -
stop?(number): The behavior after stopping (e.g.,motor.BRAKE) (optional). -
acceleration?(number): The acceleration (deg/sec²) (optional). -
deceleration?(number): The deceleration (deg/sec²) (optional). -
Returns (
number): A status constant likemotor.READYormotor.STALLED.
local status = motor.run_for_degrees(Port.B, 720, 180, motor.BRAKE)
if status == motor.READY then
print("Motor B finished turning 720 degrees.")
endRuns a Motor for a limited amount of time.
-
port(number): A port from thePortmodule. -
duration(number): The duration in milliseconds. -
velocity(number): The velocity in degrees/sec. -
stop?(number): The behavior after stopping (e.g.,motor.BRAKE) (optional). -
acceleration?(number): The acceleration (deg/sec²) (optional). -
deceleration?(number): The deceleration (deg/sec²) (optional). -
Returns (
number): A status constant likemotor.READYormotor.STALLED.
local status = motor.run_for_time(Port.C, 2000, 90, motor.COAST)
if status == motor.READY then
print("Motor C ran for 2 seconds.")
endmotor.run_to_absolute_position(port, position, velocity, direction, stop, acceleration, deceleration)
Turns a motor to an absolute position.
-
port(number): A port from thePortmodule. -
position(number): The absolute degree to turn to. -
velocity(number): The velocity in degrees/sec. -
direction?(number): The direction to turn (e.g.,motor.SHORTEST_PATH) (optional). -
stop?(number): The behavior after stopping (e.g.,motor.BRAKE) (optional). -
acceleration?(number): The acceleration (deg/sec²) (optional). -
deceleration?(number): The deceleration (deg/sec²) (optional). -
Returns (
number): A status constant likemotor.READYormotor.STALLED.
local status = motor.run_to_absolute_position(Port.A, 90, 100, motor.SHORTEST_PATH, motor.HOLD)
if status == motor.READY then
print("Motor A reached absolute position 90 degrees.")
endTurns a motor to a position relative to its current position.
-
port(number): A port from thePortmodule. -
position(number): The relative degree to turn to. -
velocity(number): The velocity in degrees/sec. -
stop?(number): The behavior after stopping (e.g.,motor.BRAKE) (optional). -
acceleration?(number): The acceleration (deg/sec²) (optional). -
deceleration?(number): The deceleration (deg/sec²) (optional). -
Returns (
number): A status constant likemotor.READYormotor.STALLED.
local status = motor.run_to_relative_position(Port.B, 180, 200, motor.BRAKE)
if status == motor.READY then
print("Motor B turned 180 degrees relative to its current position.")
endStarts a Motor with a specific PWM value.
-
port(number): A port from thePortmodule. -
pwm(number): The PWM value (-10000 to 10000). -
Returns (
void)
motor.set_duty_cycle(Port.C, 5000) -- Set Motor C to 50% power forwardStops a motor.
-
port(number): A port from thePortmodule. -
stop?(number): The stop behavior (e.g.,motor.BRAKE) (optional). -
Returns (
void)
motor.stop(Port.A, motor.BRAKE) -- Stop Motor A with braking