Modifying STL files to work with URDF

RoboFoundry
8 min readApr 19, 2022

In this article, we will explore how to adapt existing mesh files to work with your robot URDF. For folks starting new with robotics the URDF files can be a bit intimidating. For me, initially I thought its just XML and I am very used to those in programming world. However, as I got more exposed to the URDF structures it became clear it was not as simple as it sounds on the surface. On the other hand, it is very powerful framework that can represent robots of high complexity in virtual and real-world environments.

One of the harder parts of working with URDF was not the XML structure, there are some really great examples of robots in public domain like turtlebot3 or husky or linorobot2 or sambot, but how to make your robot look like its real world counterpart. The biggest challenge after having the proper URDF definition is to find/build the correct mesh files and then integrate those to work with the URDF XML for your robot to look like real-world robot in the simulator. For the uninitiated here is the step-by-step URDF tutorial.

There are two ways to get the mesh files

  1. Build them yourself — this is not always easy and can be very intimating for someone learning. It was very difficult at the beginning for me as I did not know how to use a CAD application to build the engineering designs even though I have enough engineering drawing skills with old fashioned paper/pencil. I realized that I really needed to know the basics of it if I was going to feel comfortable with it. I invested a couple of months worth of time on and off to keep learning Fusion 360 until I was at a point where I was very comfortable with building my entire robot in the Fusion 360 with all the parts. I must say that YouTube tutorials and webcasts from
    Lars Christensen were invaluable. I really admire his light style always with a smile and attention to detail and how he makes it so enjoyable to learn a topic that has a steep learning curve upfront. At the end, it was a rewarding experience because not only I learned how to represent the robot in URDF using the designs I was able to create but I was able to also solve some problems by sending my designs to 3D-printing in order to use parts from different vendors together. In short, if you can invest that time, it will be worth it. Just don’t expect going in that you will spend a couple of hours and be able to master that skill. I’m not trying to say Fusion 360 is the only way to go or trying to be a commercial for it, there are many alternatives to that like Solidworks, FreeCAD and such but you have to learn one of them. I just happen to start with Fusion 360 and found that there was a lot of support for learning it on your own. Most of the concepts are universal and they will translate to another CAD package once you learn one.
  2. Use existing mesh files — this is not going to be perfect but you’d be surprised how much that is readily available out on internet if you just look in the right places. Of course there are open source CAD file sharing forums like Thingverse and vendors like Servocity and GoBuilda have most of their part mesh files freely available on their website for you to download. You can get pretty close to what you are trying to build using the mesh files of their parts.

This particular article is about how to use existing mesh files that may not be exactly right for your own robot project and how to change the translation, rotation and scaling of the mesh file to make it work with your robot URDF.

Let’s take a very simple example of a robot with flat base and two wheels in the front and a caster wheel in the back. A robot exactly like this is readily available in Sambot project in Nav2 tutorials repository so we can just get the repo and build the sam_bot_description project to serve as base for our discussion. Assuming you have ROS2 foxy installed.

source /opt/ros/foxy/setup.bashgit clone https://github.com/ros-planning/navigation2_tutorials.gitcd navigation2_tutorials/sam_bot_description/colcon build. install/setup.bashros2 launch sam_bot_description display.launch.py

This will launch RViz2 and Gazebo both and you can see the sam bot robot that looks something like this. Make sure to uncheck the Show names, axes and arrows to reduce the clutter in the view and you can zoom into the robot by using your mouse wheel.

You can see that green box is the base_link, the sphere is the Caster wheel and the two wheels in gray in the front are the ones for differential drive of the robot.

We can modify the view a bit so we hide everything other than front wheels and caster wheel like this.

The two wheels are basically represented by two cylinders and they don’t look like real wheels. Now lets use a mesh file to represent the wheels. Lets start by creating a new meshes sub-directory under sam bot package folder and download two wheel meshes from Turtlbot3 project like this.

cd navigation2_tutorials/sam_bot_description/mkdir meshescd mesheswget https://github.com/ROBOTIS-GIT/turtlebot3/raw/master/turtlebot3_description/meshes/wheels/left_tire.stl wget https://github.com/ROBOTIS-GIT/turtlebot3/raw/master/turtlebot3_description/meshes/wheels/right_tire.stl

At this point you should have file structure like this:

navigation2_tutorials
----sam_bot_description/
--------meshes
------------left_tire.stl
------------right_tire.stl

modify navigation2_tutorials/sam_bot_description/CMakeLists.txt lines

from

install(DIRECTORY src launch rviz config worldDESTINATION share/${PROJECT_NAME})

to

install(DIRECTORY src launch rviz config world meshesDESTINATION share/${PROJECT_NAME})

We are simply adding meshes sub-directory to be copied to target folders to make sure mesh files are available at run time.

Replace the URDF with following gist to load the mesh files as part of the robot wheels.

This URDF is not that different from original sam bot URDF, I have just broken out the left and right wheels links to make it easier to understand.

Run the launch file again, after building and sourcing the workspace.

cd navigation2_tutorials/sam_bot_description/colcon build. install/setup.bashros2 launch sam_bot_description display.launch.py

In RViz2 un-check the boxes to hide base_link, imu_link, lidar_link and you will see something like this. It looks like the face of the wheel is facing upward instead of having parallel to base link. It should show exactly horizontal but the gazebo is publishing the joint states that is moving the wheel slowly so it looks a bit tilted by the time I was able to capture the screenshot after un-checking all the other clutter.

There are two ways to get the wheels to show correctly oriented relative to robot base_link.

  1. Manipulate the attributes for origin and rpy on the wheel links in URDF
  2. Change the original orientation of the mesh file

Changing the URDF

One way to fix this misalignment is by manipulating the rpy attribute of the wheel link origin, as shown below. Only showing this for left wheel link but same pattern would apply to both wheels.

Before changing the visual -> origin -> rpy value = ${pi/2} 0 0
After changing the visual -> origin -> rpy value = 0 0 0

After changing the URDF attributes it should look like this.

Updating the mesh file

It is obvious that if you can get the orientation to change easily by changing the attributes in URDF, its far easier to do than modifying the mesh file. Although, with proper tool, modifying the mesh file is not that hard either.

Let’s try to solve the same problem in original URDF file by changing the STL file this time. The STL file is oriented differently in the co-ordinate system than what we need it to show in the robot. We will fix this using a tool named — MeshLabs. I came to know about this tool while searching ROS discourse forums.

Download the tool here — https://www.meshlab.net/#download.

Select the Linux AppImage [or other OS if you need a different one] and after download make sure you check the box in properties of the file to “allow executing file as program”. Then you can double click it to launch the MeshLab.

You can simply drag and drop one of our mesh files from meshes folders on the main window and it will look like this when you move it around by dragging your left mouse. You can always reset to original position by doing Ctrl+h.

The basic steps to follow for fixing the orientation for STL image are:

  1. Click on manipulator tool to enable manipulation mode
  2. Watch this great tutorial video on how to do the manipulation step-by-step to get the STL file to the orientation you need. While you are making changes you can hit backspace to undo your changes anytime.
  3. After all the changes are done select -Filters → Mesh Layer → Matrix: Freeze Current Matrix to save current orientation as original position
  4. Export the new STL file by File → Export Mesh As → [select file type STL] and filename and save
  5. Copy the newly exported file to your meshes sub-directory and overwrite the original mesh files
  6. Play with the orientation by loading it in your URDF file until it shows correct orientation for your robot

If you don’t have STL file and would like to convert a STEP or STP file to STL file before you can manipulate it, you can use this website to do just that.

That’s it. Even though the URDF and mesh files seem intimidating at first, it’s not that complex to align everything properly for your robot to look like real world physical robot if you put in a little bit of time and using the right tools to do that.

Hope this helps folks who are new to this process in getting over the hurdles that may seem hard to get over.

Have fun building!!

References

--

--