Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Syntax for extracting string array from srv response in C++

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!