Skip to content

Commit 003cd62

Browse files
committed
py版本
1 parent 16752a9 commit 003cd62

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
########################################################################
5+
#### Copyright 2020 GuYueHome (www.guyuehome.com). ###
6+
########################################################################
7+
8+
# 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist
9+
10+
import rospy
11+
from geometry_msgs.msg import Twist
12+
13+
def velocity_publisher():
14+
# ROS节点初始化
15+
rospy.init_node('velocity_publisher', anonymous=True)
16+
17+
# 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
18+
turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
19+
20+
#设置循环的频率
21+
rate = rospy.Rate(10)
22+
23+
while not rospy.is_shutdown():
24+
# 初始化geometry_msgs::Twist类型的消息
25+
vel_msg = Twist()
26+
vel_msg.linear.x = 0.5
27+
vel_msg.angular.z = 0.2
28+
29+
# 发布消息
30+
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)
33+
34+
# 按照循环频率延时
35+
rate.sleep()
36+
37+
if __name__ == '__main__':
38+
try:
39+
velocity_publisher()
40+
except rospy.ROSInterruptException:
41+
pass

0 commit comments

Comments
 (0)