ROS学习之路(四)——topic_demo

一、功能描述

两个node,一个发布模拟的GPS消息(格式为自定义,包括坐标和工作状态),另外一个接受并处理信息(计算到远点的距离)。

二、步骤

  1. package
  2. msg
  3. talker.cpp
  4. listener.cpp
  5. CMakeList.txt&package.xml

三、创建package&msg

工作目录前面已经创建过,参考:链接: https://blog.csdn.net/qq_35259785/article/details/108281721.

$ cd catkin_ws/src
$ cakin_create_pkg topic_demo roscpp rospy std_msgs
$ cd topic_demo
$ mkdir msg
$ cd msg
$ vim gps.msg

gps.msg的内容:

float32 x
float32 y
string state

然后进行编译

$ cd ~/catkin_ws/
$ catkin_make

编译完以后会在devel/include下面生成topic_demo文件夹,包含了gps.h。

四、创建talker.cpp文件

在topic_demo/src目录下创建talker.cpp

$ cd src/topic_demo/src
$ vim talker.cpp

API接口详细用法:链接: https://docs.ros.org/api/roscpp/html/.
talker.cpp

#include<ros/ros.h>
#include<topic_demo/gps.h>
int main(int argc, char** argv){
       ros::init(argc, argv, "talker");                                //解析参数命名节点
    ros::NodeHandle nh;                                               //创建句柄,实例化node
    topic_demo:: gps msg;                                             //创建gps消息
    msg.x = 1.0 ; 
    msg.y = 1.0;
    msg.state = "working";
    ros::Publisher pub = nh.advertise<topic_demo::gps>("gps_info",1); //创建publisher
    pub.publish(msg);                                                 //发布消息
    ros::Rate loop_rate(1.0);                                         //定义循环发布频率1hz
    while(ros::ok()){
            msg.x = 1.03*msg.x;
            msg.y = 1.03*msg.y;
            ROS_INFO("Talker:GPS:x=%f,y=%f",msg.x,msg.y);             //输出当前msg
            pub.publish(msg);                                         //发布消息
            loop_rate.sleep();                                        //休眠
    } 
    return 0;
}

这种模板适合周期性信息。

五、创建listener.cpp

listener.cpp的编辑在robotware里面进行。

#include<ros/ros.h>
#include<topic_demo/gps.h>
#include<std_msgs/Float32.h>


void gpsCallback(const topic_demo::gps::ConstPtr &msg ){
    std_msgs::Float32 distance;
    distance.data = sqrt(pow(msg->x,2)+pow(msg->y,2));
    ROS_INFO("Listener:Distance to origin = %f, state = %s", distance.data,msg->state.c_str());
}

int main(int argc, char** argv){
    ros::init(argc, argv, "listener");
    ros::NodeHandle n;
    ros::Subscriber sub = n.subscribe("gps_info",1,gpsCallback); //创建subscriber
    ros::spin();                                                 //反复调用当前可触发的回调函数,阻塞
    return 0;
}

六、修改CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)
project(topic_demo)
find_package(catkin REQUIRED COMPONENTS message_generation roscpp rospy std_msgs)#添加指定依赖项
add_message_files(FILES gps.msg)#添加自定义的msg
generate_messages(DEPENDENCIES std_msgs)#生成msg对应的头文件

catkin_package(CATKIN_DEPENDS roscpp rospy std_msgs message_runtime)#用于配置ROS和package配置文件和Cmake文件
include_directories(include ${catkin_INCLUDE_DIRS})#指定C/C++的头文件

add_executable(talker src/talker.cpp)#生成可执行文件
add_dependencies(talker topic_demo_generate_messages_cpp) #添加依赖项
target_link_libraries(talker ${catkin_LIBRARIES}) #链接

add_executable(listener src/listener.cpp)
add_dependencies(listener topic_demo_generate_messages_cpp)
target_link_libraries(listener ${catkin_LIBRARIES})

七、package.xml

<?xml version="1.0"?>
<package>
  <name>topic_demo</name>
  <version>0.0.0</version>
  <description>The topic_demo package</description>
  <maintainer email="ros@todo.todo">ros</maintainer>
  <license>BSD</license>
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>message_generation</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>rospy</run_depend>
  <run_depend>std_msgs</run_depend>
   <run_depend>message_runtime</run_depend>
  <export>
  </export>
</package>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值