How can I represent a anake robot with many identical segmens in SDF?

I'd like to model a snake robot consisting of many identical segments consisting of a hobby servo with a mounting bracket (having with one DOF). The modules can be attached either in a straight fashion or rotated 90 degrees, such that the axis of rotation are perpendicular to the previous axis.

I'm trying to understand how I can represent such a robot in SDF.

I tried making one segment consisting of two links, one with the servo, and the other attached to the servo's rotational axis. Such a segment then becomes one model:

Then I wanted to attach models to each other with a fixed joint, but discovered that this was not possible (only links can be attached with fixed joints?).

What is a good representation of a robot like this in SDF, without having to repeat many identical links in the model file, and ideally using Gazebo's Model Editor?

1 Answer

So first of all, its not a good idea to create this in SDF due to its repeated structure. For this model you have to use XACROS.
It will also help you in the control and ROS integration.

Here I leave a video tutorial explaining all you need to create it: VIDEO Tutorial

Here I've done an example code of a snake made up of eight main cylinders and seven joint cylinders. In your case it would be the same but replacing the cylinders by meshes. This snake is made so that it has 2 DOF in each "joint", having roll and pitch. It has also position control to move it around and of course the joint states are published, so that you will be able to see it in RVIZ TFs and the Robot Model.

Here I leave the code with the launches and also the control parameters. Hope it helps. Sorry for the false extensions, but here some file formats are not allowed. Launch the **init_snake.launch" in an empty world to spawn the robot with the controlers.

Based on what @chapulina said I created an example also with SDF macro-Ruby system and got nice results also.
In this case I parametrised absolutly everything which allows you to generate massive length snakes, with diferent size and recalcalculation of the inertias ;). I didnt add controlers though.

Here I leave the video explaining in detail the code and how to use it: VIDEO SDF MACRO

Here you have the code for the SDF version. You will need to always convert the .erb format file into sdf with the command erb snake.erb > snake_macro.sdf. And then you can launch it through spawn of and SDF like always.

Here you have an example of a 100 element snake with self colissions ;).

And here 1000 elements ;). Just for fun:

Sorry for the fake file extensions, but erb for some reason are not allowed here.

Hope with this you have a better understanding on how to create this multiple element snake in SDF or URDF ;).
Thanks @chapulina for the SDF Macro Idea ;). Chapulina give in the comments very usefull example code for those interested in learning more about SDF-Ruby macro ;).

REEdit: Part 3: Automated Xacro by one script

This is the third edit where I give also a method on how to totaly automatise the spawning of the Snake in Xacros with the Controllers needed to make it work:

Because when you generate a variable element robot, you need to have also a variable controler.yaml file, and also pass the number of elements to the XAcro through the launch file and the list of all the controllers used, a method to generate all that is needed.

So I divided it in these steps:
1) Pass the number of elements to ...(more)

The video is working now, thanks! There is a brief mention of ERB on a [tutorial](http://gazebosim.org/tutorials?tut=model_structure&cat=build_robot#ModelSDF.ERB). For a more complex example including many files, randomization and command line options, see [srcsim](https://bitbucket.org/osrf/srcsim/wiki/world_generator), for example.