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

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

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

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

vim TheTalker2.cpp
#include <sstream>
#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<rostest2::Hello>( "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
<build_depend>message_generation</build_depend>
......
<build_export_depend>message_generation</build_export_depend>
......
<exec_depend>message_runtime</exec_depend>

看到这一堆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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值