How to Create a Launch File in ROS 2 Foxy Fitzroy

In this post, we will learn how to create a launch file in ROS 2. A launch file enables you to launch multiple ROS 2 nodes at the same time. These files are created in Python and are run using the ros2 launch command (we’ll cover this later in this tutorial).

The official tutorial is located in the ROS 2 Foxy documentation, but we’ll run through the entire process step-by-step below.

You Will Need

In order to complete this tutorial, you will need:

Prerequisites

It is helpful if you’ve completed this tutorial on the Turtlesim package in ROS 2, but it is not required.

Setup

Open a new terminal window, and type the following command to create a new folder:

mkdir launch

If you don’t have gedit installed, install it now.

sudo apt-get install gedit

Open up a new launch file.

gedit launch/turtlesim_mimic_launch.py

Write the following code inside the launch file.

# This is an example of a launch file for ROS 2

# Import launch modules that are Python-compatible
from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
  """
  This file is the launch description. It will launch three nodes:
  turtlesim1: A turtle
  turtlesim2: Another turtle
  mimic: Causes one turtle to mimic the movements of the other turtle
  """
  return LaunchDescription([
  
    # Launches a window with a turtle in it
    Node(
      package='turtlesim',
      namespace='turtlesim1',
      executable='turtlesim_node',
      name='sim'
    ),
    # Launches another window with a turtle in it
    Node(
      package='turtlesim',
      namespace='turtlesim2',
      executable='turtlesim_node',
      name='sim'
    ),
    # The mimic executable contains code that causes turtlesim2 to mimic
    # turtlesim1
    Node(
      package='turtlesim',
      executable='mimic',
      name='mimic',
      remappings=[
        ('/input/pose', '/turtlesim1/turtle1/pose'),
        ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
      ]
    )
  ])

Click Save and close the file to return to the terminal.

Launch the Launch File

To launch the launch file, move to the launch file:

cd launch

Type:

ros2 launch turtlesim_mimic_launch.py
1_terminal_outputJPG

In a new terminal tab, publish a velocity command to force turtlesim1 to move at a forward velocity of 2.0 meters per second and an angular velocity of -1.8 radians per second. All of this stuff below is a single command.

ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}"

You will see the turtles move around in unison in a circle.

2_turtles_mimicJPG

To see the relationship between nodes, open a new terminal window, and type:

rqt_graph
3-node-relationshipJPG

The hidden node n___ros2cli_4394 represents the command you entered in the command line interface (i.e. the terminal window). This node publishes the velocity command to a topic named /turtlesim1/turtle1/cmd_vel.

The /turtlesim1/sim node subscribes to velocity commands that are published on the /turtlesim1/turtle1/cmd_vel topic.

The /turtlesim1/sim node publishes to /turtlesim1/sim’s pose topic.

/mimic is subscribed to /turtlesim1/sim’s pose topic.

/mimic publishes to /turtlesim2/sim’s velocity command topic, which causes the second turtle to move like the first turtle.

When you’re done, press CTRL+C to shut down everything.

Why Most Machine Learning Books Suck

Good teaching is work. Great teaching is a lot of work. Mediocre teaching is no work at all.

Addison Sears-Collins (2019)

Yes, the title is provocative, but somebody had to say it. Here is a quick list why most machine learning books are a waste of time. I’ll get into each point later in this post.

  • Subscript and Superscript Soup
  • Too Much Focus on Irrelevant Minutiae
  • Teaching to the Tools Instead of the Problem
  • No Common Language
  • Author Has No Training on How to Teach
  • Using Words Like “Basically”, “Simple”, and “Easy”
  • Show Me Don’t Tell Me
  • Here is What Well Written Textbooks Look Like

Most of the “introductory” machine learning books (textbooks, especially) suck. While the subject matter in these books is supposed to be introductory, the way in which concepts are explained is not introductory at all. Simple concepts are often explained with such complex jargon and mathematical symbol soup that the underlying ideas get totally lost.

If you want to see mental masturbation at its finest, just pick up any of the popular machine learning textbooks used in machine learning courses at colleges and universities around the world.

So many of these machine learning textbooks spend pages and pages explaining machine learning without actually doing machine learning with step-by-step fully worked real-world examples (I presume because the publisher wants to limit the page count). It is kind of like learning how to play tennis by having someone explain to you how to play tennis vs. getting out there on the court and actually playing!

While reading these books, I always find myself asking these questions:

  • Why write an introductory book if the explanations are so convoluted that only an expert can understand? 
  • Why ask end-of-chapter questions and provide no answer key? 
  • Why provide no step-by-step practice with real-world examples?

Feedback and deliberate practice is a critical part of building confidence as you learn a new skill. Most of the “introduction” to machine learning books lose sight of this concept. Authors need to realize that you need to start with teeny tiny baby steps when you are writing for beginners.

Subscript and Superscript Soup

Take a look at this excerpt from a popular Introduction to Machine Learning textbook where the author attempts to explain logistic regression.

subscript-soup
Source: Introduction to Machine Learning

Wtf? 

This example hits on one of my biggest annoyances about machine learning books. The algorithms and mathematical equations contain so many variables, subscripts, and superscripts that you need to make a glossary on a separate sheet of paper just to keep track of it all because it would be utterly impossible to retain everything in your head while trying to decipher the points the author is trying to make. This kind of practice gets in the way of learning as well as your ability to see the big picture. 

Most of these “introduction” to machine learning books are written for people who already have a deep expertise in the subject. The subscript and superscript soup present in these books are as understandable to me as Ancient Greek. 

ancient-greek-writing

Too Much Focus on Irrelevant Minutiae

Take a look at this page taken from another popular Machine Learning book where the author introduces the K-Means clustering algorithm.

What the heck is this guy talking about? I had to double check the preface to see if this book was written for beginners or experts (i.e. it was written for beginners). To the average undergrad who wants to go out and get a job applying machine learning to real-world problems, how important is it to know all these mathematical details upfront? 

Answer: Not very important. Rarely will you ever need to know these details, and if you do, just look them up. Most textbooks focus way too much on the minutiae (as if they are writing an academic research paper) and not enough on how to apply this knowledge to solve real-world problems.

At no point do these authors convey:

  • Why does knowing this matter? 
  • What value does knowing this have on a real-world application as well as my ability to get and retain a job in this field. Most beginners don’t want to go on to get PhDs and do research. Some do, but most of the readers do not.

Teaching to the Tools Instead of the Problem

Machine learning textbooks need to teach to the problem, not to the tools and the underlying mathematics. They need to first tell you why something is important. The easiest way to do this is to explain a concept by starting with a real-world problem

Most textbooks, instead, do it the other way around. They teach you the intricate details of the tool in isolation, teach you some proof, and then (in rare cases), apply the concept to a problem (a problem which almost never resembles the types of problems you would face in the real world at a job).

Authors need to show readers how machine learning fits into the big picture (i.e. the other tools a beginner might already be familiar with, like statistics or data analysis). At the end of the day, machine learning is just a tool. It is not a panacea. 

It is just a tool, just like the World Wide Web is a tool…just like a programming language like Python is a tool…or just like Microsoft Excel is a tool. 

Machine Learning is to an engineer what a wrench is to a mechanic. A mechanic does not need to know how to build a wrench in order to use it. He just needs to know how to use it to solve a problem. The vast majority of people will not need to know the underlying proofs and mathematics at such a detailed level in order to succeed in the workplace (unless you become a researcher, in that case, you will be trying to build a better wrench).

tool_wall_tool_storage_0
Machine Learning is a tool in the toolbox

Machine learning textbooks need to start with a real-world problem, then work through that problem, set-by-step, and explain the mathematics on an as-needed basis, not just throw it on the page for the sake of pretending to be mathematically rigorous.

If you decide to deep dive into research later on in your career, THEN pick up the mathematics on an as-needed basis. 

I don’t need to know how an internal combustion engine is built to be an awesome driver, and most companies will hire you based on your problem-solving ability, not on your ability to write proofs of off-the-shelf algorithms…so problem solving is what books need to focus on.

No Common Language

It does not help that there is no common language in machine learning. There are sometimes a dozen different ways to say the same thing (feature, attribute, predictor, x-variables, independent variables, etc.) or (target, class, response variable, y-variable, hypothesis). These multiple ways of saying the same thing make it totally confusing for a beginner, and rarely do authors point out the fact that there are a myriad of ways of saying the same thing.

Author Has No Training on How to Teach

elementary_school_aboard

Many of these authors are accustomed to writing for an expert audience (via academic research papers) and have not properly developed the skill of breaking down complex subject matter into easy-to-understand, digestible bite-sized pieces that would be understandable by a competent beginner. 

Elementary and high school teachers must get a degree or diploma in teaching. They have to endure hundreds of hours of instruction on how to teach and how to deal with different learning styles. Most machine learning textbook authors have not had this training. 

I find that the best teachers of a subject are often students who are one step removed from having learned the subject. It is fresh in their minds, and they still remember what it is like to be a beginner.

Using Words Like “Basically”, “Simple”, and “Easy”

A favorite of machine learning textbook authors it to make the assumption that the reader already understands a “simple” concept that’s necessary to understand the new topic. This is frustrating for someone new.

So many of these textbooks use terms like “simple,” “straightforward,” “easy,” “obvious,” and “basically,” which hurts a beginner’s confidence if he is not easily able to grasp the material. This is a huge pet peeve of mine. Words like these have no place in a book claiming to be an “introduction.” 

You’re trying to climb the machine learning mountain. The author is already at the top of the mountain. The author forgot how hard the struggle was to get to the top. The author forgot what it is like to even take the first step because it has been so long. It all seems easy after you’ve been there, done that. 

It would be like Michael Jordan teaching someone how to play basketball. Some things are so intuitive and second nature to him that, although he is the best basketball player that ever lived, he might not be the best one to teach it because he is so far removed from what it was like as a beginner, learning the basics, when even the smallest steps are difficult and not second nature. 

Similarly, you might know how to ride a bicycle really well. However, try to teach someone else how to ride a bicycle. You will notice that teaching someone how to ride a bike is different from being a really good bike rider. In order to teach someone how to ride a bike, you have to take what you know and break it down into teeny tiny parts. The ability to do this well is a skill in and of itself…one that takes time and practice to get good at.

bicycle-2

When you learn something, and especially if it is something you’ve spent decades immersed in, it’s intuition to you. Most machine learning books are especially bad about this. They just assume that you understand exactly what is going on. They do not explain. It was just “if this, then that.” They speak in generalities and hand-wavy language when the beginner needs step-by-step detail. They use machine learning and math to explain machine learning and math.

They do not realize that in order to teach someone something, it is best to tie the new concept to a concept that the beginner might already be familiar with.

Again, the best teachers in my experience are those that are one step removed from learning a subject as they have the knowledge fresh in their mind and remember clearly what it is like to be a beginner.

Show Me Don’t Tell Me

Authors need to stop introducing a new concept by explaining it. Instead, they need to use the following teaching aids:

  • Analogies: Connect the current knowledge to previous knowledge that most beginners would have.
  • Pictures and Diagrams: Draw a picture to help me visualize the concept.
  • Real-world Examples: Why does this concept matter? Show me a real-world example of this concept in practice, solving an actual problem. Tell me a story.
  • Layman’s Terms: Explain a term in basic plain language. Act as if you are explaining a concept to a five-year-old child.

Here is What Well Written Textbooks Look Like

Here is what a well written textbooks for beginners should look like. Consider these textbooks among the GOATs (i.e. greatest of all time) of textbooks:

The books above are an absolute joy to learn from. They will make you rethink the way introductory textbooks should be written. 

For you “Introduction” to machine learning authors out there, take note.

Further Reading

I encourage you to check out Jason Brownlee’s Post, “Why Machine Learning Does Not Have to Be So Hard”, where he calls out universities and traditional courses for teaching machine learning incorrectly. He also outlines a rough process for getting started.

Also, check out the first response in this post about how to learn difficult subjects.

Mastering Turtlesim in ROS 2 Foxy Fitzroy

In this post, we will learn the basics of ROS 2 by working with Turtlesim, a ROS 2-based simulator. Follow each step below, one line at a time. Take your time, so you do everything correctly. There is no hurry.

The official tutorial is located in the ROS 2 Foxy documentation, but we’ll run through the entire process step-by-step below.

You Will Need

In order to complete this tutorial, you will need:

Prerequisites

It is helpful if you’ve completed this tutorial on Linux fundamentals, but it is not required.

Install Turtlesim

Install Turtlesim, by typing the following commands:

sudo apt update
sudo apt install ros-foxy-turtlesim

Check that Turtlesim was installed.

ros2 pkg executables turtlesim

Here is what you should see:

1_turtlesim_installedJPG

Launch Turtlesim

To launch Turtlesim, type:

ros2 run turtlesim turtlesim_node

Here is the window that you should see:

2_turtlesim_launch_windowJPG

In the terminal, you will see the position and orientation of the turtle.

3-turtle-poseJPG

Let’s move the turtle around the blue screen.

ros2 run turtlesim turtle_teleop_key

With this same terminal window selected, use the arrow keys to navigate the turtle around the screen.

To close turtlesim, go to all terminal windows and type:

CTRL + C

Common ROS 2 Commands

Open a new terminal. Let’s type some common ROS 2 commands.

To list the active ROS nodes, type the following command. A node in ROS is just a program (e.g. typically a piece of source code made in C++ or Python) that does some computation. 

ros2 node list
4-ros-2-node-listJPG

Robots require a number of programs to work together to achieve a task. You can think of a node as a small single-purpose program within a larger robotic system. 

One way nodes communicate with each other is by using messages. These messages are passed via channels called topics.

To see a list of active topics in ROS 2, type the following command:

ros2 topic list
5-ros2-topic-listJPG

Don’t worry what all those topics mean. In a later post, I’ll dive deeper into ROS topics.

Let’s see what the active services are. A ROS Service consists of a pair of messages: one for the request and one for the reply. A service-providing ROS node (i.e. Service Server) offers a service (e.g. read sensor data). 

ros2 service list
6-ros2-service-listJPG

Let’s see what actions are active now. I explain the difference between a ROS Action and a ROS Service in this post.

ros2 action list
7-ros-action-listJPG

Here is one more command you should know:

  • ros2 node info <node_name> : Shows information about the node named node_name.

Install rqt

Let’s install rqt. rqt is a tool that enables you to see the connections between nodes.

sudo apt update
sudo apt install ~nros-foxy-rqt*

Press Y and Enter to complete the installation.

Run rqt

Launch turtlesim again.

ros2 run turtlesim turtlesim_node

In another terminal tab, type:

ros2 run turtlesim turtle_teleop_key

To run rqt, open a terminal window and type:

rqt

If this is your first time running rqt, go to Plugins > Services > Service Caller in the menu bar at the top.

Here is what my window looks like:

8-service-dropdownJPG

Click the refresh button to the left of the Service dropdown menu. This button looks like this:

9-service-refreshJPG

Here is what my window looks like now:

10-service-refreshJPG

Click the dropdown list in the middle of the window, and select the /spawn service.

11-select-spawn-serviceJPG

Launch Another Turtle Using the Spawn Service

The /spawn service spawns another turtle. Let’s use this service now.

Set the values under the Request area to the following by double-clicking the values under the ‘Expression’ menu.

In this case, we want to launch a new turtle at coordinate x=1.0, y=1.0. The name of this new turtle will be turtle2.

Call this service by clicking the Call button in the upper right of the panel.

13-call-buttonJPG

You should see a new turtle spawned at coordinate x=1.0, y=1.0.

14-new-turtle-is-spawnedJPG

Click the Refresh button.

You now have new services for turtle2.

15-click-refreshJPG

Using the Set Pen Service

Let’s change the color of the pen for the first turtle, turtle1.

Go to the Service dropdown list, and scroll down to /turtle1/set_pen.

16-width-of-penJPG

We want turtle1’s pen to be green, so we set the g (Green) color value to 255. I also want the width of the pen to be 5.

Click Call to call the service.

If you go to the terminal and select the terminal where you typed the “ros2 run turtlesim turtle_teleop_key” command, you can move turtle1 around the screen with your keyboard. You will see a green pen color.

17-green-lineJPG

Move turtle2

Let’s move turtle2. To do that, we need to perform remapping. We need to remap the velocity command for turtle1 to turtle2. Open a new terminal tab, and type (this is all a single command):

ros2 run turtlesim turtle_teleop_key --ros-args --remap turtle1/cmd_vel:=turtle2/cmd_vel

You can use your keyboard to move turtle2 around the screen.

18-move-turtle2JPG

To close turtlesim, go to all terminals and type:

CTRL + C