Skip to main content

Overview

The SensorModule class provides a unified interface to all hardware sensors on the VEX IQ robotic arm. It manages the Brain, inertial sensor, distance sensors, bumper, and touch LED, offering convenient methods for reading sensor data and controlling outputs. Source: ~/workspace/source/arm_controller/src/main.py:76

Constructor

__init__()

Initializes all sensor hardware components with their port configurations.
sensors = SensorModule()
Initialized Sensors:
brain
Brain
VEX Brain controller with screen and button interface
inertial
Inertial
3-axis gyroscope for angle and rotation measurements. Requires calibration before use.
gripper_distance
Distance
Distance sensor on gripper (Port 7). Measures object distance in gripper proximity.Port: Ports.PORT7
touchled
Touchled
RGB LED with touch sensitivity (Port 10). Used for status indication.Port: Ports.PORT10
base_distance
Distance
Distance sensor on rotating base (Port 11). Primary sensor for environmental scanning.Port: Ports.PORT11
bumper
Bumper
Mechanical bumper switch (Port 12). Safety sensor for shoulder collision detection.Port: Ports.PORT12
Source: main.py:77

Brain Screen Methods

clear_screen()

Clears all content from the VEX Brain display.
sensors.clear_screen()
Source: main.py:86 Displays text at specified screen coordinates.
sensors.print_screen("Temperature: 25°C", 10, 50)
text
str
required
Text string to display on screen
coordinate_x
int
required
Horizontal pixel position (0 = left edge)
coordinate_y
int
required
Vertical pixel position (0 = top edge)
Screen Resolution:
  • VEX IQ Brain: 228×128 pixels
  • Origin (0,0) at top-left corner
Source: main.py:89

Inertial Sensor Methods

calibrate_intertial_sensor()

Calibrates the inertial sensor. Robot must remain stationary during calibration.
sensors.calibrate_intertial_sensor()
The robot must be completely still during calibration. Movement will cause inaccurate readings.
Duration: Typically 2-3 seconds When to Calibrate:
  • On robot startup
  • After significant temperature changes
  • If angle readings drift
  • After physical impacts
Source: main.py:93

get_angle()

Returns the current heading angle from the inertial sensor.
angle = sensors.get_angle()
print(f"Current heading: {angle}°")
return
float
Current heading angle in degrees (0-360°)
  • : Initial calibration position
  • Increases with clockwise rotation
  • Wraps around at 360°
Use Cases:
  • Tracking rotation during environmental scans
  • Calculating object angular positions
  • Returning to specific orientations
Source: main.py:96

Distance Sensor Methods

get_base_distance()

Reads distance from the base-mounted distance sensor.
distance = sensors.get_base_distance()
if 50 <= distance <= 300:
    print(f"Object detected at {distance}mm")
return
float
Distance to nearest object in millimeters (mm)
  • Range: 20-2000mm (typical)
  • Accuracy: ±3mm
  • Returns large value if no object detected
Detection Range:
  • Minimum: ~20mm
  • Optimal: 50-300mm (used for object detection)
  • Maximum: ~2000mm
Source: main.py:100

get_base_object_size()

Returns the raw size measurement from base distance sensor.
size = sensors.get_base_object_size()
print(f"Object size metric: {size}")
return
int
Raw size value (0-400 typical)
  • Higher values indicate larger objects
  • Unitless metric (not physical dimensions)
  • Used for relative size comparison
Interpretation:
  • 0: No object detected
  • 1-100: Small object (bottle cap, pen)
  • 100-200: Medium object (cup, bottle)
  • 200+: Large object (box, container)
Source: main.py:103

get_gripper_distance()

Reads distance from the gripper-mounted distance sensor.
gripper_dist = sensors.get_gripper_distance()
if gripper_dist < 30:
    print("Object in gripper range")
return
float
Distance in millimeters from gripper sensor to nearest object
  • Range: 20-200mm (shorter range than base sensor)
  • Used to verify object grip
Use Cases:
  • Confirming object capture
  • Fine-tuning gripper approach
  • Verifying gripper is empty
Source: main.py:106

Bumper Methods

is_bumper_pressed()

Checks if the mechanical bumper switch is pressed.
if sensors.is_bumper_pressed():
    print("WARNING: Bumper collision detected!")
    # Emergency stop
return
bool
  • True: Bumper is currently pressed (collision detected)
  • False: Bumper is not pressed (normal state)
Bumper activation indicates physical contact. Immediate motor stop is recommended to prevent damage.
Typical Usage:
  • Safety checks during shoulder movement
  • Emergency stop trigger
  • Collision avoidance validation
Source: main.py:110

Touch LED Methods

set_color(color: tuple)

Sets the RGB color of the touch LED for status indication.
# Red for error
sensors.set_color((255, 0, 0))

# Green for ready
sensors.set_color((0, 255, 0))

# Blue for scanning
sensors.set_color((0, 0, 255))
color
tuple[int, int, int]
required
RGB color tuple (red, green, blue)
  • Each component: 0-255
  • (0, 0, 0): Off
  • (255, 255, 255): White
Standard Colors (from LEDColors class):
ERROR = (255, 0, 0)             # Red: Error/Stop
WARNING = (255, 150, 0)         # Orange: Warning
READY = (0, 255, 0)             # Green: Ready
RUNNING = (0, 0, 255)           # Blue: Process Running
OBJECT_DETECTED = (0, 255, 155) # Cyan: Object Detected
Source: main.py:114

Hardware Verification

check_sensors()

Verifies that all critical sensors are properly installed and responsive.
if sensors.check_sensors():
    print("All sensors operational")
else:
    print("ERROR: Sensor installation issue")
return
bool
Returns True if all sensors installed, False if any sensor missing or unresponsive
Checked Sensors:
  • Base distance sensor (Port 11)
  • Gripper distance sensor (Port 7)
  • Bumper switch (Port 12)
Not Checked:
  • Brain (always available)
  • Inertial sensor (always available)
  • Touch LED (always available)
Source: main.py:119

Port Configuration Summary

SensorPortTypePurpose
Gripper DistancePORT7DistanceGripper proximity detection
Touch LEDPORT10TouchledStatus indication
Base DistancePORT11DistanceEnvironmental scanning
BumperPORT12BumperCollision safety
InertialInternalInertialAngle measurement
BrainInternalBrainDisplay and control

Sensor Data Flow

The sensor data flows through the system as follows:
SensorModule

PerceptionModule (processes sensor data)

MappingModule (builds spatial map)

RoboticServices (coordinates actions)
Example Scan Cycle:
# 1. Read sensors
angle = sensors.get_angle()              # 45.0°
distance = sensors.get_base_distance()   # 150mm
size = sensors.get_base_object_size()    # 85

# 2. Update status LED
if 50 <= distance <= 300:
    sensors.set_color((0, 255, 155))  # Cyan: object detected
else:
    sensors.set_color((0, 0, 255))    # Blue: scanning

# 3. Display on screen
sensors.print_screen(f"Angle: {angle:.1f}°", 10, 20)
sensors.print_screen(f"Dist: {distance}mm", 10, 40)

Usage Examples

Complete Sensor Initialization

from vex import *
import time

# Initialize sensor module
sensors = SensorModule()

# Clear screen
sensors.clear_screen()

# Verify hardware
if not sensors.check_sensors():
    sensors.set_color((255, 0, 0))  # Red error
    sensors.print_screen("SENSOR ERROR", 10, 50)
    raise Exception("Sensors not installed")

# Calibrate inertial sensor
sensors.print_screen("Calibrating...", 10, 10)
sensors.calibrate_intertial_sensor()
time.sleep(2)

# Ready state
sensors.set_color((0, 255, 0))  # Green
sensors.print_screen("READY", 10, 10)

Object Detection Loop

while True:
    # Read sensors
    distance = sensors.get_base_distance()
    size = sensors.get_base_object_size()
    angle = sensors.get_angle()
    
    # Check for objects
    if 50 <= distance <= 300:
        sensors.set_color((0, 255, 155))  # Cyan
        sensors.print_screen(f"OBJECT: {size}", 10, 30)
        sensors.print_screen(f"Dist: {distance}mm", 10, 50)
        sensors.print_screen(f"Angle: {angle:.1f}°", 10, 70)
    else:
        sensors.set_color((0, 0, 255))  # Blue
        sensors.print_screen("Scanning...", 10, 30)
    
    time.sleep(0.05)  # 20Hz update rate

Safety Monitoring

def monitor_safety(sensors, control_module):
    """Continuous safety monitoring"""
    if sensors.is_bumper_pressed():
        # Emergency stop
        control_module.general_stop()
        sensors.set_color((255, 0, 0))  # Red
        sensors.print_screen("COLLISION!", 10, 60)
        return False
    return True

Build docs developers (and LLMs) love