快速开始
本指南将帮助你快速上手 Daystar API。
基本概念
状态码
所有 API 调用都返回包含状态码的响应对象:
response = some_function()
if response.state.code == StateCode.success:
print("成功")
elif response.state.code == StateCode.timeout:
print("超时")
else:
print(f"失败: {response.state.describe}")
阻塞与非阻塞模式
一般的函数都支持两种模式:
阻塞模式 (
block=True): 等待操作完成后返回非阻塞模式 (
block=False): 立即返回,操作在后台执行
# 阻塞模式 - 等待导航完成
response = navigation_to_location('point_A', block=True)
# 非阻塞模式 - 立即返回
response = navigation_to_location('point_B', block=False)
# 继续执行其他任务...
第一个程序
简单导航示例
# 导航到预设点
print("开始导航到 point_A...")
response = navigation_to_location(
location='point_A',
timeout=60
)
if response.state.code == StateCode.success:
print("导航成功!")
else:
print(f"导航失败: {response.state.describe}")
带回调的导航
def on_complete(event: NavCompleteEvent):
print(f"✓ 到达目标: {event.target}")
print(f" 耗时: {event.navigation_time} 秒")
def on_failed(event: NavFailedEvent):
print(f"✗ 导航失败: {event.error_msg}")
def on_progress(event: NavProgressEvent):
print(f" 剩余距离: {event.distance_remaining:.2f} 米")
# 带回调的导航
response = navigation_to_location(
location='point_A',
complete_callback=on_complete,
failed_callback=on_failed,
progress_callback=on_progress
)
常见模式
巡检任务
patrol_points = ['point_A', 'point_B', 'point_C', 'point_D']
for point in patrol_points:
print(f"导航到 {point}...")
response = navigation_to_location(point)
if response.state.code == StateCode.success:
print(f"✓ 到达 {point}")
# 在此处执行巡检任务
# capture_image(f"inspection_{point}")
else:
print(f"✗ 无法到达 {point}")
break
虚拟打点
# 创建虚拟位置
waypoints = [
('checkpoint_1', 1.0, 0.0),
('checkpoint_2', 2.0, 1.0),
('checkpoint_3', 3.0, 2.0),
]
for name, x, y in waypoints:
pose = Pose()
pose.position = Point(x=x, y=y, z=0.0)
pose.orientation = Quaternion(x=0.0, y=0.0, z=0.0, w=1.0)
response = add_location(
location=name,
use_virtual_pose=True,
virtual_pose=pose
)
if response.response.success:
print(f"✓ 创建虚拟点: {name}")