ROS基础学习笔记(五):自定义消息练习以及订阅者发布者节点代码简介

ROS中我们自定义的消息文件以".msg"作为后缀名,我们在功能包文件夹下新建msg文件路径,并且在其中:

vim Hello.msg
string Hello    //定义消息
int64 Count    //定义数据

返回到功能包文件夹下的src文件夹中

vim TheTalker2.cpp
#include 
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "rostest2/Hello.h"
    /*
        This header file is created by ROS with Config information
    */
int main(int argc , char ** argv)
{
    ros::init( argc , argv , "talker");
    /*
        named node "talker" and initialize it
    */
    ros::NodeHandle nh;
    /*
        get a node handle to operate the package
    */
    ros::Publisher Sender = nh.advertise( "Sender" , 1000 );
    /*
        PAY ATTENTION!!! You should use 
            package::.msg file's name!!
        named a Sender to be Publisher and give 
        it node handle , the length of queue is
        1000 , if too long then quit
    */
    ros::Rate loop_rate(10);
    /*
        wait for 10 hz each times,equals 100ms
    */

    int count = 0;
    rostest2::Hello msg;

    while( ros::ok )
    {
        std::stringstream ss;
        ss << "Hello World by sender: " ;
        msg.Hello = ss.str();
        msg.Count = count;
        ROS_INFO( "Send Succeed %d" , count );
        Sender.publish(msg);

        ros::spinOnce();
        loop_rate.sleep();

        count++;
    }

    return 0;
}
vim TheListener2.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "rostest2/Hello.h"

void SenderCallback( const rostest2::Hello::ConstPtr& msg )
{
    /*
        When Sender sending a message , this founction will do something
        ROS_INFO will misunderstand what you want to say , so , remember
        add ".c_str()" to transfer format
    */
    ROS_INFO( " Receive : [%s] [%d] " , msg->Hello.c_str() , msg->Count );
}

int main( int argc , char ** argv )
{
    ros::init( argc , argv , "Receiver" );
    /*
        named node "Receiver" and initialize it
    */
    ros::NodeHandle nh;
    /*
        get a node handle to operate the package
    */
    ros::Subscriber rec = nh.subscribe( "Sender" , 1000 , SenderCallback );
    /*
        named a rec to be Subscriber and give 
        it node handle , the length of queue is
        1000 , if too long then quit
    */
    ros::spin();
    /*
        if there are no message , just wait .
    */
    return 0;
}

上次讲到这里就结束了,但其实还有两个重要的文件需要修改,分别是CMakelist.txt 和package.xml , 用于添加依赖辅助编译

vim package.xml
message_generation
......
message_generation
......
message_runtime

看到这一堆depend,想必就不用多说了吧

vim CMakeLists.txt

剩下的部分由于太过冗长,仅挑选必要部分列出,ROS生成的源文档中这些大部分都被注释,只需删除注释符号并补充部分内容即可

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)
/*
    All of these files is the ROS system dependencies at compile time,
    but we haven't used message_generation when we create package
*/
add_message_files(
   FILES
   Hello.msg
 )
 generate_messages(
   DEPENDENCIES
   std_msgs
 )
catkin_package(
  INCLUDE_DIRS include
  LIBRARIES rostest2
  CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
  DEPENDS system_lib
)
include_directories(
 include
  ${catkin_INCLUDE_DIRS}
)
add_executable( TheTalker2 src/TheTalker2.cpp)
add_executable( TheListener2 src/TheListener2.cpp)
//add our files
target_link_libraries( TheTalker2 ${catkin_LIBRARIES} )
target_link_libraries( TheListener2 ${catkin_LIBRARIES} )

这些都编辑完成后任务就结束啦,下面编译一下,可使用之前定义的cm命令或者cd ~/catkin_ws , catkin_make

ROS基础学习笔记(五):自定义消息练习以及订阅者发布者节点代码简介_第1张图片

ROS基础学习笔记(五):自定义消息练习以及订阅者发布者节点代码简介_第2张图片

ROS基础学习笔记(五):自定义消息练习以及订阅者发布者节点代码简介_第3张图片

ROS基础学习笔记(五):自定义消息练习以及订阅者发布者节点代码简介_第4张图片

 

你可能感兴趣的:(ROS)