Using the new srv and msg files

First, we are going to learn how to create a service and how to use it in ROS. Our service will calculate the sum of three numbers. We need two nodes: a server and a client.

In the chapter2_tutorials package, create two new nodes with the following names: example2_a.cpp and example2_b.cpp. Remember to put the files in thesrc folder.

In the first file, example2_a.cpp, add the following code:

#include "ros/ros.h" 
#include "chapter2_tutorials/chapter2_srv1.h" 
bool add(chapter2_tutorials::chapter2_srv1::Request  &req, 
         chapter2_tutorials::chapter2_srv1::Response &res) 
  res.sum = req.A + req.B + req.C; 
  ROS_INFO("request: A=%ld, B=%ld C=%ld", (int)req.A, (int)req.B,  (int)req.C); ROS_INFO("sending back response: ...

Get ROS Programming: Building Powerful Robots now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.