8.ROS topic subscribers

The previous section introduced how to write a publisher node program in ros. This section introduces how to write a subscriber program.

8.1 Create a subscriber

The general creation steps are as follows:

The callback function is an important part of the subscriber. It processes the subscribed message, then parses the message data, and determines how to proceed with the subsequent program based on the parsed message data.

8.2 C++ version

8.2.1 Writing source code

In the src folder of the function package learn_topic, create a C++ file (the file suffix is .cpp), name it turtle_pose_subscriber.cpp, and paste the following content into turtle_pose_subscriber.cpp,

8.2.2 Modify CMakelist.txt file

Configure in CMakelist.txt, under the build area, add the following content,

add_executable shows that the generated executable program file is turtle_pose_subscriber, and the compiled source code is turtle_pose_subscriber.cpp in the src directory.

target_link_libraries specifies the libraries that need to be linked when compiling and generating an executable file.

8.2.3 Compile

Terminal input,

image-20231023163058208

After the compilation is passed, you need to re-source the current environment variables to find or update the program. Enter in the terminal.

8.2.4 Running the program

Run roscore

Run the little turtle node

Run the turtle speed control node

Run the subscriber node to continuously receive the pose data sent by the little turtle.

After all programs are running, click on the terminal running the turtle speed control node and press [up, down, left, and right] on the keyboard to control the movement of the turtle. The terminal running the subscriber will print the xy coordinate value of the turtle to the terminal in real time.As shown below,

image-20231023163258938

We can use the rqt_graph tool to view the communication between nodes and terminal input.

image-20231023163655762

The inside of the ellipse represents the node name, the inside of the rectangle represents the topic name, and the arrow represents the direction of topic message transmission. The subscriber node program turtle_pose_subscriber we wrote is subscribed to the topic message of /turtle1/pose.This is consistent with the code ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, turtle_poseCallback);, and then in the callback function, print the data, the code is as follows,

8.2.5 Program flow chart

image-20231023164512450

8.3 Python version

8.3.1 Writing source code

Create a new python file (file suffix .py) in the scripts folder under the function package learn_topic, name it turtle_pose_subscriber.py, copy and paste the following program code into the turtle_pose_subscriber.py file,

The python program does not need to be compiled, but it needs to add executable permissions and enter it in the terminal.

8.3.2 Run

Open roscore,

Run the little turtle node,

Run the turtle speed control node,

Run the subscriber node and continue to receive the pose data sent by the little turtle.

After all programs are running, click on the terminal running the turtle speed control node and press [up, down, left, and right] on the keyboard to control the movement of the turtle. The terminal running the subscriber will print the xy coordinate value of the turtle to the terminal in real time.As shown below,

image-20231023165328965

8.3.3 Program flow chart

image-20231023165516756