β LEGO Hub Requirements:
- Hub is powered ON
- Hub is named "test" (or update name in
robot_runner.pyline 9) - Hub is within Bluetooth range (3-10 feet from computer)
- Hub has Pybricks firmware installed
β Motor Connections:
- Port A: Base motor (rotates left/right)
- Port F: Shoulder motor (moves up/down)
python -c "open('commands.txt', 'w', encoding='utf-8').write('')"python robot_runner.pyExpected output:
[Runner] π€ Robot command runner started
[Runner] π Monitoring: commands.txt
Open a new terminal and run:
python main.py --use_robot --target_object bottleExpected output:
π€ Robot control enabled
[STARTUP] Target object: bottle
[STARTUP] Center zone: 256.0 to 384.0
- Hold a bottle in front of the camera
- Move it left or right
- Watch the robot rotate to track it!
[Runner] Running on hub (attempt 1/3): BASE:40
[Runner] β Command successful: BASE:40
β¬
οΈ LEFT
π€ SENT: BASE:-40
β‘οΈ RIGHT
π€ SENT: BASE:40
| Setting | Value |
|---|---|
| BASE movement | Β±40Β° |
| SHOULDER movement | Β±20Β° |
| Motor speed | 1500 deg/s |
| Command cooldown | 0.05s (20 commands/sec) |
| Center threshold | 20% of frame width |
[Runner] Error: 'utf-8' codec can't decode byte 0xff
Fix:
python -c "open('commands.txt', 'w', encoding='utf-8').write('')"[HUB] TimeoutError
[HUB] Searching for test...
Fix:
- Check hub is powered ON
- Check hub name is "test"
- Move hub closer to computer (within 10 feet)
- Make sure hub isn't connected to another device
Could not open camera at index 0
Fix:
python webcam.py # Find correct camera index
python main.py --use_robot --target_object bottle --camera_index 1 # Use different indexChecklist:
- Both terminals are running
-
robot_runner.pyshows "β Command successful" - Hub motors are in correct ports (A=base, F=shoulder)
- Bottle is visible in camera feed
- Bluetooth connection is stable
| File | Purpose |
|---|---|
main.py |
Vision detection and command generation |
robot_runner.py |
Bluetooth bridge to send commands to hub |
robot_hub.py |
Hub-side script that controls motors |
commands.txt |
Temporary file for command communication |
commands_log.txt |
Log of all commands sent (optional) |
Commands written to commands.txt:
BASE:40 # Rotate base right 40Β°
BASE:-40 # Rotate base left 40Β°
SHOULDER_UP # Move shoulder up 20Β°
SHOULDER_DOWN # Move shoulder down 20Β°
STOP # Stop all motors
echo to write to commands.txt - it creates UTF-16 files which cause errors
β Only let Python write to the file, or use:
python -c "open('commands.txt', 'w', encoding='utf-8').write('BASE:40')"pip install -r requirements.txtultralytics- YOLOv8 object detectionopencv-python- Computer vision and camera handlingpybricksdev- LEGO hub Bluetooth communicationsupervision- Detection utilities
If issues persist:
- Restart both terminals
- Power cycle the LEGO hub
- Clear commands file again
- Check Bluetooth connectivity
βββββββββββββββ ββββββββββββββββ βββββββββββββββ
β Camera ββββββββ βΆβ main.py ββββββββββΆβcommands.txt β
β (Bottle) β β (YOLOv8) β β β
βββββββββββββββ ββββββββββββββββ ββββββββ¬βββββββ
β
ββββββββββββββββ β
βrobot_runner ββββββββββββββββββ
β .py β
ββββββββ¬ββββββββ
β Bluetooth
ββββββββΌββββββββ
β LEGO Hub β
β (robot_hub) β
ββββββββ¬ββββββββ
β
ββββββββΌββββββββ
β Motors β
β A=Base F=Arm β
ββββββββββββββββ
MIT License - Feel free to use and modify!
Built with β€οΈ for LEGO Mindstorms robot tracking