ROS C++

ROS C++节点创建及基础功能。


  1. ROS节点
    1. ROS节点-消息
    2. ROS节点-服务
  2. ROS消息
    1. ROS消息-包含消息
    2. ROS消息-创建msg
  3. ROS服务
    1. ROS服务-创建srv

ROS节点

  1. 消息发布器和订阅器程序结构
    #include "ros/ros.h"
    #include "std_msgs/String.h"
    void chatterCallback(const std_msgs::String::ConstPtr& msg)
    {
    ROS_INFO("I heard: [%s]", msg->data.c_str());
    }
    int main(int argc, char **argv)
    {
      ros::init(argc, argv, "talker");
      ros::NodeHandle n;
      ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter1", 1000);
      ros::Subscriber sub = n.subscribe("chatter2", 1000, chatterCallback);
      ros::Rate loop_rate(10);
      int count = 0;
      while (ros::ok())
      {
        std_msgs::String msg;
        //do sth
        chatter_pub.publish(msg);
        ros::spinOnce();
        loop_rate.sleep();
      }
      return 0;
    }
  2. 服务端和服务客户端程序结构
    • 若使用ros::spin()将进入循环,直到CTRL+C或Master退出
    • 服务函数返回布尔值,返回true表示完成
      #include "ros/ros.h"
      #include "beginner_tutorials/AddTwoInts.h"
      bool add(beginner_tutorials::AddTwoInts::Request  &req,
       beginner_tutorials::AddTwoInts::Response &res)
      {
      res.sum = req.a + req.b;
      return true;
      }
      int main(int argc, char **argv)
      {
      ros::init(argc, argv, "add_two_ints_server");
      ros::NodeHandle n;
      ros::ServiceServer service = n.advertiseService("add_two_ints", add);
      ros::spin();
      return 0;
      }
      int main(int argc, char **argv)
      {
      ros::init(argc, argv, "add_two_ints_client");
      ros::NodeHandle n;
      ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");
      beginner_tutorials::AddTwoInts srv;
      srv.request.a = 1;
      srv.request.b = 2;
      if (client.call(srv))
          ROS_INFO("Sum: %ld", (long int)srv.response.sum);
      else
          ROS_ERROR("Failed to call service add_two_ints");
      return 0;
      }

Messages

包含消息


1.

    #include <std_msgs/String.h>
    std_msgs::String msg;

创建msg

  1. msg文件在包的msg/文件夹内
    Header header
    string child_frame_id
    geometry_msgs/PoseWithCovariance pose
    geometry_msgs/TwistWithCovariance twist
  2. 在package.xml中添加message_generation和message_runtime依赖
    <build_depend>message_generation</build_depend>
    <exec_depend>message_runtime</exec_depend>
  3. 在CMakeLists.txt中添加message_generation和message_runtime依赖
    find_package(catkin REQUIRED COMPONENTS
           roscpp
           rospy
           std_msgs
           message_generation
    )
    catkin_package(
      ...
      CATKIN_DEPENDS message_runtime ...
      ...)
  4. 在CMakeLists.txt中添加msg文件,并执行generate_messages()
    add_message_files(
          FILES
          Num.msg
    )
    generate_messages(
          DEPENDENCIES
          std_msgs
    )

Services

创建srv

  1. srv文件在包的srv/文件夹内,由request和response构成,由—分隔

    int64 A
    int64 B
    ---
    int64 Sum
  2. 添加srv文件,其他步骤均与创建msg相同

    add_service_files(
      FILES
      AddTwoInts.srv
    )

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注