Rospy的官方教程代码讲解(二)消息头和用户数据

Rospy的官方教程代码讲解(二)消息头和用户数据刚刚发现的重大事实是ROS wiki里的rospy教程和github里rospy_tutorials代码是完全不同的 Σ(っ°Д°;)っ男默女泪啊 щ(゚Д゚щ) 我整个人都方了有木有。。。 先讲rospy_tutorials的,因为这个好像没有相关教程,只有代码(´•ω•`)Rospy的官方教程代码讲解二消息头和用户数据消息头
摘要由CSDN通过智能技术生成

Rospy的官方教程代码讲解(二)消息头和用户数据

刚刚发现的重大事实是ROS wiki里的rospy教程和github里rospy_tutorials代码是完全不同的
Σ(っ°Д°;)っ男默女泪啊 щ(゚Д゚щ) 我整个人都方了有木有。。。
先讲rospy_tutorials的,因为这个好像没有相关教程,只有代码(´•ω•`)

消息头

直接上代码,带消息头的发布者:

#!/usr/bin/env python
# Software License Agreement (BSD License)

import sys

import rospy
from rospy_tutorials.msg import HeaderString

NAME = 'talker_header'

def talker_header():
    #对比之前发布的String,这里的HeaderString类型就是String前面加了一个Header信息
    pub = rospy.Publisher("chatter_header", HeaderString, queue_size=10)
    rospy.init_node(NAME) #blocks until registered with master
    count = 0
    while not rospy.is_shutdown():
        str = 'hello world %s'%count
        print str
        # 如果Header信息中填None,ROS会自动填入消息头信息
        pub.publish(HeaderString(None, str))
        count += 1
        rospy.s
  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用rospy编写小海龟走方形的publisher和subscriber的示例代码: ```python #!/usr/bin/env python import rospy from geometry_msgs.msg import Twist from turtlesim.msg import Pose def pose_callback(pose): global x, y, theta x = pose.x y = pose.y theta = pose.theta def move(): rospy.init_node('turtle_square', anonymous=True) velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) pose_subscriber = rospy.Subscriber('/turtle1/pose', Pose, pose_callback) rate = rospy.Rate(10) # Move forward vel_msg = Twist() vel_msg.linear.x = 1 vel_msg.linear.y = vel_msg.linear.z = vel_msg.angular.x = vel_msg.angular.y = vel_msg.angular.z = t = rospy.Time.now().to_sec() while not rospy.is_shutdown(): velocity_publisher.publish(vel_msg) t1 = rospy.Time.now().to_sec() if t1 - t > 2: break rate.sleep() # Stop and turn vel_msg.linear.x = vel_msg.angular.z = 1 t = rospy.Time.now().to_sec() while not rospy.is_shutdown(): velocity_publisher.publish(vel_msg) t1 = rospy.Time.now().to_sec() if t1 - t > 1.5: break rate.sleep() # Move forward again vel_msg.linear.x = 1 vel_msg.angular.z = t = rospy.Time.now().to_sec() while not rospy.is_shutdown(): velocity_publisher.publish(vel_msg) t1 = rospy.Time.now().to_sec() if t1 - t > 2: break rate.sleep() # Stop and turn again vel_msg.linear.x = vel_msg.angular.z = 1 t = rospy.Time.now().to_sec() while not rospy.is_shutdown(): velocity_publisher.publish(vel_msg) t1 = rospy.Time.now().to_sec() if t1 - t > 1.5: break rate.sleep() # Move forward one last time vel_msg.linear.x = 1 vel_msg.angular.z = t = rospy.Time.now().to_sec() while not rospy.is_shutdown(): velocity_publisher.publish(vel_msg) t1 = rospy.Time.now().to_sec() if t1 - t > 2: break rate.sleep() # Stop the turtle vel_msg.linear.x = vel_msg.angular.z = velocity_publisher.publish(vel_msg) if __name__ == '__main__': try: move() except rospy.ROSInterruptException: pass ``` 这段代码使用了geometry_msgs/Twist和turtlesim/Pose消息类型,其中Twist消息类型用于控制小海龟的运动,Pose消息类型用于获取小海龟的位置信息。在move函数中,我们首先初始化了节点,然后创建了一个publisher和一个subscriber,分别用于控制小海龟的运动和获取小海龟的位置信息。接着,我们使用Twist消息类型控制小海龟走方形,具体实现方式是先向前走一段时间,然后停下来并向左转,再向前走一段时间,再停下来并向左转,最后再向前走一段时间。在每个阶段结束时,我们都会检查时间是否已经超过了规定的时间,如果超过了就停止运动。最后,我们停止小海龟的运动并退出程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值