ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Return array in service in c++

asked 2016-07-11 12:50:09 -0600

javietos gravatar image

updated 2016-07-11 13:48:44 -0600

Hi people! I'm starting with ROS and I have a problem implementing a client and server. I used the files created in the tutorials of ROS but I would like to return an array of strings and I don't know how to do it. These are my codes:

get_element_server.cpp:

#include "ros/ros.h"
#include "beginner_tutorials/GetElement.h"

bool add(beginner_tutorials::GetElement::Request  &req,
         beginner_tutorials::GetElement::Response &res)
{
  res.result = {"table", "chair"};
  ROS_INFO("request: x=%s", req.name.c_str());
  ROS_INFO("sending back response: [%s] [%s]", res.result[0].c_str(), res.result[1].c_str());
  return true;
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "get_element_server");
  ros::NodeHandle n;

  ros::ServiceServer service = n.advertiseService("get_element", add);
  ROS_INFO("Ready to get element.");
  ros::spin();

  return 0;
}

get_element_client.cpp:

#include "ros/ros.h"
#include "beginner_tutorials/GetElement.h"
#include <cstdlib>

int main(int argc, char **argv)
{
  ros::init(argc, argv, "get_element_client");
  if (argc != 2)
  {
    ROS_INFO("usage: get_element_client name");
    return 1;
  }

  ros::NodeHandle n;
  ros::ServiceClient client = n.serviceClient<beginner_tutorials::GetElement>("get_element");
  beginner_tutorials::GetElement srv;
  srv.request.name = argv[1];
  if (client.call(srv))
  {
    ROS_INFO("Elements: %s, %s", srv.response.result[0].c_str(), srv.response.result[1].c_str());
  }
  else
  {
    ROS_ERROR("Failed to call service get_element");
    return 1;
  }

  return 0;
}

GetElement.h

string name
---
string[] result

The error that I obtain is the following when I use 'catkin_make':

/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp: In function ‘bool add(beginner_tutorials::GetElement::Request&, beginner_tutorials::GetElement::Response&)’:
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:7:7: error: ‘beginner_tutorials::GetElement::Response’ has no member named ‘result’
   res.result = {"table", "chair"};
       ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:7:33: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
   res.result = {"table", "chair"};
                                 ^
In file included from /opt/ros/indigo/include/ros/ros.h:40:0,
                 from /home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:1:
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:8:33: error: ‘beginner_tutorials::GetElement::Request’ has no member named ‘name’
   ROS_INFO("request: x=%s", req.name.c_str());
                                 ^
/opt/ros/indigo/include/ros/console.h:342:165: note: in definition of macro ‘ROSCONSOLE_PRINT_AT_LOCATION_WITH_FILTER’
     ::ros::console::print(filter, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__)
                                                                                                                                                                     ^
/opt/ros/indigo/include/ros/console.h:375:7: note: in expansion of macro ‘ROSCONSOLE_PRINT_AT_LOCATION’
       ROSCONSOLE_PRINT_AT_LOCATION(__VA_ARGS__); \
       ^
/opt/ros/indigo/include/ros/console.h:557:35: note: in expansion of macro ‘ROS_LOG_COND’
 #define ROS_LOG(level, name, ...) ROS_LOG_COND(true, level, name, __VA_ARGS__)
                                   ^
/opt/ros/indigo/include/rosconsole/macros_generated.h:110:23: note: in expansion of macro ‘ROS_LOG’
 #define ROS_INFO(...) ROS_LOG(::ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
                       ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:8:3: note: in expansion of macro ‘ROS_INFO’
   ROS_INFO("request: x=%s", req.name.c_str());
   ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:9:52: error: ‘beginner_tutorials::GetElement::Response’ has no member named ‘result’
   ROS_INFO("sending back response: [%s] [%s]", res.result[0].c_str(), res.result[1].c_str());
                                                    ^
/opt/ros/indigo/include/ros/console ...
(more)
edit retag flag offensive close merge delete

Comments

It looks like most of the error message is missing; please include the full error message from the compiler.

ahendrix gravatar image ahendrix  ( 2016-07-11 13:10:55 -0600 )edit

Now I include all the error message, if you prefer I can add some images to see it better.

javietos gravatar image javietos  ( 2016-07-11 13:49:33 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2016-07-11 14:24:46 -0600

ahendrix gravatar image

The first errors messages are often the most important. In your errors the important ones seem to be:

/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:7:7: error: ‘beginner_tutorials::GetElement::Response’ has no member named ‘result’

and

/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:8:33: error: ‘beginner_tutorials::GetElement::Request’ has no member named ‘name’

these errors are repeated several more times in various different contexts within your code.

These errors seem odd, because as listed these fields are present in your service definition.

I would start by checking:

  • You list your service definition file as GetElement.h; this should be GetElement.srv
  • You should confirm that C++ headers are being generated from your service definition. They should be automatically generated in the devel/include/path/to/package/GetElement.h file in your catkin workspace.
  • Check that you're including the service definition from the correct package
edit flag offensive delete link more

Comments

Sorry, my fault. I had the names in spanish and at the time of changing them to english I forgot those. Now the error is much shorter.

javietos gravatar image javietos  ( 2016-07-11 14:47:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-11 12:50:09 -0600

Seen: 1,907 times

Last updated: Jul 11 '16