1、话题消息的定义和使用
1.1话题模型
自定义消息的类型,并创建发布者和订阅者,利用话题进行传输。 发布者:Publihser(person_publisher) 发布者发布Message(learning_topic::Person) 话题:Topic(person_info) 订阅者:Subscriber(person_subscriber)
1.2自定义话题消息
strin name
uint8 sex
uint8 age
uint8 unknow = 0
uint8 male = 1
uint8 female = 2
将上面内容放在Person.msg中。 在package.xml中添加功能包依赖
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
find_package( …… message_generation)
add_message_files(FILES Person.msg)
generate_messages(DEPENDENCIES std_msgs)
catkin_package(…… message_runtime)
1.3话题消息具体步骤
1.3.1打开功能包下面并创建文件夹
ubuntu@ubuntu:~/catkin_ws$ cd ~/catkin_ws/src/learning_topic/
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ mkdir msg
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ ls
CMakeLists.txt include msg package.xml scripts src
1.3.2创建文件并输入内容
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ cd msg/
ubuntu@ubuntu:~/catkin_ws/src/learning_topic/msg$ touch Person.msg
ubuntu@ubuntu:~/catkin_ws/src/learning_topic/msg$ gedit Person.msg
strin name
uint8 sex
uint8 age
uint8 unknow = 0
uint8 male = 1
uint8 female = 2
1.3.3修改配置文件package.xml
ubuntu@ubuntu:~/catkin_ws/src/learning_topic/msg$ cd ../
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ ls
CMakeLists.txt include msg package.xml scripts src
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ gedit package.xml
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
<build_depend>xxx</build_depend>是编译依赖,依赖于xxx包 <exec_depend>xxx</exec_depend>是执行依赖,依赖于运行的xxx包 上面两个依赖是进行创建话题消息的固定的依赖包。 添加之后保存退出
1.3.4修改配置文件CMakeLists.txt
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ ls
CMakeLists.txt include msg package.xml scripts src
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ gedit CMakeLists.txt
打开后再合适的地方添加,根据关键词查位置:find_package、add_message_files、generate_messages、catkin_package
# 一般在开头
find_package(
xxx #之前的依赖包
message_generation
)
## Declare ROS messages, services and actions ##
add_message_files(
FILES
Person.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
## catkin specific configuration ##
catkin_package(
xxx #之前的依赖包
CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim
message_runtime
)
1.3.5回到根目录下做编译
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ cd ~/catkin_ws/
ubuntu@ubuntu:~/catkin_ws$ catkin_make
ROS会根据编写的固定格式转换成C++的数据类型定义,进行使用,转换完成的内容在/catkin_ws/devel/include/learning_topic中
1.4发布者和订阅者的编写
ubuntu@ubuntu:~/catkin_ws/devel/include/learning_topic$ roscd learning_topic/
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ ls
CMakeLists.txt include msg package.xml scripts src
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ cd src/
ubuntu@ubuntu:~/catkin_ws/src/learning_topic/src$ touch person_publisher.cpp
ubuntu@ubuntu:~/catkin_ws/src/learning_topic/src$ touch person_subscriber.cpp
ubuntu@ubuntu:~/catkin_ws/src/learning_topic/src$ gedit person_publisher.cpp
#include <ros/ros.h>
#include "learning_topic/Person.h"
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "person_publisher");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10
ros::Publisher person_info_pub = n.advertise<learning_topic::Person>("/person_info", 10);
// 设置循环的频率
ros::Rate loop_rate(1);
int count = 0;
while (ros::ok())
{
// 初始化learning_topic::Person类型的消息
learning_topic::Person person_msg;
person_msg.name = "Tom";
person_msg.age = 18;
person_msg.sex = learning_topic::Person::male;
// 发布消息
person_info_pub.publish(person_msg);
ROS_INFO("Publish Person Info: name:%s age:%d sex:%d",
person_msg.name.c_str(), person_msg.age, person_msg.sex);
// 按照循环频率延时
loop_rate.sleep();
}
return 0;
}
ubuntu@ubuntu:~/catkin_ws/src/learning_topic/src$ gedit person_subscriber.cp
#include <ros/ros.h>
#include "learning_topic/Person.h"
// 接收到订阅的消息后,会进入消息回调函数
void personInfoCallback(const learning_topic::Person::ConstPtr& msg)
{
// 将接收到的消息打印出来
ROS_INFO("Subcribe Person Info: name:%s age:%d sex:%d",
msg->name.c_str(), msg->age, msg->sex);
}
int main(int argc, char **argv)
{
// 初始化ROS节点
ros::init(argc, argv, "person_subscriber");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback
ros::Subscriber person_info_sub = n.subscribe("/person_info", 10, personInfoCallback);
// 循环等待回调函数
ros::spin();
return 0;
}
ubuntu@ubuntu:~/catkin_ws/src/learning_topic$ cd ~/catkin_ws/
ubuntu@ubuntu:~/catkin_ws$ catkin_make
Base path: /home/ubuntu/catkin_ws
Source space: /home/ubuntu/catkin_ws/src
Build space: /home/ubuntu/catkin_ws/build
Devel space: /home/ubuntu/catkin_ws/devel
Install space: /home/ubuntu/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/ubuntu/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/ubuntu/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/ubuntu/catkin_ws/devel;/opt/ros/melodic
-- This workspace overlays: /home/ubuntu/catkin_ws/devel;/opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.17", minimum required is "2")
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/ubuntu/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python2 (found version "2.7.17")
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.29
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 2 packages in topological order:
-- ~~ - learning_topic
-- ~~ - py_learning_topic
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'learning_topic'
-- ==> add_subdirectory(learning_topic)
-- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy
-- learning_topic: 1 messages, 0 services
-- +++ processing catkin package: 'py_learning_topic'
-- ==> add_subdirectory(py_learning_topic)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/catkin_ws/build
####
#### Running command: "make -j1 -l1" in "/home/ubuntu/catkin_ws/build"
####
[ 0%] Built target std_msgs_generate_messages_cpp
[ 0%] Built target _learning_topic_generate_messages_check_deps_Person
[ 6%] Built target learning_topic_generate_messages_cpp
Scanning dependencies of target person_subscriber
[ 13%] Building CXX object learning_topic/CMakeFiles/person_subscriber.dir/src/person_subscriber.cpp.o
[ 20%] Linking CXX executable /home/ubuntu/catkin_ws/devel/lib/learning_topic/person_subscriber
[ 20%] Built target person_subscriber
Scanning dependencies of target person_publisher
[ 26%] Building CXX object learning_topic/CMakeFiles/person_publisher.dir/src/person_publisher.cpp.o
[ 33%] Linking CXX executable /home/ubuntu/catkin_ws/devel/lib/learning_topic/person_publisher
[ 33%] Built target person_publisher
[ 46%] Built target pose_subscriber
Scanning dependencies of target velocity_publisher
[ 53%] Building CXX object learning_topic/CMakeFiles/velocity_publisher.dir/src/velocity_publisher.cpp.o
[ 60%] Linking CXX executable /home/ubuntu/catkin_ws/devel/lib/learning_topic/velocity_publisher
[ 60%] Built target velocity_publisher
[ 60%] Built target std_msgs_generate_messages_nodejs
[ 66%] Built target learning_topic_generate_messages_nodejs
[ 66%] Built target std_msgs_generate_messages_py
[ 80%] Built target learning_topic_generate_messages_py
[ 80%] Built target std_msgs_generate_messages_lisp
[ 86%] Built target learning_topic_generate_messages_lisp
[ 86%] Built target std_msgs_generate_messages_eus
[100%] Built target learning_topic_generate_messages_eus
[100%] Built target learning_topic_generate_messages
1.5运行编写的程序
1.5.1运行roscore
ubuntu@ubuntu:~/catkin_ws$ roscore
1.5.2运行发布者
ubuntu@ubuntu:~/catkin_ws$ rosrun learning_topic person_publisher
1.5.3运行订阅者
ubuntu@ubuntu:~/catkin_ws$ rosrun learning_topic person_subscriber
1.5.4结果说明
ubuntu@ubuntu:~/catkin_ws$ rosrun learning_topic person_publisher
[ INFO] [ 1636890566.998334986 ] : Publish Person Info: name:Tom age:18 sex:1
[ INFO] [ 1636890567.999594084 ] : Publish Person Info: name:Tom age:18 sex:1
ubuntu@ubuntu:~/catkin_ws$ rosrun learning_topic person_subscriber
[ INFO] [ 1636890591.999855074 ] : Subcribe Person Info: name:Tom age:18 sex:1
[ INFO] [ 1636890592.999571488 ] : Subcribe Person Info: name:Tom age:18 sex:1
欢迎关注微信工作号,推送文章更及时。