1.新建msg文件夹,创建定义Person.msg 文件
mkdir -p ~/catkin_ws/src/mypackage/msg
cd ~/catkin_ws/src/mypackage/msg
touch Person.msg
打开 Person.msg 文件,添加内容( std_msgs/Header header 是一个标准格式的头信息(也可以不加))
std_msgs/Header header
string name
uint8 sex
uint8 age
2.修改添加~/catkin_ws/src/mypackage/package.xml 文件中内容
...
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
...
3.修改添加~/catkin_ws/src/mypackage/CMakeList.txt 文件中内容
// 添加生成依赖的功能包
find_package(catkin REQUIRED COMPONENTS
...
message_generation
)
// 添加 catkin 依赖
catkin_package(
...
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
)
// 添加需要编译的.msg文件
add_message_files(
FILES
Person.msg
)
generate_messages(
DEPENDENCIES
std_msgs )
4.查看msg
cd ~/catkin_ws
rosmsg show Person
显示:
[mypackage/Person]:
string name
uint8 sex
uint8 age
1,~/catkin_ws/mypackage/src 目录中,编写俩个源文件talker.cpp和listener.cpp
(编译器编写或文本编写)
talker.cpp 源码:
#include "ros/ros.h"
#include "std_msgs/ "
#include "mypackage/Preson.h" // 包含自定义信息头文件
#include <sstream>
#include <string.h>
int main(int argc, char** argv)
{
//ROS 节点初始化
//创建节点句柄
ros::init(argc, argv, "talker"); // 节点名称
ros::NodeHandle n;
// 话题topic的名称:chatter
ros::Publisher chatter_pub = n.advertise<mypackage::Preson>("chatter", 1000);
ros::Rate loop_rate(10); // 话题发布循环时间
int count = 0; // 计数
std::string name("Allison");
while( ros::ok() )
{
mypackage::Preson msg;
msg.header.seq = count;// 顺序标识
msg.header.stamp = ros::Time::now();// 时间戳
msg.header.frame_id = "t_d_m";// ID
msg.sex = 1;
msg.age = 12;
msg.name = name;
// 终端显示自己发布的消息(可以不加)
ROS_INFO("Publish/\n");
ROS_INFO("Header/\n");
ROS_INFO(" seq [%d]\n", msg.header.seq);
ROS_INFO(" frame_id [%s]\n", msg.header.frame_id.c_str() );
ROS_INFO(" name [%s]\n", msg.name.c_str() );
ROS_INFO(" sex [%d]\n", msg.sex);
ROS_INFO(" age [%d]\n", msg.age);
// Publisher对象 chatter_pub 消息发布
chatter_pub.publish(msg);
ros::spinOnce();// 发布一次
loop_rate.sleep();
++count;// 计数
}
return 0;
}
listener.cpp 源码:
#include <ros/ros.h>
#include "std_msgs/String.h"
#include "mypackage/Preson.h"
// 建立回调函数,当ros::spin()被触发时,该函数被激活,处理接收到的消息
void chatterCalllback(const msg_json::json msg)
{
// 显示订阅的话题 "chatter" 发布的信息
ROS_INFO("Header/\n");
ROS_INFO(" seq [%d]\n", msg.header.seq);
ROS_INFO(" frame_id [%s]\n", msg.header.frame_id.c_str() );
ROS_INFO(" name [%s]\n", msg.name.c_str() );
ROS_INFO(" sex [%d]\n", msg.sex);
ROS_INFO(" age [%d]\n", msg.age);
}
int main(int argc, char** argv)
{
//ROS 节点初始化
//创建节点句柄
ros::init(argc, argv, "listener");
ros::NodeHandle n;
// 创建 publisher
ros::Subscriber sub = n.subscribe( "chatter", 1000, chatterCalllback );
// 触发回调函数,并循环处理,若处理一次则使用ros::spinOnce()
ros::spin();
return 0;
}
2 编译功能包源码(catkin_make方式编译)
打开 CMakeList.txt 文件:
1)设置头文件相对位置,默认为功能包的所在目录
2)生成可执行文件 talker 和 listener
3)设置链接库(系统或第三方库函数)此处并没有使用其他库
4)设置依赖
5) 生成talker和listener 两个可执行文件于 ~/catkin_ws/devel/lib/mypackage 中
CMakeList.txt 添加代码如下:
## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(talker ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
3.终端工作空间根目录 ~/catkin_ws 开始编译
cd ~/catkin_ws
catkin_make
source devel/setup.bash // 已在于终端环境变量文件中设置的无需重新设置
运行ros master
cd ~/catkin_ws
roscore
运行talker
rosrun mypackage talker
运行listenter
rosrun mypackage listener
4.创建launch文件夹,创建启动文件 mypackage.launch
cd catkin_ws/src/mypackage/
mkdir launch
cd launch/
touch mypackage.launch
mypackage.launch文件内容,设置同时启动两个节点:
5.运行launch文件并查看消息(默认会运行roscore)
roslaunch mypackage mypackage.launch
1.显示活动中的话题列表:
rostopic list
2.显示话题消息内容:(不运行 listener)
rostopic echo /chatter
3.发布话题内容:(不运行 talker)
rostopic pub /chatter