Monday, May 4, 2020

Gazebo: make an animated box using a separate model file(s)

The Gazebo tutorial at http://gazebosim.org/tutorials?tut=actor&cat=build_robot shows how to make a box actor rotate around the vertical axis using only a world file. I wanted to use create a separate actor model and include it in the world file so I don't have to repeat the code. After fiddling around, I managed to figure out the procedure to do it. This post summarizes the steps.

Create an actor model
  1. In the user's home directory ~/.gazebo/models/, create a model folder, e.g. animated_box

    $ cd ~/.gazebo/models/
    $ mkdir -p animated_box/materials/scripts
    $ mkdir -p animated_box/materials/textures


  2. Change directory into the animated_box folder.

    $ cd animated_box


  3. Using a text editor, create a model.config file. Type in the following:
    <?xml version="1.0" encoding="utf-8"?>
    <model>
     <name>Animated Box</name>
     <version>1.0</version>
     <sdf version="1.4">model.sdf</sdf>
     <description>My animated box</description>
    </model>
    


  4. Using a text editor, create a model.sdf file. Type in the following:

    Note: instead of the model tag, use the actor tag.
    <?xml version="1.0" encoding="utf-8"?>
    <sdf version="1.4">
     <actor name="animated_box">
      <static>false</static>
       <link name="link">
        <visual name="visual">
         <geometry>
          <box>
           <size>0.2 0.2 0.2</size>
          </box>
         </geometry>
        </visual>
       </link>
       <script>
        <loop>true</loop>
        <delay_start>0.0</delay_start>
        <auto_start>true</auto_start>
        <trajectory id="0" type="square">
         <waypoint>
          <time>0.0</time>
          <pose>-1 -1 1 0 0 0</pose>
         </waypoint>
         <waypoint>
          <time>1.0</time>
          <pose>-1 1 1 0 0 0</pose>
         </waypoint>
         <waypoint>
          <time>2.0</time>
          <pose>1 1 1 0 0 0</pose>
         </waypoint>
         <waypoint>
          <time>3.0</time>
          <pose>1 -1 1 0 0 0</pose>
         </waypoint>
         <waypoint>
          <time>4.0</time>
          <pose>-1 -1 1 0 0 0</pose>
         </waypoint>
        </trajectory>
       </script>
    
     </actor>
    </sdf>
    


Create and run the world file
  1.  Using a text editor, create a world file, e.g. my_animated_box.world. Type in the following commands:
    <?xml version="1.0" ?>
    <sdf version="1.3">
     <world name="default">
      <include>
      <uri>model://sun</uri>
      </include>
      <include>
       <uri>model://ground_plane</uri>
      </include>
      <include>
       <uri>model://animated_box</uri>
      </include>
     </world>
    </sdf>
    

    Note: the previously created animated_box model is included as a model.

  2. Run the world file.

    $ gazebo my_animated_box.world

    Gazebo opens and an animated box is shown circling the z axis.

    No comments: