Course 4
ROS Service
- 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
- The structure of the service is very similar to the information messages, which are defined in *.srv
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
- Structure diagram:
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; }
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
- Similar to service calls, but generally used to provide task cancellation and process feedback;
- is the best way to achieve time-expanding, goal-directed behavior;
- Similar in structure to services, it is defined by *.action files;
- Internally, behavior is implemented by a series of topics;
- Structure diagram:
Comparison of ROS parameters, dynamic reconfiguration, themes, services and behaviors
ROS Time
- In general, ROS uses the PC's system clock as the time source
- For simulation or playback of recorded data, it is convenient to use the simulation time (pause, slow down, etc.).
To use an analog clock:
Set the /use_sim_time parameter:
> rosparam set use_sim_time true
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
- A package is a format for storing message data
- Use extended format *.bag to store binary format
- Suitable for recording and recording datasets for later visual analysis
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