前言:ros给我们提供了众多的消息结构,但是更多时候我们需要根据自己的研发需求定义自己的消息结构。
我们最常用的一个消息结构就是std_msgs,那么怎么查看这个消息结构支持可以定义哪些数据类型呢?
我们使用roscd std_msgs/这个命令打开该消息结构所在的文件夹
我们打开msg这个子文件夹,可以看到如下:
这些就是.msg文件就是std_msgs所定义的消息类型,我们打开几个看下,
ros支持的所有消息结构参考以下博客:
https://blog.csdn.net/qq_14925953/article/details/80459776
我们自定义的消息同样以.msg格式的文件保存,我们暂且把这个放到我们创建的功能包里。
我们直接使用上两节课创建的工作空间(catkin_ws)和功能包(communication),在功能包所在的文件夹下创建msg文件夹用来存放自定义的消息类型。
message_generation
message_runtime
find_package(catkin REQUIRED COMPONENTS
std_msgs
roscpp
message_generation
)
catkin_package(
CATKIN_DEPENDS std_msgs roscpp message_runtime
)
## Generate messages in the 'msg' folder
add_message_files(
FILES
my_Mecanum_speed.msg
)
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
)
执行catkin_make命令,编译工作空间。编译完成之后进行检查
看到此现象说明自定义消息成功!
note:有的小伙伴可能会问两个问题:
1:为什么要在package.xml文件中添加两个功能包依赖?
2.为什么要在CMakeLists.txt中添加几个编译选项?
其实这些在CMakeLists.txt的注释里面可以找到答案
################################################
## Declare ROS messages, services and actions ##
################################################
## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend and a run_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependencies might have been
## pulled in transitively but can be declared for certainty nonetheless:
## * add a build_depend tag for "message_generation"
## * add a run_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
关于CMakeLists.txt、package.xml的详细用法可以参考以下几篇博客:
https://blog.csdn.net/lcc816/article/details/82962561
https://blog.csdn.net/lcc816/article/details/82978648
https://blog.csdn.net/lcc816/article/details/82955065
文件内容如下:
#include
#include "ros/ros.h"//包含了ros当中常用的API,订阅,发布,日志的输出
#include "std_msgs/String.h"//常用的变量形式
#include "communication/my_Mecanum_speed.h" //虽然是my_Mecanum_speed.msg,这里却是.h
int main(int argc, char **argv)
{
// ROS节点初始化 argc argv和main函数保持一致 my_talker是节点名称
ros::init(argc, argv, "my_talker");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Publisher,发布名为chatter的topic,消息类型为scommunication::my_Mecanum_speed 1000是缓存队列长度,
ros::Publisher chatter_pub = n.advertise("chatter", 1000);
// 设置循环的频率
ros::Rate loop_rate(10);//10hz
int count = 0;
while (ros::ok())//循环发布消息
{
// 初始化std_msgs::String类型的消息
communication::my_Mecanum_speed speed;
// 发布消息
float i;
i++;
speed.front_left_speed = i;
speed.front_right_speed = i+1;
speed.back_left_speed= i+2;
speed.back_right_speed= i+3;
chatter_pub.publish(speed);//发布消息
// 循环等待回调函数
ros::spinOnce();//查询一次回调函数是否有消息进来
// 按照循环频率延时
loop_rate.sleep();
++count;
}
return 0;
}
注意是功能包文件夹下,不是工作空间文件夹下
add_executable(UseMyMsg src/UseMyMsg.cpp)
target_link_libraries(UseMyMsg ${catkin_LIBRARIES})
exbot@ubuntu:~$ roscore
exbot@ubuntu:~$ rosrun communication UseMyMsg
rosrun rqt_plot rqt_plot
REF:
https://blog.csdn.net/CSDNhuaong/article/details/78601639