Skip to content

Commit 8c5025a

Browse files
committed
订阅者
1 parent 89c88ca commit 8c5025a

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

catkin_wp/src/learning_topic/script/velocity_publisher.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ def velocity_publisher():
2828

2929
# 发布消息
3030
turtle_vel_pub.publish(vel_msg)
31-
rospy.loginfo("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]",
32-
vel_msg.linear.x, vel_msg.angular.z)
31+
rospy.loginfo("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", vel_msg.linear.x, vel_msg.angular.z)
3332

3433
# 按照循环频率延时
3534
rate.sleep()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/***********************************************************************
2+
Copyright 2020 GuYueHome (www.guyuehome.com).
3+
***********************************************************************/
4+
5+
/**
6+
* 该例程将订阅/turtle1/pose话题,消息类型turtlesim::Pose
7+
*/
8+
9+
#include <ros/ros.h>
10+
#include "turtlesim/Pose.h"
11+
12+
// 接收到订阅的消息后,会进入消息回调函数
13+
void poseCallback(const turtlesim::Pose::ConstPtr& msg)
14+
{
15+
// 将接收到的消息打印出来
16+
ROS_INFO("Turtle pose: x:%0.6f, y:%0.6f", msg->x, msg->y);
17+
}
18+
19+
int main(int argc, char **argv)
20+
{
21+
// 初始化ROS节点
22+
ros::init(argc, argv, "pose_subscriber");
23+
24+
// 创建节点句柄
25+
ros::NodeHandle n;
26+
27+
// 创建一个Subscriber,订阅名为/turtle1/pose的topic,注册回调函数poseCallback
28+
ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, poseCallback);
29+
30+
// 循环等待回调函数
31+
ros::spin();
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)