Closed TouchDeeper closed 3 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);
@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!
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.
Closing due to inactivity. Does anyone still need help with this? Then please re-open.
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.
But the model can't spawn normally. I use the rosrun command, the model can spawn normally.