JenniferBuehler / gazebo-pkgs

A collection of tools and plugins for Gazebo
BSD 3-Clause "New" or "Revised" License
206 stars 104 forks source link

Can I spawn a opbject by existed model.sdf instead of a hard_code streamstring #37

Closed TouchDeeper closed 3 years ago

TouchDeeper commented 5 years ago

Hello

The cube_spawner spawn a cube by hard-code stringstream. Is there a way to use an existed model.xml file.

I tried to spawn my model by the way below, as you can see, I transfer the model.xml to the stringstream s instead of hard-code.

    std::ifstream xml_file;
    xml_file.open(xml_path,std::ios::in);
    if(!xml_file)
        std::cerr<<"can't find xml file: "<<xml_path<<std::endl;
    std::stringstream s;
    s<<xml_file.rdbuf();

But the model can't spawn normally. I use the rosrun command, the model can spawn normally.

rosrun gazebo_ros spawn_model -file `rospack find object_description`/urdf/banana.xml -urdf -y 1 -model banana
matbol commented 4 years ago

Try something like:


std::ifstream file("/path2/model.sdf"); 
gazebo_msgs::SpawnModel model;

if (file.is_open()) {
    std::string line;
    while (std::getline(file, line)) {
                   model.request.model_xml+=line;
    }
    file.close();
}

model.request.model_name="YourOwnModel";
model.request.initial_pose.position.x = 4;
model.request.initial_pose.position.y = 4;
model.request.initial_pose.position.z = 4;
client_spwn.call(model);
TouchDeeper commented 4 years ago

@matbol I test your code, but there is no model occur in gazebo.

        std::ifstream file;
        file.open(path);
        if(!file){
            std::cout<<"can't open "<<path<<std::endl;
            exit(-1);
        }
        gazebo_msgs::SpawnModel model;
        ros::ServiceClient client_spwn = nh.serviceClient< gazebo_msgs::SpawnModel> ("/gazebo/spawn_urdf_model");
        if (file.is_open()) {
            std::string line;
            while (std::getline(file, line)) {
                model.request.model_xml+=line;
            }
            file.close();
        }

        model.request.model_name = model_name;
        model.request.initial_pose.position.x = 4;
        model.request.initial_pose.position.y = 4;
        model.request.initial_pose.position.z = 4;
        client_spwn.call(model);

The model file is here. Coud you please provide some advice? Thanks a lot!

matbol commented 4 years ago

Instead ros::ServiceClient client_spwn = nh.serviceClient< gazebo_msgs::SpawnModel> ("/gazebo/spawn_urdf_model"); use ros::ServiceClient client_spwn = nh.serviceClient< gazebo_msgs::SpawnModel> ("/gazebo/spawn_sdf_model"); for .sdf files.

JenniferBuehler commented 3 years ago

Closing due to inactivity. Does anyone still need help with this? Then please re-open.