ROS study notes six
Topic model
The picture below is from Gu Yue's "21 Lectures on ROS Introduction"
ROS Master manages two nodes, the publisher turtlesim and the subscriber Pose Listener communicate through the /turtle1/pose topic transmission turtlesim::Pose type messages, this section mainly implements the subscriber Subscriber
Create subscriber code (C++)
Introduce the header file
#include <ros/ros.h>
#include <turtlesim/Pose.h>
//引入消息类型头文件
Main function
int main(int argc, char **argv)
{
//初始化ROS节点
ros::init(argc, argv, "pose_subscriber");
//创建节点句柄
ros::NodeHandle n;
//创建一个subscriber,订阅名为/turtle1/pose的topic,注册回调函数poseCallback
ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, poseCallback);
//循环等待回调函数
ros::spin();
return 0;
}
Callback
void poseCallback(const turtlesim::Pose::ConstPtr& msg)
{
//将接收到的消息打印出来
ROS_INFO("Turtle pose : x:%0.6f, y:%.6f", msg->x, msg->y);
}
Complete code
/**
* 该例程将订阅/turtle1/pose话题,消息类型为turtlesim::Pose
*/
#include <ros/ros.h>
#include <turtlesim/Pose.h>
//引入消息类型头文件
//接收到订阅消息后,会进入回调函数
void poseCallback(const turtlesim::Pose::ConstPtr& msg)
{
//将接收到的消息打印出来
ROS_INFO("Turtle pose : x:%0.6f, y:%.6f", msg->x, msg->y);
}
int main(int argc, char **argv)
{
//初始化ROS节点
ros::init(argc, argv, "pose_subscriber");
//创建节点句柄
ros::NodeHandle n;
//创建一个subscriber,订阅名为/turtle1/pose的topic,注册回调函数poseCallback
ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, poseCallback);
//循环等待回调函数
ros::spin();
return 0;
}
Process combing
- Initialize the ROS node
- Create subscribers, subscribe to the required topics
- Wait for the topic message in a loop, and enter the callback function after receiving the message
- Complete message processing in the callback function
Configure subscriber code compilation rules
The same as the configuration method in Note 5:
Use:
add_executable(pose_subscriber src/pose_subscriber.cpp)
target_link_libraries(pose_subscriber ${catkin_LIBRARIES})
- Set the code to be compiled and the executable file generated
- Set up the link library
Compile and run the subscriber
$ cd~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun learning_topic velocity_publisher
$ rosrun learning_topic pose_subscriber
You can observe the output result:
Subscriber code (Python)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将订阅/turtle1/pose话题,消息类型turtlesim::Pose
import rospy
from turtlesim.msg import Pose
def poseCallback(msg):
rospy.loginfo("Turtle pose: x:%0.6f, y:%0.6f", msg.x, msg.y)
def pose_subscriber():
# ROS节点初始化
rospy.init_node('pose_subscriber', anonymous=True)
# 创建一个Subscriber,订阅名为/turtle1/pose的topic,注册回调函数poseCallback
rospy.Subscriber("/turtle1/pose", Pose, poseCallback)
# 循环等待回调函数
rospy.spin()
if __name__ == '__main__':
pose_subscriber()
Part of the code in this article comes from Gu Yue "21 Lectures on ROS Introduction"
Previous link
ROS study notes 5 (programming implementation of Publisher Publisher)
ROS study notes 4 (create workspace and function package)
ROS study notes 3 (use of ROS command line tools)
ROS study notes 2 (ROS core concepts)
ROS study notes 1 (Basic operation of Linux system)