[ROS2 Q&A] 226 – Work with ROS2 turtlesim / How to move it around

Written by Ruben Alves

21/01/2022

What we are going to learn

  1. How to launch turtlesim
  2. How to remap topics from the terminal in order to move the robot around

List of resources used in this post

  1. ROS Development Studio (ROSDS) —▸ http://rosds.online
  2. Robot Ignite Academy –▸ https://www.robotigniteacademy.com
  3. The question that this post answers –> https://answers.ros.org/question/390930/unable-to-move-the-turtle-in-turtlesim/
  4. Use the rosject: https://app.theconstructsim.com/#/l/4623f16e/
  5. ROS2 Tutorials:
    1. ROS2 Basics in 5 Days (C++): https://app.theconstructsim.com/#/Course/61
    2. ROS2 Navigation: https://app.theconstructsim.com/#/Course/50

Creating a rosject

In order to learn how to work with turtlesim using ros2, we need to have ros2 installed.

You can use rosject we provide ( https://app.theconstructsim.com/#/l/4623f16e/) or you can create your own rosject. We are going to use The Construct (https://www.theconstruct.ai/) for this tutorial, but if you have ROS2 installed on your own computer, you should be able to do ~everything on your own computer, except this creating a rosject part.

Let’s start by opening The Construct (https://www.theconstruct.ai/) and logging in. You can easily create a free account if you still don’t have one.

Once inside, let’s create My Rosjects and then, Create a new rosject if you decide for this step instead of using the rosject already provided (https://app.theconstructsim.com/#/l/4623f16e/) :

My Rosjects

My Rosjects

 

Create a new rosject

Create a new rosject

For the rosject, let’s select ROS2 Foxy for the ROS Distro, let’s name the rosject as Turtlesim. You can leave the rosject public. You should see your rosject just as in the example below (although in the picture the name is different)

Battery Level Node - Conditional Publisher in ROS2

Battery Level Node – Conditional Publisher in ROS2

If you mouse over the recently created rosject, you should see a Run button. Just click that button to launch the rosject.

Launching the turtlesim node using ros2

Now, assuming that you have opened the rosject by clicking the Run button that appears when you hover the mouse on the rosject, we can now open a terminal.

Open a new Terminal

Open a new Terminal

After opening the terminal, we can run the turtlesim just by running the following command:

ros2 run turtlesim turtlesim_node

.The logs you see should be similar to the following:

QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-user'
[INFO] [1641240027.604446440] [turtlesim]: Starting turtlesim with node name /turtlesim
[INFO] [1641240027.609126169] [turtlesim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]

If we now open the Graphical Tools, we should be able to see the turtlesim.

Open Graphical Tools to see the turtlesim

Open Graphical Tools to see the turtlesim

Moving the turtlesim around with ros2

Now that the simulation is up and running, we should be able to easily move the robot. However, before doing that, we have to list the topics to identify the topic that turtlesim listens to in order to move the robot.

Let’s list the topic by running the following command in a second terminal.

 ros2 topic list

The list of topic should be as follows:

/parameter_events
/rosout
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose

The important topis here for the purpose of moving the robot is the /turtle1/cmd_vel one.

If you are used to moving robots with ROS, you may know that by default, the teleop nodes normally publish to the topic named /cmd_vel, but in this case, the robot is listening to a topic with a different name: /turtle1/cmd_vel

How do we solve that then? By mapping /cmd_vel  to /turtle1/cmd_vel when launching the teleop node. We can achieve that with the following command, where we specify that we want to the topic:

ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args --remap /cmd_vel:=/turtle1/cmd_vel

We should now see the instructions to move the robot around:

This node takes keypresses from the keyboard and publishes them
as Twist messages. It works best with a US keyboard layout.
---------------------------
Moving around:
   u    i    o
   j    k    l
   m    ,    .
For Holonomic mode (strafing), hold down the shift key:
---------------------------
   U    I    O
   J    K    L
   M    <    >
t : up (+z)
b : down (-z)
anything else : stop

q/z : increase/decrease max speeds by 10%
w/x : increase/decrease only linear speed by 10%
e/c : increase/decrease only angular speed by 10%

CTRL-C to quit

currently:      speed 0.5       turn 1.0

If we now press u, i or o, for example, we should be able to see the robot moving.

With turtlesim you can do a lot of testing for mobile robots, where you don’t need to worry about x and y velocities, you can also spawn more turtles to simulate more robots.

We hope this little post is helpful for your ROS Learning journey.

Youtube video

So this is the post for today. Remember that we have the live version of this post on YouTube. If you liked the content, please consider subscribing to our youtube channel. We are publishing new content ~every day.

Keep pushing your ROS Learning.

Topics:
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

1 Comment

  1. Nemisha Vikamsey

    I used the same commands, but the turtle is not moving at all. the terminal is the active window

    Reply

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