以自定义数据类型为基础,完成一个节点发布消息,另一个节点接收前一个节点发布的消息
lee_ws
mkdir -p ~/lee_ws/src
cd ~/lee_ws/src/
catkin_init_workspace
cd ~/lee_ws/
catkin_make
echo "source ~/lee_ws/devel/setup.bash" >> ~/.bashrc
source devel/setup.bash
lee_msg
cd ~/lee_ws/src
catkin_create_pkg lee_msg std_msgs rospy roscpp
cd ~/lee_ws/
catkin_make
source ~/lee_ws/devel/setup.bash
路径: /home/lee/lee_ws/src/lee_msg/msg/Person.msg
string name
uint16 age
float64 height
在package.xml最下面的build_depend
和exec_depend
附近添加即可
<build_depend>message_generationbuild_depend>
<exec_depend>message_runtimeexec_depend>
在CMakeLists.txt中配置下面相应的代码
# 需要加入 message_generation,必须有 std_msgs
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
# 配置 msg 源文件
add_message_files(
FILES
Person.msg
)
# 生成消息时依赖于 std_msgs
generate_messages(
DEPENDENCIES
std_msgs
)
#执行时依赖
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES lee
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib
)
Python 需要调用的中间文件(…/工作空间/devel/lib/python3/dist-packages/包名/msg)
快捷键 ctrl + shift + B 调用编译,选择:catkin_make:build这一行,点击小齿轮配置设置,修改.vscode/tasks.json 文件,将下面复制进去即可
这么配置的原因:下一次快捷键 ctrl + shift + B直接进行代码编译
路径:.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
//代表提示的描述性信息
"label": "catkin_make:debug",
//可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
"type": "shell",
//这个是我们需要运行的命令
"command": "catkin_make",
"args": [],
"group": {"kind":"build","isDefault":true},
//可选always或者silence,代表是否输出信息
"presentation": {"reveal": "always"},
"problemMatcher": "$msCompile"
}
]
}
为了方便代码提示以及避免误抛异常,需要先配置 vscode,将1.3-1.5生成的 head 中间文件路径配置进 settings.json 的 "python.autoComplete.extraPaths"属性,如果不配置vscode会报错代码,但是编译不受影响
路径: /home/lee/lee_ws/.vscode/settings.json
{
"python.autoComplete.extraPaths": [
"/opt/ros/noetic/lib/python3/dist-packages",
"/lee/catkin_ws/devel/lib/python3/dist-packages"
]
}
路径: /home/lee/lee_ws/src/lee_msg/scripts/lee_msg_pub.py
#! /usr/bin/env python
# coding=utf-8
import rospy
from lee_msg.msg import Person
if __name__ == "__main__":
#初始化 ROS 节点:命名(唯一)
rospy.init_node("lee_msg_pub")
#创建一个Publisher,发布名为chatter的topic,消息类型为Person
pub = rospy.Publisher("chatter_msg",Person,queue_size=10)
#等待3秒钟,让publisher有足够的时间完成roscore注册,不然还未注册成功就开始发送消息了
rospy.sleep(3)
#组织发布的数据
p = Person()
p.name = "迪迦奥特曼"
p.age = 2000
p.height = 66.8
#设置循环频率,1秒3次
rate = rospy.Rate(3)
#节点存在,is_shutdown()为1
while not rospy.is_shutdown():
#发布消息
pub.publish(p)
#终端打印发布的消息
rospy.loginfo("姓名:%s, 年龄:%d, 身高:%.2f",p.name, p.age, p.height)
#延时
rate.sleep()
p.age = p.age+1
在install下面直接添加如下代码即可
catkin_install_python(PROGRAMS scripts/lee_msg_pub.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
路径: /home/lee/lee_ws/src/lee_msg/src/lee_msg_sub.cpp
#! /usr/bin/env python
# coding=utf-8
import rospy
from lee_msg.msg import Person
#接收到订阅的消息后,进入回调函数
def chatterCallback(lee):
rospy.loginfo("订阅的人信息:姓名:%s, 年龄:%d, 身高:%.2f",lee.name, lee.age, lee.height)
if __name__ == "__main__":
#初始化 ROS 节点:命名(唯一)
rospy.init_node("lee_msg_sub")
#创建一个Subscriber,订阅名为chatter的topic,注册回调函数为chatterCallback
sub = rospy.Subscriber("chatter_msg",Person,chatterCallback,queue_size=10)
#设置循环调用回调函数
rospy.spin()
在install下面直接添加如下代码即可
catkin_install_python(PROGRAMS scripts/lee_msg_sub.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
roscore
rosrun lee_msg lee_msg_pub.py
rosrun lee_msg lee_msg_sub.py