In this section we will learn how to combine 2 links. Let’s modify the code that we have to include another link:
<?xml version="1.0"?>
<robot name="my_robot">
<material name="blue">
<color rgba="0 0 0.5 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="blue" />
</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>
</robot>
Here, we created a new link that’s called second_link.
Note that if we try to launch this file we will get this error:
[robot_state_publisher-1] Error: Failed to find root link: Two root links found: [base_link] and [second_link]
The error occurs because your URDF model defines two root links: base_link and second_link. In a URDF, there should only be one root link, which serves as the base of the robot. This root link must have no parent joints connecting it to another link.
In your code:
base_link is a standalone link.second_link is also a standalone link.Since neither base_link nor second_link is connected via a joint, the parser sees them both as root links, which is not allowed.
In a URDF, all links must form a single connected tree structure. The root link is the base of this tree, and all other links are connected to it directly or indirectly through joints. Having two unconnected links violates this rule, causing the robot_state_publisher to fail.
Below is the modified code after adding a joint:
<?xml version="1.0"?>
<robot name="my_robot">
<material name="blue">
<color rgba="0 0 0.5 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="blue" />
</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>
Explanation of the code:
<joint name="base_second_joint" type="fixed"> This line specifies the joint name. It is best practice to name the joint by the links that it connects.<parent link="base_link" /> This line specifies the parent link.<child link="second_link" /> This line specifies the child link.