3. ROS2 Topics Newsletter

Topic communication is one of the most frequently used communication methods in ROS2. Topic communication is based on the publish-subscribe model, where there is a publisher who publishes data on a specified topic, and a subscriber who subscribes to the topic can receive the data as long as it is subscribed to the topic. The next step is to explain how to use Python language to achieve topic communication between nodes.

1. Create a new workspace

Terminal input.

ros2_ws is the name of the workspace and src is the directory where the feature package is stored.

2. New function package

Terminal input,

image-20230428145702547

Switching to the topic_pkg folder, there are the following files and folders, the python program we wrote is placed in the topic_pkg folder in that directory, i.e. , the

3. Write publisher python file

3.1 Programme source code

Create a new file named publisher_demo.py.

Copy the following section into the file.

Here is the idea of modularity is used to implement, this approach is good for us to port and modify the code. First of all, a class is defined, the class name is Topic_Pub, there are two parts, one is init, the other is pub_msg. init is the initialisation of the class itself, pub_msg is the method of the class, that is to say, as long as we create the object of the class, we can use the variables and methods inside.

3.2 Modifying the setup.py file

Terminal input.

Find the location shown below.

image-20230428164527355

Inside 'console_scripts': [] add the following, in the format of

image-20230428164648037

The name of the executable file is customised by us, that is, we need to execute which program when we ros2 run, the name of the function package is the function package where your python file is located, followed by the name of the python file, and the main at the end is the entry point of the program execution. As long as the program is written in python format and compiled to generate an executable file, you need to add it here according to the above format.

3.3 Compilation Workspace

Compile successfully and add the workspace to the system environment variable by entering the following command.

Then, reopen the terminal or refresh the environment variables to take effect.

3.4. Run the programme

Terminal input.

Program successfully run is not printed anything, we can ros2 topic tool to see the data, first of all, first check this whether there is a topic published, terminal input, the

image-20230428170044755

This topic_demo is the topic data defined in the program, next, we use ros2 topic echo to print this data, terminal input.

image-20230428170548198

As you can see, the "Hi,I send a message." printed in the terminal matches the msg.data = "Hi,I send a message." in our code.

4. Write subscriber python file

4.1 Program source code

Create a new file named subscriber_demo.py.

Copy the following section into the file.

4.2 Modifying the setup.py file

Terminal input.

Find the location shown below.

image-20230504092717152

Inside 'console_scripts': [] add the following, in the format of

image-20230504092834784

4.3. Compilation workspace

After compiling, refresh the workspace environment variables.

4.4. Run the programme

Terminal input.

image-20230504093741770

As shown above, the terminal running the subscriber at this point prints the information about /topic_demo posted by the publisher.

The topic communication between the two nodes can be viewed with the following command.

image-20230504094102343

5. Summary

5.1 python framework for writing topic communication programmes

image-20230504095534969

5.2 Modifying the contents of the setup.py file

To write a node program using python, you need to modify the setup.py file, refer to the above content and add to generate your own node program.