#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
import math
def draw_circle():
# 初始化ROS节点
rospy.init_node('draw_circle', anonymous=True)
# 创建一个发布器,向/turtle1/cmd_vel话题发送Twist消息
pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
rate = rospy.Rate(10) # 10Hz
# 创建Twist消息
vel_msg = Twist()
vel_msg.linear.x = 1.0 # 向前运动速度
vel_msg.angular.z = 1.0 # 角速度
# 发布消息
while not rospy.is_shutdown():
pub.publish(vel_msg)
rate.sleep()
def draw_square():
# 初始化ROS节点
rospy.init_node('draw_square', anonymous=True)
# 创建一个发布器,向/turtle2/cmd_vel话题发送Twist消息
pub = rospy.Publisher('/turtle2/cmd_vel', Twist, queue_size=10)
rate = rospy.Rate(1) # 1Hz
# 创建Twist消息
vel_msg = Twist()
while not rospy.is_shutdown():
# 向前运动
vel_msg.linear.x = 2.0
vel_msg.angular.z = 0.0
pub.publish(vel_msg)
rospy.sleep(2) # 直行2秒
# 停止前进,准备转弯
vel_msg.linear.x = 0.0
pub.publish(vel_msg)
rospy.sleep(1)
# 左转90度
vel_msg.angular.z = math.pi / 2 # 90度
pub.publish(vel_msg)
rospy.sleep(1) # 转弯1秒
# 停止转弯
vel_msg.angular.z = 0.0
pub.publish(vel_msg)
rospy.sleep(1)
if __name__ == '__main__':
try:
# 启动两个节点,一个控制画圆圈,一个控制画方块
draw_circle()
except rospy.ROSInterruptException:
pass
1ros中实现两只小乌龟一个画圆圈一个画方块的代码实现
最新推荐文章于 2024-08-28 20:09:03 发布