Using array as parameter in service
I am using array as input and output in a service. I am trying to extend the existing tutorial) for getting started. Below is the client code-
#include "ros/ros.h"
#include "beginner_tutorials/AddTwoInts.h"
#include <cstdlib>
int main(int argc, char **argv) {
ros::init(argc, argv, "add_two_array_client");
ros::NodeHandle n;
ros::ServiceClient client =
n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_array");
beginner_tutorials::AddTwoInts srv;
std::vector<double> a(3);
std::vector<double> b(3);
a.push_back(1.0);
a.push_back(2.0);
a.push_back(3.0);
b.push_back(4.0);
b.push_back(5.0);
b.push_back(6.0);
srv.request.a = a;
srv.request.b = b;
if (client.call(srv)) {
for (int i = 0; i < 3; i++) {
ROS_INFO("Sum: %f", (float)srv.response.sum[i]);
}
} else {
ROS_ERROR("Failed to call service add_two_array");
return 1;
}
return 0;
}
Following is the server code-
#include "ros/ros.h"
#include "beginner_tutorials/AddTwoInts.h"
bool add(beginner_tutorials::AddTwoInts::Request &req,
beginner_tutorials::AddTwoInts::Response &res) {
res.sum.resize(3);
for (int i = 0; i < 3; i++) {
res.sum[i] = req.a[i] + req.b[i];
ROS_INFO("request: x=%f, y=%f", req.a.at(i), req.b[i]);
ROS_INFO("sending back response: [%f]", res.sum[i]);
}
return true;
}
int main(int argc, char **argv) {
ros::init(argc, argv, "add_two_array_server");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("add_two_array", add);
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;
}
The srv
file looks like following-
float64[] a
float64[] b
---
float64[] sum
While running, the input parameters shows 0 value. Below is the snippet from terminal-
[ INFO] [1465212136.691882517, 8990.505000000]: request: x=0.000000, y=0.000000
[ INFO] [1465212136.691913058, 8990.505000000]: sending back response: [0.000000]
How to use array as parameter in service?
The existing tutorial can be found here http://wiki.ros.org/ROS/Tutorials/Wri...