11import subprocess
22import threading
33
4+
45class 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