This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAKD_to_Network_tables.py
163 lines (116 loc) · 4.25 KB
/
OAKD_to_Network_tables.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
import Network_Tables_Sender as nts
import tag
import cv2
import depthai as dai
#Special MOE one
import moe_apriltags as apriltag
import numpy as np
from dataclasses import dataclass
import matplotlib.pyplot as plt
from scipy.spatial.transform import Rotation as R
debug = 1
if debug:
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
buffer = []
#Creating pipeline
def create_pipeline():
pipeline = dai.Pipeline()
monoout = pipeline.createXLinkOut()
monoout.setStreamName("mono")
monocam = pipeline.createMonoCamera()
monocam.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
monocam.setFps(60)
monocam.setBoardSocket(dai.CameraBoardSocket.LEFT)
monocam.out.link(monoout.input)
return pipeline
detector = apriltag.Detector(families="tag16h5", nthreads=2)
class Transform:
translation: np.ndarray
rotation: R
def __init__(self, translation: np.ndarray, rotation: R):
if translation.shape != (3,):
raise ValueError(f'Shape of translation is {translation.shape}')
self.translation = translation
self.rotation = rotation
def inv(self) -> 'Transform':
return Transform(
rotation = self.rotation.inv(),
translation = self.rotation.apply(-self.translation, inverse = True)
)
def combine(self, other: 'Transform') -> 'Transform':
rotated = self.rotation.apply(other.translation)
return Transform(
rotation = self.rotation * other.rotation,
translation = self.translation + rotated
)
#return R.concatenate([self, other])
camera_rs = Transform(
translation = np.array([0,0,0]),
rotation = R.identity(),
)
def calculate_pose(det: apriltag.Detection):
#negate to account for rotation of tag
tag_cs = Transform(
translation=result.pose_t[:,0],
rotation=R.from_matrix(-result.pose_R)
)
if debug:
print(result.pose_R)
cam_ts = tag_cs.inv()
# print(translation_inv, tinv)
tag_tl_fs = tag.tag_translation[det.tag_id]
tag_ro_fs = tag.tag_rotation[det.tag_id]
tag_fs = Transform(
translation= tag_tl_fs,
rotation=tag_ro_fs
)
cam_fs = tag_fs.combine(cam_ts) #Transforms camera in field space to tag in field space. Camera in robot space is then transformed into robot in camera space, which allows us to get robot in field space.
robot_cs = camera_rs.inv()
robot_fs = cam_fs.combine(robot_cs)
return robot_fs
with dai.Device(create_pipeline()) as device:
monoq = device.getOutputQueue(name="mono", maxSize=1, blocking=False)
calibdata = device.readCalibration()
intrinsics = calibdata.getDefaultIntrinsics(dai.CameraBoardSocket.LEFT)[0]
oak_d_camera_params = (
intrinsics[0][0],
intrinsics[1][1],
intrinsics[0][2],
intrinsics[1][2],
)
while True:
img = monoq.get().getCvFrame()
results = detector.detect(img,
l=1,
r=8,
maxhamming=0,
estimate_tag_pose=True,
tag_size=.1524,
camera_params=oak_d_camera_params)
if debug:
cv2.imshow('foo', img)
if cv2.waitKey(1) == ord('q'):
break
if len(results) == 0:
continue
results.sort(reverse=True, key = lambda x: x.pose_err)
result: apriltag.Detection = results[0]
rotation_cs = result.pose_R
translation_cs = result.pose_t[:,0]
robot_fs = calculate_pose(result)
if(debug):
if len(buffer) > 20:
buffer = buffer[-20:]
buffer.append([*robot_fs.translation, *translation_cs])
b = np.array(buffer)
ax1.cla()
ax1.set(xlim=(-5,5), ylim=(-5,5))
ax1.scatter([0], [0])
ax1.plot(b[:,0], b[:,2])
ax1.plot(b[:,3], b[:,5])
plt.draw()
plt.pause(.001)
pose = [*robot_fs.translation, *robot_fs.rotation.as_quat()]
nts.send_pose(pose) #Returns robot in field space.