@@ -68,8 +68,8 @@ def makePoseObject(temp):
6868 temp ["rotation" ]["quaternion" ]["Y" ],
6969 temp ["rotation" ]["quaternion" ]["Z" ],
7070 )
71- ).rotateBy (wpi .Rotation3d (0 , 0 , math .radians (180 ))))
72-
71+ ).rotateBy (wpi .Rotation3d (0 , 0 , math .radians (180 ))),
72+ )
7373
7474 # ROT MUST BE A 3x3 ROTATION MATRIX
7575 def getTransform (trans , rot ):
@@ -79,7 +79,7 @@ def getTransform(trans, rot):
7979
8080 def getTagTransform (jsonID ):
8181 pose = Processor .makePoseObject (jsonID )
82- rot = pose .rotation ().rotateBy (wpi .Rotation3d (0 ,0 , math .radians (180 )))
82+ rot = pose .rotation ().rotateBy (wpi .Rotation3d (0 , 0 , math .radians (180 )))
8383 translation = np .asarray ([pose .X (), pose .Y (), pose .Z ()]).reshape (3 , 1 )
8484 rot , _ = cv2 .Rodrigues (np .asarray ([rot .X (), rot .Y (), rot .Z ()]).reshape (3 , 1 ))
8585 return Processor .getTransform (translation , rot )
@@ -97,6 +97,37 @@ def addCorners(tagPos, cornerPos):
9797 tagPos .translation () + cornerPos .rotateBy (tagPos .rotation ())
9898 )
9999
100+ @staticmethod
101+ def getTransRots (tvecs , rvecs , curId , layout ):
102+
103+ camTvecs = tvecs
104+
105+ # ambig.append(reproj[0][0])
106+
107+ x = camTvecs [0 ]
108+ y = camTvecs [1 ]
109+ z = camTvecs [2 ]
110+
111+ camTvecs = np .asarray ([z , - x , - y ]).reshape (3 , 1 )
112+
113+ camRvecs = rvecs
114+
115+ x = camRvecs [0 ]
116+ y = camRvecs [1 ]
117+ z = camRvecs [2 ]
118+
119+ camRvecs = np .asarray ([z , - x , - y ]).reshape (3 , 1 )
120+
121+ camRotMat , _ = cv2 .Rodrigues (camRvecs )
122+ cam2Tag = Processor .getTransform (camTvecs , camRotMat )
123+ world2Tag = Processor .getTagTransform (layout [curId - 1 ]["pose" ])
124+
125+ world2Cam = np .dot (world2Tag , np .linalg .inv (cam2Tag ))
126+
127+ transVec = Processor .getTranslationFromTransform (world2Cam )
128+ rvecs = Processor .getRotFromTransform (world2Cam )
129+ return (transVec , rvecs )
130+
100131 # Find AprilTags and calculate the camera's pose
101132 def imagePose (self , images , K , D , layout , arucoDetector ):
102133 poses = []
@@ -105,11 +136,13 @@ def imagePose(self, images, K, D, layout, arucoDetector):
105136
106137 # Loop through images
107138 for imgIndex , img in enumerate (images ):
139+ pose1 = 0
140+ pose2 = 0
108141 curTags = []
109142
110143 # If an invalid image is given or no calibration return an error pose
111144 if img is None or K [imgIndex ] is None or D [imgIndex ] is None :
112- poses .append (Processor .BAD_POSE )
145+ poses .append (( Processor .BAD_POSE , Processor . BAD_POSE ) )
113146 tags .append ([])
114147 ambig .append (2767 )
115148 continue
@@ -230,7 +263,7 @@ def imagePose(self, images, K, D, layout, arucoDetector):
230263 if len (ids ) > 1 :
231264 ambig .append (2767 )
232265
233- if len (ids ) == 0 :
266+ if len (ids ) == 1 :
234267 tempc1 = np .asarray (
235268 [self .squareLength / 2 , self .squareLength / 2 , 0 ]
236269 )
@@ -251,32 +284,19 @@ def imagePose(self, images, K, D, layout, arucoDetector):
251284 D [imgIndex ],
252285 flags = cv2 .SOLVEPNP_IPPE_SQUARE ,
253286 )
254- # Processor.logger.info(tvecs)
255- curId = ids [0 ][0 ]
256- camTvecs = tvecs [0 ].reshape (3 , 1 )
257-
258- x = camTvecs [0 ]
259- y = camTvecs [1 ]
260- z = camTvecs [2 ]
261-
262- camTvecs = np .asarray ([z , - x , - y ]).reshape (3 , 1 )
287+ t1 , r1 = Processor .getTransRots (tvecs [0 ].reshape (3 ,1 ), rvecs [0 ], ids [0 ][0 ], layout )
288+ t2 , r2 = Processor .getTransRots (tvecs [1 ].reshape (3 ,1 ), rvecs [1 ], ids [0 ][0 ], layout )
263289
264- camRvecs = rvecs [0 ]
290+ rot3D = wpi .Rotation3d (r1 )
291+ trans = wpi .Translation3d (t1 [0 ], t1 [1 ], t1 [2 ])
265292
266- x = camRvecs [0 ]
267- y = camRvecs [1 ]
268- z = camRvecs [2 ]
293+ pose1 = wpi .Pose3d (trans , rot3D )
269294
270- camRvecs = np .asarray ([z , - x , - y ]).reshape (3 , 1 )
295+ rot3D = wpi .Rotation3d (r2 )
296+ trans = wpi .Translation3d (t2 [0 ], t2 [1 ], t2 [2 ])
271297
272- camRotMat , _ = cv2 .Rodrigues (camRvecs )
273- cam2Tag = Processor .getTransform (camTvecs , camRotMat )
274- world2Tag = Processor .getTagTransform (layout [curId - 1 ]["pose" ])
298+ pose2 = wpi .Pose3d (trans , rot3D )
275299
276- world2Cam = np .dot (world2Tag , np .linalg .inv (cam2Tag ))
277-
278- transVec = Processor .getTranslationFromTransform (world2Cam )
279- rvecs = Processor .getRotFromTransform (world2Cam )
280300
281301 else :
282302 # Calculate robot pose with 2d and 3d points
@@ -288,30 +308,33 @@ def imagePose(self, images, K, D, layout, arucoDetector):
288308 flags = cv2 .SOLVEPNP_SQPNP ,
289309 )
290310
311+ # ambig.append(reproj[0])
312+
291313 # Grab the rotation matrix and find the translation vector
292314 rotMat , _ = cv2 .Rodrigues (rvecs )
293315 transVec = - np .dot (np .transpose (rotMat ), tvecs )
294316 transVec = np .asarray ([transVec [2 ], - transVec [0 ], - transVec [1 ]])
295317
296- # Convert the rotation matrix to a three rotation system (yaw, pitch, roll)
297- rot3D = wpi .Rotation3d (
298- np .array ([rvecs [2 ][0 ], - rvecs [0 ][0 ], + rvecs [1 ][0 ]]),
299- (rvecs [0 ][0 ] ** 2 + rvecs [1 ][0 ] ** 2 + rvecs [2 ][0 ] ** 2 ) ** 0.5 ,
300- )
318+ rots , _ = cv2 .Rodrigues (rotMat )
319+ rotMat , _ = cv2 .Rodrigues (np .asarray ([rots [2 ], - rots [0 ], rots [1 ]]))
301320
321+ # Convert the rotation matrix to a three rotation system (yaw, pitch, roll)
322+ rot3D = wpi .Rotation3d (rotMat )
323+ pose1 = wpi .Pose3d (
324+ # Translation between openCV and WPILib
325+ wpi .Translation3d (transVec [0 ], transVec [1 ], transVec [2 ]),
326+ rot3D ,
327+ )
328+ pose2 = pose1
302329 # Append the pose
303330 poses .append (
304- wpi .Pose3d (
305- # Translation between openCV and WPILib
306- wpi .Translation3d (transVec [0 ], transVec [1 ], transVec [2 ]),
307- rot3D ,
308- )
331+ (pose1 , pose2 )
309332 )
310333 tags .append (curTags )
311334 num += 1
312335 else :
313336 # If no tags
314- poses .append (Processor .BAD_POSE )
337+ poses .append (( Processor .BAD_POSE , Processor . BAD_POSE ) )
315338 tags .append ([])
316339 ambig .append (2767 )
317340
0 commit comments