Introduction to ROS (4)

Course 4

ROS Service

  1. The information exchange between the request and the reply is realized by the service Service
    • The server of the service server sends information to the service
    • The client of the service receives information from this server
  2. The structure of the service is very similar to the information messages, which are defined in *.srv
  3. code

    • List of services:

      > rosservice list
      
    • Show service type:

      > rosservice type /servoce_name
      
    • Call a service with the request content:

      > rosservice call /service_name args
      
  4. Structure diagram:

write picture description here

  1. Service example: add_two_ints_server.cpp

    #include <ros/ros.h>
    #include <roscpp_tutorials/TwoInts.h>
    bool add(roscpp_tutorials::TwoInts::Request &request,
    roscpp_tutorials::TwoInts::Response &response)
    {
        response.sum = request.a + request.b;
        ROS_INFO("request: x=%ld, y=%ld", (long int)request.a,
        (long int)request.b);
        ROS_INFO(" sending back response: [%ld]",
        (long int)response.sum);
        return true;
    }
    int main(int argc, char **argv)
    {
        ros::init(argc, argv, "add_two_ints_server");
        ros::NodeHandle nh;
        ros::ServiceServer service =
        nh.advertiseService("add_two_ints", add);
        ros::spin();
        return 0;
    }
    
  2. Client example: add_two_ints_client.cpp

    #include <ros/ros.h>
    #include <roscpp_tutorials/TwoInts.h>
    #include <cstdlib>
    int main(int argc, char **argv) {
        ros::init(argc, argv, "add_two_ints_client");
        if (argc != 3) {
            ROS_INFO("usage: add_two_ints_client X Y");
            return 1;
        }
        ros::NodeHandle nh;
        ros::ServiceClient client =
        nh.serviceClient<roscpp_tutorials::TwoInts>("add_two_ints");
        roscpp_tutorials::TwoInts service;
        // atoi(): 把字符串转换成整型数
        service.request.a = atoi(argv[1]);
        service.request.b = atoi(argv[2]);
        if (client.call(service)) {
            ROS_INFO("Sum: %ld", (long int)service.response.sum);
        } else {
            ROS_ERROR("Failed to call service add_two_ints");
            return 1;
        }
            return 0;
    } 
    

ROS Action behavior

  1. Similar to service calls, but generally used to provide task cancellation and process feedback;
  2. is the best way to achieve time-expanding, goal-directed behavior;
  3. Similar in structure to services, it is defined by *.action files;
  4. Internally, behavior is implemented by a series of topics;
  5. Structure diagram:

write picture description here

Comparison of ROS parameters, dynamic reconfiguration, themes, services and behaviors

write picture description here

ROS Time

  1. In general, ROS uses the PC's system clock as the time source
  2. For simulation or playback of recorded data, it is convenient to use the simulation time (pause, slow down, etc.).
  3. To use an analog clock:

    • Set the /use_sim_time parameter:

      > rosparam set use_sim_time true
      
  4. ROS Time API:

    • ros :: Time

      ros::Time begin = ros::Time::now();
      double secs = begin.toSec();
      
    • ros::Duration

      ros::Duration duration(0.5); //0.5s
      
    • ros::Rate

      ros::Rate rate(10); //10Hz
      

ROS Bags data logging package

  1. A package is a format for storing message data
  2. Use extended format *.bag to store binary format
  3. Suitable for recording and recording datasets for later visual analysis
  4. Code:

    • Log all topics in the package:

      > rosbag record --all
      
    • Log a given topic:

      > rosbag record topic_1 topic_2
      
    • Display information about a package:

      > rosbag info bag_name.bag
      
    • Read and publish the contents of a package:

      > rosbag play bag_name.bag
      
    • Define playback options:

      > rosbag play --rate=0.5 bag_name.bag
      
      • –rate=factor : Publish rate factor;
      • –clock : Publish the clock time (set param use_sim_time to true)
      • –loop : Loop playback

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772526&siteId=291194637