2025-05-11 15:44:54 +00:00
|
|
|
|
import time
|
2025-05-14 11:25:44 +00:00
|
|
|
|
import sys
|
|
|
|
|
import os
|
2025-05-15 23:17:29 +08:00
|
|
|
|
import math
|
2025-05-11 15:44:54 +00:00
|
|
|
|
|
2025-05-14 11:25:44 +00:00
|
|
|
|
# 添加父目录到路径,以便能够导入utils
|
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2025-05-17 19:21:32 +08:00
|
|
|
|
from base_move.move_base_hori_line import (
|
|
|
|
|
arc_turn_around_hori_line,
|
|
|
|
|
go_straight_by_hori_line,
|
|
|
|
|
move_to_hori_line
|
|
|
|
|
)
|
2025-05-17 11:27:39 +08:00
|
|
|
|
from base_move.go_straight import go_straight
|
2025-05-17 19:21:32 +08:00
|
|
|
|
from base_move.turn_degree import turn_degree
|
2025-05-17 12:34:02 +08:00
|
|
|
|
from utils.log_helper import LogHelper, get_logger, section, info, debug, warning, error, success, timing
|
|
|
|
|
|
|
|
|
|
# 创建本模块特定的日志记录器
|
|
|
|
|
logger = get_logger("任务1")
|
2025-05-12 08:06:08 +00:00
|
|
|
|
|
2025-05-18 04:29:05 +00:00
|
|
|
|
observe = True
|
2025-05-14 12:12:57 +00:00
|
|
|
|
|
2025-05-14 19:35:29 +08:00
|
|
|
|
def run_task_1(ctrl, msg):
|
2025-05-17 12:34:02 +08:00
|
|
|
|
section('任务1:寻找横线并绕行', "启动")
|
|
|
|
|
info('开始执行任务1...', "启动")
|
2025-05-12 08:06:08 +00:00
|
|
|
|
|
2025-05-16 07:59:36 +00:00
|
|
|
|
# v2
|
2025-05-17 12:34:02 +08:00
|
|
|
|
section('任务1-1:转弯并扫描QR码', "移动")
|
2025-05-16 07:59:36 +00:00
|
|
|
|
# 在 arc_turn_around_hori_line 中启用 QR 码扫描
|
2025-05-18 04:29:05 +00:00
|
|
|
|
turn_success, res = arc_turn_around_hori_line(
|
2025-05-16 11:35:53 +00:00
|
|
|
|
ctrl=ctrl,
|
|
|
|
|
msg=msg,
|
2025-05-18 16:26:20 +08:00
|
|
|
|
angle_deg=-85, # 负值表示右转
|
2025-05-16 11:35:53 +00:00
|
|
|
|
observe=observe,
|
|
|
|
|
scan_qrcode=True, # 启用 QR 码扫描
|
|
|
|
|
qr_check_interval=0.3 # 每 0.3 秒检查一次扫描结果
|
|
|
|
|
)
|
2025-05-15 23:17:29 +08:00
|
|
|
|
|
2025-05-17 19:21:32 +08:00
|
|
|
|
if res and res['qr_result']:
|
|
|
|
|
success(f"在任务1-1中成功扫描到QR码: {res['qr_result']}", "扫描")
|
2025-05-16 11:35:53 +00:00
|
|
|
|
else:
|
2025-05-17 12:34:02 +08:00
|
|
|
|
warning("任务1-1中未能扫描到任何QR码", "警告")
|
2025-05-12 08:06:08 +00:00
|
|
|
|
|
2025-05-17 12:34:02 +08:00
|
|
|
|
section('任务1-2:移动到横线', "移动")
|
2025-05-16 11:35:53 +00:00
|
|
|
|
# 执行常规的移动操作,不需要 QR 码扫描
|
2025-05-18 07:57:15 +00:00
|
|
|
|
move_to_hori_line(ctrl, msg, target_distance=0.8, observe=observe)
|
2025-05-12 08:06:08 +00:00
|
|
|
|
|
2025-05-17 12:34:02 +08:00
|
|
|
|
section('任务1-3:转弯', "旋转")
|
2025-05-18 07:57:15 +00:00
|
|
|
|
direction = False # if res['qr_result'] == 'A-2' else True
|
2025-05-18 04:29:05 +00:00
|
|
|
|
turn_success, res = arc_turn_around_hori_line(
|
2025-05-15 23:17:29 +08:00
|
|
|
|
ctrl=ctrl,
|
2025-05-15 23:42:09 +08:00
|
|
|
|
msg=msg,
|
2025-05-18 16:26:20 +08:00
|
|
|
|
angle_deg=-170, # 负值表示右转
|
2025-05-18 07:57:15 +00:00
|
|
|
|
target_distance=0.5, # TODO 优化这里的参数
|
2025-05-16 11:35:53 +00:00
|
|
|
|
pass_align=True,
|
2025-05-15 23:17:29 +08:00
|
|
|
|
observe=observe,
|
2025-05-16 11:35:53 +00:00
|
|
|
|
# TODO clear
|
|
|
|
|
bad_big_angle_corret=True
|
2025-05-15 23:17:29 +08:00
|
|
|
|
)
|
2025-05-15 23:42:09 +08:00
|
|
|
|
|
2025-05-18 07:57:15 +00:00
|
|
|
|
if turn_success:
|
|
|
|
|
success("任务1-3完成", "完成")
|
|
|
|
|
else:
|
|
|
|
|
warning("任务1-3失败", "警告")
|
|
|
|
|
return
|
|
|
|
|
|
2025-05-17 12:34:02 +08:00
|
|
|
|
section('任务1-4:直线移动', "移动")
|
2025-05-18 07:57:15 +00:00
|
|
|
|
move_distance = 0.3
|
2025-05-17 19:21:32 +08:00
|
|
|
|
if direction:
|
|
|
|
|
# TODO 这里的校准不一定好用,需要优化
|
|
|
|
|
go_straight_by_hori_line(ctrl, msg, distance=move_distance, speed=0.5, observe=observe)
|
|
|
|
|
else:
|
|
|
|
|
go_straight(ctrl, msg, distance=move_distance, speed=0.5, observe=observe)
|
2025-05-15 23:17:29 +08:00
|
|
|
|
|
2025-05-17 12:34:02 +08:00
|
|
|
|
section('任务1-5:模拟装货', "停止")
|
|
|
|
|
info('机器人躺下,模拟装货过程', "信息")
|
|
|
|
|
start_time = time.time()
|
2025-05-17 03:39:27 +00:00
|
|
|
|
ctrl.base_msg.lie_down(wait_time=3000) # TODO 比赛时改成 5s
|
2025-05-17 12:34:02 +08:00
|
|
|
|
elapsed = time.time() - start_time
|
|
|
|
|
timing("装货过程", elapsed)
|
2025-05-17 11:27:39 +08:00
|
|
|
|
|
|
|
|
|
# 站起来
|
2025-05-17 12:34:02 +08:00
|
|
|
|
info('机器人站起来,准备继续任务', "信息")
|
2025-05-17 11:27:39 +08:00
|
|
|
|
ctrl.base_msg.stand_up()
|
|
|
|
|
|
2025-05-17 12:34:02 +08:00
|
|
|
|
section('任务1-6:返回', "移动")
|
2025-05-18 07:57:15 +00:00
|
|
|
|
go_straight(ctrl, msg, distance=-(move_distance + res['radius']), speed=0.5, observe=observe)
|
2025-05-17 19:21:32 +08:00
|
|
|
|
|
|
|
|
|
# turn and back
|
|
|
|
|
turn_degree(ctrl, msg, degree=-90, absolute=True)
|
|
|
|
|
|
|
|
|
|
section('任务1-7:90度转弯', "旋转")
|
2025-05-18 04:29:05 +00:00
|
|
|
|
turn_success, res = arc_turn_around_hori_line(
|
2025-05-17 19:21:32 +08:00
|
|
|
|
ctrl=ctrl,
|
|
|
|
|
msg=msg,
|
2025-05-18 16:26:20 +08:00
|
|
|
|
angle_deg=85, # 正值表示左转
|
2025-05-17 19:21:32 +08:00
|
|
|
|
observe=observe
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
section('任务1-8:直线移动', "移动")
|
|
|
|
|
move_to_hori_line(ctrl, msg, target_distance=0.4, observe=observe)
|
|
|
|
|
|
|
|
|
|
section('任务1-9:90度旋转', "旋转")
|
|
|
|
|
turn_degree(ctrl, msg, degree=90, absolute=True)
|
|
|
|
|
|
|
|
|
|
section('任务1-10: y校准,准备 task-2', "移动")
|
|
|
|
|
# TODO
|
2025-05-17 12:34:02 +08:00
|
|
|
|
|
|
|
|
|
success("任务1完成", "完成")
|
2025-05-11 15:44:54 +00:00
|
|
|
|
|