Skip to content

More ROS

Faizaan Datoo edited this page Oct 16, 2021 · 1 revision

In ROS Fundamentals, we explored some fundamental ROS concepts, like nodes and topics. In this module, we'll explore how to create ROS packages, using ROS launch files, and TODO: finish

Creating a ROS Package

As we talked about in the previous module, a package is just a folder that contains related code (like a package in Java and Python) that gets compiled together. We're going to create a package in your Pathfinder project.

Open up a Pathfinder terminal (using the pathfinder.py file) and run the following two commands:

cd /Pathfinder/robot/src
catkin_create_pkg my_first_package std_msgs rospy roscpp

You'll see the following output:

catkin_create_pkg

If you get an error, make sure you cd into the aforementioned directory. If you go to your Pathfinder/robot/src folder, you'll see a brand new directory called "my_first_package" with a bunch of files and folders inside. Let's take a closer look at what each of these are:

  • package.xml is the package manifest file. It contains some basic metadata about your package. If you scroll down, you'll see a list of packages that this package depends on (i.e. requires or else it won't be able to build). Remember how we included "std_msgs rospy roscpp" at the end of the previous command? Those were the dependencies that you now see listed here.

  • CMakeLists.txt contains the recipe for building this package using CMake, which is a very popular C++ build system. It contains a lot of comments instructing you on what each option means; skim it quick!

  • include contains the header files that our package's C++ files will be based on.

  • src contains the C++ files for our package. This is where our nodes and other classes would go.

Now that we have a package, let's do something with it.

Launch Files

Launch files allow us to run multiple nodes in a predefined configuration. An example of where this is really helpful in the Rover codebase is the launch file for autonomous navigation. It starts up the GPS service, the coordinate system transformation node, the Lidar node, and finally starts up the navigation planning node. Imagine having to do all that manually each time we wanted to run the rover?

Create a folder inside the Pathfinder/robot/src/my_first_package directory called "launch". Inside this launch folder, create a file called turtle_mimic.launch and paste the following:

<launch>

  <!-- Here, we'll start two turtlesims named "turtlesim1" and "turtlesim2". -->
  <!-- The ns (namespace) attribute lets us run two of the same node without naming conflicts. -->
  <group ns="turtlesim1">
    <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
  </group>

  <group ns="turtlesim2">
    <node pkg="turtlesim" name="sim" type="turtlesim_node"/>
  </group>

  <!-- This will run the "mimic" node from the "turtlesim" package. -->
  <!-- It's a built-in node that comes with the turtle simulator. -->
  <node pkg="turtlesim" name="mimic" type="mimic">
    <remap from="input" to="turtlesim1/turtle1"/>
    <remap from="output" to="turtlesim2/turtle1"/>
  </node>

</launch>

In the mimic node's section, we are remapping the "input" topic to the velocity commands of the first turtle, and the "output" topic to the velocity commands of the second turtle. That way, the second turtle will copy the actions of the first turtle.

Let's actually launch this launch file. Open up a terminal using the pathfinder.py script, and run the following three commands:

cd /Pathfinder
source robot/devel/setup.bash
roslaunch my_first_package turtle_mimic.launch

We told roslaunch to look inside "my_first_package" for a launch file called "turtle_mimic.launch". The previous "source" command told the system which directories to search for launch files in.

You'll see two turtlesims pop up! Let's watch the second turtle mimic the first; open a new terminal instance (using the pathfinder.py script) and send the following Twist message to turtle1's velocity topic:

rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

Now, both turtles will start moving in circles forever, even though we only told turtle1 to do that!

If you run rqt as we did in the ROS Fundamentals module, you'll see a more complex node graph:

ROS mimic graph

Next Steps

Now that you've completed ROS Fundamentals and More ROS, you have a taste of what it's like to work with ROS. We've talked about packages, nodes, and having nodes communicate via topics. We saw how to analyze nodes and topics using analytic tools like RQT and some command-line tools. We even synthesized all these ideas by creating a node that has one turtle mimic another.

You might have noticed, though, that we did not write a single line of code. In the next couple modules, we'll take a break from building the Pathfinder project and take some time to learn a bit about C++ and Python, the two main programming languages we use on Rover.

Move onto the Python module next.

Clone this wiki locally