Ask Your Question
0

Using array as parameter in service

asked 2016-06-06 06:33:54 -0500

Ravi Joshi gravatar image

updated 2016-06-06 06:37:00 -0500

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?

edit retag flag offensive close merge delete

Comments

The existing tutorial can be found here http://wiki.ros.org/ROS/Tutorials/Wri...

Ravi Joshi gravatar image Ravi Joshi  ( 2016-06-06 06:37:43 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-06-06 08:54:28 -0500

JohnDoe2991 gravatar image

updated 2016-06-06 08:57:51 -0500

Simple, but tricky error: You are using the vector's fill constructor here:

  std::vector<double> a(3);
  std::vector<double> b(3);

a and b will already be filled with zeros like this:

a = [0 0 0]
b = [0 0 0]

with the command push_back you'll add another three values:

a = [0 0 0 1 2 3]
b = [0 0 0 4 5 6]

but this values won't be used in the calculation on your server, since your loop only iterates over the first three elements.

So either don't fill your vector in the beginning or don't use push_back

edit flag offensive delete link more

Comments

Thanks a lot.. You really saved my time. I was debugging it but couldn't figure out the actual mistake.

Ravi Joshi gravatar image Ravi Joshi  ( 2016-06-06 09:35:30 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-06-06 06:33:54 -0500

Seen: 2,900 times

Last updated: Jun 06 '16