Gazebo, when run through ROS, provides a ROS API for accessing and controlling the simulation. There is a good tutorial available in the Gazebo documentation:
http://gazebosim.org/tutorials?tut=ro...
Specifically for creating and destroying robots, you can use the spawn_model
and delete_model
services. If you want to do this based on conditions, you should create a node to manage your simulated robots. It should these services and decide when to call them.
You can also use the spawn_model
node in the gazebo_ros
package to spawn models from a launch file. For example:
<node name="spawn_hector" pkg="gazebo_ros" type="spawn_model" args="-file hector.srdf -sdf -model hector_the_quadrotor"/>
This is an alternative way to set up your simulation, but I think that if you are going to manage them as the simulation runs, then the node responsible for managing the models should also be responsible for the initial creation of them.