教程
本页面包含各种使用场景的详细教程。
高级导航
自定义导航参数
# 创建导航参数
travel_params = MsgTravelParams()
travel_params.speed_mode = 2 # LOW 低速
travel_params.distance_tolerance = 0.2 # 20cm 精度
travel_params.gait = 3 # CRAWL 爬行步态
travel_params.disable_body_obstacle_avoidance = False # 启用避障
travel_params.ignore_final_yaw = False # 匹配目标朝向
travel_params.path_following_mode = 1 # 默认循线
travel_params.direction_constraint = 1 # 无方向限制
# 使用自定义参数导航
response = navigation_to_location(
location='narrow_passage',
travel_params=travel_params
)
多点路径导航
def create_waypoint(x, y, yaw=0.0):
\"\"\"创建路径点\"\"\"
import math
pose_stamped = PoseStamped()
pose_stamped.header.frame_id = "map"
pose_stamped.pose.position = Point(x=x, y=y, z=0.0)
# 从 yaw 角度转换为四元数
qz = math.sin(yaw / 2.0)
qw = math.cos(yaw / 2.0)
pose_stamped.pose.orientation = Quaternion(
x=0.0, y=0.0, z=qz, w=qw
)
return pose_stamped
# 创建路径点序列
waypoints = [
create_waypoint(1.0, 0.0, 0.0),
create_waypoint(2.0, 1.0, 1.57), # 90度转弯
create_waypoint(2.0, 2.0, 1.57),
create_waypoint(3.0, 2.0, 0.0),
]
# 严格追踪模式 - 经过所有点
response = navigation_via_poses(
exec_waypoints=waypoints,
exec_type=2 # STRICTTRACK
)
暂停和恢复导航
import time
# 开始导航(非阻塞)
response = navigation_to_location('point_A', block=False)
# 等待一段时间
time.sleep(5)
# 暂停导航
print("暂停导航...")
pause_navigation()
# 执行其他任务
time.sleep(3)
# 恢复导航
print("恢复导航...")
resume_navigation()
地图管理
切换地图
# 查看可用地图
response = get_available_maps()
if response.state.code == 0:
print("可用地图:", response.response.map_names)
# 加载地图
response = load_map(
map_name='floor_1',
auto_reload=True, # 永久加载
timeout=30
)
if response.state.code == 0:
print("地图加载成功")
建图流程
# 1. 开始建图
print("开始建图模式...")
response = start_mapping(timeout=120)
if response.state.code != 0:
print("启动建图失败")
exit(1)
print("建图模式已启动,请移动机器人...")
# 2. 手动控制机器人移动建图
# (使用遥控器或其他方式)
input("建图完成后按 Enter 继续...")
# 3. 停止建图并保存
print("保存地图...")
response = stop_mapping(
map_name='new_office_map',
auto_reload=True
)
if response.state.code == 0:
print("地图已保存")
云台控制
云台扫描
import time
# 扫描拍照
scan_positions = [
(0, 0), # 中心
(-45, 30), # 左上
(0, 30), # 正上
(45, 30), # 右上
(45, 0), # 右中
(45, -30), # 右下
]
for i, (pan, tilt) in enumerate(scan_positions):
print(f"移动云台到 pan={pan}, tilt={tilt}")
# 设置云台角度
set_ptzf(pan=pan, tilt=tilt, relative=False)
time.sleep(1) # 等待云台稳定
# 拍照
capture_image(f'scan_{i}', type='rgb')
print(f" 已拍照: scan_{i}")
定时巡检任务
完整的巡检示例
import time
from datetime import datetime
class PatrolTask:
def __init__(self, points):
self.points = points
self.api = get_api_instance(task='patrol')
def wait_ready(self):
\"\"\"等待系统就绪\"\"\"
print("等待系统就绪...")
state = daystar_ready(timeout=30)
if state.code != StateCode.success:
raise RuntimeError("系统未就绪")
print("✓ 系统就绪")
def inspect_at_point(self, point_name):
\"\"\"在指定点进行巡检\"\"\"
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# 多角度拍照
angles = [0, 45, 90, -45, -90]
for angle in angles:
set_ptzf(pan=angle, tilt=0, relative=False)
time.sleep(0.5)
image_name = f"{point_name}_{angle}_{timestamp}"
capture_image(image_name, type='rgb')
print(f" ✓ 拍照: {image_name}")
def run_patrol(self):
\"\"\"执行巡检\"\"\"
self.wait_ready()
for i, point in enumerate(self.points, 1):
print(f"\n[{i}/{len(self.points)}] 前往 {point}...")
response = navigation_to_location(point)
if response.state.code == StateCode.success:
print(f"✓ 到达 {point}")
self.inspect_at_point(point)
else:
print(f"✗ 无法到达 {point}: {response.state.describe}")
print("\n✓ 巡检完成")
patrol_points = ['checkpoint_1', 'checkpoint_2', 'checkpoint_3']
task = PatrolTask(patrol_points)
task.run_patrol()
错误处理
重试机制
import time
def navigate_with_retry(location, max_retries=3):
\"\"\"带重试的导航\"\"\"
for attempt in range(max_retries):
print(f"尝试 {attempt + 1}/{max_retries}...")
response = navigation_to_location(location)
if response.state.code == StateCode.success:
print("✓ 导航成功")
return True
if response.state.code == StateCode.timeout:
print("超时,重试...")
time.sleep(2)
continue
else:
print(f"失败: {response.state.describe}")
return False
print("✗ 达到最大重试次数")
return False
# 使用
navigate_with_retry('difficult_point', max_retries=3)