|
from robomaster import robot |
|
from typing import Callable, Optional, Union |
|
from enum import Enum |
|
from dora import DoraStatus |
|
|
|
import pyarrow as pa |
|
|
|
|
|
CONN = "ap" |
|
|
|
|
|
class Command(Enum): |
|
NOD_YES = [ |
|
{"action": "gimbal", "value": [20.0, 0.0]}, |
|
{"action": "gimbal", "value": [0.0, 0.0]}, |
|
] |
|
NOD_NO = [ |
|
{"action": "gimbal", "value": [0.0, -20.0]}, |
|
{"action": "gimbal", "value": [0.0, 20.0]}, |
|
{"action": "gimbal", "value": [0.0, 0.0]}, |
|
] |
|
FORWARD = [ |
|
{ |
|
"action": "control", |
|
"value": [0.5, 0.0, 0.0, 0.6, 0], |
|
} |
|
] |
|
BACKWARD = [ |
|
{ |
|
"action": "control", |
|
"value": [-0.5, 0.0, 0.0, 0.6, 0], |
|
} |
|
] |
|
TURN_LEFT = [ |
|
{"action": "gimbal", "value": [0.0, -45.0]}, |
|
{ |
|
"action": "control", |
|
"value": [0.0, 0.0, 45.0, 0.0, 50], |
|
}, |
|
] |
|
TURN_RIGHT = [ |
|
{"action": "gimbal", "value": [0.0, 45.0]}, |
|
{ |
|
"value": [0.0, 0.0, -45.0, 0.0, 50], |
|
"action": "control", |
|
}, |
|
] |
|
UNKNOWN = [ |
|
{ |
|
"value": [0.0, 0.0, 0.0, 0.0, 0], |
|
"action": "control", |
|
} |
|
] |
|
|
|
|
|
|
|
@classmethod |
|
def parse(cls, value): |
|
for k, v in cls.__members__.items(): |
|
if k == value: |
|
return v |
|
return cls.UNKNOWN |
|
|
|
|
|
class Operator: |
|
def __init__(self): |
|
self.ep_robot = robot.Robot() |
|
print("Initializing robot...") |
|
assert self.ep_robot.initialize(conn_type=CONN), "Could not initialize ep_robot" |
|
assert self.ep_robot.camera.start_video_stream( |
|
display=False |
|
), "Could not start video stream" |
|
|
|
self.ep_robot.gimbal.recenter().wait_for_completed() |
|
self.backlog = [] |
|
self.event = None |
|
|
|
def on_event( |
|
self, |
|
dora_event: str, |
|
send_output: Callable[[str, Union[bytes, pa.UInt8Array], Optional[dict]], None], |
|
) -> DoraStatus: |
|
event_type = dora_event["type"] |
|
if event_type == "INPUT": |
|
if not ( |
|
self.event is not None |
|
and not (self.event._event.isSet() and self.event.is_completed) |
|
): |
|
if dora_event["id"] == "tick": |
|
if len(self.backlog) > 0: |
|
command = self.backlog.pop(0) |
|
print(command, flush=True) |
|
if command["action"] == "control": |
|
[x, y, z, xy_speed, z_speed] = command["value"] |
|
print(command, flush=True) |
|
self.event = self.ep_robot.chassis.move( |
|
x=x, y=y, z=z, xy_speed=xy_speed, z_speed=z_speed |
|
) |
|
elif command["action"] == "gimbal": |
|
[pitch, yaw] = command["value"] |
|
print(command, flush=True) |
|
self.event = self.ep_robot.gimbal.moveto( |
|
pitch=pitch, yaw=yaw, pitch_speed=0.0, yaw_speed=50.0 |
|
) |
|
elif dora_event["id"] == "control": |
|
raw_command = dora_event["value"][0].as_py() |
|
print(raw_command, flush=True) |
|
cmd = Command.parse(raw_command) |
|
self.backlog += cmd.value |
|
|
|
return DoraStatus.CONTINUE |
|
|