[ROS study notes] (5) The definition and use of topic messages

First, the purpose

In ROS Master, you can publish and subscribe to defined messages, such as the movement and pose of the turtle. But sometimes we need to define the type of message ourselves.

The main purpose of this section is to define a person's personal information, Publisher publishes personal information, and Subscriber subscribes to personal information.

2. Custom topic message

1. Define the msg file

learning_topicCreate a new folder in the function package folder, create a file msgin this folder Person.msg, and add the following code to it

string name
uint8 sex
uint8 age

uint8 unknown=0
uint8 male=1
uint8 female=2

Note: uint8And string, it needs to be expanded to the corresponding format in different programs, so some configuration is required first.
Insert picture description here

2. Add the function package dependency in the package.xml file

At the end of the file, add the following code

<build_depend>message_generation</build_depend><exec_depend>message_runtime</exec_depend>

build_depend, compile dependency, rely on a function package that dynamically generates message

exec_depend, execution dependency, a function package that depends on the running time of the message
Insert picture description here

3. Add compilation options in CMakeLists.txt

First, find_packageadd a statement therein in order to add function package dependency

find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  roscpp
  rospy
  std_msgs
  turtlesim
  message_generation
)

Insert picture description here

Add below this function

add_message_files(
  FILES
  Person.msg
)

generate_messages(
  DEPENDENCIES
  std_msgs
)

add_message_files, using Person.msg as the defined interface

generate_messages, a function package that needs to be relied upon when compiling the Person.msg file
Insert picture description here

Then below catkin specific configurationin catkin_packages, add-dependent message_runtime, the modified code as follows:

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES learning_topic
   CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim message_runtime
#  DEPENDS system_lib
)

Insert picture description here

4. Compile and generate related files
cd ~/catkin_ws
catkin_make

Three, create a publisher

~/catkin_ws/src/learning_topic/srcCreate a person_publisher.cppfile in the folder with the following content:

/**
 * 该例程将发布/person_info话题,自定义消息类型learning_topic::Person
 REFERENC:www.guyuehome.com.
 */
 
#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 = "huffie";
		person_msg.age  = 21;
		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;
}

Fourth, create subscribers

~/catkin_ws/src/learning_topic/srcCreate a person_subscriber.cppfile in the folder with the following content:

/**
 * 该例程将订阅/person_info话题,自定义消息类型learning_topic::Person
 REFERENC:www.guyuehome.com.
 */
 
#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;
}

Insert picture description here

Five, configure publisher/subscriber code compilation rules

Add the following code in the CMakeLists.txtfile buildarea

add_executable(person_publisher src/person_publisher.cpp)
target_link_libraries(person_publisher ${catkin_LIBRARIES})
add_dependencies(person_publisher ${PROJECT_NAME}_generate_messages_cpp)

add_executable(person_subscriber src/person_subscriber.cpp)
target_link_libraries(person_subscriber ${catkin_LIBRARIES})
add_dependencies(person_subscriber ${PROJECT_NAME}_generate_messages_cpp)

Insert picture description here

Six, compile and run

First compile, go back to the main directory

cd ~/catkin_ws
catkin_make

Run roscore

roscore

Run Subscriber

rosrun learning_topic person_subscriber

Run Publisher

rosrun learning_topic person_Publisher

You can see that the publisher is publishing personal information, and the subscriber is receiving information.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44543463/article/details/114108345