How to do it…

In this section, we will practice what we have learned until now. We will learn about ROS tools for navigating through the ROS filesystem and work with examples to practice setting up the ROS Workspace and the creation and building of a package and the metapackage. For creating the ROS workspace, we will perform the following steps:

  1. First, we will create our own workspace. In this workspace, we will centralize all the example source code used throughout this cookbook.
  2. Get the workspace that ROS is using, using the following command:
$ echo $ROS_PACKAGE_PATH

This will show an output similar to the following:

/opt/ros/kinetic/share:/opt/ros/kinetic/stacks 
  1.  Create and initialize the ROS workspace:
$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_init_workspace

We have created an empty ROS workspace, which contains only a CMakeList.txt file.

  1. To build the workspace, write the following commands:
$ cd ~/catkin_ws
$ catkin_make 

We can see that the new folders, build and devel, were created with the previous make command.

  1. To finish the configuration, use the following command:
$ source devel/setup.bash

This step will reload the setup.bash file from the ROS workspace and set up an overlay over the default configuration.

  1. To get the same environment for every shell on opening and closing, the following should be added in the ~/.bashrc scripts:
$ echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
$ echo "source /home/usrname/catkin_ws/devel/setup.bash" >> ~/.bashrc

ROS provides a number of command-line tools for navigating through the filesystem. We will look into some of most used ones.

To get information about the packages and stacks in the ROS environment, such as their paths, dependencies, and so on, rospack and rosstack can be used. Similarly, to move through packages and stacks, as well as listing their contents, roscd and rosls can be used.

For example, the path of the turtlesim package can be found with the following command:

$ rospack find turtlesim

This will result in the following output:

/opt/ros/kinetic/share/turtlesim 

A similar result will be shown for installed metapackages with the following command:

$ rosstack find ros_comm
  1. To get the path for the ros_comm metapackage, use the following:
/opt/ros/kinetic/share/ros_comm 

The following command lists the files inside the package or metapackage:

$ rosls turtlesim

The output of the preceding command will be as follows:

cmake    images    srv      package.xml  msg 
  1. The current working directory can be changed with roscd:
$ roscd turtlesim
$ pwd

The new working directory will be as follows:

/opt/ros/kinetic/share/turtlesim 

In ROS, package creation can be done manually, but to avoid the complex and tedious work involved, it is recommended to use the catkin_create_pkg command-line tool instead.

We will create a new package in our recently initialized workspace using the following commands:

$ cd ~/catkin_ws/src
$ catkin_create_pkg chapter2_tutorials std_msgs roscpp

This command includes the name of the package and the dependencies, in our case, std_msgs and roscpp.

The syntax of the command is as follows:

catkin_create_pkg [package_name] [dependency1] ... [dependencyN]
The std_msgs package contains common message types representing primitive data types and other basic message constructs, such as multiarray. The
roscpp package is a C++ implementation of ROS. It provides a client library that enables C++ programmers to quickly interface with ROS topics, services, and parameters.

As discussed earlier, we can use the rospack, roscd, and rosls commands to retrieve information about the ROS package.

  • rospack profile: It informs us about the newly-added packages to the ROS system, which is useful after installing any new package.
  • rospack find chapter2_tutorials: It helps us in finding the path of the package.
  • rospack depends chapter2_tutorials: It retrieves the package dependencies.
  • rosls chapter2_tutorials: It shows the package content.
  • roscd chapter2_tutorials: This command changes the working directory.

Although the creation of a metapackage has similar steps to those for creating a package, it has a few limitations, and special cases, as discussed in the previous section so, should be handled with care. Once we have our package created and have written some code, it is necessary to build the package. The build process for a package compiles not only the code added by the user but also the code generated from the messages and services.

  1. To build a package, we will use the catkin_make tool, as follows:
$ cd ~/catkin_ws/
$ catkin_make

The catkin_make command should be run in the workspace folder. If it were run in any other folder, eventually the command would fail. However, catkin_make is executed in the catkin_ws folder, and we will get a complete successful compilation. Moreover, a compilation of a single package using catkin_make could be done using the following command:

$ catkin_make --pkg <package name>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.123