-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter3d.py
More file actions
331 lines (288 loc) · 11.3 KB
/
Copy pathplotter3d.py
File metadata and controls
331 lines (288 loc) · 11.3 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import cv2
import matplotlib.pyplot as plt
from multiprocessing import Process, Manager, Queue
import numpy as np
import os
import pyqtgraph as pg
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtCore
from stl import mesh
import transformations as tf
from helpers import in2m, besph2cart
from particle_filter import (
ROBOT_HALF_LENGTH,
ROBOT_HALF_WIDTH,
TARGET_CENTER_X,
TARGET_CENTER_Y,
TARGET_CENTER_Z,
TARGET_RADIUS,
FIELD_LENGTH,
FIELD_WIDTH,
FOV_HORIZ,
FOV_VERT,
)
FIELD_IMAGE = os.path.join(os.path.dirname(__file__), "./2026-field.png")
# FIELD_MESH = mesh.Mesh.from_file(os.path.join(os.path.dirname(__file__), "../../field.stl"))
# ROBOT_MESH = mesh.Mesh.from_file(os.path.join(os.path.dirname(__file__), "../../robot.stl"))
ROBOT_Z_HEIGHT = 0.1
ROBOT_LINES = np.array(
[
# Edges
[ROBOT_HALF_LENGTH, ROBOT_HALF_WIDTH, ROBOT_Z_HEIGHT],
[-ROBOT_HALF_LENGTH, ROBOT_HALF_WIDTH, ROBOT_Z_HEIGHT],
[-ROBOT_HALF_LENGTH, ROBOT_HALF_WIDTH, ROBOT_Z_HEIGHT],
[-ROBOT_HALF_LENGTH, -ROBOT_HALF_WIDTH, ROBOT_Z_HEIGHT],
[-ROBOT_HALF_LENGTH, -ROBOT_HALF_WIDTH, ROBOT_Z_HEIGHT],
[ROBOT_HALF_LENGTH, -ROBOT_HALF_WIDTH, ROBOT_Z_HEIGHT],
[ROBOT_HALF_LENGTH, -ROBOT_HALF_WIDTH, ROBOT_Z_HEIGHT],
[ROBOT_HALF_LENGTH, ROBOT_HALF_WIDTH, ROBOT_Z_HEIGHT],
# Arrow
[-0.1, 0.1, ROBOT_Z_HEIGHT],
[0.2, 0.0, ROBOT_Z_HEIGHT],
[0.2, 0.0, ROBOT_Z_HEIGHT],
[-0.1, -0.1, ROBOT_Z_HEIGHT],
[-0.1, -0.1, ROBOT_Z_HEIGHT],
[-0.1, 0.1, ROBOT_Z_HEIGHT],
]
)
ROBOT_END_EFFECTOR = [
[in2m(60.0), in2m(2.5), in2m(36.0)],
[in2m(45.0), in2m(2.5), in2m(36.0)],
]
FOV_X = 1.0
FOV_Y = FOV_X * np.tan(0.5 * FOV_HORIZ)
FOV_Z = FOV_X * np.tan(0.5 * FOV_VERT)
FOV_TOP_LEFT = [FOV_X, FOV_Y, FOV_Z]
FOV_TOP_RIGHT = [FOV_X, -FOV_Y, FOV_Z]
FOV_BOTTOM_LEFT = [FOV_X, FOV_Y, -FOV_Z]
FOV_BOTTOM_RIGHT = [FOV_X, -FOV_Y, -FOV_Z]
FOV_LINES = np.array(
[
[0, 0, 0],
FOV_TOP_LEFT,
[0, 0, 0],
FOV_TOP_RIGHT,
[0, 0, 0],
FOV_BOTTOM_LEFT,
[0, 0, 0],
FOV_BOTTOM_RIGHT,
FOV_TOP_LEFT,
FOV_TOP_RIGHT,
FOV_TOP_RIGHT,
FOV_BOTTOM_RIGHT,
FOV_BOTTOM_RIGHT,
FOV_BOTTOM_LEFT,
FOV_BOTTOM_LEFT,
FOV_TOP_LEFT,
]
)
class Plotter3d:
def __init__(self, num_particles, is_red=True):
self.num_particles = num_particles
self.is_red = is_red
self.camera_infos = {}
self.fovs = []
def start(self):
self.q = Queue(maxsize=5)
self.targets_q = Queue()
self.camera_infos_q = Queue()
self.p = Process(target=self.run)
self.p.start()
return self.q, self.targets_q, self.camera_infos_q
def join(self):
return self.p.join()
def run(self):
app = pg.mkQApp("QuixPF")
self.w = gl.GLViewWidget()
self.w.show()
self.w.orbit(210, 0)
self.w.setCameraPosition(distance=20)
self.w.opts["distance"] = 2000
self.w.opts["fov"] = 1
ax = gl.GLAxisItem()
ax.setSize(1, 1, 1)
self.w.addItem(ax)
grid = gl.GLGridItem()
grid.setSize(18, 10)
grid.setSpacing(1, 1)
self.w.addItem(grid)
field_img = plt.imread(FIELD_IMAGE)
scale = FIELD_LENGTH / field_img.shape[1]
image_item = gl.GLImageItem(pg.makeRGBA(field_img, levels=(0, 1))[0])
image_item.scale(scale, scale, scale)
image_item.rotate(-90, 0, 0, 1)
image_item.translate(0, FIELD_WIDTH, 0)
image_item.setGLOptions("opaque")
self.w.addItem(image_item)
# points = FIELD_MESH.points.reshape(-1, 3)
# faces = np.arange(points.shape[0]).reshape(-1, 3)
# mesh_data = gl.MeshData(vertexes=points, faces=faces)
# field_mesh = gl.GLMeshItem(
# meshdata=mesh_data,
# smooth=True,
# drawFaces=True,
# drawEdges=True,
# color=(0.5, 0.5, 0.5, 1),
# edgeColor=(0, 0, 0, 1)
# )
# field_mesh.translate(0, 0, -0.01)
# self.w.addItem(field_mesh)
self.retroreflective_targets = gl.GLScatterPlotItem(
color=(0, 1, 0, 1), size=0.1, pxMode=False
)
self.retroreflective_targets.setGLOptions("opaque")
self.w.addItem(self.retroreflective_targets)
# points = ROBOT_MESH.points.reshape(-1, 3)
# faces = np.arange(points.shape[0]).reshape(-1, 3)
# mesh_data = gl.MeshData(vertexes=points, faces=faces)
# self.robot_mesh = gl.GLMeshItem(
# meshdata=mesh_data,
# smooth=True,
# drawFaces=True,
# drawEdges=True,
# color=(0.5, 0.5, 0.5, 1),
# edgeColor=(0, 0, 0, 1)
# )
# self.robot_mesh.scale(1e-3, 1e-3, 1e-3)
# self.w.addItem(self.robot_mesh)
self.robot = gl.GLLinePlotItem(
pos=ROBOT_LINES, width=5, color=(0, 0, 0, 1), mode="lines"
)
self.robot.setGLOptions("opaque")
self.w.addItem(self.robot)
self.robot_end_effector = gl.GLScatterPlotItem(
pos=ROBOT_END_EFFECTOR, color=(1, 1, 1, 1), size=0.1, pxMode=False
)
self.robot_end_effector.setGLOptions("opaque")
self.w.addItem(self.robot_end_effector)
self.particles = gl.GLScatterPlotItem(size=2)
self.w.addItem(self.particles)
# By target ID
self.rays = {}
timer = QtCore.QTimer()
timer.timeout.connect(self._update)
timer.start(int(1000.0 / 60.0))
pg.exec()
def _update(self):
# Update targets
while not self.targets_q.empty():
retroreflective_targets, apriltag_targets = self.targets_q.get()
if retroreflective_targets:
pos = []
for t in retroreflective_targets:
pos.append((t.x, t.y, t.z))
self.retroreflective_targets.setData(pos=pos)
if apriltag_targets:
pos = []
for t in apriltag_targets.values():
target = gl.GLMeshItem(
meshdata=gl.MeshData(
vertexes=np.array(
[
[0.0, 0.5, 0.5],
[0.0, -0.5, 0.5],
[0.0, 0.5, -0.5],
[0.0, -0.5, -0.5],
]
),
faces=np.array([[0, 1, 2], [3, 2, 1]]),
),
smooth=True,
color=(1, 0, 1, 1),
)
target.setTransform(pg.Transform3D(t.getPoseMatrix()))
target.scale(t.size, t.size, t.size)
self.w.addItem(target)
# Fast fowrard to the latest element
latest = None
while not self.q.empty():
latest = self.q.get()
if latest is not None:
vision, particles, best_estimate, has_vision = latest
# Particles
if particles is not None:
NUM_TO_PLOT = min(5000, particles.shape[0])
pos = np.zeros((NUM_TO_PLOT, 3))
pos[:, 0] = particles[:NUM_TO_PLOT, 0, 3]
pos[:, 1] = particles[:NUM_TO_PLOT, 1, 3]
self.particles.setData(
pos=pos, color=(0, 1, 0, 1) if has_vision else (1, 0, 0, 1)
)
# Robot
x, y, theta = best_estimate
field_robot_T = pg.Transform3D()
field_robot_T.translate(x, y, 0.0)
field_robot_T.rotate(np.rad2deg(theta), 0, 0, 1)
self.robot.setTransform(field_robot_T)
self.robot_end_effector.setTransform(field_robot_T)
# self.robot_mesh.setTransform(field_robot_T)
# self.robot_mesh.scale(1e-3, 1e-3, 1e-3)
# self.robot_mesh.rotate(90, 0, 0, 1, local=True)
# Update cameras
while not self.camera_infos_q.empty():
self.camera_infos = self.camera_infos_q.get()
# Add untransformed FOVs as necessary
while len(self.fovs) < len(self.camera_infos):
fov = gl.GLLinePlotItem(
pos=FOV_LINES, width=1, color=(1, 1, 1, 1), mode="lines"
)
fov.setGLOptions("opaque")
self.w.addItem(fov)
self.fovs.append(fov)
# Transform FOV viz
for i, fov in enumerate(self.fovs):
camera_info = self.camera_infos.get(i, None)
if camera_info is None:
continue
field_camera_T = field_robot_T * pg.Transform3D(
camera_info.getTransformMatrix()
)
fov.setTransform(field_camera_T)
# Measurement rays
detected_ids = set()
if vision:
for i, v in enumerate(vision):
camera_info = self.camera_infos.get(v.cameraID, None)
if camera_info is None or not camera_info.hasCalibration():
continue
# Add if we haven't seen this cameraID + target ID + cornerID before
id = f"{v.cameraID}-{-1 - i if v.targetID == -1 else v.targetID}-{v.targetCornerID}"
isRetro = v.targetID == -1
if id not in self.rays:
self.rays[id] = gl.GLLinePlotItem(mode="lines")
self.rays[id].setGLOptions("opaque")
self.w.addItem(self.rays[id])
# Update ID
detected_ids.add(id)
ray_length = in2m(25 * 12)
self.rays[id].setTransform(
field_robot_T * pg.Transform3D(camera_info.getTransformMatrix())
)
# Convert pixels to ray sfor plotting
cameraMatrix = camera_info.getCameraMatrix()
distCoeffs = camera_info.getDistCoeffs()
if cameraMatrix is None or distCoeffs is None:
continue
undistorted = cv2.undistortPoints(
np.array([v.pixelX, v.pixelY]),
cameraMatrix,
distCoeffs,
)
x, y = undistorted[0][0]
z = 1.0 # Assume z = 1 (on the normalized plane)
self.rays[id].setData(
pos=np.array([[0, 0, 0], ray_length * np.array([z, -x, -y])]),
color=(0, 1, 0, 1)
if isRetro
else ((1, 0, 1, 1) if v.targetCornerID == -1 else (0, 1, 1, 1)),
width=4 if isRetro else 2,
)
self.rays[id].setGLOptions("additive")
# Turn off all unseen IDs
for id, item in self.rays.items():
if id not in detected_ids:
item.setGLOptions("additive")
item.setData(color=(0, 0, 0, 0))
if __name__ == "__main__":
plotter = Plotter3d(5000)
plotter_queue = plotter.start()