练习1:ROS2命令
ros2 run turtlesim turtlesim_node
ros2 node list
ros2 topic list
ros2 topic pub /turtle1/cmd_vel geometry_msgs/Twist “{linear: {x: 1.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}”
ros2 topic echo /turtle1/pose 练习2:Python编程
import rclpy from rclpy.node import Node from geometry_msgs.msg import Twist import time
class SquareNode(Node): def init(self): super().init(‘square_node’) self.pub = self.create_publisher(Twist, ‘/turtle1/cmd_vel’, 10)
def move(self, speed, duration):
msg = Twist()
msg.linear.x = speed
start = self.get_clock().now()
while (self.get_clock().now() - start).nanoseconds < duration * 1e9:
self.pub.publish(msg)
time.sleep(0.01)
self.pub.publish(Twist())
def turn(self, speed, duration):
msg = Twist()
msg.angular.z = speed
start = self.get_clock().now()
while (self.get_clock().now() - start).nanoseconds < duration * 1e9:
self.pub.publish(msg)
time.sleep(0.01)
self.pub.publish(Twist())
def square(self):
for _ in range(4):
self.move(1.0, 2.0) # 2米
self.turn(1.0, 1.57) # 90度 练习3:运动学计算 题目: 机器人左轮速度0.5m/s,右轮速度1.5m/s,轮间距0.5m
求:
解答:
答案:
• 比例(P):根据当前误差调整输出,响应速度快
• 积分(I):累积历史误差,消除稳态误差
• 微分(D):根据误差变化率调整,抑制振荡
