欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ros学习之topic实例1

程序员文章站 2022-07-12 09:49:35
...

参考自

https://sychaichangkun.gitbooks.io/ros-tutorial-icourse163/content/chapter6/6.2.html

这里写一个demo:,一个talker,发布gps的信息,一个listener来接收信息,并算出到原点的距离.

创建工作空间

makdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
# 创建一个包叫topic_demo
catkin_create_pkg topic_demo roscpp rospy std_msgs

会发现已生自动生成了这些

.
└── topic_demo
    ├── CMakeLists.txt
    ├── include
    │   └── topic_demo
    ├── package.xml
    └── src

在package.xml里面除了基本的信息之外,以及注释掉的那些之外,就是这几行了

	<buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>std_msgs</exec_depend>

没错这是一些依赖.

在 CMake*.txt里面也是除了


find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
)


其他的都是注释掉的.

创建自定义的msg消息类型

新建 msg/gps.mgs

string state
float32 x
float32 y

内容很好理解,即一个状态,一个坐标的xy,

修改package.xml

在package.xml中要 要加入 “定义编译” 和运行msg 所需要的,即

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

修改完后如下

  <buildtool_depend>catkin</buildtool_depend>
  <buildtool_depend>message_generation</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>std_msgs</exec_depend>
  <exec_depend>message_runtime</exec_depend>


修改CMake*.txt

主要是以下几处

# 添加依赖
find_package(catkin REQUIRED COMPONENTS
  message_generation
  roscpp
  rospy
  std_msgs
)

# 添加msg文件
add_message_files(
   FILES
   gps.msg
   #Message2.msg
)

generate_messages(
   DEPENDENCIES
   std_msgs
 )

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES topic_demo
  CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
#  DEPENDS system_lib
)

include_directories(
 include
  ${catkin_INCLUDE_DIRS}
)

编写发布者talker.cpp

//引入ROS头文件
#include <ros/ros.h>

//引入自定义 ms*生的头文件
#include <topic_demo/gps.h>

int main(int argc, char **argv)
{
    //ros的初始化
    ros::init(argc, argv, "talker");
    //这里的前两个参数用来解析ros参数,第三个参数是这个节点的名字,这里叫talker

    //实例化句柄,初始化该node
    ros::NodeHandle nh;

    //声明自定义的gps 类型的msg, 并给初始值
    topic_demo::gps msg;
    msg.x = 1.0;
    msg.y = 1.0;
    msg.state = "working";

    //创建发布者的实例
    ros::Publisher pub = nh.advertise<topic_demo::gps>("gps_info", 1);

    //定义其发布的频率
    ros::Rate loop_rate(1.0);

    //循环发布msg
    int idx=1;
    while (ros::ok())
    {
        msg.x = 2.0 * msg.x;
        msg.y = 1.0 * msg.y;
        ROS_INFO("Talker: %d times: GPS: x = %.f, y = %.f", msg.x, msg.y);

        //发布消息
        pub.publish(msg);
        //根据前面定义的频率,进行sleep
        loop_rate.sleep();
        idx++;

    }
    return 0;
}

编写 接收者 listener.cpp


#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 dis;
    dis.data = sqrt(pow(msg->x, 2) + pow(msg->y, 2));
    ROS_INFO("listener: dis to origin = %f, state: %s", dis.data, msg->state.c_str());

}

int main(int argc, char **argv){
    ros::init(argc, argv, "listener");
    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe("gps_info", 1, gpsCallback);
    ros::spin();
    return 0;

}