|
|
|
|
@ -62,45 +62,123 @@ import time
|
|
|
|
|
def main():
|
|
|
|
|
rclpy.init()
|
|
|
|
|
|
|
|
|
|
node = Node('circle_navigation')
|
|
|
|
|
|
|
|
|
|
node = Node('t_shape_movement')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
publisher = node.create_publisher(Twist, '/cmd_vel', 10)
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("=== 开始绕圈导航 ===")
|
|
|
|
|
node.get_logger().info("=== 开始T字形运动控制 ===")
|
|
|
|
|
node.get_logger().info("机器人将循环执行T字形路径")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
time.sleep(2.0)
|
|
|
|
|
|
|
|
|
|
cycle_count = 0
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
movements = [
|
|
|
|
|
{"name": "前进3秒", "linear": 0.2, "angular": 0.0, "duration": 3.0},
|
|
|
|
|
{"name": "左转2秒", "linear": 0.0, "angular": 0.5, "duration": 2.0},
|
|
|
|
|
{"name": "前进3秒", "linear": 0.2, "angular": 0.0, "duration": 3.0},
|
|
|
|
|
{"name": "左转2秒", "linear": 0.0, "angular": 0.5, "duration": 2.0},
|
|
|
|
|
{"name": "前进3秒", "linear": 0.2, "angular": 0.0, "duration": 3.0},
|
|
|
|
|
{"name": "左转2秒", "linear": 0.0, "angular": 0.5, "duration": 2.0},
|
|
|
|
|
{"name": "前进3秒回到起点", "linear": 0.2, "angular": 0.0, "duration": 3.0},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for i, move in enumerate(movements):
|
|
|
|
|
node.get_logger().info(f"动作 {i+1}/{len(movements)}: {move['name']}")
|
|
|
|
|
while True:
|
|
|
|
|
cycle_count += 1
|
|
|
|
|
|
|
|
|
|
# T字向上移动
|
|
|
|
|
node.get_logger().info("T字竖线: 向前移动")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
twist.linear.x = move['linear']
|
|
|
|
|
twist.angular.z = move['angular']
|
|
|
|
|
twist.linear.x = 0.15 # 前进速度
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(2.5) # 前进2.5秒
|
|
|
|
|
|
|
|
|
|
# 停止一下
|
|
|
|
|
node.get_logger().info("暂停")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
# T字左转并前进
|
|
|
|
|
node.get_logger().info("T字横线左半: 左转")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
twist.angular.z = 0.4 # 左转速度
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(1.2) # 左转1.2秒(约45度)
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("暂停")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(0.3)
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("T字横线左半: 向前移动")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
twist.linear.x = 0.15
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(2.0) # 前进2秒
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("暂停")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
# 回到T字中心点
|
|
|
|
|
node.get_logger().info("回到T字中心: 向后移动")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
twist.linear.x = -0.15 # 后退速度
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(2.0) # 后退2秒
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("暂停")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(0.3)
|
|
|
|
|
|
|
|
|
|
# T字右转并前进
|
|
|
|
|
node.get_logger().info("T字横线右半: 右转")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
twist.angular.z = -0.4 # 右转速度
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(2.4) # 右转2.4秒(约90度)
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("暂停")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(0.3)
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("T字横线右半: 向前移动")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
twist.linear.x = 0.15
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(2.0) # 前进2秒
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("暂停")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
# 回到起点准备下一次循环
|
|
|
|
|
node.get_logger().info("回到起点: 向后移动")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
twist.linear.x = -0.15
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(2.0) # 后退2秒
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("回到起点: 左转")
|
|
|
|
|
twist = Twist()
|
|
|
|
|
twist.angular.z = 0.4
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(1.2) # 左转1.2秒
|
|
|
|
|
|
|
|
|
|
node.get_logger().info(f"=== 第 {cycle_count} 次T字形完成 ===")
|
|
|
|
|
node.get_logger().info("等待3秒后开始下一次循环...")
|
|
|
|
|
|
|
|
|
|
# 停止等待
|
|
|
|
|
twist = Twist()
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
time.sleep(3.0)
|
|
|
|
|
|
|
|
|
|
time.sleep(move['duration'])
|
|
|
|
|
|
|
|
|
|
node.get_logger().info("=== 绕圈导航完成 ===")
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
stop_twist = Twist()
|
|
|
|
|
publisher.publish(stop_twist)
|
|
|
|
|
# 停止机器人
|
|
|
|
|
twist = Twist()
|
|
|
|
|
publisher.publish(twist)
|
|
|
|
|
node.get_logger().info("机器人已停止")
|
|
|
|
|
node.get_logger().info("很多没明白用AI写的呜呜呜")
|
|
|
|
|
|
|
|
|
|
# 关闭节点
|
|
|
|
|
node.destroy_node()
|
|
|
|
|
rclpy.shutdown()
|
|
|
|
|
|
|
|
|
|
|