首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

ROS2 Python编程基础

ROS最重要的特性之一就是多语言的支持,可以使用C++、Python等语言进行程序的开发,ROS2会继续强化这个特性,对更多语言提供丰富的支持。

这里我们尝试ROS2当中的Python编程,实现话题和服务通信。

代码链接:

https://github.com/ros2/demos

官方教程:

https://index.ros.org/doc/ros2/Tutorials/Python-Programming

在进行操作前,请先参考以下链接,完成ROS 2的安装和例程编译:

— 一、话题通信—

代码编译后,在终端设置环境变量,并使用如下命令运行listener和talker:

运行成功后,可以看到如下效果:

以上talker和listener的具体实现,我们可以来分析下代码,分析内容请见代码中的注释:

talker.py

listener.py

— 二、服务通信—

接下来我们看下ROS另外一种核心通信机制——服务的实现,运行以下命令实现ROS 1教程当中经典的加法求和服务例程:

可以看到如下效果:

add_two_ints_server和add_two_ints_client的具体实现,继续来分析代码:

add_two_ints_server.py

add_two_ints_client.py

— 三、QoS配置—

ROS 2中的通信中间件改用DDS,而QoS是DDS中非常重要的一环,控制了各方面与底层的通讯机制,主要从时间限制、可靠性、持续性、历史记录几个方面,满足用户针对不同场景的数据应用需求。

以话题通信为例,我们看下Python程序中该如何配置QoS:

talker_qos.py

listener_qos.py

ROS 2针对四种类型的通信,提供了QoS的默认配置:

Default QoS settings for publishers and subscriptions

In order to make the transition from ROS1 to ROS2, exercising a similar network behavior is desirable. By default, publishers and subscriptions are reliable in ROS2, have volatile durability, and “keep last” history.

Services

In the same vein as publishers and subscriptions, services are reliable. It is especially important for services to use volatile durability, as otherwise service servers that re-start may receive outdated requests.

Sensor data

For sensor data, in most cases it’s more important to receive readings in a timely fashion, rather than ensuring that all of them arrive. That is, developers want the latest samples as soon as they are captured, at the expense of maybe losing some. For that reason the sensor data profile uses best effort reliability and a smaller queue depth.

Parameters

Parameters are based on services, and as such have a similar profile. The difference is that parameters use a much larger queue depth so that requests do not get lost when, for example, the parameter client is unable to reach the parameter service server.

参考:http://design.ros2.org/articles/qos.html

以上例程运行时,默认使用的是Best effort模式:

可以通过命令输入切换成Reliable模式:

还可以设置发布或订阅的次数:

号外!号外!

古月君开通新浪微博了

微博名:古月春旭

~ 欢迎各位关注啦 ~

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190114G13CQU00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券