-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharena_app.py
291 lines (240 loc) · 8.7 KB
/
arena_app.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
"""
This file is the main ARENA app which takes data coming in from cozmo-tools over HTTP requests
and sends it on to ARENA, and also makes any incoming events from ARENA available to cozmo-tools
in a polling manner. The reason for the use of HTTP requests and this multi-process system (as
opposed to directly connecting to ARENA from the cozmo program) is primarily for flexibility;
this system allows the cozmo library to be swapped out or even run on a different computer from
the ARENA program without interruptions. Furthermore, it improves the startup time of the cozmo
program because connecting to ARENA can be slightly slow.
Tested and working with arenaxr.org platform as of 04/28/2022 (ARENA-core e57f42d).
"""
from flask import Flask
import math, time, threading
from arena import *
from scipy.spatial.transform import Rotation as R
# Reduce verbosity of Flask built-in logging
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
app = Flask(__name__)
"""
Configuration parameters
Most of these should probably not need to change
"""
COZMO_GLB = "store/users/asinghan/cozmo.glb"
COZMO_SCALE = 0.01 # relative to GLB file
ORIGIN_GLB = "store/public/origin.glb"
ORIGIN_SCALE = 0.03 # relative to GLB file
CUBE_COLOR = (255, 0, 0) # RGB
CUBE_DIM = 0.045 # meters
APRILTAG_SIZE = 50 # mm
APRILTAG_HEIGHT = 0.07 # meters
ARUCO_MARKER_HEIGHT = 0.035 # meters
ARUCO_MARKERS = [(0, 0.035), (0.1, 0.035), (0.25, 0.2), (0.25, 0.3)]
ARUCO_COLOR = (0, 170, 170) # small circle in center of marker
ARENA_HOST = "arenaxr.org"
ARENA_SCENE = "cozmo-new"
# Whether to use the user's camera to place destination markers
USE_RAYCAST = True
# Used for immersive mode; system scale doesn't match world scale
WORLD_SCALE = 1.0
HTTP_PORT = 8000
scene = Scene(host=ARENA_HOST, scene=ARENA_SCENE)
"""
Main dictionary of objects maintained on this end of the system
x (meters), y (meters), theta (degrees), visible (boolean)
"""
objects = {"cozmo": (0, 0, 0, False), "cube1": (0, 0, 0, False),
"cube2": (0, 0, 0, False), "cube3": (0, 0, 0, False)}
arena_objects = {}
waypoint = None
"""
Recieve updated location of an object
"""
@app.route("/update_obj/<obj>/<x>,<y>,<theta>,<visible>")
def update_obj(obj, x, y, theta, visible):
global objects
if obj not in objects:
print("Unknown object", obj)
return "ERROR_UNKNOWN_OBJECT"
objects[obj] = (float(x), float(y), float(theta), int(visible))
return "OK"
"""
Get the latest waypoint (if one exists)
"""
@app.route("/get_waypoint")
def get_waypoint():
if waypoint:
return f"{waypoint[0]},{waypoint[1]}"
else:
return "NONE"
"""
Acknowledge that the robot finished navigation
"""
@app.route("/reset_waypoint")
def reset_waypoint():
global waypoint
waypoint = None
return "OK"
"""
Handler for incoming messages from ARENA
"""
def on_message(scene, evt, msg):
oid = msg.get("object_id")
if USE_RAYCAST:
if not oid.startswith("camera"): return
# Get the position and orientation of the user's head
pos = msg["data"]["position"]
rot = msg["data"]["rotation"]
rot = R.from_quat([rot["x"], rot["y"], rot["z"], rot["w"]])
# Compute a raycast from the user's head to the ground plane
vec = rot.apply([0, 0, -1])
res = (0 - pos["y"]) / vec[1]
x = pos["x"] + res*vec[0]
z = pos["z"] + res*vec[2]
if waypoint is None:
# Move the pushpin so it feels like the user is controlling it by moving their head
arena_objects["pushpin"].data.position = Position(x, 0, z)
arena_objects["pushpin"].data.color = Color(0, 180, 0)
scene.update_object(arena_objects["pushpin"])
else:
if not oid.startswith("handRight"): return
if waypoint is None:
pos = msg["data"]["position"]
# Edge case when the hand is moving in and out of the frame
if pos["x"] == 0 and pos["z"] == 0:
return
# If the object was put into the ground
if pos["y"] < 0.4:
arena_objects["pushpin"].data.position = Position(pos["x"], 0.4, pos["z"])
scene.update_object(arena_objects["pushpin"])
set_nav_pos()
else:
arena_objects["pushpin"].data.position = Position(pos["x"], pos["y"] - 0.2, pos["z"])
arena_objects["pushpin"].data.color = Color(0, 180, 0)
scene.update_object(arena_objects["pushpin"])
"""
Set up the 3D objects in ARENA with placeholder positions
"""
@scene.run_once
def arena_init():
global arena_objects
# Create the origin apriltag for the cozmo "world"
origin = GLTF(
object_id="origin",
url=ORIGIN_GLB,
position=(0, APRILTAG_HEIGHT*WORLD_SCALE, 0),
scale=(ORIGIN_SCALE*WORLD_SCALE,)*3,
rotation=(-90, 0, 0),
persist=True
)
if WORLD_SCALE == 1:
origin.data["armarker"] = {
"markerid": "1",
"markertype": "apriltag_36h11",
"size": 50,
"buildable": False,
"dynamic": False
}
scene.add_object(origin)
# Add a small marker on each AruCo marker
for i in range(len(ARUCO_MARKERS)):
x, z = ARUCO_MARKERS[i]
y = ARUCO_MARKER_HEIGHT
color = ARUCO_COLOR
sphere = Sphere(
object_id=f"aruco{i}",
position=(x*WORLD_SCALE, y*WORLD_SCALE, z*WORLD_SCALE),
scale=(0.005*WORLD_SCALE,)*3,
rotation=(0, 0, 0),
color=color,
persist=True
)
scene.add_object(sphere)
# Create cozmo and the cubes
arena_objects["cozmo"] = GLTF(
object_id="cozmo",
url=COZMO_GLB,
position=(999, 999, 999),
rotation=(90, 180, 90),
scale=(.01*WORLD_SCALE,) * 3,
persist=True
)
scene.add_object(arena_objects["cozmo"])
for cube in ("cube1", "cube2", "cube3"):
arena_objects[cube] = Box(
object_id=cube,
position=(999, 999, 999),
scale=(CUBE_DIM*WORLD_SCALE,) * 3,
persist=True,
color=CUBE_COLOR
)
scene.add_object(arena_objects[cube])
if USE_RAYCAST:
# Create the pushpin object as a thin flat disk which acts like a "navigation target"
arena_objects["pushpin"] = Cylinder(
object_id="pushpin",
position=(999, 999, 999),
rotation=(0, 0, 0),
scale=(.023, .005, .023),
color=(0, 120, 0),
persist=True,
clickable=True,
evt_handler=click_handler
)
scene.add_object(arena_objects["pushpin"])
else:
# Create the pushpin object as a "stake" which can be placed into the ground
arena_objects["pushpin"] = Cylinder(
object_id="pushpin",
position=(999, 999, 999),
rotation=(0, 0, 0),
scale=(.015, .8, .015),
color=(0, 120, 0),
persist=True,
clickable=True,
evt_handler=click_handler
)
scene.add_object(arena_objects["pushpin"])
scene.on_msg_callback = on_message
"""
Event handler for when the pushpin is clicked
"""
def click_handler(scene, evt, msg):
if evt.type == "mousedown":
set_nav_pos()
"""
Send a new waypoint based on the pushpin position
"""
def set_nav_pos():
global waypoint
if waypoint is None:
pos = arena_objects["pushpin"].data.position
waypoint = (pos["z"] / WORLD_SCALE, pos["x"] / WORLD_SCALE)
print("Navigating to", waypoint)
arena_objects["pushpin"].data.color = Color(240, 10, 0)
scene.update_object(arena_objects["pushpin"])
"""
Invoked in a loop once ARENA is connected, pushes updates to ARENA objects
"""
@scene.run_forever(interval_ms=100)
def arena_update():
global objects, arena_objects
for obj in objects:
x, y, theta, visible = objects[obj]
# Really big coordinates ~ invisible (in ARENA at least)
if not visible:
x, y = 999, 999
arena_objects[obj].data.position.x = y * WORLD_SCALE
arena_objects[obj].data.position.z = x * WORLD_SCALE
arena_objects[obj].data.position.y = 0
if obj == "cozmo":
arena_objects[obj].data.rotation = Rotation(90, 180, theta+90)
else:
arena_objects[obj].data.rotation = Rotation(0, theta, 0)
scene.update_object(arena_objects[obj])
if __name__ == "__main__":
# Start the HTTP server thread and the ARENA asyncio loop
threading.Thread(target=lambda: app.run(host="0.0.0.0", port=HTTP_PORT)).start()
scene.run_tasks()