Overview
Thesim.py script bridges the MuJoCo simulation with ROS2, enabling:
- Real-time camera image streaming
- Body state (position/orientation) publishing
- Remote movement commands via topics
- Simulation restart service
Architecture
ROS2 Topics
Published Topics
robot_camera (
sensor_msgs/Image)- Camera feed from robot’s perspective
- 640×480 RGB images at 10 Hz
- Frame ID:
robot_camera
std_msgs/Float32MultiArray)- Robot pose:
[x, y, z, roll, pitch, yaw] - Updated at 10 Hz
- Position in meters, angles in radians
Subscribed Topics
movement_command (
std_msgs/Int32)0: Stop (freeze gait)1: Forward movement2: Backward movement- Applied immediately to gait controller
ROS2 Services
restart_simulation (std_srvs/Trigger)
- Resets MuJoCo simulation state
- Resets gait controller phase
- Returns success confirmation
Two-Terminal Setup
You need two terminals: one for the simulation, one for the GUI controller.Terminal 1: Launch Simulation
Start the ROS2-enabled simulation:Output:The MuJoCo viewer opens and the robot starts in place (waiting for movement commands).
- Flat Terrain
- Rough Terrain
Terminal 2: Launch GUI Controller
In a new terminal, start the control GUI:The PyQt5 GUI window opens with:
- Login screen (credentials in
gui/users.db) - Camera feed view
- Gamepad control interface
- Body state visualization
Topic Details
- Camera Images
- Body State
- Movement Commands
robot_camera Topic
Message Type:sensor_msgs/msg/ImagePublished by: sim.py (RobotControlNode)Frequency: 10 Hz (every 0.1 seconds)Specifications:- Resolution: 640×480 pixels
- Encoding:
rgb8(24-bit color) - Frame ID:
robot_camera - Source: MuJoCo offscreen renderer (
sim.py:233)
Services
restart_simulation Service
Service Type:std_srvs/srv/Trigger
Server: sim.py (RobotControlNode)
Behavior (sim.py:90-96):
- Resets MuJoCo data (
mj_resetData) - Resets gait controller phase
- Resets camera capture timer
- Returns success response
Debugging
Check Active Topics
Check Active Topics
List all active ROS2 topics:Expected output:
Monitor Topic Data
Monitor Topic Data
Echo messages from a topic:Expected camera rate: ~10 Hz
Inspect Topic Info
Inspect Topic Info
Get detailed topic information:Output:
Test Services
Test Services
List available services:Call restart service manually:Expected response:
Check Node Status
Check Node Status
List running nodes:Expected nodes:View node info:
Common Issues
No Camera Images Received
No Camera Images Received
Symptoms: GUI shows no camera feedChecks:
- Verify topic is publishing:
- Check for errors in
sim.pyterminal - Ensure cv_bridge is installed:
Movement Commands Not Working
Movement Commands Not Working
Symptoms: Robot doesn’t respond to gamepadChecks:
- Verify commands are being published:
- Check gamepad detection in GUI terminal:
- Ensure pygame is installed:
Simulation Crashes on Restart
Simulation Crashes on Restart
Symptoms:
sim.py exits when calling restart serviceSolution: This is a known issue with viewer restarts. Current implementation handles in-place data reset (sim.py:248-258) without recreating the viewer.If crashes persist, restart both terminals manually.Advanced Usage
Custom ROS2 Node
Create your own control node:custom_controller.py
sim.py:
Next Steps
GUI Control
Learn about the PyQt5 GUI and gamepad integration
Baseline Testing
Compare different control strategies