|
from robomaster import robot, led |
|
from typing import Callable, Optional |
|
from dora import DoraStatus |
|
|
|
import numpy as np |
|
import pyarrow as pa |
|
|
|
|
|
CONN = "ap" |
|
|
|
print("Initialization...", flush=True) |
|
|
|
|
|
class Operator: |
|
def __init__(self): |
|
self.ep_robot = robot.Robot() |
|
print("Initializing robot...", flush=True) |
|
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.last_control = "" |
|
self.position = np.array([0.0, 0.0, 0.0]) |
|
self.count = -1 |
|
self.event = None |
|
self.rgb = [0, 0, 0] |
|
|
|
def execute_backlog(self): |
|
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"] |
|
self.position += np.array([x, y, z]) |
|
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"] |
|
self.event = self.ep_robot.gimbal.moveto( |
|
pitch=pitch, yaw=yaw, pitch_speed=50.0, yaw_speed=50.0 |
|
) |
|
|
|
def on_event( |
|
self, |
|
dora_event: str, |
|
send_output: Callable[[str, pa.Array, Optional[dict]], None], |
|
) -> DoraStatus: |
|
event_type = dora_event["type"] |
|
if event_type == "INPUT": |
|
if dora_event["id"] == "tick": |
|
|
|
if not ( |
|
self.event is not None |
|
and not (self.event._event.isSet() and self.event.is_completed) |
|
): |
|
if len(self.backlog) > 0: |
|
self.execute_backlog() |
|
else: |
|
print(f"sending control reply: {self.count}", flush=True) |
|
send_output("position", pa.array(self.position)) |
|
send_output("control_reply", pa.array([self.count])) |
|
return DoraStatus.CONTINUE |
|
|
|
print("sending position", flush=True) |
|
send_output("position", pa.array(self.position)) |
|
|
|
elif dora_event["id"] == "planning_control": |
|
command = dora_event["value"].to_pylist() |
|
self.count = command[0]["count"] |
|
if len(self.backlog) == 0: |
|
self.backlog += command |
|
self.execute_backlog() |
|
elif dora_event["id"] == "led": |
|
[r, g, b] = dora_event["value"].to_numpy() |
|
rgb = [r, g, b] |
|
if rgb != self.rgb: |
|
self.ep_robot.led.set_led( |
|
comp=led.COMP_ALL, r=r, g=g, b=b, effect=led.EFFECT_ON |
|
) |
|
self.rgb = rgb |
|
return DoraStatus.CONTINUE |
|
|