42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
|
|
from typing import List
|
|||
|
|
from agent.device import controller
|
|||
|
|
from agent.tools.base_tool import BaseTool, ToolResult
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
class Tool(BaseTool):
|
|||
|
|
name = "uav_control"
|
|||
|
|
description = (
|
|||
|
|
"该工具用于对无人系统进行控制并在飞行控制中完成一系列的活动(Action),例如起飞、移动、悬停、照相等"
|
|||
|
|
)
|
|||
|
|
parameters = {
|
|||
|
|
"type": "object",
|
|||
|
|
"properties": {
|
|||
|
|
"command": {
|
|||
|
|
"type": "string",
|
|||
|
|
"description": "无人系统飞行控制命令,例如arm、disarm、takeoff、rtl、goto等",
|
|||
|
|
},
|
|||
|
|
"params": {
|
|||
|
|
"type": "object",
|
|||
|
|
"description": "控制命令参数"
|
|||
|
|
},
|
|||
|
|
"actions": {
|
|||
|
|
"type": "array",
|
|||
|
|
"description": "过程中执行的一些列的动作,例如pause、capture_phone、standby等"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"required": ["command"],
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def execute(self, command: str, params: dict={}, actions: List[object]=()) -> ToolResult:
|
|||
|
|
if not controller.connect():
|
|||
|
|
return ToolResult(success=False, output="连接无人机失败")
|
|||
|
|
try:
|
|||
|
|
command_handler = getattr(controller, command)
|
|||
|
|
try:
|
|||
|
|
return ToolResult(success=True, output=str(command_handler(**params)))
|
|||
|
|
except Exception as e:
|
|||
|
|
return ToolResult(success=False, output=str(e))
|
|||
|
|
finally:
|
|||
|
|
controller.disconnect()
|