Controlling Multiple Agents Using ROS2 Integration: Is It Possible?

Hi everyone,

I’m currently working on integrating BeamNG.tech with ROS2 and would like to know if it’s possible to control multiple vehicles (agents) simultaneously using the ROS2 bridge. Right now, I’m using the following script to control just one vehicle:

def __init__(self):
    super().__init__(NODE_NAME)

    host = self.declare_parameter("host", "###.##.224.1").value
    port = self.declare_parameter("port", 25252).value
    vehicle_id = self.declare_parameter("vehicle_id", "ego").value

    if not vehicle_id:
        self.get_logger().fatal("No Vehicle ID given, shutting down node.")
        sys.exit(1)

    self.game_client = bngpy.BeamNGpy(host, port)
    try:
        self.game_client.open(listen_ip="*", launch=False, deploy=False)
        self.get_logger().info("Successfully connected to BeamNG.tech.")
    except TimeoutError:
        self.get_logger().error("Could not establish game connection.")
        sys.exit(1)

    current_vehicles = self.game_client.get_current_vehicles()
    if vehicle_id not in current_vehicles:
        self.get_logger().fatal(f"No vehicle with id {vehicle_id} exists.")
        sys.exit(1)

    self.vehicle_client = current_vehicles[vehicle_id]
    try:
        self.vehicle_client.connect(self.game_client)
        self.get_logger().info(f"Connected to vehicle {self.vehicle_client.vid}")
    except TimeoutError:
        self.get_logger().fatal("Could not establish vehicle connection.")
        sys.exit(1)

My questions are:

  1. Is it possible to control more than one vehicle at the same time using this ROS2 bridge setup?
  2. Can I use the same IP and port for all vehicles and just manage multiple vehicle clients in code? Or is it necessary to use different ports (or even different IP addresses) for each vehicle connection?
  3. In the scenario definition i just have to define a new vehicle?

Thanks in advance for any answer! And sorry for the specific question :sweat_smile:

Hello @Daddysublime,

  1. yes, you have to add all ros-controlled vehicles in your scenario, any other vehicle won’t be accessible to the ROS bridge
  2. you don’t need to change anything in your bridge setup — same IP and port work for all; just add all your vehicles’ configuration in your scenario file
  3. exactly — just define a new vehicle in your scenario

you can use the beamng_agent package to create a message translator between ROS Twist messages and BeamNGpy commands.
reference: beamng-ros2-integration/beamng_agent/beamng_agent/beamng_agent.py at main · BeamNG/beamng-ros2-integration · GitHub

I suggest you use the launch files in the beamng_bringup package. You can either create a node for each vehicle or create a separate launch file to run the beamng_agent script with its corresponding parameters.
reference: beamng-ros2-integration/beamng_bringup/launch/beamng_agent.launch.py at main · BeamNG/beamng-ros2-integration · GitHub

Then you can start beamng-teleop-keyboard to send keyboard commands to all vehicles simultaneously, as beamng_agent nodes subscribe to the /cmd_vel topic then publish the moving commands to all vehicles.
reference: beamng-ros2-integration/beamng_teleop_keyboard/beamng_teleop_keyboard/teleop.py at main · BeamNG/beamng-ros2-integration · GitHub

Best regards,
Abdul

1 Like

Thank you so much for the detailed explanation, Abdul!

Really appreciate the references and guidance! I’ll give it a go soon and follow up if I run into any issues.

Thanks again!

1 Like

Hi Abdul

Little update just to tell that everything worked fine !! Thank you for the great answer !

1 Like

You’re very welcome @Daddysublime — I’m really glad to hear it worked out! :tada:

Good luck with the rest of your integration!

Best regards,
Abdul