以自定义数据类型为基础,完成一个节点发布消息,另一个节点接收前一个节点发布的消息
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
)
C++ 需要调用的中间文件(…/工作空间/devel/include/包名/xxx.h)
快捷键 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 中间文件路径配置进 c_cpp_properties.json 的 includepath属性,如果不配置vscode会报错代码,但是编译不受影响
路径: /home/lee/lee_ws/.vscode/c_cpp_properties.json
{
"configurations": [
{
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/opt/ros/noetic/include/**",
"/home/lee/catkin_ws/src/lee/include/**",
"/usr/include/**",
"/home/lee/catkin_ws/devel/include/**" //配置 head 文件的路径
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
路径: /home/lee/lee_ws/src/lee_msg/src/lee_msg_pub.cpp
#include "ros/ros.h"
#include "lee_msg/Person.h"
int main(int argc, char *argv[])
{
//解决在终端上打印中文不会出现乱码,二选一或两个一起选都行
setlocale(LC_CTYPE, "zh_CN.utf8");
setlocale(LC_ALL, "");
//初始化节点:命名(唯一)
// 参数1和参数2 为节点传值使用
// 参数3 是节点名称,是一个标识符,需要保证运行后,在 ROS 网络拓扑中唯一
ros::init(argc,argv,"lee_msg_pub");
//实例化节点句柄
ros::NodeHandle nh;
//创建一个Publisher,发布名为chatter_msg的topic,消息类型为lee_msg::Person
ros::Publisher pub = nh.advertise<lee_msg::Person>("chatter_msg",1000);
//组织发布的数据
lee_msg::Person p;
p.name = "迪迦奥特曼";
p.age = 2000;
p.height = 66.8;
//等待3秒钟,让publisher有足够的时间完成roscore注册,不然还未注册成功就开始发送消息了
ros::Duration(3.0).sleep();
//设置循环周期1秒3次的频率
ros::Rate F(3);
//节点存在,ros::ok()为1
while (ros::ok())
{
//发布消息
pub.publish(p);
//终端打印发布的消息
ROS_INFO("我叫:%s,今年%d岁,身高%.2f米", p.name.c_str(), p.age, p.height);
//延时
F.sleep();
p.age += 1;
ros::spinOnce();
}
return 0;
}
在install上面直接添加如下代码即可
add_executable(lee_msg_pub src/lee_msg_pub.cpp)
add_dependencies(lee_msg_pub ${PROJECT_NAME}_generate_messages_cpp)
target_link_libraries(lee_msg_pub ${catkin_LIBRARIES})
路径: /home/lee/lee_ws/src/lee_msg/src/lee_msg_sub.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
//接收到订阅的消息后,进入回调函数
void chatterCallback(const std_msgs::String::ConstPtr& msg){
ROS_INFO("接收到了:%s",msg->data.c_str());
}
int main(int argc, char *argv[])
{
//解决在终端上打印中文不会出现乱码,二选一或两个一起选都行
setlocale(LC_CTYPE, "zh_CN.utf8");
setlocale(LC_ALL, "");
//初始化节点:命名(唯一)
// 参数1和参数2 为节点传值使用
// 参数3 是节点名称,是一个标识符,需要保证运行后,在 ROS 网络拓扑中唯一
ros::init(argc,argv,"lee_sub");
//实例化节点句柄
ros::NodeHandle nh;
//创建一个Subscriber,订阅名为chatter的topic,注册回调函数chatterCallback
ros::Subscriber sub = nh.subscribe<std_msgs::String>("chatter",10,chatterCallback);
//设置循环调用回调函数
ros::spin();
return 0;
}
在install上面直接添加如下代码即可
add_executable(lee_msg_sub src/lee_msg_sub.cpp)
add_dependencies(lee_msg_sub ${PROJECT_NAME}_generate_messages_cpp)
target_link_libraries(lee_msg_sub ${catkin_LIBRARIES})
roscore
rosrun lee_msg lee_msg_pub
rosrun lee_msg lee_msg_sub