ROS2话题通信与包创建与ROS1的异同

ROS2 键盘控制小乌龟

打开乌龟节点

roslaunch turtle_sim turtle_node
ros2 run turtlesim turtlesim_node

键盘控制乌龟行走

rosrun turtlesim turtlesim_teleop_key
ros2 run turtlesim turtle_teleop_key

查看所有节点

rostopic list
ros2 topic list

查看话题通信的msg类型

rostopic info /turtle1/cmd_vel
ros2 topic info /turtle1/cmd_vel


编译包

ROS2与ROS1有以下差异

  • ros2 pkg create –build-type ament_python <pkg_name>
  • colcon build –packages-select <pkg_name>
  • ros2 run <pkg_name> <node_name>

一图流如下:
创建工作空间和pkg包的异同

colcon命令替代了catkin

ROS2默认没有安装编译包,也就是对应于ROS1的catkin,

sudo apt install python3-colcon-common-extensions

有两种包ROS 2 Python and CMake packages,它们是依赖ament_cmake或ament_python来区分的,包创建的文件不同
前者类似于catkin_make的文件结构:
ament_cmake

  • package.xml file containing meta information about the package

  • CMakeLists.txt file that describes how to build the code within the package

ament_python

  • package.xml file containing meta information about the package

  • setup.py containing instructions for how to install the package

  • setup.cfg is required when a package has executables, so ros2 run can find them

  • /<package_name> contains __init__.py

这两种包的创建方式不同,src的语言也不同,前者支持C++语言,后者只能采用python语言。我这里用python版的。

示例

Create a Workspace

设置source /opt/ros/humble/setup.bash

$ mkdir -p ~/dev_ws/src
$ cd ~/dev_ws/src
$ cd ~/dev_ws
$ colcon build --symlink-install
$ source /opt/ros/foxy/setup.bash
$ source install/local_setup.bash

Create a ROS 2 Package

cd ~/dev_ws/src
ros2 pkg create --build-type ament_python --node-name my_node my_package
cd ~/dev_ws
colcon build --packages-select my_package
. install/setup.bash
ros2 run my_package my_node

ROS2 话题通信

话题通信基本上只是把ROS1原有的命令后面加了一个2,然后拆开,如下:

topic通信发送msg的异同

topic的msg及其可视化

列出 Nodes

ros2 node list
ros2 node info /turtlesim

话题与节点可视化

ros2需要安装rqt

sudo apt install ~nros-humble-rqt*

查看节点关系及其message

rqt_graph

查看Topic 类型

ros2 topic type /turtle1/cmd_vel

查看Message类型

ros2 msg show geometry_msgs/Twist

ros2 interface show geometry_msgs/msg/Twist

发布消息

rostopic pub -r 10 /turtle1/cmd_vel geometry_msgs/#(到这里tab一下就可以自动给出格式,改变它的数字即可)
ros2 topic pub -r 10 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

reference

  1. 常用代码可以参考 tertiarycourses / Full-ROS2-Training 的topic2, topic3
  2. ROS2官网文档,如何编译一个Publisher-Subscriber包
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值