Developer Docs

API Reference

Complete code documentation for MRXcode. Control motors, read sensors, build autonomous routines, and more.

Motors

Control DC motors, smart motors, and servos with precision speed, position, and torque control.

Motor(port) Constructor

Creates a new motor object on the specified port.

Parameters

port int Motor port number (1-21)
Example
# Initialize a motor on port 1
left_motor = Motor(1)

# Initialize with reversed direction
right_motor = Motor(2, reversed=True)
spin(direction, velocity, units) Method

Spins the motor in the specified direction at the given velocity.

Parameters

direction DirectionType FORWARD or REVERSE
velocity float Speed value
units VelocityUnits PCT, RPM, or DPS
Example
# Spin motor forward at 50% speed
left_motor.spin(FORWARD, 50, PCT)

# Spin motor in reverse at 100 RPM
left_motor.spin(REVERSE, 100, RPM)
spin_to_position(position, units, velocity, wait) Method

Rotates the motor to an absolute position.

Parameters

position float Target position value
units RotationUnits DEG, REV, or RAW
velocity float Speed (default: 50)
wait bool Wait for completion (default: True)
Example
# Rotate to 90 degrees
arm_motor.spin_to_position(90, DEG)

# Rotate 2 full revolutions at 75% speed
arm_motor.spin_to_position(2, REV, 75)
stop(brake_type) Method

Stops the motor using the specified braking mode.

Parameters

brake_type BrakeType COAST, BRAKE, or HOLD
Example
# Stop and hold position
arm_motor.stop(HOLD)

# Stop and coast (no resistance)
drive_motor.stop(COAST)

Sensors

Read distance, touch, color, gyro, and other sensor values for robot awareness.

Distance(port) Constructor

Creates an ultrasonic distance sensor object.

Example
# Initialize distance sensor on port 5
front_distance = Distance(5)

# Read distance in millimeters
dist = front_distance.object_distance(MM)

# Check if object is detected
if front_distance.is_object_detected():
    print("Object found at", dist, "mm")
Gyro(port) Constructor

Creates a gyroscope sensor for rotation and heading tracking.

Example
# Initialize gyro sensor
gyro = Gyro(3)

# Calibrate before use
gyro.calibrate()

# Get current heading (0-360)
heading = gyro.heading()

# Get rotation from start
rotation = gyro.rotation()
Bumper(port) Constructor

Creates a touch/bumper sensor for collision detection.

Example
# Initialize bumper sensor
front_bumper = Bumper(7)

# Check if pressed
if front_bumper.pressing():
    drivetrain.stop()
    drivetrain.drive_for(REVERSE, 200, MM)
LineTracker(port) Constructor

Creates a line tracking sensor for following lines.

Example
# Initialize line trackers
left_line = LineTracker(1)
right_line = LineTracker(2)

# Get reflectivity (0-100)
left_val = left_line.reflectivity()

# Simple line following
while True:
    if left_line.reflectivity() > 50:
        drivetrain.turn(LEFT)
    elif right_line.reflectivity() > 50:
        drivetrain.turn(RIGHT)
    else:
        drivetrain.drive(FORWARD)

Drivetrain

High-level movement control for differential and holonomic drive systems.

Drivetrain(left_motor, right_motor, wheel_travel, track_width) Constructor

Creates a differential drivetrain from two motors or motor groups.

Example
# Create motor groups
left_motors = MotorGroup(Motor(1), Motor(2))
right_motors = MotorGroup(Motor(3, True), Motor(4, True))

# Initialize drivetrain (wheel: 319.19mm circumference, track: 320mm)
drivetrain = Drivetrain(left_motors, right_motors, 319.19, 320)
drive_for(direction, distance, units, velocity, wait) Method

Drives the robot for a specified distance.

Example
# Drive forward 500mm at 50% speed
drivetrain.drive_for(FORWARD, 500, MM, 50)

# Drive backward 12 inches
drivetrain.drive_for(REVERSE, 12, INCHES)
turn_for(direction, angle, units, velocity, wait) Method

Turns the robot by a specified angle.

Example
# Turn right 90 degrees
drivetrain.turn_for(RIGHT, 90, DEG)

# Turn left 45 degrees at 25% speed
drivetrain.turn_for(LEFT, 45, DEG, 25)
arcade(forward, turn) Method

Controls the drivetrain using arcade-style inputs.

Example
# Driver control loop
while True:
    forward = controller.axis3.position()
    turn = controller.axis1.position()
    drivetrain.arcade(forward, turn)
    wait(20, MSEC)

Vision

Computer vision APIs for object detection, color tracking, and AI recognition.

Vision(port, signatures) Constructor

Creates a vision sensor with color signatures for object detection.

Example
# Define color signatures
RED_BALL = Signature(1, 8973, 11143, 10058, -2119, -1053, -1586, 5.4, 0)
BLUE_BALL = Signature(2, -2477, -1387, -1932, 6561, 8421, 7491, 2.5, 0)

# Initialize vision sensor
vision = Vision(8, RED_BALL, BLUE_BALL)
take_snapshot(signature) Method

Captures current frame and returns detected objects matching the signature.

Example
# Take snapshot looking for red balls
vision.take_snapshot(RED_BALL)

# Check if objects found
if vision.object_count > 0:
    # Get largest object
    obj = vision.largest_object
    
    # Get object properties
    center_x = obj.centerX  # 0-316
    center_y = obj.centerY  # 0-212
    width = obj.width
    height = obj.height
    
    # Track object (center screen = 158)
    error = 158 - center_x
    drivetrain.arcade(30, error * 0.5)
AIVision(port) Constructor

Creates an AI-powered vision sensor for advanced object recognition.

Example
# Initialize AI Vision (MRX AIR platform)
ai_vision = AIVision(10)

# Detect game elements
ai_vision.take_snapshot(AI_GAME_ELEMENT)

for obj in ai_vision.objects:
    print(f"Found: {obj.label} at ({obj.centerX}, {obj.centerY})")
    print(f"Confidence: {obj.confidence}%")
    
# AprilTag detection for positioning
ai_vision.take_snapshot(AI_APRILTAG)
if ai_vision.object_count > 0:
    tag = ai_vision.largest_object
    print(f"Tag ID: {tag.id}, Distance: {tag.distance}mm")

Controller

Read joystick positions, button states, and display information on the controller screen.

Controller() Constructor

Creates a controller object for driver input.

Example
# Initialize controller
controller = Controller()

# Read joystick axes (-100 to 100)
forward = controller.axis3.position()  # Left Y
turn = controller.axis1.position()     # Right X

# Check button states
if controller.buttonA.pressing():
    intake.spin(FORWARD)
    
if controller.buttonB.pressing():
    intake.stop()

# Use button events
def on_button_r1_pressed():
    catapult.spin_for(FORWARD, 180, DEG)
    
controller.buttonR1.pressed(on_button_r1_pressed)
screen.print(text) Method

Displays text on the controller's LCD screen.

Example
# Display on controller screen
controller.screen.clear_screen()
controller.screen.set_cursor(1, 1)
controller.screen.print("Battery:", brain.battery.capacity(), "%")
controller.screen.next_row()
controller.screen.print("Temp:", left_motor.temperature(), "C")

Utilities

Timing functions, brain display, competition control, and system utilities.

wait(time, units) Function

Pauses program execution for the specified time.

Example
# Wait 1 second
wait(1, SECONDS)

# Wait 500 milliseconds
wait(500, MSEC)

# Control loop timing
while True:
    # Process inputs
    drivetrain.arcade(controller.axis3.position(), controller.axis1.position())
    wait(20, MSEC)  # 50Hz update rate
Timer() Constructor

Creates a timer for measuring elapsed time.

Example
# Create and use timer
match_timer = Timer()

# Reset timer
match_timer.reset()

# Check elapsed time
if match_timer.time(SECONDS) > 15:
    # Autonomous period over
    pass
    
# Time an operation
match_timer.reset()
drivetrain.drive_for(FORWARD, 1000, MM)
print("Drive took:", match_timer.time(MSEC), "ms")
Competition() Constructor

Handles competition state for autonomous and driver control periods.

Example
# Define autonomous routine
def autonomous():
    drivetrain.drive_for(FORWARD, 600, MM)
    drivetrain.turn_for(RIGHT, 90, DEG)
    intake.spin_for(FORWARD, 2, SECONDS)

# Define driver control
def driver_control():
    while True:
        drivetrain.arcade(
            controller.axis3.position(),
            controller.axis1.position()
        )
        wait(20, MSEC)

# Register callbacks
competition = Competition()
competition.autonomous(autonomous)
competition.driver_control(driver_control)

Additional Resources

Explore more documentation and learning materials

Ready to Start Coding?

Download MRXcode Studio and bring your robot to life