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

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

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!

[Gazebo in 5 mins] 010 – How to launch RViz using a configuration file

[Gazebo in 5 mins] 010 – How to launch RViz using a configuration file

In this post, you’ll learn how to launch RViz using a configuration file. You will learn how to close and open RViz as many times you need without having to configure or customize it all over again.

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: Run RViz without loading a configuration file

Open the Code Editor and create a new file rviz_plain.launch in the location shown in the image below:

Here is the content of the file:

<?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" />

</launch>

Next, launch the rviz_plain.launch file. Open a Web Shell and type the command that follows.

roslaunch my_robot_description rviz_plain.launch

Now you should see the rviz window, with no display added. We would need to add the display one by one and then configure them. But this is not necessary, since we have a configuration file saved already. Let’s load it next.

Step 3: Run RViz in a launch file and pass the configuration

Stop the current program in the web shell using Ctrl + C and run the following command instead.

roslaunch my_robot_description rviz.launch

Now you should see a different rviz screen, generated from the configuration!

Can you spot some differences in the two launch files? Here is the rviz.launch file.

<?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: Check your learning + Extra Tip

Do you now understand how to launch rviz using a configuration file? If not, please go over the post again, more carefully this time.

You can create a configuration file by using the File -> Save Config As... command on the rviz window. Use File -> Save Config to update the current config.

Step 5 (optional): watch the video

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!

[Gazebo in 5 minutes] 004 – How to create a gazebo model using SDF

[Gazebo in 5 minutes] 004 – How to create a gazebo model using SDF

What you will learn

  • Learn how to  create a gazebo model using the SDF Format

List of resources used in this post:

Preparing the environment

In order to load gazebo using ROS, you need to have Gazebo and ROS installed. If you don’t want to install everything, we highly recommend using ROSDS (ROS Development Studio) which gives you access to an online environment with ROS already installed. Indeed, we are going to use that tool for easiness. You can follow the same steps on your computer as well if you don’t want to use ROSDS.

To use ROSDS, you can just create an account and start using it.

Once you have your account created, you have to create a ROSject by clicking on the New ROSject button:

Creating a new ROSject in ROSDS

Creating a new ROSject in ROSDS

Once you have the ROSject, you can open it by clicking Open:

Opening a ROSject in ROSDS

Creating the ROS Package

In order to run anything using ROS, we need a ROS package, so, let’s create one. For that, you are going to need a terminal/shell. In ROSDS, you can have a terminal by clicking Tools -> Shell.

Let’s first create the workspace. In this case, let’s call it ~/simulation_ws

mkdir ~/simulation_ws/src -p

Now let’s then compile our empty workspace

source /opt/ros/kinetic/setup.bash 
source /usr/share/gazebo/setup.sh

cd ~/simulation_ws/

catkin_make

Now let’s create our ROS package. Let’s call it my_simulations:

source ~/simulation_ws/devel/setup.bash

cd ~/simulation_ws/src

catkin_create_pkg my_simulations

Now, let’s create a launch and a world folder inside the my_simulations package.

cd my_simulations

mkdir launch world

Now, inside the launch folder, let’s create a file named my_world.launch:

touch launch/my_world.launch

In that file, let’s put the following content:

<?xml version="1.0" encoding="UTF-8" ?>
<launch>
        <!-- overwriting these args -->
        <arg name="debug" default="false" />
        <arg name="gui" default="true" />
        <arg name="pause" default="false" />
        <arg name="world" default="$(find my_simulations)/world/empty_world.world" />

        <!-- include gazebo_ros launcher -->
        <include file="$(find gazebo_ros)/launch/empty_world.launch">
                <arg name="world_name" value="$(arg world)" />
                <arg name="debug" value="$(arg debug)" />
                <arg name="gui" value="$(arg gui)" />
                <arg name="paused" value="$(arg pause)" />
                <arg name="use_sim_time" value="true" />
        </include>
</launch>

In order to put the content on that file, you can do it using the code editor. For that, click on Tools -> IDE

"Tools

Now, let’s create a file named empty_world.world in the world folder:

touch world/empty_world.world

In that file, let’s add the following content:

<?xml version="1.0" ?>

<sdf version="1.5">
	<world name="default">
		<!-- A global light source -->
		<include>
			<uri>model://sun</uri>
		</include>

		<!-- A ground plane -->
		<include>
			<uri>model://ground_plane</uri>
		</include>
	</world>
</sdf>

 

If you followed the instructions correctly, you should have the following structure:

user:~/simulation_ws/src$ tree .
.
|-- CMakeLists.txt -> /opt/ros/kinetic/share/catkin/cmake/toplevel.cmake
`-- my_simulations
    |-- CMakeLists.txt
    |-- launch
    |   `-- my_world.launch
    |-- package.xml
    `-- world
        `-- empty_world.world

3 directories, 5 files

Creating our model

Let’s create a model named my1stmodel on the my_simulations package:

~/simulation_ws/src/my_simulations/
mkdir -p models/my1stmodel

Inside the my1stmodel folder, let’s create two files, one named model.config and another named model.sdf.

cd models/my1stmodel/
touch model.config model.sdf

These two files, model.config and model.sdf, are required for every gazebo model. You can find those files in the ground_plane model provided by gazebo, for example.

Let’s paste the following content on our my1stmodel/model.config

<?xml version="1.0"?>

<model>
  <name>My first model</name>
  <version>1.0</version>
  <sdf version="1.5">model.sdf</sdf>

  <author>
    <name>Your name</name>
    <email>your@email.com</email>
  </author>

  <description>
	My first model for gazebo
  </description>
</model>

And in our models/my1stmodel/model.sdf, let’s add the following:

<?xml version="1.0" ?>
<sdf version="1.5">
  <model name="my1stmodel">
    <static>false</static>
    <link name="link">
      <collision name="collision">
        <geometry>
          <box>
            <size>3 2 5</size>
          </gox>
        </geometry>
        <surface>
          <friction>
            <ode>
              <mu>100</mu>
              <mu2>50</mu2>
            </ode>
          </friction>
        </surface>
      </collision>
      <visual name="visual">
        <geometry>
          <box>
            <size>3 2 5</size>
          </gox>
        </geometry>
        <material>
          <script>
            <uri>file://media/materials/scripts/gazebo.material</uri>
            <name>Gazebo/Grey</name>
          </script>
        </material>
      </visual>
    </link>
  </model>
</sdf>

Launching our Gazebo model

Let’s modify our world/empty_world.world file created earlier to add the following:

<!-- A ground plane -->
<include>
 <uri>model://my1stmodel</uri>
</include>

In the end, the final content of the file would be:

<?xml version="1.0" ?>

<sdf version="1.5">
	<world name="default">
		<!-- A global light source -->
		<include>
			<uri>model://sun</uri>
		</include>

		<!-- A ground plane -->
		<include>
			<uri>model://ground_plane</uri>
		</include>

        <!-- Our custom model -->
		<include>
			<uri>model://my1stmodel</uri>
		</include>
        
	</world>
</sdf>

Now that we have everything in place, we can run our package in two ways:

Option one: Click Simulation -> Choose File, then select my_world.launch. This should automatically load the web version of gazebo, called gzweb.

Custom gazebo robot model running in ROSDS

Custom gazebo robot model running in ROSDS

Option two: If you are running the tests on your computer, or you want to manually run the simulation, you can just:

source ~/simulation_ws/devel/setup.bash
roslaunch my_simulations my_world.launch --screen

If you are in ROSDS and chose to run the simulation manually, then you have to manually open the Gazebo Web (gzweb) by clicking on Tools -> Gazebo.

Congratulations. You have successfully launched your first custom Gazebo World using ROS.

Youtube video

If you didn’t understand well all the steps explained here or need more understanding of the files we created, remember that we have the live version of this post on YouTube. Also, if you liked the content, please consider subscribing to our youtube channel. We are publishing new content ~every day.


 

[Gazebo in 5 minutes] 003 – How to spawn a robot in gazebo

[Gazebo in 5 minutes] 003 – How to spawn a robot in gazebo

What you will learn

  • Learn how to spawn a robot from its package in any gazebo simulation you may have running

List of resources used in this post:

Preparing the environment

In order to spawn a gazebo model in a simulation using ROS, you need to have Gazebo and ROS installed. If you don’t want to install everything, we highly recommend using ROSDS (ROS Development Studio) which gives you access to an online environment with ROS already installed. Indeed, we are going to use that tool for easiness. You can follow the same steps on your computer as well if you don’t want to use ROSDS.

To use ROSDS, you can just create an account and start using it.

Once you have your account created, you have to create a ROSject by clicking on the New ROSject button:

Creating a new ROSject in ROSDS

Once you have the ROSject, you can open it by clicking Open:

Spawning a two-wheeled robot in a gazebo simulation

When you open a ROSject, you are able to open web shell, code editor, etc.

Let’s start opening a web shell by clicking Tools -> Shell in ROSDS. Once you have the shell, let’s clone the repository that contains the robot:

cd ~/simulation_ws/src/

git clone https://bitbucket.org/theconstructcore/two-wheeled-robot-simulation

If you see, the repository we just cloned, you will find the file m2wr_description/launch/spawn.launch which contains the code used to spawn the robot. The code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<launch>
    <param name="robot_description" command="$(find xacro)/xacro --inorder '$(find m2wr_description)/urdf/m2wr.xacro'" />
    
    <arg name="x" default="0"/>
    <arg name="y" default="0"/>
    <arg name="z" default="0.5"/>
    
    <node name="mybot_spawn" pkg="gazebo_ros" type="spawn_model" output="screen"
          args="-urdf -param robot_description -model m2wr -x $(arg x) -y $(arg y) -z $(arg z)" />
          
</launch>

A quick look into this file shows that the file loaded in gazebo will be the m2wr_description/urdf/m2wr.xacro, in the positions xyz = 0, 0, 0.5.

Now, to actually spawn the robot, you can first load an empty world by clicking Simulations -> Empty World -> Start Simulation.

Starting an empty simulation in ROSDS

Starting an empty simulation in ROSDS

With the empty simulation running, we can now spawn our robot into the current simulation. For that, let’s first compile the workspace to make sure the package we cloned is ok:

cd ~/simulation_ws/
catkin_make
source devel/setup.bash
rospack profile

Now let’s spawn our robot:

roslaunch m2wr_description spawn.launch

If everything went as expected, you should have the two-wheeled robot in the simulation:

A two-whelled robot running in ROSDS

A two-whelled robot running in ROSDS

Congratulations!!! You just learned how to spawn a robot in a gazebo simulation.

Youtube video

If you didn’t understand well all the steps explained here or need more understanding of the files we created, remember that we have the live version of this post on YouTube. Also, if you liked the content, please consider subscribing to our youtube channel. We are publishing new content ~every day.

Keep pushing your ROS Learning.


Links & Resources mentioned in the video:

 

[Gazebo in 5 minutes] 002 – How to add gazebo models to a simulation

[Gazebo in 5 minutes] 002 – How to add gazebo models to a simulation

What you will learn

  • Learn how to add a gazebo model to a running gazebo simulation

List of resources used in this post:

  • Robot Ignite Academy, the place to learn to program robots using only a web browser
  • ROS Development Studio (the environment used in the video), another powerful online tool for pushing your ROS learning in a practical way
  • Gazebo models repository where you can find different models to be spawned

Including our model in the .world file

In this post, we are assuming you already have created a ROS package called my_simulations  and the following files: ~/simulation_ws/src/my_simulations/launch/my_world.launch and ~/simulation_ws/src/my_simulations/world/empty_world.world.

If you don’t have these files, please remember to first follow the previous post: https://www.theconstruct.ai/gazebo-5-mins-launch-first-gazebo-world-using-ros/

If you check the my_world.world file, from the previous post we have the following content:

<?xml version="1.0" ?>

<sdf version="1.5">
	<world name="default">
		<!-- A global light source -->
		<include>
			<uri>model://sun</uri>
		</include>

		<!-- A ground plane -->
		<include>
			<uri>model://ground_plane</uri>
		</include>
	</world>
</sdf>

Let’s add a postbox model. Remember that you can find many Gazebo moels on the Gazebo models repository.

The final content of ~/simulation_ws/src/my_simulations/world/empty_world.world after adding the postbox would be:

<?xml version="1.0" ?>

<sdf version="1.5">
	<world name="default">
		<!-- A global light source -->
		<include>
			<uri>model://sun</uri>
		</include>

		<!-- A ground plane -->
		<include>
			<uri>model://ground_plane</uri>
		</include>

                <!-- A post box plane -->
		<include>
			<uri>model://postbox</uri>
		</include>        

	</world>
</sdf>

This adds a model called postbox in the initial position (x=0, y=0, z=0). If you want it in a different position, you can add a <pose> like in the example below:

<include>
  <uri>model://postbox</uri>
  <pose>2 1 0 0 0 0</pose>
</include>

 

Adding the gazebo model to the simulation

Now that we have our empty_world.world file set, we can start our simulation in two ways:

Option one: Click Simulation -> Choose File, then select my_world.launch. This should automatically load the web version of gazebo, called gzweb.

Postbox gazebo simulation loaded in ROSDS

Postbox gazebo simulation loaded in ROSDS

 

Option two: If you are running the tests on your computer, or you want to manually run the simulation, you can just:

source ~/simulation_ws/devel/setup.bash
roslaunch my_simulations my_world.launch --screen

If you are in ROSDS and chose to run the simulation manually, then you have to manually open the Gazebo Web (gzweb) by clicking on Tools -> Gazebo.

 

Congratulations. You have successfully loaded a custom model in a Gazebo World using ROS.

Youtube video

If you didn’t understand well all the steps explained here or needs more understanding of the files we created, remember that we have the live version of this post on YouTube. Also, if you liked the content, please consider subscribing to our youtube channel. We are publishing new content ~every day.

Keep pushing your ROS Learning.


 

Pin It on Pinterest