You can delete the model and respawn it. Have a look at how to spawn model here.
You can also use python script to do it for your. In gazebo_ros_pkgs package there is spawn_model python script that takes care of it (it's the same one used with roslaunch
command).
You can spawn model using python like this:
from gazebo_msgs.srv import SpawnModel
def spawn():
req = SpawnModelRequest()
req.model_name = "model"
req.model_xml = model_xml
req.robot_namespace = "namespace"
req.initial_pose.position.x = 0.0
req.initial_pose.position.y = 0.0
req.initial_pose.position.z = 0.0
req.initial_pose.orientation.x = 0.0
req.initial_pose.orientation.y = 0.0
req.initial_pose.orientation.z = 0.0
req.initial_pose.orientation.w = 1.0
req.reference_frame = ''
try:
srv = ServiceProxy('/gazebo/spawn_sdf_model', SpawnModel)
resp = srv(req)
except ServiceException, e:
print(" Service call failed: %s" % e)
sys.exit(1)
if resp.success:
print(resp.status_message)
return 0
else:
print(resp.status_message)
return 1
Where model_xml
is your urdf
or sdf
file.
Then you can delete models like this:
def delete():
try:
srv = ServiceProxy('/gazebo/delete_model', DeleteModel)
req = DeleteModelRequest()
req.model_name = "model"
resp = srv(req)
except ServiceException, e:
print("Service call failed: %s" % e)
sys.exit(2)
if resp.success:
print(resp.status_message)
return 0
else:
print(resp.status_message)
return 1
So if you want you can create your own python script which takes care of it. Using roslaunch
command is fine as well, but you're limited sometimes.