0
$\begingroup$

Rosanswers logo

Hi everyone

I try to spawn a hector quadrotor model in c++ file as a ros node. But it always return "failed to call the service". I attached my code below:

#include "ros/ros.h"
#include <geometry_msgs/Pose.h> 
#include <gazebo_msgs/SpawnModel.h>
#include <stdlib.h>
#include<iostream>
#include<fstream>
#include <tf/transform_datatypes.h>

int main(int argc, char **argv)
{
    ros::init(argc, argv, "commander"); // Node setup
    ros::NodeHandle nh;
    
        std::ifstream ifs;
        ifs.open("quadrotor_hokuyo_utm30lx.gazebo.xacro");
        ros::ServiceClient spawn = nh.serviceClient< gazebo_msgs::SpawnModel> ("/gazebo/spawn_urdf_model");
        
        gazebo_msgs::SpawnModel s1;
        geometry_msgs::Pose p1;
        s1.request.model_name = "uav3";
        ifs >> s1.request.model_xml;
        s1.request.robot_namespace = "uav3";
        p1.position.x = 2.0;
        p1.position.y = 2.0;
        p1.position.z = 0.0;
        p1.orientation = tf::createQuaternionMsgFromYaw(0.0);
        s1.request.initial_pose = p1;
        s1.request.reference_frame = "";

   if (spawn.call(s1))
   {
     ROS_INFO("success");
   }
   else
   {
     ROS_ERROR("Failed to call service");
     return 1;
   }
    return 0;
}

Does anyone know what is wrong with it?

Thanks


Originally posted by [email protected] on ROS Answers with karma: 31 on 2017-04-22

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You are loading an xacro file into model_xml, which should be the contents of a URDF file. You need to run the xacro file through the xacro parser first.

You are calling spawn_urdf_model but trying to use a Gazebo model description file (probably SRDF?). So the correct service to call is spawn_sdf_model.


Originally posted by Geoff with karma: 4203 on 2017-04-23

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

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