@@ -75,7 +75,7 @@ def _to_simple_dict(data):
7575
7676class ControllerInstance (object ):
7777
78- def __init__ (self , config_robot , config_env , ros_subs ):
78+ def __init__ (self , config_robot , config_env , ros_subs , events = None ):
7979 self .config_robot = config_robot
8080 self .config_env = config_env
8181
@@ -84,6 +84,7 @@ def __init__(self, config_robot, config_env, ros_subs):
8484 self ._cmds = None
8585 self ._processes = None
8686 self ._log_files = None
87+ self ._events = events
8788
8889 def _replace_variables (self , text ):
8990 for k , v in VARIABLES .items ():
@@ -161,7 +162,8 @@ def start(self):
161162 # Wait until we move into a running state
162163 start_time = time .time ()
163164 while not self .is_running ():
164- time .sleep (0.25 )
165+ if self ._events and self ._events .wait (0.25 ):
166+ return False
165167 if not self .health_check (check_running = False ):
166168 return False
167169 elif (time .time () - start_time > TIMEOUT_STARTUP and
@@ -189,9 +191,10 @@ def start_logging(self):
189191 ]
190192
191193 def stop (self ):
192- if not self .is_running ():
193- print ("Controller Instance is not running. Skipping stop." )
194- return False
194+ # We could be in the process of starting, so this check is inappropriate
195+ # if not self.is_running():
196+ # print("Controller Instance is not running. Skipping stop.")
197+ # return False
195198
196199 # Stop all of the open processes & logging
197200 for p in self ._processes :
@@ -225,7 +228,6 @@ def __init__(self, port=10000, auto_start=True):
225228 self .robot_address = 'http://0.0.0.0:' + str (port )
226229
227230 self ._auto_start = auto_start
228-
229231 self .config = None
230232 self .config_valid = False
231233
@@ -236,6 +238,10 @@ def __init__(self, port=10000, auto_start=True):
236238
237239 self .instance = None
238240
241+ self .evt = event .Event ()
242+ for s in [signal .SIGINT , signal .SIGQUIT , signal .SIGTERM ]:
243+ signal .signal (s , lambda n , frame : self .evt .set ())
244+
239245 self .wipe ()
240246
241247 @staticmethod
@@ -468,18 +474,14 @@ def __selected_env():
468474 # Configure our server
469475 robot_server = pywsgi .WSGIServer (
470476 re .split ('http[s]?://' , self .robot_address )[- 1 ], robot_flask )
471- evt = event .Event ()
472- signal .signal (signal .SIGINT , evt .set )
473- signal .signal (signal .SIGQUIT , evt .set )
474- signal .signal (signal .SIGTERM , evt .set )
475477
476478 # Run the server & start the real robot controller
477479 robot_server .start ()
478480 print ("\n Robot controller is now available @ '%s' ..." %
479481 self .robot_address )
480482 print ("Waiting to receive valid config data..." )
481483 while not self .config_valid :
482- if evt .wait (0.1 ):
484+ if self . evt .wait (0.1 ):
483485 break
484486
485487 if self ._auto_start and self .config_valid :
@@ -491,7 +493,7 @@ def __selected_env():
491493
492494 # Wait until we get an exit signal or crash, then shut down gracefully
493495 while self .instance .health_check ():
494- if evt .wait (0.1 ):
496+ if self . evt .wait (0.1 ):
495497 break
496498 print ("\n Shutting down the real robot ROS stack & exiting ..." )
497499 robot_server .stop ()
@@ -543,7 +545,8 @@ def start(self):
543545 c ['ros' ]
544546 for c in self .connections .values ()
545547 if c ['type' ] == CONN_ROS_TO_API and c ['ros' ] != None
546- ])
548+ ],
549+ events = self .evt )
547550 self .instance .start ()
548551
549552 def stop (self ):
0 commit comments