0
$\begingroup$

Rosanswers logo

I'm trying to call a service and get a response from my ROS package. The specific srv is gazebo_msgs/GetWorldProperties, which is as follows:

---
float64 sim_time
string[] model_names
bool rendering_enabled
bool success
string status_message

I can receive the response, but I cannot figure out how to extract the string array ("model_names") from it in C++. Could anyone give a snippet example of code that would allow me to loop through this string array? The current code (which I just took a wild guess at, but obviously doesn't work) is as follows:

#include "ros/ros.h"
#include <gazebo_msgs/GetWorldProperties.h>

int main(int argc, char **argv)
{
    ros::init(argc, argv, "simWrapper");
    ros::NodeHandle n;
    ros::ServiceClient client = n.serviceClient<gazebo_msgs::GetWorldProperties>("gazebo/get_world_properties");
    gazebo_msgs::GetWorldProperties worldPropSrv;
    if (client.call(worldPropSrv))
    {
        std::vector<String> models = worldPropSrv.response.model_names;
        ROS_INFO("Number of models: %i", models.size());
    }
    else
    {
        ROS_ERROR("Failed to call service get_world_properties");
        return 1;
    }
    return 0;
}

Thanks!


Originally posted by Jordan9 on ROS Answers with karma: 113 on 2015-04-01

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

ROS' built-in string msg type is mapped onto std::string in C++, while ROS msg arrays are mapped onto std::vector<T> (from wiki/msg - 2.1.1 Field Types). Changing std::vector<String> to std::vector<std::string> should work (but in this case the copy is unnecessary, you could just work directly with the message).


Originally posted by gvdhoorn with karma: 86574 on 2015-04-02

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by Jordan9 on 2015-04-03:
Thank you, that makes way more sense now!

Comment by Will Chamberlain on 2016-02-01:
I'm with Jordan9 on this one: I'm pretty new to C++ and to me that typing is not obvious from wiki/msg - 2.1.1 Field Types.

Comment by gvdhoorn on 2016-02-01:
While I can sympathise, the information given in the wiki should be more than sufficient when starting with ROS, but already knowing some C++. The Array handling section clearly states that arrays are mapped onto std::vector, with string mapped onto std::string. The rest is normal C++.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.