迁移Python包 [15975]
目录 []
构建工具 [15665]
在ROS 2中,不再使用 catkin_make
、catkin_make_isolated
或 catkin build
,而是使用命令行工具 colcon 来构建和安装一组包。请查看 初学者教程 以开始使用 colcon
。 [15666]
构建系统 [15667]
对于纯Python包,ROS 2使用标准的 setup.py
安装机制,这对Python开发人员来说是熟悉的。 [15976]
更新文件以使用 setup.py [15977]
如果ROS 1包仅使用CMake来调用 setup.py
文件,且除了Python代码外没有其他内容(例如没有消息、服务等),则应将其转换为ROS 2中的纯Python包: [15978]
更新或在``package.xml``文件中添加构建类型: [15979]
<export> <build_type>ament_python</build_type> </export>
移除
CMakeLists.txt
文件 [15980]将
setup.py
文件更新为标准的 Python 设置脚本 [15981]
ROS 2 仅支持 Python 3。虽然每个软件包可以选择支持 Python 2,但如果它使用其他 ROS 2 软件包提供的任何 API,则必须使用 Python 3 调用可执行文件。 [15982]
更新源代码 [15703]
节点初始化 [15983]
在ROS 1中: [15984]
rospy.init_node('asdf')
rospy.loginfo('Created node')
在ROS 2中: [15985]
rclpy.init(args=sys.argv)
node = rclpy.create_node('asdf')
node.get_logger().info('Created node')
ROS参数 [15986]
在ROS 1中: [15984]
port = rospy.get_param('port', '/dev/ttyUSB0')
assert isinstance(port, str), 'port parameter must be a str'
baudrate = rospy.get_param('baudrate', 115200)
assert isinstance(baudrate, int), 'baudrate parameter must be an integer'
rospy.logwarn('port: ' + port)
在ROS 2中: [15985]
port = node.declare_parameter('port', '/dev/ttyUSB0').value
assert isinstance(port, str), 'port parameter must be a str'
baudrate = node.declare_parameter('baudrate', 115200).value
assert isinstance(baudrate, int), 'baudrate parameter must be an integer'
node.get_logger().warn('port: ' + port)
创建发布者 [15987]
在ROS 1中: [15984]
pub = rospy.Publisher('chatter', String)
# or
pub = rospy.Publisher('chatter', String, queue_size=10)
在ROS 2中: [15985]
pub = node.create_publisher(String, 'chatter', rclpy.qos.QoSProfile())
# or
pub = node.create_publisher(String, 'chatter', 10)
创建订阅者 [15988]
在ROS 1中: [15984]
sub = rospy.Subscriber('chatter', String, callback)
# or
sub = rospy.Subscriber('chatter', String, callback, queue_size=10)
在ROS 2中: [15985]
sub = node.create_subscription(String, 'chatter', callback, rclpy.qos.QoSProfile())
# or
sub = node.create_subscription(String, 'chatter', callback, 10)
创建服务 [15989]
在ROS 1中: [15984]
srv = rospy.Service('add_two_ints', AddTwoInts, add_two_ints_callback)
在ROS 2中: [15985]
srv = node.create_service(AddTwoInts, 'add_two_ints', add_two_ints_callback)
创建服务客户端 [15990]
在ROS 1中: [15984]
rospy.wait_for_service('add_two_ints')
add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
resp = add_two_ints(req)
在ROS 2中: [15985]
add_two_ints = node.create_client(AddTwoInts, 'add_two_ints')
while not add_two_ints.wait_for_service(timeout_sec=1.0):
node.get_logger().info('service not available, waiting again...')
resp = add_two_ints.call_async(req)
rclpy.spin_until_future_complete(node, resp)