ROS话题发布与订阅
话题发布#!/usr/bin/env pythonimport rospyfrom geometry_msgs.msg importTwistdef velocity_publisher():# ROS节点初始化#anonymous=True,表示后面定义相同的node名字时候,按照序号进行排列rospy.init_node('velocity_publisher',anonymous=True)
·
话题发布
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
def velocity_publisher():
# ROS节点初始化
#anonymous=True,表示后面定义相同的node名字时候,按照序号进行排列
rospy.init_node('velocity_publisher',anonymous=True)
# 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,数据类型Twist
turtle_vel_pub=rospy.Publisher('/turtle1/cmd_vel',Twist,queue_size=10)
rate=rospy.Rate(10);
while not rospy.is_shutdown():
vel_msg=Twist()
vel_msg.linear.x=0.5
vel_msg.angular.z = 0.2
turtle_vel_pub.publish(vel_msg)
rospy.loginfo('%0.2f m/s, %0.2f rad/s',vel_msg.linear.x,vel_msg.angular.z)
rate.sleep()
if __name__=='__main__':
try:
velocity_publisher()
except rospy.ROSInterruptException:
pass
话题订阅
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 第一行很重要
import rospy
from turtlesim.msg import Pose
def lanelineInfoCallback(msg):
rospy.loginfo("Subcribe laneline Info: angle:%d delta_x:%d",
msg.x, msg.y)
def laneline_subscriber():
# ROS节点初始化
#anonymous=True,表示后面定义相同的node名字时候,按照序号进行排列
rospy.init_node('pose_subscriber', anonymous=True)
# 创建一个Subscriber,订阅名为/laneline_info的topic,注册回调函数lanelineInfoCallback
rospy.Subscriber("/turtle1/pose", Pose, lanelineInfoCallback)
# 循环等待回调函数
rospy.spin()
if __name__ == '__main__':
laneline_subscriber()
更多推荐



所有评论(0)