[Gazebo in 5 minutes] 011 – How to use the joint_state_publisher

[Gazebo in 5 mins] 011 - How to use joint_state_publisher

Written by Bayode Aderinola

11/07/2023

In this post, you will learn how to use the joint_state_publisher node. At the end of the post, you will be able to debug your robot model joints using a graphical interface. Let’s go!

Step 1: Fire up a system with ROS installation

“Hey, do you mean I have to install ROS first?” Absolutely not! Just log in to The Construct to get access to virtual machines pre-installed with ROS.

Once logged in, click here to copy the ROS project (rosject) we’ll use for this post. Once copied, click the Run button to open the rosject.

You might also want to try this on a local PC if you have ROS installed. However, please note that we don’t support local PCs and you will have to fix any errors you run into on your own. The rest of the instruction assumes that you are working on The Construct; please adapt them to your local PC and ROS installation.

PS: we are using ROS Kinetic. You should be able to replicate the same on any ROS 1 distro, with minor changes.

Step 2: Identify the joints in your robot

For this post, we are using the following robot model, which you’ll find by opening the IDE and locating the file as shown in the image below.

<?xml version="1.0" ?>
<robot name="my_robot" xmlns:xacro="http://www.ros.org/wiki/xacro">
    
    <!-- 1st link -->
  <link name="link_chassis">
    <pose>0 0 0.1 0 0 0</pose>
    
    <inertial>
      <mass value="5"/>
      <origin xyz="0 0 0.1" rpy="0 0 0"/>
      <inertia ixx="0.0395416666667" ixy="0" ixz="0" iyy="0.106208333333" iyz="0" izz="0.106208333333"/>
    </inertial>
    
    <collision name="collision_chassis">
      <geometry>
        <box size="1 1 2"/>
      </geometry>
    </collision>
    
    <visual>
      <origin rpy="0 0 0" xyz="0 0 0"/>
      <geometry>
        <box size="1 1 2"/>
      </geometry>
    </visual>
  </link>
  
  <joint name="joint1" type="continuous">
    <origin xyz="0.6 0 1" rpy="0 0 0"/>
    <parent link="link_chassis"/>
    <child link="link_arm"/>
  </joint>
  
  <!-- 2nd link -->
  <link name="link_arm">
    <pose>0 0 0 0 0 0</pose>
    
    <inertial>
      <mass value="0.5"/>
      <origin xyz="0 0 0" rpy="0 0 0"/>
      <inertia ixx="0.0395416666667" ixy="0" ixz="0" iyy="0.106208333333" iyz="0" izz="0.106208333333"/>
    </inertial>
    
    <collision name="collision_chassis">
      <origin xyz="0 0 0.5" rpy="0 0 0"/>
      <geometry>
        <box size="0.2 0.2 1"/>
      </geometry>
    </collision>
    
    <visual>
      <origin xyz="0 0 0.5" rpy="0 0 0"/>
      <geometry>
        <box size="0.2 0.2 1"/>
      </geometry>
    </visual>
  </link>
 
</robot>

The joint in question here is highlighted in the code above.

Step 3: Use the joint_state_publisher node with the rviz node

The launch file is in simulation_ws/src/my_robot_description/launch/rviz.launch. The lines where the joint_state_publisher node is started is highlighted below.

<?xml version="1.0"?>
<launch>

    <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find my_robot_description)/urdf/robot.urdf'" />    

    <!-- send fake joint values -->
    <node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher">
        <param name="use_gui" value="True"/>
    </node>
    
    <!-- Combine joint values -->
    <node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher"/>
    
    <!-- Show in Rviz   -->
    <node name="rviz" pkg="rviz" type="rviz" args="-d $(find my_robot_description)/rviz/robotmodel.rviz" />

</launch>

Step 4: Use the joint_state_publisher to move the joint

Open a web shell and launch rviz with the command shown below.

roslaunch my_robot_description rviz.launch

You should now see two displays: rviz and the joint_state_publisher gui. You should be able to move the joint as shown in the image below.

Step 5: Check your learning

Do you now understand how to use the joint_state_published node? If not, please go over the post again, more carefully this time.

Step 6 (Optional): watch the video to learn more

Feedback

Did you like this video? Whatever the case, please leave a comment in the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area, and we will do a video about it.

Thank you!

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This