Spawn Objects in Gazebo of ROS Kinetic & Melodic (Python 2.7)

Spawn objects in the TIAGO’s gripper

Sometimes we need to add new objects to Gazebo to build a dynamic environment, particularly in some manipulation tasks. Suppose we test if the robot can pick up items from the floor. Thus, we may need a program to spawn models in a random position in the environment to verify our manipulation algorithms’ robustness. Like the top picture, a new cylinder is created in TIAGO’s gripper. Then TIAGO can deliver the cylinder to a free container.

Spawning Models

To spawn a new model in the Gazebo, we focus on the function spawn_sdf_model_client of gazebo_interface. If you have time, look at its document. If not, I have already prepared such a function for you.

To explain the above function, Let’s say we have a model named red_ball.

Your model directory should have the same name red_ball. And also the same name is in the model.config file.

Make sure you model directory is in the Gazebo environment variable (The model name should be visible from the sidebar of Gazebo).

Thus, our first argument passed into the above function should be red_ball. The second argument is a set of float values representing the object’s desired position x, y, and z. The third argument is also a set of float values, representing the desired orientations of the object in Euler angles, which will be transformed to quaternion in line 16 and build the Pose. If the last argument is True, the model will hold the static property (line 9), which means the object is immovable. We add a suffix to the original model name to make it unique in Gazebo (line 17). Last we call the function spawn_sdf_model_client (line 18) with our arguments then object will be visible in Gazebo.

Deleting Models

To delete models from Gazebo, we need to know the exact name of the model, which is a required parameter of /gazebo/delete_model service.

We can also subscribe the topic /gazebo/model_states to have information of all the models, and then search for models with a specific keyword and delete it.

The delete function looks like this.

We create the proxy of deleting service in line 6. This delete function accepts one parameter representing the keyword used to search the model. We search objects using that keyword in the for loop of line 12. Finally, we call the deleting service by passing the full model name in line 21.

Overall

In the end, we can make a Class to handle these two functions.

Leave a Reply

Your email address will not be published. Required fields are marked *