Webots simulation with ROS2 - <extern> controller (robot Ros2Supervisor)

I am having some problem with a repository of mine, in which I aim to simulate a robot in Webots with an extern controller defined and employing ROS2-Webots packages and features. The link to my repo is the one below:

UPDATE:

The thing is I can’t control my robot as expected. Right now, the robot is launched with the respective supervisor and controller, but the controller connects and disconnects continuously, leading to a situation where the robot is static and the controller process is aborted. When I launch it, I get this below:

$: ros2 launch ugv_pkg robot_launch.py 
[INFO] [launch]: All log files can be found below /home/caio_iriarte/.ros/log/2025-04-08-16-23-34-946577-pc-692368
[INFO] [launch]: Default logging verbosity is set to INFO
WARNING: No valid Webots directory specified in `ROS2_WEBOTS_HOME` and `WEBOTS_HOME`, fallback to default installation folder /usr/local/webots.
[INFO] [webots-1]: process started with pid [692369]
[INFO] [ros2_supervisor.py-2]: process started with pid [692371]
[INFO] [webots_controller_ugv_robot-3]: process started with pid [692373]
[webots_controller_ugv_robot-3] The specified robot (at /tmp/webots/caio_iriarte/1234/ipc/ugv_robot/extern) is not in the list of robots with <extern> controllers, retrying for another 50 seconds...
[ros2_supervisor.py-2] The specified robot (at /tmp/webots/caio_iriarte/1234/ipc/Ros2Supervisor/extern) is not in the list of robots with <extern> controllers, retrying for another 50 seconds...
[webots_controller_ugv_robot-3] terminate called after throwing an instance of 'std::runtime_error'
[webots_controller_ugv_robot-3]   what():  Error: The Python module with the WebotsNode class cannot be compiled.
[webots_controller_ugv_robot-3] [ros2run]: Aborted
[ERROR] [webots_controller_ugv_robot-3]: process has died [pid 692373, exit code 250, cmd '/home/caio_iriarte/ros2_robot/ugv_ws/install/webots_ros2_driver/share/webots_ros2_driver/scripts/webots-controller --robot-name=ugv_robot --protocol=ipc --port=1234 ros2 --ros-args -p robot_description:=/home/caio_iriarte/ros2_robot/ugv_ws/install/ugv_pkg/share/ugv_pkg/resource/ugv_robot.urdf'].
[INFO] [webots_controller_ugv_robot-3]: process started with pid [692477]
[webots_controller_ugv_robot-3] terminate called after throwing an instance of 'std::runtime_error'
[webots_controller_ugv_robot-3]   what():  Error: The Python module with the WebotsNode class cannot be compiled.
[webots_controller_ugv_robot-3] [ros2run]: Aborted

I suppose the main error is on the lines:

[INFO] [webots_controller_ugv_robot-3]: process started with pid [692477]
[webots_controller_ugv_robot-3] terminate called after throwing an instance of 'std::runtime_error'
[webots_controller_ugv_robot-3]   what():  Error: The Python module with the WebotsNode class cannot be compiled.
[webots_controller_ugv_robot-3] [ros2run]: Aborted

If anyone has got this error or knows how to handle it, please give me some advice. Thank you.

Problem:

The error:

what():  Error: The Python module with the WebotsNode class cannot be compiled.

…means the Webots ROS2 controller is failing to compile the Python code that implements the WebotsNode class. This usually happens due to:

  • Missing Python dependencies
  • Incompatible Python version
  • Improper build or install of webots_ros2_driver

Solution (step-by-step):

  1. Check Python version
    Make sure you are using Python 3.10, which is compatible with ROS 2 Humble and Webots ROS2.
  2. Install missing dependencies
    Run:
sudo apt update
sudo apt install python3-pybind11 python3-dev
  1. Rebuild your workspace
    From inside your ugv_ws:
colcon build --packages-select webots_ros2_driver --symlink-install
source install/setup.bash
  1. Check Webots extern controller
    In your Webots .wbt world file, make sure your robot has this line:
controller "<extern>"

Not <void>, not a named controller, it must be <extern>.
5. Set Webots environment variable
Export WEBOTS_HOME if it’s not set:

export WEBOTS_HOME=/usr/local/webots
  1. Try again
    Launch your robot again:
ros2 launch ugv_pkg robot_launch.py

If it still fails after this, share your ugv_robot.urdf and robot_launch.py, and I’ll help you further.