@@ -75,7 +75,7 @@ def _to_simple_dict(data):
75
75
76
76
class ControllerInstance (object ):
77
77
78
- def __init__ (self , config_robot , config_env , ros_subs ):
78
+ def __init__ (self , config_robot , config_env , ros_subs , events = None ):
79
79
self .config_robot = config_robot
80
80
self .config_env = config_env
81
81
@@ -84,6 +84,7 @@ def __init__(self, config_robot, config_env, ros_subs):
84
84
self ._cmds = None
85
85
self ._processes = None
86
86
self ._log_files = None
87
+ self ._events = events
87
88
88
89
def _replace_variables (self , text ):
89
90
for k , v in VARIABLES .items ():
@@ -161,7 +162,8 @@ def start(self):
161
162
# Wait until we move into a running state
162
163
start_time = time .time ()
163
164
while not self .is_running ():
164
- time .sleep (0.25 )
165
+ if self ._events and self ._events .wait (0.25 ):
166
+ return False
165
167
if not self .health_check (check_running = False ):
166
168
return False
167
169
elif (time .time () - start_time > TIMEOUT_STARTUP and
@@ -189,9 +191,10 @@ def start_logging(self):
189
191
]
190
192
191
193
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
195
198
196
199
# Stop all of the open processes & logging
197
200
for p in self ._processes :
@@ -225,7 +228,6 @@ def __init__(self, port=10000, auto_start=True):
225
228
self .robot_address = 'http://0.0.0.0:' + str (port )
226
229
227
230
self ._auto_start = auto_start
228
-
229
231
self .config = None
230
232
self .config_valid = False
231
233
@@ -236,6 +238,10 @@ def __init__(self, port=10000, auto_start=True):
236
238
237
239
self .instance = None
238
240
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
+
239
245
self .wipe ()
240
246
241
247
@staticmethod
@@ -468,18 +474,14 @@ def __selected_env():
468
474
# Configure our server
469
475
robot_server = pywsgi .WSGIServer (
470
476
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 )
475
477
476
478
# Run the server & start the real robot controller
477
479
robot_server .start ()
478
480
print ("\n Robot controller is now available @ '%s' ..." %
479
481
self .robot_address )
480
482
print ("Waiting to receive valid config data..." )
481
483
while not self .config_valid :
482
- if evt .wait (0.1 ):
484
+ if self . evt .wait (0.1 ):
483
485
break
484
486
485
487
if self ._auto_start and self .config_valid :
@@ -491,7 +493,7 @@ def __selected_env():
491
493
492
494
# Wait until we get an exit signal or crash, then shut down gracefully
493
495
while self .instance .health_check ():
494
- if evt .wait (0.1 ):
496
+ if self . evt .wait (0.1 ):
495
497
break
496
498
print ("\n Shutting down the real robot ROS stack & exiting ..." )
497
499
robot_server .stop ()
@@ -543,7 +545,8 @@ def start(self):
543
545
c ['ros' ]
544
546
for c in self .connections .values ()
545
547
if c ['type' ] == CONN_ROS_TO_API and c ['ros' ] != None
546
- ])
548
+ ],
549
+ events = self .evt )
547
550
self .instance .start ()
548
551
549
552
def stop (self ):
0 commit comments