@@ -79,8 +79,13 @@ def check_args(argv):
7979 Colors .OKBLUE , Colors .ENDC ))
8080
8181 args = parser .parse_args ()
82+ # get ROS version from environment variable
83+ ros_version = os .environ .get ('ROS_VERSION' , '2' ) # Default is ROS 2
84+ if ros_version not in ['1' , '2' ]:
85+ logger .error ('Invalid ROS_VERSION environment variable. Must be "1" or "2". Killing program...' )
86+ sys .exit (- 1 )
8287
83- config_data = {'config' : None , 'gui' : None , 'tui' : None , 'script' : None , 'random' : False }
88+ config_data = {'config' : None , 'gui' : None , 'tui' : None , 'script' : None , 'random' : False , 'ros_version' : int ( ros_version ) }
8489 if args .config :
8590 config_data ['config' ] = []
8691 for config_file in args .config :
@@ -104,6 +109,31 @@ def check_args(argv):
104109 return config_data
105110
106111
112+ def init_node (ros_version ):
113+ """
114+ Initializes the ROS node based on the selected version.
115+
116+ Arguments:
117+ ros_version (str): The ROS version ("ros1" or "ros2").
118+
119+ Returns:
120+ For ROS1: returns None (as rospy manages the node globally).
121+ For ROS2: returns the created node.
122+ """
123+ if ros_version == 1 :
124+ import rospy
125+ rospy .init_node ('my_ros1_node' )
126+ return None # rospy maneja la instancia globalmente
127+ elif ros_version == 2 :
128+ import rclpy
129+ rclpy .init ()
130+ node = rclpy .create_node ('my_ros2_node' )
131+ logger .info ('ROS2 node initialized' )
132+ return node
133+ else :
134+ logger .error (f"Unsupported ROS version: { ros_version } " )
135+ sys .exit (- 1 )
136+
107137def conf_window (configuration ):
108138 """Gui windows for configuring the app. If not configuration file specified when launched, this windows appears,
109139 otherwise, main_win is called.
@@ -132,7 +162,7 @@ def conf_window(configuration):
132162 pass
133163
134164
135- def main_win (configuration , controller ):
165+ def main_win (configuration , controller , node ):
136166 """shows the Qt main window of the application
137167
138168 Arguments:
@@ -146,7 +176,7 @@ def main_win(configuration, controller):
146176 app = QApplication (sys .argv )
147177 main_window = ParentWindow ()
148178
149- views_controller = ViewsController (main_window , configuration , controller )
179+ views_controller = ViewsController (main_window , configuration , controller , node )
150180 views_controller .show_main_view (True )
151181
152182 main_window .show ()
@@ -161,11 +191,13 @@ def main():
161191
162192 # Check and generate configuration
163193 config_data = check_args (sys .argv )
194+ node = init_node (config_data ['ros_version' ]) # Initialize the ROS node shared by all the application
195+
164196 for config in config_data ['config' ]:
165197 app_configuration = Config (config )
166198
167199 # Create controller of model-view
168- controller = ControllerGazebo ()
200+ controller = ControllerGazebo (node )
169201
170202 # If there's no config, configure the app through the GUI
171203 if app_configuration .empty and config_data ['gui' ]:
@@ -194,14 +226,15 @@ def main():
194226
195227 if not config_data ['script' ]:
196228 # Launch control
197- pilot = PilotGazebo (app_configuration , controller , app_configuration .brain_path )
229+
230+ pilot = PilotGazebo (node , app_configuration , controller , app_configuration .brain_path )
198231 pilot .daemon = True
199232 pilot .start ()
200233 logger .info ('Executing app' )
201234
202235 # If GUI specified, launch it. Otherwise don't
203236 if config_data ['gui' ]:
204- main_win (app_configuration , controller )
237+ main_win (app_configuration , controller , node )
205238 else :
206239 pilot .join ()
207240
0 commit comments