ros----发布者和订阅者模型

话题模型:

ros----发布者和订阅者模型_第1张图片

ros----发布者和订阅者模型_第2张图片

如何自定义话题消息 

1.定义msg文件

ros----发布者和订阅者模型_第3张图片

2.在package.xml中添加功能包依赖

message_generation
message_runtime

 3.在CMakeList.txt文件中添加编译选项

ros----发布者和订阅者模型_第4张图片

4.编译生成语言的相关文件 

ros----发布者和订阅者模型_第5张图片

头文件换成你在msg文件夹中创建的文件,在catkin_make编译之后,在devel/inlcude里面会生成.h文件

ros----发布者和订阅者模型_第6张图片

 将编译规则加入到cMakeLists.txt

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)

创建一个发布者节点,发布一个话题:

/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/

/**
 * 该例程将发布/person_info话题,自定义消息类型learning_topic::Person
 */
 
#include 
//改为添加对应的头文件
#include "learning_topic/Person.h" //#include 

int main(int argc, char **argv)
{
    // ROS节点初始化,定义新的节点名字
    ros::init(argc, argv, "person_publisher");
    //ros::init(argc,argv,"velocity_publisher");

    // 创建节点句柄
    ros::NodeHandle n;

    // 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10
    ros::Publisher person_info_pub = n.advertise("/person_info", 10);
    //ros::Publisher turtle_vel_pub = n.advertise("/turtle1/cmd_vel",10);

    // 设置循环的频率,频率为1Hz
    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;//宏调用
		/*
		geometry_msgs::Twist vel_msg;
		vel_msg.linear.x=2.0;
		vel_msg.angular.z=1.0;
		*/

        // 发布消息
		person_info_pub.publish(person_msg);
		//turtle_vel_pub.publish(vel_msg);

       	ROS_INFO("Publish Person Info: name:%s  age:%d  sex:%d", 
				  person_msg.name.c_str(), person_msg.age, person_msg.sex);
				  //注意:字符串形式的输出:c_str()

        // 按照循环频率延时
        loop_rate.sleep();
    }

    return 0;
}

 创建一个订阅者节点,接收刚才相同话题类型的消息

/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/

/**
 * 该例程将订阅/person_info话题,自定义消息类型learning_topic::Person
 */
 
#include 
#include "learning_topic/Person.h"
//#include 

// 接收到订阅的消息后,会进入消息回调函数
void personInfoCallback(const learning_topic::Person::ConstPtr& msg)
//void poseCallback(const turtlesim::Pose::ConstPtr& msg)//指针
{
    // 将接收到的消息打印出来
    ROS_INFO("Subcribe Person Info: name:%s  age:%d  sex:%d", 
			 msg->name.c_str(), msg->age, msg->sex);
    //ROS_INFO("Turtle pose: x:%0.6f,y:%0.6f",msg->x,msg->y); 
}

int main(int argc, char **argv)
{
    // 初始化ROS节点
    ros::init(argc, argv, "person_subscriber");
    //ros::init(argc,argv,"pose_subscriber");

    // 创建节点句柄
    ros::NodeHandle n;

    // 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback
    ros::Subscriber person_info_sub = n.subscribe("/person_info", 10, personInfoCallback);
    //ros::Subscriber pose_sub=n.subscribe("/turtle1/pose",10,poseCallback);

    // 循环等待回调函数
    ros::spin();

    return 0;
}
c

你可能感兴趣的:(ros,笔记,学习)