File size: 3,704 Bytes
12d535c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ca4d16
 
12d535c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4a3c67
12d535c
10e96ef
12d535c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ca4d16
 
 
 
10e96ef
 
6ca4d16
 
 
12d535c
 
 
6ca4d16
12d535c
 
 
6ca4d16
12d535c
 
6ca4d16
 
 
 
 
 
12d535c
 
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from robomaster import robot, blaster, led
from typing import Callable, Optional, Union

# from robot import RobotController
import pyarrow as pa

from dora import DoraStatus

# Global variables, change it to adapt your needs
FREQ = 20
CONN = "ap"


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.position = [0, 0, 0]
        self.gimbal_position = [0, 0]
        self.event = None
        self.brightness = 0
        self.rgb = [0, 0, 0]

    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 dora_event["id"] == "tick":
                send_output(
                    "position",
                    pa.array(self.position + self.gimbal_position),
                    dora_event["metadata"],
                )

            elif dora_event["id"] == "control":
                if not (
                    self.event is not None
                    and not (self.event._event.isSet() and self.event.is_completed)
                ):
                    [x, y, z] = dora_event["value"].to_numpy()
                    self.event = self.ep_robot.chassis.move(
                        x=x, y=y, z=z, xy_speed=0.8, z_speed=0.8
                    )
                    self.position[0] += x
                    self.position[1] += y
                    self.position[2] += z
                else:
                    print("control not completed", flush=True)
                    print("Set: ", self.event._event.isSet(), flush=True)
                    print("Completed:", self.event.is_completed, flush=True)

            elif dora_event["id"] == "gimbal_control":
                if not (
                    self.event is not None
                    and not (self.event._event.isSet() and self.event.is_completed)
                ):
                    [
                        gimbal_pitch,
                        gimbal_yaw,
                    ] = dora_event["value"].to_numpy()

                    if self.gimbal_position != [gimbal_pitch, gimbal_yaw]:
                        self.event = self.ep_robot.gimbal.moveto(
                            pitch=gimbal_pitch,
                            yaw=gimbal_yaw,
                            pitch_speed=20,
                            yaw_speed=20,
                        )
                        self.gimbal_position[0] = gimbal_pitch
                        self.gimbal_position[1] = gimbal_yaw

            elif dora_event["id"] == "blaster":
                [brightness] = dora_event["value"].to_numpy()
                if brightness != self.brightness:
                    self.ep_robot.blaster.set_led(
                        brightness=brightness, effect=blaster.LED_ON
                    )
                    self.brightness = brightness
            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