Robotics StackExchange | Archived questions

How to launch a launch file using python?

Hi I had been following te ros tutorial for launching a launch file using python. Accrodingly, I built my code here.

#!/usr/bin/env python

import roslaunch
import rospy


# rospy.init_node("tester")

uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
roslaunch.configure_logging(uuid)

launch_file = ['ur5_notebook', 'main_r2_mt.launch']
roslaunch_file = roslaunch.rlutil.resolve_launch_arguments(launch_file)

parent = roslaunch.parent.ROSLaunchParent(uuid, roslaunch_file)

parent.start()

But I get the following error,

process[robot2/robot_controllers-19]: started with pid [27636]
[robot2/robot_controllers-19] killing on exit
Traceback (most recent call last):
  File "/usr/lib/python2.7/encodings/__init__.py", line 31, in <module>
    import codecs
  File "/usr/lib/python2.7/codecs.py", line 174, in <module>
    class IncrementalEncoder(object):
  File "/usr/lib/python2.7/codecs.py", line 174, in IncrementalEncoder
    class IncrementalEncoder(object):
KeyboardInterrupt
[robot2/ros_control_controller_manager-18] killing on exit
[robot2/arm_controller_spawner-17] killing on exit
[robot2/joint_state_controller_spawner-16] killing on exit
[robot2/fake_joint_calibration-15] killing on exit
[robot2/robot_state_publisher-14] killing on exit
[robot2/spawn_gazebo_model-13] killing on exit
[broadcaster_fixed_2-12] killing on exit
Traceback (most recent call last):
  File "/home/murtaza/catkin_ws3/src/ur5_notebook/fixed_tf_broadcaster2.py", line 3, in <module>
    roslib.load_manifest('ur5_notebook')
  File "/opt/ros/melodic/lib/python2.7/dist-packages/roslib/launcher.py", line 62, in load_manifest
    sys.path = _generate_python_path(package_name, _rospack) + sys.path
  File "/opt/ros/melodic/lib/python2.7/dist-packages/roslib/launcher.py", line 93, in _generate_python_path
    m = rospack.get_manifest(pkg)
  File "/usr/lib/python2.7/dist-packages/rospkg/rospack.py", line 167, in get_manifest
    return self._load_manifest(name)
  File "/usr/lib/python2.7/dist-packages/rospkg/rospack.py", line 211, in _load_manifest
    retval = self._manifests[name] = parse_manifest_file(self.get_path(name), self._manifest_name, rospack=self)
  File "/usr/lib/python2.7/dist-packages/rospkg/manifest.py", line 410, in parse_manifest_file
    from rosdep2.rospack import init_rospack_interface, is_ros_package, is_system_dependency, is_view_empty
  File "/usr/lib/python2.7/dist-packages/rosdep2/__init__.py", line 40, in <module>
    from .installers import InstallerContext, Installer, \
  File "/usr/lib/python2.7/dist-packages/rosdep2/installers.py", line 418, in <module>
[broadcaster_fixed-11] killing on exit
Traceback (most recent call last):
    class RosdepInstaller(object):
  File "/home/murtaza/catkin_ws3/src/ur5_notebook/fixed_tf_broadcaster.py", line 2, in <module>
KeyboardInterrupt
    import numpy as np
  File "/home/murtaza/.local/lib/python2.7/site-packages/numpy/__init__.py", line 145, in <module>
    from . import lib
  File "/home/murtaza/.local/lib/python2.7/site-packages/numpy/lib/__init__.py", line 25, in <module>
    from .financial import *
  File "/home/murtaza/.local/lib/python2.7/site-packages/numpy/lib/financial.py", line 15, in <module>
    from decimal import Decimal
  File "/usr/lib/python2.7/decimal.py", line 393, in <module>
    import threading
  File "/usr/lib/python2.7/threading.py", line 631, in <module>
    class Thread(_Verbose):
  File "/usr/lib/python2.7/threading.py", line 958, in Thread
    @property
KeyboardInterrupt

I don't understand why there is keyboard error showing up!

Asked by mkb_10062949 on 2020-04-01 03:59:34 UTC

Comments

Answers

Hi @mkb_10062949,

You need the parent to spin, to continue being alive, if not a signal is sent to stop all execution. Try this:

#!/usr/bin/env python

import roslaunch
import rospy

# rospy.init_node("tester")

uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
roslaunch.configure_logging(uuid)

launch_file = ['ur5_notebook', 'main_r2_mt.launch']
roslaunch_file = roslaunch.rlutil.resolve_launch_arguments(launch_file)

parent = roslaunch.parent.ROSLaunchParent(uuid, roslaunch_file)

parent.start()

try:
  parent.spin()
finally:
  parent.shutdown()

Asked by Weasfas on 2020-04-04 11:28:24 UTC

Comments