Skip to content

Commit 18d4e47

Browse files
Merge pull request #85 from Intelligent-Testing-Lab/79-integrate-bridge-into-agent
79 integrate bridge into agent
2 parents 22de0a6 + b258172 commit 18d4e47

2 files changed

Lines changed: 56 additions & 32 deletions

File tree

srunner/autoagents/autoware_agent.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
from std_msgs.msg import Empty
1515

16+
from srunner.tools import ROS2_launch
17+
18+
1619
import threading
1720
import rclpy
1821
import time
@@ -57,10 +60,14 @@ def setup(self, config: EnvironmentConfig | None = None) -> None:
5760

5861
self.sent_route = False
5962

63+
# launch the bridge
64+
ROS2_launch.ROS2Launch.launch_file(
65+
"autoware_carla_interface", "autoware_carla_interface.launch"
66+
)
67+
6068
# check the bridge is ready
6169
# publish sensor information to the bridge
6270
# wait for it to return the correct message
63-
6471
ego_config_msg = EgoConfig()
6572
ego_config_msg.ego_name = config.ego_name
6673
ego_config_msg.ego_model = config.ego_model
@@ -148,12 +155,12 @@ def run_step(self) -> None:
148155
waypoints.append(
149156
self._convert_to_waypoint(waypoint).autoware_from_world_coords()
150157
)
151-
158+
152159
# autoware cannot handle many waypoints, becomes unreliable
153160
n_waypoints = len(waypoints)
154161
segment_size = int(n_waypoints / 3)
155-
156-
#self.route_node.request_route(goal_pose, waypoints[0::segment_size])
162+
163+
# self.route_node.request_route(goal_pose, waypoints[0::segment_size])
157164
self.route_node.publish_route(goal_pose, waypoints[0::segment_size])
158165
self.sent_route = True
159166

srunner/tools/ROS2_launch.py

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,71 @@
11
import subprocess
22
import threading
33

4+
45
class ROS2Launch(object):
56
threads = dict()
67
stop_event = threading.Event()
7-
8-
@classmethod
9-
def launch_file(self, package_name: str, launch_file: str, args: dict) -> None:
8+
9+
@staticmethod
10+
def launch_file(
11+
package_name: str, launch_file: str, args: dict | None = None
12+
) -> None:
1013
"""will launch the file specified from the package specified
1114
1215
Args:
1316
package_name (str): ROS2 package name
1417
launch_file (str): launch file name
1518
args (dict): dictionary of arguments to pass to launch file
1619
"""
17-
command = self._construct_command(package_name, launch_file, args)
18-
thread = threading.Thread(target=self._run_command, args=(command, self.stop_event))
19-
self.threads[self.package_name] = {
20-
"thread": thread,
21-
"stop": False
22-
}
20+
command = ROS2Launch._construct_command(package_name, launch_file, args)
21+
thread = threading.Thread(
22+
target=ROS2Launch._run_command, args=(command, ROS2Launch.stop_event)
23+
)
24+
ROS2Launch.threads[package_name] = {"thread": thread, "stop": False}
2325
thread.start()
24-
25-
@classmethod
26-
def cleanup(self, package_name: str) -> None:
26+
27+
@staticmethod
28+
def cleanup(package_name: str) -> None:
2729
"""provide package name to stop running
2830
2931
Args:
3032
package_name (str): name of the ROS2 package to kill
3133
"""
32-
if package_name in self.threads:
33-
thread = self.threads[package_name]
34-
thread['stop'] = True
35-
36-
def _run_command(self, command: list[str], package_name) -> None:
37-
process = subprocess.Popen(command, check=True)
38-
39-
if package_name in self.threads:
40-
while(not self.threads[package_name]['stop']):
34+
if package_name in ROS2Launch.threads:
35+
thread = ROS2Launch.threads[package_name]
36+
thread["stop"] = True
37+
thread["thread"].join()
38+
39+
@staticmethod
40+
def _run_command(command: list[str], package_name) -> None:
41+
command = (
42+
f"/bin/bash && source /opt/ros/humble/setup.bash && {' '.join(command)}"
43+
)
44+
45+
process = subprocess.Popen(
46+
command,
47+
shell=True,
48+
stdout=subprocess.PIPE,
49+
stderr=subprocess.PIPE,
50+
text=True,
51+
)
52+
53+
if package_name in ROS2Launch.threads:
54+
while not ROS2Launch.threads[package_name]["stop"]:
4155
if process.poll() is not None:
4256
break
43-
57+
4458
if process.poll() is None:
4559
process.kill()
46-
47-
def _construct_command(self, package_name, launch_file, args) -> list[str]:
60+
61+
@staticmethod
62+
def _construct_command(
63+
package_name: str, launch_file: str, args: dict | None = None
64+
) -> list[str]:
4865
command = []
4966
command.append("ros2 launch").append(package_name).append(launch_file)
50-
for key, value in args.dict():
51-
command.append(f"{key}:={value}")
67+
68+
if args is not None:
69+
for key, value in args.dict():
70+
command.append(f"{key}:={value}")
5271
return command
53-
54-

0 commit comments

Comments
 (0)