Complete code documentation for MRXcode. Control motors, read sensors, build autonomous routines, and more.
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.
port |
int | Motor port number (1-21) |
# 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.
direction |
DirectionType | FORWARD or REVERSE |
velocity |
float | Speed value |
units |
VelocityUnits | PCT, RPM, or DPS |
# 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.
position |
float | Target position value |
units |
RotationUnits | DEG, REV, or RAW |
velocity |
float | Speed (default: 50) |
wait |
bool | Wait for completion (default: True) |
# 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.
brake_type |
BrakeType | COAST, BRAKE, or HOLD |
# Stop and hold position
arm_motor.stop(HOLD)
# Stop and coast (no resistance)
drive_motor.stop(COAST)
Read distance, touch, color, gyro, and other sensor values for robot awareness.
Distance(port)
Constructor
Creates an ultrasonic distance sensor object.
# 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.
# 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.
# 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.
# 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)
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.
# 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.
# 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.
# 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.
# Driver control loop
while True:
forward = controller.axis3.position()
turn = controller.axis1.position()
drivetrain.arcade(forward, turn)
wait(20, MSEC)
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.
# 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.
# 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.
# 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")
Read joystick positions, button states, and display information on the controller screen.
Controller()
Constructor
Creates a controller object for driver input.
# 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.
# 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")
Timing functions, brain display, competition control, and system utilities.
wait(time, units)
Function
Pauses program execution for the specified time.
# 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.
# 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.
# 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)
Explore more documentation and learning materials
Download MRXcode Studio and bring your robot to life