-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy_client.py
263 lines (220 loc) · 7.75 KB
/
dummy_client.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import animus_client as animus
import animus_utils as utils
import sys
import logging
import numpy as np
import random
import cv2
import time
import logging
import pygame
import os
import datetime
import os
from rollbody_utils import make_motor_cmd, send_speed_cmd, send_head_cmd
robot_name = "rollbody"
log = utils.create_logger("DebugClient", logging.INFO)
log.info(animus.version)
audio_params = utils.AudioParams(
Backends=[""],
SampleRate=16000,
Channels=1,
SizeInFrames=True,
TransmitRate=30
)
setup_result = animus.setup(audio_params, "Python3AnimusBasics", loglatency=True)
if not setup_result.success:
sys.exit(-1)
# This will throw an error is system_login=True
email = os.environ["CYBERSELVES_EMAIL"]
pwd = os.environ["CYBERSELVES_PWD"]
log.info(f"CYBERSELVES email and pwd: {email}, {pwd}")
login_result = animus.login_user(email, pwd, system_login=False)
if login_result.success:
log.info("Logged in")
else:
sys.exit(-1)
get_robots_result = animus.get_robots(True, True, get_system=False)
if not get_robots_result.localSearchError.success:
log.error(get_robots_result.localSearchError.description)
if not get_robots_result.remoteSearchError.success:
log.error(get_robots_result.remoteSearchError.description)
if len(get_robots_result.robots) == 0:
log.info("No Robots found")
animus.close_client_interface()
sys.exit(-1)
for robot_details in get_robots_result.robots:
if robot_details.name == robot_name:
break
else:
sys.exit(-1)
robot = animus.Robot(robot_details)
# Init all the communication channels
connected_result = robot.connect()
if not connected_result.success:
log.error("Could not connect with robot {}".format(robot.robot_details.robot_id))
animus.close_client_interface()
sys.exit(-1)
# # ----------------Motor Loop------------------------------------
"""
open_success = robot.open_modality("motor")
if not open_success:
log.error("Could not open robot motor modality")
sys.exit(-1)
screen = pygame.display.set_mode((300, 300))
pygame.display.set_caption('WindowToRegisterInputs')
screen.fill((234, 212, 252))
pygame.display.flip()
counter = 0
pygame.init()
clock = pygame.time.Clock()
while True:
counter += 1
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.event.pump() # update the keyboard state in memory
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
print('key left')
send_speed_cmd(robot, 0, -1.)
elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
print('key right')
send_speed_cmd(robot, 0, 1.)
elif keys[pygame.K_UP] or keys[pygame.K_w]:
print('key up')
send_speed_cmd(robot, 1., 0)
elif keys[pygame.K_DOWN] or keys[pygame.K_s]:
send_speed_cmd(robot, -1., 0)
print('key down')
elif keys[pygame.K_u]:
send_head_cmd(robot, 1.)
print('key u')
elif keys[pygame.K_p]:
send_head_cmd(robot, -1.)
print('key p')
else:
send_speed_cmd(robot, 0., 0.)
"""
# ----------------Motor Visual Loop------------------------------------
# Open modalities
open_success = robot.open_modality("vision")
if not open_success:
log.error("Could not open robot vision modality")
sys.exit(-1)
open_success = robot.open_modality("motor")
if not open_success:
log.error("Could not open robot motor modality")
sys.exit(-1)
open_success = robot.open_modality("proprioception")
if not open_success:
log.error("Could not open robot proprioception modality")
sys.exit(-1)
# screen = pygame.display.set_mode((300, 300))
# pygame.display.set_caption('WindowToRegisterInputs')
# screen.fill((234, 212, 252))
# pygame.display.flip()
motion_counter = 0
counter = 0
pygame.init()
clock = pygame.time.Clock()
compr_factor = 1
n = 0
try:
while True:
n += 1
# PROPRIOCEPTION
try:
arr, err = robot.get_modality("proprioception")
if err.success:
compr_factor = arr[1]
#print(arr)
except Exception as e:
print("SOME PROPRIOCEPTION ISSUE", e)
continue
# VISION
try:
# the backend receives YUV and automatically converts to BRG
image_list, err = robot.get_modality("vision", True) # returns a list object
except Exception as e:
print("SOME VISION ISSUE", e)
continue
if err.success:
# cv2.cvtColor(myim, cv2.COLOR_RGB2BGR, myim)
print("shapes")
for img in image_list:
print(img.image.shape)
try:
if len(image_list) in [1, 2]:
cv2.imshow("LeftCam", image_list[0].image)
cv2.imwrite(f"imgs/{n}.jpg", image_list[0].image)
elif len(image_list) in [9, 18]:
# undo patch compr
resize = lambda x: cv2.resize(x, (int(x.shape[1]*compr_factor), int(x.shape[0]*compr_factor))) # W,H
# stack horizontally
img_above_fov = resize(image_list[1].image)
row1 = np.concatenate((resize(image_list[0].image),
img_above_fov,
resize(image_list[2].image)), axis=1)
img_left_fov = resize(image_list[3].image)
fov = image_list[4].image[:img_left_fov.shape[0], :img_above_fov.shape[1], :]
print(img_above_fov.shape, img_left_fov.shape, fov.shape)
row2 = np.concatenate((img_left_fov,
fov,
resize(image_list[5].image)), axis=1)
row3 = np.concatenate((resize(image_list[6].image),
resize(image_list[7].image),
resize(image_list[8].image)), axis=1)
# stack vertically
img = np.concatenate((row1, row2, row3), axis=0)
cv2.imshow("Patch Compressed", img)
j = cv2.waitKey(1)
if j == 27:
break
except Exception as e:
print(e)
# MOTOR
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.event.pump() # update the keyboard state in memory
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
print('key left')
send_speed_cmd(robot, 0, -1.)
elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
print('key right')
send_speed_cmd(robot, 0, 1.)
elif keys[pygame.K_UP] or keys[pygame.K_w]:
print('key up')
send_speed_cmd(robot, 1., 0)
elif keys[pygame.K_DOWN] or keys[pygame.K_s]:
send_speed_cmd(robot, -1., 0)
print('key down')
elif keys[pygame.K_u]:
send_head_cmd(robot, 1.)
print('key u')
elif keys[pygame.K_p]:
send_head_cmd(robot, -1.)
print('key p')
else:
send_speed_cmd(robot, 0., 0.)
except KeyboardInterrupt:
cv2.destroyAllWindows()
log.info("Closing down")
stopFlag = True
except SystemExit:
cv2.destroyAllWindows()
log.info("Closing down")
stopFlag = True
cv2.destroyAllWindows()
if stopFlag:
# Disconnect from robot
robot.disconnect()
# Close client interface
animus.close_client_interface()
sys.exit(-1)