友情提示:380元/半年,儿童学编程,就上码丁实验室。
ROS2入门教程-ROS1与ROS2通讯
说明:
- 介绍ros1_bridge包的使用
概要
-
ros1_bridge包提供了一个网络桥接器,以便ROS1和ROS2之间能够交换消息
-
该包目前是通过C++来实现的,故仅支持限于桥的编译时可用的消息/服务类型
-
提供预编译ros2二进制文件的桥支持常用的ros接口(消息/服务),如 ros2/common_interfaces repository 和
tf2_msgs
列出的接口包 -
更多关于ros1和ros2接口相关联的信息,请参阅:https://github.com/ros2/ros1_bridge/blob/master/doc/index.rst
-
若要使用其他接口的桥(如自定义的类型),需要在源代码上构建
-
Note: For binary releases up to and including
release-alpha8
, some of the interfaces incommon_interfaces
are skipped – check the git tag of the release in question to see which interfaces were built.
前提准备
-
需要预先安装好ros2和ros1以及ros1_bridge包
-
需要用到的ros1包:
-
catkin
-
roscpp
-
roslaunch
(forroscore
executable) -
rosmsg
-
std_msgs
-
as well as the Python package
rospkg
-
-
若要运行以下例子,仍需这些ros1包:
-
rosbash
(forrosrun
executable) -
roscpp_tutorials
-
rospy_tutorials
-
rostopic
-
rqt_image_view
-
用源代码构建ros1_bridge
-
注意事项:
-
开始前请确认
ros_comm
和rosbag
是否是1.11.16以上的版本 -
请完成前面ros2和ros1的安装再进行ros1_bridge的安装
-
Here are the steps (for Linux and OSX; you probably don’t have ROS 1 installed on Windows).
You should first build everything but the ROS 1 bridge with normal make arguments. We don’t recommend having your ROS 1 environment sourced during this step as it can add OpenCV 3 to your path. The ROS 2 image demos you build in this step would then use OpenCV 3 and require it to be on your path when you run them, while the standard installation on Ubuntu Xenial is OpenCV 2.colcon build –symlink-install –packages-skip ros1_bridge
-
-
source一下环境
source /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
- The bridge will be built with support for any message/service packages that are on your path and have an associated mapping between ROS 1 and ROS 2. Therefore you must add any ROS 1 or ROS 2 workspaces that have message/service packages that you want to be bridged to your path before building the bridge. This can be done by adding explicit dependencies on the message/service packages to the package.xml of the bridge, so that colcon will add them to the path before it builds the bridge. Alternatively you can do it manually by sourcing the relevant workspaces yourself, e.g.:
# You have already sourced your ROS installation.
# Source your ROS 2 installation:
. <install-space-with-ros2>/local_setup.bash
# And if you have a ROS 1 overlay workspace, something like:
# . <install-space-to-ros1-overlay-ws>/setup.bash
# And if you have a ROS 2 overlay workspace, something like:
# . <install-space-to-ros2-overlay-ws>/local_setup.bash
- Then build just the ROS 1 bridge:
colcon build --symlink-install --packages-select ros1_bridge --cmake-force-configure
注意:编译时需占用大量内存,但若机器内存不足,可以尝试设置环境变量
MAKEFLAGS=-j1
来限制并行作业的数量。
示例1:run the bridge and the example talker and listener
- talker和listener可以是ROS1或ROS2节点。桥将透明地传递消息。
Note: When you are running these demos make sure to only source the indicated workspaces. You will get errors from most tools if they have both workspaces in their environment.
-
示例1.1 ROS 1 talker and ROS 2 listener
-
首先运行ros1的
roscore
# Shell A:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
roscore
- 然后运行
dynamic bridge
,该程序将定期开始输出ROS 1和ROS 2中当前可用的主题
d# Shell B:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
. <install-space-with-bridge>/setup.bash
export ROS_MASTER_URI=http://localhost:11311
ros2 run ros1_bridge dynamic_bridge
- 接着运行ROS 1 talker,可以看到该节点发布的消息会显示在终端上
# Shell C:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
rosrun rospy_tutorials talker
- 运行ROS 2 listener,同样可以看到该节点发布的消息会显示在终端上
# Shell D:
. <install-space-with-ros2>/setup.bash -
ros2 run demo_nodes_cpp listener
- 在shell B上会看到一段关于该topic的桥已创建的说明:
created 1to2 bridge for topic '/chatter' with ROS 1 type 'std_msgs/String' and ROS 2 type 'std_msgs/String'
- 当中止程序后,可以看到shell B出现关于桥已被移除的说明:
removed 1to2 bridge for topic '/chatter'
Note: When the bridge is run using the default ROS 2 middleware implementation, which uses Fast RTPS, it does not always remove bridges instantly. See https://github.com/ros2/ros1_bridge/issues/38.
- 示例运行截图如下:
![请输入图片描述]:4
-
示例1.2:ROS 2 talker and ROS 1 listener
-
第一步:
# Shell A:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
roscore
- 第二步:
# Shell B:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
. <install-space-with-bridge>/setup.bash
export ROS_MASTER_URI=http://localhost:11311
ros2 run ros1_bridge dynamic_bridge
- 第三步:
# Shell C:
. <install-space-with-ros2>/setup.bash
ros2 run demo_nodes_py talker
- 最后:
# Shell D:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
rosrun roscpp_tutorials listener
示例2:run the bridge and exchange images
-
该例子将演示桥能够传递更大更复杂的消息。ROS2节点正在发布从相机拍到的图像,在ROS1节点中使用
rqt_image_view
在GUI中渲染图像。ROS1发布者可以发送消息来切换ROS2节点中的选项。 -
第一步:
# Shell A:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
roscore
# Shell B:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
. <workspace-with-bridge>/install/setup.bash
export ROS_MASTER_URI=http://localhost:11311
ros2 run ros1_bridge dynamic_bridge
- 第二步:
# Shell C:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
rqt_image_view /image
- 第三步:
# Shell D:
. <workspace-with-ros2>/install/setup.bash
ros2 run image_tools cam2image
- 通过ros1节点向ros2节点发布。通过向
flip_image
topic发布true
或false
,相机节点将在发送之前翻转图像。可以使用rqt
中的Message Publisher
插件在主题flip_image
上发布std_msgs/Bool
消息,也可以运行以下两个rostopic命令之一:
# Shell E:
. /opt/ros/melodic/setup.bash
# Or, on OSX, something like:
# . ~/ros_catkin_ws/install_isolated/setup.bash
rostopic pub -r 1 /flip_image std_msgs/Bool "{data: true}"
rostopic pub -r 1 /flip_image std_msgs/Bool "{data: false}"
- 例子运行截图:
示例3:run the bridge for AddTwoInts service
-
该示例桥接了ros/roscpp_tutorials的TwoInts服务和ros2/roscpp_examples的AddTwoInts
-
在构建时,
ros1_bridge
会查找所有已安装的ROS和ROS2服务。通过比较请求和响应中的包名称,服务名称和字段来匹配找到的服务。如果ROS和ROS2服务中的所有名称相同,则将创建桥。也可以通过创建包含相应服务名称的yaml文件来手动配对服务。若想了解更多信息请参阅:https://github.com/ros2/ros1_bridge/blob/master/doc/index.rst -
运该示例时,请确保已安装
roscpp_tutorials
软件包,并在构建ros1_bridge
时正确设置了环境 -
运行ROS master
# Shell A:
. <ros-install-dir>/setup.bash
roscore -p 11311
- 运行dynamic_bridge
# Shell B:
. <ros-install-dir>/setup.bash
. <ros2-install-dir>/setup.bash
export ROS_MASTER_URI=http://localhost:11311
ros2 run ros1_bridge dynamic_bridge
- 运行TwoInts server
# Shell C:
. <ros-install-dir>/setup.bash
export ROS_MASTER_URI=http://localhost:11311
rosrun roscpp_tutorials add_two_ints_server
- 运行 AddTwoInts client
# Shell D:
. <ros2-install-dir>/setup.bash
ros2 run demo_nodes_cpp add_two_ints_client
参考链接:
- https://github.com/ros2/ros1_bridge/blob/master/README.md