记录 发个牢骚:这学期学校教ros1,很生草的是我装ros1失败太多次了就扭头装ros2去了,装完才知道这俩不是一个东西。结果老师让交‘用代码发话题给turtlesim让小海龟跑一个多边形’。我兴冲冲地听了课之后在网上找rclpy的资料,结果除了源码毛都没有,我大意了奥,没有闪。
事先说明,本人不是计算机院的,python基础也没整好,代码要多烂有多烂,看不下去了别喷我。。。
from geometry_msgs import msg
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist
import math
class Talker(Node):
def __init__(self):
super().__init__('talker')
self.pub = self.create_publisher(Twist, '/turtle1/cmd_vel', 10)
timer_period = 1.0
self.tmr = self.create_timer(timer_period, self.timer_callback)
def timer_callback(self):
global i
self.a = math.pi
self.a = self.a/2.5
msg = Twist()
if i%2 == 0:
msg.linear.x = 1.0
msg.angular.z = 0.0
i += 1
elif i%2 == 1:
msg.linear.x = 0.0
msg.angular.z = self.a
i += 1
self.pub.publish(msg)
def main(args=None):
global i
i = 0
rclpy.init(args=args)
node = Talker()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
|