Learning Robotics using Python
上QQ阅读APP看书,第一时间看更新

Creating a ROS package

In this section, we will look at how to create a sample package that contains two Python nodes. One of the nodes is used to publish a Hello World string message on a topic called /hello_pub and the other node will subscribe to this topic.

A catkin ROS package can be created using the catkin_create_pkg command in ROS.

The package is created inside the src folder that we created during the creation of the workspace. Before creating the packages, switch to the src folder using the following command:

    $ cd ~/catkin_ws/src  

The following command will create a hello_world package with std_msgs dependencies, which contain standard message definitions. The rospy is the Python client library for ROS:

    $ catkin_create_pkg hello_world std_msgs rospy  

This is the message we get upon a successful creation:

    Created file hello_world/package.xml 
    Created file hello_world/CMakeLists.txt 
    Created folder hello_world/src 
    Successfully created files in /home/lentin/catkin_ws/src/hello_world. 
Please adjust the values in package.xml.

After the successful creation of the hello_world package, we need to add two Python nodes or scripts to demonstrate the subscribing and publishing of topics.

First, create a folder named scripts in the hello_world package using the following command:

    $ mkdir scripts  

Switch to the scripts folder and create a script named hello_world_publisher.py and another script called hello_world_subscriber.py to publish and subscribe to the hello world message. The following section covers the code and function of these scripts or nodes: