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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.