File size: 5,803 Bytes
034b730
 
 
 
 
fe79d42
034b730
 
 
 
 
 
 
 
 
ffc2aa4
034b730
 
ffc2aa4
 
034b730
 
 
 
ffc2aa4
034b730
 
 
ffc2aa4
034b730
 
ffc2aa4
357c750
034b730
357c750
ffc2aa4
034b730
 
ffc2aa4
034b730
 
357c750
ffc2aa4
034b730
 
ffc2aa4
034b730
 
357c750
ffc2aa4
034b730
ffc2aa4
034b730
357c750
034b730
357c750
ffc2aa4
357c750
ffc2aa4
357c750
 
 
 
034b730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357c750
 
 
034b730
357c750
fe79d42
 
357c750
 
 
 
 
 
 
 
 
 
fe79d42
 
357c750
 
 
 
 
 
034b730
 
 
 
357c750
034b730
 
 
357c750
fe79d42
357c750
 
 
 
034b730
357c750
 
 
fe79d42
 
 
 
 
357c750
 
 
 
ffc2aa4
 
 
 
357c750
 
 
 
 
 
ffc2aa4
357c750
 
 
 
034b730
357c750
ffc2aa4
 
 
 
 
 
 
 
 
 
 
 
fe79d42
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from robomaster import robot
from typing import Callable, Optional, Union
from enum import Enum
from dora import DoraStatus

import numpy as np
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, -55.0]},
        {"action": "gimbal", "value": [0.0, 0.0]},
    ]
    FORWARD = [
        {
            "action": "control",
            "value": [2.0, 0.0, 0.0, 0.8, 0],
        }
    ]
    BACKWARD = [
        {"action": "gimbal", "value": [0, -180.0]},
        {
            "action": "control",
            "value": [-2.0, 0, 180.0, 0.8, 50],
        },
    ]
    LEFT = [
        {"action": "gimbal", "value": [0, -90.0]},
        {
            "action": "control",
            "value": [0.0, -1.0, 90.0, 0.6, 50],
        },
    ]
    SLIGHT_LEFT = [
        {"action": "gimbal", "value": [0.0, -30.0]},
        {
            "action": "control",
            "value": [1.0, -0.5, 30.0, 0.6, 50],
        },
    ]
    RIGHT = [
        {"action": "gimbal", "value": [0.0, 90.0]},
        {
            "value": [0.0, 1.0, -90.0, 0.6, 50],
            "action": "control",
        },
    ]
    SLIGHT_RIGHT = [
        {"action": "gimbal", "value": [0.0, 30.0]},
        {
            "value": [1.0, 0.5, -30.0, 0.6, 50],
            "action": "control",
        },
    ]
    UNKNOWN = []
    # STOP = [0, 0, 0, 0]
    # COMPLETED = [0, 0, 0, 0]

    @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.event = self.ep_robot.gimbal.moveto(
            pitch=-5, yaw=0, pitch_speed=50.0, yaw_speed=50.0
        )
        self.backlog = []
        self.last_control = ""
        self.position = np.array([0, 0, 0])
        self.count = -1

    def execute_backlog(self):
        if len(self.backlog) > 0:
            command = self.backlog.pop(0)
            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
                )
                self.position = np.array([x, y, z])

            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=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.last_control}", flush=True)
                        send_output("position", pa.array(self.position))
                        send_output("control_reply", pa.array([self.count]))
                elif self.event is None:
                    send_output("position", pa.array(self.position))

            elif dora_event["id"] == "control":
                raw_command = dora_event["value"][0].as_py()
                print(raw_command, flush=True)
                self.last_control = raw_command
                if "slight right" in raw_command:
                    cmd = Command.BACKWARD
                elif "slight left" in raw_command:
                    cmd = Command.BACKWARD
                elif "right" in raw_command:
                    cmd = Command.RIGHT
                elif "left" in raw_command:
                    cmd = Command.LEFT
                elif "forward" in raw_command:
                    cmd = Command.FORWARD
                elif "backward" in raw_command:
                    cmd = Command.BACKWARD
                else:
                    cmd = Command.UNKNOWN
                if len(self.backlog) == 0:
                    self.backlog += cmd.value
                    self.execute_backlog()
            elif dora_event["id"] == "assistant_message":
                raw_command = dora_event["value"][0].as_py()
                print(raw_command, flush=True)
                self.last_control = raw_command
                if "No, " in raw_command:
                    cmd = Command.NOD_NO
                elif "Yes, " in raw_command:
                    cmd = Command.NOD_YES
                else:
                    cmd = Command.UNKNOWN
                    self.backlog += cmd.value
                    self.execute_backlog()
            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()

        return DoraStatus.CONTINUE