Skip to content

Commit ab87cc6

Browse files
small cleanup
1 parent 99c6297 commit ab87cc6

File tree

1 file changed

+87
-47
lines changed

1 file changed

+87
-47
lines changed

doggo.py

Lines changed: 87 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
"drill_seargent": "DGzg6RaUqxGRTHSBjfgF",
2929
"knox": "dPah2VEoifKnZT37774q",
3030
"pirate": "PPzYpIqttlTYA83688JI",
31+
"michael": "ldTgmMTsxAK2Vs3NZO03"
3132
}
3233

34+
from unitree_webrtc_connect.constants import RTC_TOPIC, SPORT_CMD
3335

3436
class Tool:
3537
def __init__(self, name, description, filepath, callback, awake=True):
@@ -46,12 +48,93 @@ def __init__(self, name, description, filepath, callback, awake=True):
4648
async def run(self, params):
4749
return await self.callback(params)
4850

51+
class Trick:
52+
def __init__(self, dog, name, description, file):
53+
self.dog = dog
54+
55+
async def act(self, params):
56+
pass
57+
58+
def tool(self):
59+
return Tool(
60+
name=self.name,
61+
description=self.description,
62+
parameters=self.parameters,
63+
callback=self.act,
64+
awake=True,
65+
)
66+
67+
68+
class StandUp(Trick):
69+
def __init__(self, dog):
70+
super().__init__(dog, "stand_up", "Make the dog stand up", "tools/stand_up.json")
71+
72+
async def act(self, params):
73+
await self.dog.maybe_reconnect()
74+
await self.dog.robot.datachannel.pub_sub.publish_request_new(
75+
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["StandUp"]}
76+
)
77+
return "Doggo is now standing up"
78+
79+
class Damp(Trick):
80+
def __init__(self, dog):
81+
super().__init__(dog, "lie_down", "Make the dog lie down", "tools/lie_down.json")
82+
83+
async def act(self, params):
84+
await self.dog.maybe_reconnect()
85+
await self.dog.robot.datachannel.pub_sub.publish_request_new(
86+
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Damp"]}
87+
)
88+
return "Doggo is now damping"
89+
90+
class Hello(Trick):
91+
def __init__(self, dog):
92+
super().__init__(dog, "hello", "Make the dog say hello", "tools/hello.json")
93+
94+
async def act(self, params):
95+
await self.dog.maybe_reconnect()
96+
await self.dog.robot.datachannel.pub_sub.publish_request_new(
97+
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Hello"]}
98+
)
99+
return "Doggo is now saying hello"
100+
101+
class Move(Trick):
102+
def __init__(self, dog):
103+
super().__init__(dog, "move", "Make the dog move", "tools/move.json")
104+
105+
async def act(self, params):
106+
await self.dog.maybe_reconnect()
107+
await self.dog.robot.datachannel.pub_sub.publish_request_new(
108+
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Move"], "parameter": params}
109+
)
110+
return "Doggo is now moving"
111+
112+
class Stop(Trick):
113+
def __init__(self, dog):
114+
super().__init__(dog, "stop", "Make the dog stop", "tools/stop.json")
115+
116+
async def act(self, params):
117+
await self.dog.maybe_reconnect()
118+
await self.dog.robot.datachannel.pub_sub.publish_request_new(
119+
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Stop"]}
120+
)
121+
return "Doggo is now stopping"
122+
123+
def trick_tools(dog):
124+
tricks = [
125+
StandUp(dog),
126+
Damp(dog),
127+
Hello(dog),
128+
Move(dog),
129+
Stop(dog),
130+
]
131+
return [trick.tool() for trick in tricks]
49132

50133
class Doggo:
51134
awake = True
52135
alive = True
53136

54-
def __init__(self, voice="burt", alive=True, output_sample_rate=48000, echo=False):
137+
def __init__(self, voice="michael", alive=True, output_sample_rate=48000, echo=False):
55138
self.voice_id = VOICES[voice]
56139
self.alive = alive
57140
self.robot = None
@@ -78,17 +161,9 @@ def __init__(self, voice="burt", alive=True, output_sample_rate=48000, echo=Fals
78161
"tools/sleep.json",
79162
lambda _: self.toggle_sleep(True),
80163
awake=False,
81-
),
82-
Tool(
83-
"move",
84-
"Move the doggo by an x, y, and z coordinate",
85-
"tools/move.json",
86-
self.move,
87-
),
88-
Tool("stand_up", "Stand up the doggo", "tools/empty.json", self.stand_up),
89-
Tool("lie_down", "Have the doggo lie down", "tools/empty.json", self.damp),
90-
Tool("hello", "The doggo says hello", "tools/empty.json", self.hello),
164+
)
91165
]
166+
self.tools.extend(trick_tools(self))
92167

93168
self.asleep_prompt, self.awake_prompt = None, None
94169

@@ -102,41 +177,6 @@ async def connect_robot(self):
102177
if self.robot:
103178
await self.robot.connect()
104179

105-
async def stand_up(self, _):
106-
await self.maybe_reconnect()
107-
await self.robot.datachannel.pub_sub.publish_request_new(
108-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["StandUp"]}
109-
)
110-
return "Doggo is now standing up"
111-
112-
async def damp(self, _):
113-
await self.maybe_reconnect()
114-
await self.robot.datachannel.pub_sub.publish_request_new(
115-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Damp"]}
116-
)
117-
return "Doggo is now damping"
118-
119-
async def hello(self, _):
120-
await self.maybe_reconnect()
121-
await self.robot.datachannel.pub_sub.publish_request_new(
122-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Hello"]}
123-
)
124-
return "Doggo is now saying hello"
125-
126-
async def move(self, params):
127-
await self.maybe_reconnect()
128-
await self.robot.datachannel.pub_sub.publish_request_new(
129-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Move"], "parameter": params}
130-
)
131-
return "Doggo is now moving"
132-
133-
async def stop(self, _):
134-
await self.maybe_reconnect()
135-
await self.robot.datachannel.pub_sub.publish_request_new(
136-
RTC_TOPIC["SPORT_MOD"], {"api_id": SPORT_CMD["Stop"]}
137-
)
138-
return "Doggo is now stopping"
139-
140180
def toggle_sleep(self, sleep):
141181
self.awake = sleep
142182
if sleep:
@@ -291,7 +331,7 @@ async def loop(dog):
291331

292332

293333
@click.command()
294-
@click.option("--voice", type=click.Choice(VOICES.keys()), default="burt")
334+
@click.option("--voice", type=click.Choice(VOICES.keys()), default="michael")
295335
@click.option("--alive/--dead", is_flag=True, default=True)
296336
@click.option("--configure-input/--no-configure-input", is_flag=True, default=False)
297337
@click.option("--sample-rate", type=int, default=44100)

0 commit comments

Comments
 (0)