yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
T-mini Plus LiDAR📖
01.Linux basic course
  • 1.Introduction to Linux system
  • 2.Ubuntu file system
  • 3.Ubuntu common commands
  • 4.Ubuntu common editors
  • 5.Ubuntu software operation commands
  • 6.Virtual machine installation
  • 7.SSH remote control
  • 8.VNC remote control
  • 9.Transfer files remotely
  • 10.Driver library and communication
  • 11.Static IP and hotspot mode
  • 12.Bind device ID
  • 13.Capacity expansion and resource allocation
  • 14.Update system software sources
  • 15.Set root user password
  • 16.sudo free password
  • 17.Connect to WiFi network
  • 18.View system version
  • 19.Customized service management
  • 20.Back up system image
02.ROS1 basic course
  • 1.ROS introduction
  • 2.ROS installation
  • 3.ROS common command tools
  • 4.ROS workspace
  • 5.ROS function package
  • 6.ROS node
  • 7.ROS topic publisher
  • 8.ROS topic subscribers
  • 9.ROS service client
  • 10.ROS service server
  • 11.ROS action client
  • 12.ROS action server
  • 13.ROS custom message reception
  • 14.ROS-launch file
  • 15.ROS-TF transformation
  • 16.ROS parameter service
  • 17.ROS-rviz use
  • 18.ROS-rqt tool usage
  • 19.Topic message recording and playback
  • 20.urdf model introduction
  • 21.gazebo introduction
  • 22.ROS distributed communication
03.ROS1 lidar course
  • 0.Instructions before use
  • 1.Preparation
  • 2.Handheld lidar mapping
  • 3.Lidar avoiding
  • 4.Lidar guard
  • 5.Lidar follow
  • 6.Lidar mapping
  • 7.rrt_exploration mapping algorithm
  • 8.Navigation
  • 9.APP Mapping and Navigation
  • 10.RTAB 3D mapping and navigation
04.ROS2 basic course
  • 1. Introduction to ROS2
  • 2.ROS2 install Humble
  • 3.ROS2 development environment
  • 4.ROS2 workspace
  • 5.ROS2 function package
  • 6.ROS2 node
  • 7.ROS2 topic communication
  • 8.ROS2 service communication
  • 9.ROS2 action communication
  • 10.ROS2 custom interface message
  • 11.ROS2 parameter service case
  • 12.ROS2 meta-function package
  • 13.ROS2 distributed communication
  • 14.ROS2 DDS
  • 15.ROS2 time related API
  • 16.ROS2 common command tools
  • 17.ROS2 rviz2 use
  • 18.ROS2 rqt toolbox
  • 19.ROS2 Launch startup file configuration
  • 20.ROS2 recording and playback tool
  • 21.ROS2 URDF model
  • 22.ROS2 Gazebo simulation platform
  • 23.ROS2 TF2 coordinate transformation
05.ROS2 lidar course
  • 0.ReadMe
  • 1.Preparation
  • 2.Handheld lidar mapping
  • 3.Lidar avoiding
  • 4.Lidar follow
  • 5.Lidar guard
  • 6.Lidar patrol
  • 7.gmapping mapping
  • 8.cartographer mapping
  • 9.Navigation and avoiding
  • 10.APP Mapping and Navigation
  • 11.RTAB 3D mapping and navigation
06.Raspberry Pi5 course
2.ROS1 lidar course
  • 0.Instructions before use
  • 1.Preparation
  • 2.Handheld lidar mapping
  • 3.Lidar avoiding
  • 4.Lidar guard
  • 5.Lidar follow
  • 6.Lidar mapping
  • 7.rrt_exploration mapping algorithm
  • 8.Navigation
  • 9.APP Mapping and Navigation
  • 10.RTAB 3D mapping and navigation
3.ROS2 lidar course
  • 0.ReadMe
  • 1.Preparation
  • 2.Handheld lidar mapping
  • 3.Lidar avoiding
  • 4.Lidar follow
  • 5.Lidar guard
  • 6.Lidar patrol
  • 7.gmapping mapping
  • 8.cartographer mapping
  • 9.Navigation and avoiding
  • 10.APP Mapping and Navigation
  • 11.RTAB 3D mapping and navigation
1.Docker course
  • 1. Overview and installation
  • 2. Common commands
  • 3. Docker images deeply understand and publish images
  • 4. Docker hardware interaction and data processing
  • 5. Enter the robot's docker container
Download
  • Code
  • VM_File
  • RPi5_SystemFile
  • Data Sheet
  • 3D_Model_File
  • PC_Software
  • ROS2_Basic_VM
Download ZIP

Welcome to T-mini Plus LiDAR repository

8.ROS topic subscribers

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:

  • Initialize ROS nodes
  • Create handle
  • Subscribe to the topics you need
  • Wait in a loop for topic messages, and enter the callback function after receiving the message
  • Complete message processing in the callback function

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,

/*Create a small turtle to receive the current pose information*/
#include <ros/ros.h>
#include "turtlesim/Pose.h"
// After receiving the message, you will enter the message callback function, which will process the received data.
void turtle_poseCallback(const turtlesim::Pose::ConstPtr& msg)
{
    // Print received messages
    ROS_INFO("Turtle pose: x:%0.3f, y:%0.3f", msg->x, msg->y);
}
int main(int argc, char **argv)
{
    ros::init(argc, argv, "turtle_pose_subscriber");// Initialize ROS node
    ros::NodeHandle n;//Here is create handle
    // Create a subscriber. The topic of subscription is the topic of /turtle1/pose. poseCallback is the callback function.
    ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10,turtle_poseCallback);
    ros::spin(); // Loop waiting for callback function
    return 0;
}

8.2.2 Modify CMakelist.txt file

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

xxxxxxxxxx
add_executable(turtle_pose_subscriber src/turtle_pose_subscriber.cpp)
target_link_libraries(turtle_pose_subscriber ${catkin_LIBRARIES})

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,

xxxxxxxxxx
cd ~/ros_ws
catkin_make

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.

xxxxxxxxxx
cd ~/ros_ws
source devel/setup.bash

8.2.4 Running the program

Run roscore

xxxxxxxxxx
roscore

Run the little turtle node

xxxxxxxxxx
rosrun turtlesim turtlesim_node

Run the turtle speed control node

xxxxxxxxxx
rosrun turtlesim turtle_teleop_key

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

xxxxxxxxxx
rosrun learn_topic turtle_pose_subscriber

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.

xxxxxxxxxx
rqt_graph

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,

xxxxxxxxxx
void turtle_poseCallback(const turtlesim::Pose::ConstPtr& msg)
{
    // Print received messages
    ROS_INFO("Turtle pose: x:%0.3f, y:%0.3f", msg->x, msg->y);
}

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,

xxxxxxxxxx
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
from turtlesim.msg import Pose
def poseCallback(msg):
    rospy.loginfo("Turtle pose: x:%0.3f, y:%0.3f", msg.x, msg.y)
def turtle_pose_subscriber():
    rospy.init_node('turtle_pose_subscriber', anonymous=True)# ROS node initialization
    # Create a Subscriber, subscribe to the topic named /turtle1/pose, and register the callback function poseCallback
    rospy.Subscriber("/turtle1/pose", Pose, poseCallback)
    rospy.spin()# Loop waiting for callback function
if __name__ == '__main__':
    turtle_pose_subscriber()

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

xxxxxxxxxx
cd ~/ros_ws/src/learn_topic/scripts
sudo chmod a+x turtle_pose_subscriber.py

8.3.2 Run

Open roscore,

xxxxxxxxxx
roscore

Run the little turtle node,

xxxxxxxxxx
rosrun turtlesim turtlesim_node

Run the turtle speed control node,

xxxxxxxxxx
rosrun turtlesim turtle_teleop_key

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

xxxxxxxxxx
rosrun learn_topic turtle_pose_subscriber.py

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

ROS topic publisher
ROS service client

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文