File size: 3,131 Bytes
8852f54
c641eb7
034b730
 
fe79d42
034b730
 
 
 
 
c641eb7
034b730
 
 
 
 
c641eb7
034b730
 
 
 
 
 
 
357c750
c641eb7
fe79d42
c641eb7
8852f54
357c750
 
 
 
c641eb7
357c750
 
c641eb7
357c750
 
 
fe79d42
357c750
 
 
 
 
034b730
 
 
 
357c750
034b730
 
 
357c750
fe79d42
357c750
 
 
 
034b730
357c750
 
c641eb7
fe79d42
 
c641eb7
 
 
 
fe79d42
 
 
 
 
 
 
8852f54
 
 
 
 
 
 
 
034b730
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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