def testPyrosROSCtx(): # start ros system , before PyrosROS process and Client otherwise Client assumes there is problem ( discovery timeout ) # we might need to load pyros_setup here... try: import pyros_utils except ImportError: # TODO : find a proper way to log from a test like here... try: #_logger.warning("loading pyros_setup and configuring your ROS environment") import pyros_setup # This will load the pyros_setup configuration from the environment pyros_setup.configurable_import().configure().activate() import pyros_utils except ImportError: # This is expected when testing pyros by itself raise nose.SkipTest( "pyros_utils could not be imported, and trying to import pyros_setup for dynamic ros setup failed." ) master, roscore_proc = pyros_utils.get_master( spawn=True) # we start the master if needed assert master.is_online() with pyros_ctx(node_impl=pyros_interfaces_ros.PyrosROS) as ctx: assert isinstance(ctx.client, PyrosClient) # TODO : assert the context manager does his job ( HOW ? ) if roscore_proc is not None: roscore_proc.terminate() while roscore_proc.is_alive(): time.sleep(0.2) # waiting for roscore to die
def testPyrosROSCtx(): # start ros system , before PyrosROS process and Client otherwise Client assumes there is problem ( discovery timeout ) # we might need to load pyros_setup here... try: import pyros_utils except ImportError: # TODO : find a proper way to log from a test like here... try : #_logger.warning("loading pyros_setup and configuring your ROS environment") import pyros_setup # This will load the pyros_setup configuration from the environment pyros_setup.configurable_import().configure().activate() import pyros_utils except ImportError: # This is expected when testing pyros by itself raise nose.SkipTest("pyros_utils could not be imported, and trying to import pyros_setup for dynamic ros setup failed.") master, roscore_proc = pyros_utils.get_master(spawn=True) # we start the master if needed assert master.is_online() with pyros_ctx(node_impl=pyros_interfaces_ros.PyrosROS) as ctx: assert isinstance(ctx.client, PyrosClient) # TODO : assert the context manager does his job ( HOW ? ) if roscore_proc is not None: roscore_proc.terminate() while roscore_proc.is_alive(): time.sleep(0.2) # waiting for roscore to die
def test_rosnode_started(): master, roscore = pyros_utils.get_master() try: assert master.is_online() # hack needed to wait for master until fix for https://github.com/ros/ros_comm/pull/711 is released roslaunch.rlutil.get_or_generate_uuid(None, True) launch = roslaunch.scriptapi.ROSLaunch() launch.start() assert launch.started echo_node = roslaunch.core.Node('pyros_test', 'echo.py', name='echo') echo_process = launch.launch(echo_node) assert echo_process.is_alive() node_api = None with timeout(5) as t: while not t.timed_out and node_api is None: node_api = rosnode.get_api_uri( master, 'echo' ) # would be good to find a way to do this without rosnode dependency assert node_api is not None launch.stop() finally: # to make sure we always shutdown everything, even if test fails if roscore is not None: roscore.terminate() rospy.signal_shutdown('test_rosnode_started done') while roscore and roscore.is_alive(): time.sleep(0.2) # waiting for roscore to die assert not (roscore and master.is_online())
def test_roscore_started(): master, roscore = pyros_utils.get_master() try: assert master.is_online() finally: # to make sure we always shutdown everything, even if test fails if roscore is not None: roscore.terminate() rospy.signal_shutdown('test_roscore_started done') while roscore and roscore.is_alive(): time.sleep(0.2) # waiting for roscore to die assert not (roscore and master.is_online())
def test_roslaunch_started(): master, roscore = pyros_utils.get_master() try: assert master.is_online() # hack needed to wait for master until fix for https://github.com/ros/ros_comm/pull/711 is released roslaunch.rlutil.get_or_generate_uuid(None, True) launch = roslaunch.scriptapi.ROSLaunch() launch.start() assert launch.started launch.stop() finally: # to make sure we always shutdown everything, even if test fails if roscore is not None: roscore.terminate() rospy.signal_shutdown('test_roslaunch_started done') while roscore and roscore.is_alive(): time.sleep(0.2) # waiting for roscore to die assert not (roscore and master.is_online())
def __init__(self, node_name, publishers=None, subscribers=None, services=None, params=None, enable_cache=False, argv=None): # This runs in a child process (managed by PyrosROS) and as a normal ros node) # First thing to do : find the rosmaster... # master has to be running here or we just wait for ever # TODO : improve... m, _ = pyros_utils.get_master(spawn=False) while not m.is_online(): _logger.warning("ROSMASTER not found !!!...") time.sleep(0.5) # Second thing to do : initialize the ros node and disable signals to avoid overriding callers behavior rospy.init_node(node_name, argv=argv, disable_signals=True) rospy.loginfo( 'RosInterface {name} node started with args : {argv}'.format( name=node_name, argv=argv)) # note on dynamic update (config reload, etc.) this is reinitialized # rospy.init_node supports being reinitialized with the exact same arguments self.enable_cache = enable_cache if self.enable_cache and rocon_python_comms is None: rospy.logerr( "Connection Cache enabled for RosInterface, but rocon_python_comms not found. Disabling." ) self.enable_cache = False # only one queue to avoid sync issues self.cb_ss = Queue.Queue() # we add params from ROS environment, if we get something there (bwcompat behavior) services = services or [] publishers = publishers or [] subscribers = subscribers or [] params = params or [] services += list( set(ast.literal_eval(rospy.get_param('~services', "[]")))) # bwcompat publishers += list( set(ast.literal_eval(rospy.get_param('~topics', "[]")))) subscribers += list( set(ast.literal_eval(rospy.get_param('~topics', "[]")))) # new publishers += list( set(ast.literal_eval(rospy.get_param('~publishers', "[]")))) subscribers += list( set(ast.literal_eval(rospy.get_param('~subscribers', "[]")))) params += list(set(ast.literal_eval(rospy.get_param('~params', "[]")))) enable_cache = rospy.get_param('~enable_cache', enable_cache) if enable_cache is not None: self.enable_cache = enable_cache # Note : None means no change ( different from [] ) rospy.loginfo("""[{name}] ROS Interface initialized with: - services : {services} - publishers : {publishers} - subscribers : {subscribers} - params : {params} - enable_cache : {enable_cache} """.format(name=__name__, publishers="\n" + "- ".rjust(10) + "\n\t- ".join(publishers) if publishers else [], subscribers="\n" + "- ".rjust(10) + "\n\t- ".join(subscribers) if subscribers else [], services="\n" + "- ".rjust(10) + "\n\t- ".join(services) if services else [], params="\n" + "- ".rjust(10) + "\n\t- ".join(params) if params else [], enable_cache=enable_cache)) # This base constructor assumes the system to interface with is already available ( can do a get_svc_available() ) params_pool = RosParamIfPool(params) services_pool = RosServiceIfPool(services) subscribers_pool = RosSubscriberIfPool(subscribers) publishers_pool = RosPublisherIfPool(publishers) super(RosInterface, self).__init__(publishers_pool, subscribers_pool, services_pool, params_pool) # connecting to the master via proxy object self._master = rospy.get_master() #: If enabled, connection cache proxy will be setup in update() to allow dynamic update via config. # TODO : double check : maybe useless now since we completely reinit the interface for dynamic update... self.connection_cache = None # Setup our debug log # We need this debug log since rospy.logdebug does NOT store debug messages in the log. # But it should Ref : http://wiki.ros.org/rospy/Overview/Logging#Reading_log_messages # TODO : find why. ros_home = os.environ.get( 'ROS_HOME', os.path.join(os.path.expanduser("~"), '.ros')) self._debug_logger = logging.getLogger('pyros.ros_interface') if not os.path.exists(os.path.join(ros_home, 'logdebug')): os.makedirs(os.path.join(ros_home, 'logdebug')) logfilename = os.path.join( ros_home, 'logdebug', rospy.get_name()[1:].replace(os.path.sep, "-") + '_pyros_rosinterface.log') file_handler = logging.handlers.RotatingFileHandler(logfilename, maxBytes=100 * 131072, backupCount=10) self._debug_logger.propagate = False # to avoid propagating to root logger self._debug_logger.setLevel(logging.DEBUG) self._debug_logger.addHandler(file_handler)