Overview
TheRoboticServices class serves as the main orchestrator for the Trash Classification AI System’s robotic arm operations. It manages multiple subsystems including sensors, perception, mapping, control, and safety, coordinating their interactions through a state machine architecture.
Source: ~/workspace/source/arm_controller/src/main.py:294
Constructor
__init__()
Initializes all robotic subsystems and service state variables.
sensor_module: SensorModule instance for hardware sensorsperception_module: PerceptionModule for object detectionmapping_module: MappingModule for spatial mappingcontrol_module: ControlModule for motor controlsafety_module: SafetyModule for safety checkscommunication: CommunicationManager for serial communication
Hardware Check Methods
check_service()
Verifies that all motors and sensors are properly installed and operational.
Returns
True if all hardware components are installed and functional, False otherwise- Sets LED to green (READY) on success
- Sets LED to red (ERROR) on failure
- Checks both sensors and motors through safety module
main.py:330
Safety Methods
safety_state_service()
Executes a sequential safety verification process for shoulder and gripper mechanisms.
Returns
True when the entire safety sequence (shoulder → gripper) completes successfully-
Shoulder Safety Check
- Monitors bumper sensor
- Stops all motors if bumper pressed
- Reverses shoulder motor briefly
- Sets LED to ERROR (red) or WARNING (orange)
-
Gripper Safety Check (after shoulder completes)
- Monitors gripper motor current
- Activates gripper at 20 RPM
- Stops when current exceeds 0.3A
- Indicates object grip or mechanical limit
-
Completion
- Sets LED to READY (green)
- Updates internal state flags
main.py:339
reset_safety_sequence()
Resets all safety sequence state variables to initial values.
safety_mode_active:Falsesafety_shoulder:Falsesafety_gripper:Falsesafety_service_loop:True
main.py:363
Scanning Methods
scan_service()
Executes a complete 360° environmental scan to detect and map objects.
Returns
True when scan completes (360° rotation or timeout)- Calls
start_scan_service()to initialize - Repeatedly calls
update_scan_service()in loop - Continues until rotation reaches 360° or 30-second timeout
- Returns mapped objects data
main.py:374
start_scan_service()
Initializes the scanning operation.
Returns
True if scan initialization succeeds, False if already complete- Records start timestamp
- Captures initial angle from inertial sensor
- Activates base motor at 20 RPM
- Sets LED to RUNNING (blue)
- Updates
scan_mode_activetoTrue
main.py:391
update_scan_service()
Processes sensor data during active scan operation.
Returns
True when scan is complete (360° or timeout), False to continue scanning-
Rotation Tracking
- Reads current angle from inertial sensor
- Calculates angular delta with wraparound handling
- Accumulates total rotation
-
Object Detection
- Processes distance and size sensor data
- Updates object detection state
- Changes LED color when objects detected (cyan)
-
Mapping
- Records object positions and dimensions
- Tracks object start/end angles
- Builds spatial map
-
Completion Check
- Stops motors when 360° reached or timeout
- Returns completion status
main.py:404
reset_scan_sequence()
Resets all scan sequence state variables to initial values.
scan_mode_active:Falsescan_start_time:0scan_timeout:30accumulated_rotation:0last_angle:0scan_start:Truescan_update:Falsescan_complete:False
main.py:428
Communication Methods
process_raspberry_message(message: dict)
Processes incoming JSON messages from the Raspberry Pi controller.
JSON message dictionary from Raspberry Pi
Triggers hardware verification checkResponse:or
Initiates safety sequence (shoulder and gripper checks)Response:
Starts 360° environmental scanResponse:
main.py:443
Main Execution
run()
Main execution loop that initializes communication and processes service requests.
- Initializes serial communication
- Enters infinite loop
- Reads and processes incoming messages
- Executes active services (check, safety, scan)
- Sends response messages
- Displays status on brain screen
- Handles exceptions and emergency stops
- Catches all exceptions
- Displays errors on screen (first 20 chars)
- Executes emergency stop on all motors
main.py:464
Service State Management
The RoboticServices class uses a flag-based state machine:- Activated by incoming messages via
process_raspberry_message() - Deactivated after service completion or error
- Multiple services cannot run simultaneously
- Each service has internal sub-states for step-by-step execution
Usage Example
Related Classes
- SensorModule - Hardware sensor management
- ControlModule - Motor control operations
- CommunicationManager - Serial communication protocol