URDF for Robot - dhanushshettigar/Getting-Started-With-ROS2 GitHub Wiki

URDF for Robot

This guide explains how to create a URDF file, add links and joints, and visualize your robot in ROS. It also covers advanced topics such as adding colors and different types of joints.

Table of Contents

  1. Introduction to URDF
  2. Your First URDF File
  3. Adding Color to Your Robot
  4. Combining Links with Joints
  5. Types of Joints in URDF
  6. Creating a Revolute Joint
  7. Creating a Prismatic Joint
  8. Learn More

Introduction to URDF

URDF is an XML format used in ROS (Robot Operating System) to describe the physical and kinematic properties of a robot. It defines the robot's links (parts) and joints (connections between parts), allowing ROS to simulate and visualize the robot.

Your First URDF File

Step 1: Create a URDF File

Run the following command in your terminal to create a new URDF file:

touch my_robot.urdf

Step 2: Edit the URDF File

Open the newly created file with your preferred editor:

code my_robot.urdf

Step 3: Write a Basic URDF

Add the following XML to your my_robot.urdf file to define a simple robot with one box-shaped link.

<?xml version="1.0" encoding="UTF-8"?>
<robot name="my_robot">
   <link name="base_link">
       <visual>
           <geometry>
               <box size="0.6 0.4 0.2"/>
           </geometry>
           <origin xyz="0 0 0" rpy="0 0 0"/>
       </visual>
   </link>
</robot>

Step 4: Visualize the Robot

Launch ROS to visualize the URDF model:

ros2 launch urdf_tutorial display.launch.py model:=/home/<your-pc-name>/Documents/URDFs/my_robot.urdf

ROS2 Rviz

Change the box size, save the file, and rerun the command to see changes.

Adding Color to Your Robot

You can add color to the robot by defining a material and applying it to the link. Update your URDF as follows:

<?xml version="1.0" encoding="UTF-8"?>
<robot name="my_robot">
   <material name="green">
       <color rgba="0 0.5 0 1"/>
   </material>

   <link name="base_link">
       <visual>
           <geometry>
               <box size="0.6 0.4 0.2"/>
           </geometry>
           <origin xyz="0 0 0" rpy="0 0 0"/>
           <material name="green"/>
       </visual>
   </link>
</robot>

Combining Links with Joints

To create more complex robots, you need to connect links using joints. Here’s how you can define multiple links and joints.

URDF Without a Joint (Expecting an Error)

When defining multiple links without a joint, you will encounter an error. Try running the following URDF without a joint and check the screenshot of the error:

<?xml version="1.0" encoding="UTF-8"?>
<robot name="my_robot">
   <link name="base_link">
       <visual>
           <geometry>
               <box size="0.6 0.4 0.2"/>
           </geometry>
       </visual>
   </link>

   <link name="second_link">
       <visual>
           <geometry>
               <cylinder radius="0.1" length="0.2"/>
           </geometry>
       </visual>
   </link>
</robot>

ROS2 Rviz

Adding Joints to the Robot

Now let’s add joints between the links to avoid the error:

<?xml version="1.0" encoding="UTF-8"?>
<robot name="my_robot">
   <material name="green">
       <color rgba="0 0.5 0 1"/>
   </material>

   <material name="grey">
       <color rgba="0.5 0.5 0.5 1"/>
   </material>

   <link name="base_link">
       <visual>
           <geometry>
               <box size="0.6 0.4 0.2"/>
           </geometry>
           <origin xyz="0 0 0.1" rpy="0 0 0"/>
           <material name="green"/>
       </visual>
   </link>

   <link name="second_link">
       <visual>
           <geometry>
               <cylinder radius="0.1" length="0.2"/>
           </geometry>
           <origin xyz="0 0 0.1" rpy="0 0 0"/>
           <material name="grey"/>
       </visual>
   </link>

   <joint name="base_second_joint" type="fixed">
       <parent link="base_link"/>
       <child link="second_link"/>
       <origin xyz="0 0 0.2" rpy="0 0 0"/>
   </joint>
</robot>

ROS2 Rviz

Types of Joints in URDF

In URDF, joints define how different links of a robot are connected and can move relative to each other. Here are the types of joints that can be specified in URDF:

  1. revolute

    • A hinge joint that rotates around a defined axis with a limited range of motion.
    • The rotation is constrained by the upper and lower limits.
    • Example use: a robotic arm elbow joint.
  2. continuous

    • Similar to a revolute joint, but it can rotate indefinitely without upper or lower limits.
    • Example use: a wheel or a robot joint that can rotate fully.
  3. prismatic

    • A sliding joint that moves along a defined axis.
    • The movement is constrained by the upper and lower limits.
    • Example use: a linear actuator.
  4. fixed

    • This is not a joint in the traditional sense, as it does not allow movement. All degrees of freedom are locked.
    • No need for attributes like axis, limits, or dynamics.
    • Example use: parts of the robot that are rigidly attached to each other.
  5. floating

    • Allows motion in all 6 degrees of freedom (translation in x, y, z, and rotation about x, y, z).
    • Example use: representing free-floating objects in space.
  6. planar

    • Allows movement in a plane perpendicular to the axis, with translation in x and y and rotation around the z-axis.
    • Example use: a sliding platform that moves within a plane.

Creating a Revolute Joint

Let’s modify the joint type to a revolute joint to allow rotation between the links:

<?xml version="1.0" encoding="UTF-8"?>
<robot name="my_robot">
   <material name="green">
       <color rgba="0 0.5 0 1"/>
   </material>

   <material name="grey">
       <color rgba="0.5 0.5 0.5 1"/>
   </material>

   <link name="base_link">
       <visual>
           <geometry>
               <box size="0.6 0.4 0.2"/>
           </geometry>
           <origin xyz="0 0 0.1" rpy="0 0 0"/>
           <material name="green"/>
       </visual>
   </link>

   <link name="second_link">
       <visual>
           <geometry>
               <cylinder radius="0.1" length="0.2"/>
           </geometry>
           <origin xyz="0 0 0.1" rpy="0 0 0"/>
           <material name="grey"/>
       </visual>
   </link>

   <joint name="base_second_joint" type="revolute">
       <parent link="base_link"/>
       <child link="second_link"/>
       <origin xyz="0 0 0.2" rpy="0 0 0"/>
       <axis xyz="0 0 1" />
       <limit lower="-1.57" upper="1.57" velocity="100" effort="100"/>
   </joint>
</robot>

You can change the axis attribute to rotate around different axes (e.g., x or y).

ROS2 Rviz

Creating a Prismatic Joint

A prismatic joint allows linear movement (sliding). Modify the joint as follows:

<joint name="base_second_joint" type="prismatic">
   <parent link="base_link"/>
   <child link="second_link"/>
   <origin xyz="0 0 0.2" rpy="0 0 0"/>
   <axis xyz="1 0 0" />
   <limit lower="0" upper="0.2" velocity="100" effort="100"/>
</joint>

Now, the second link can slide along the x-axis between 0 and 0.2 meters.

Learn More

For more details on URDF, check out the URDF Documentation and explore different joint types and robot configurations.

⚠️ **GitHub.com Fallback** ⚠️