blob: 8d1c614d2b99065af57e1a54a167682e42e9c8df [file] [log] [blame]
Jim Ostrowski007e2ea2022-01-30 13:13:26 -08001import copy
2import glog
3import json
4import math
5import numpy as np
6import os
7
8glog.setLevel("INFO")
9
10
11# Quick fix to naming games that happen with bazel
12def bazel_name_fix(filename):
13 ret_name = filename
14 try:
15 from bazel_tools.tools.python.runfiles import runfiles
16 r = runfiles.Create()
17 ret_name = r.Rlocation('org_frc971/y2022/vision/' + filename)
18 #print("Trying directory: ", ret_name)
19 except:
20 print("Failed bazel_name_fix")
21 pass
22
23 ### TODO<Jim>: Need to figure out why this isn't working
24 ### Hardcoding for now
25 if ret_name == None:
26 ret_name = '/home/jim/code/FRC/971-Robot-Code/y2022/vision/calib_files'
27 return ret_name
28
29
30class CameraIntrinsics:
31 def __init__(self):
32 self.camera_matrix = []
33 self.dist_coeffs = []
34
35 pass
36
37
38class CameraExtrinsics:
39 def __init__(self):
40 self.R = []
41 self.T = []
42
43
44class CameraParameters:
45 def __init__(self):
46 self.camera_int = CameraIntrinsics()
47 self.camera_ext = CameraExtrinsics()
48 self.turret_ext = None
49 self.node_name = ""
50 self.team_number = -1
Jim Ostrowskifec0c332022-02-06 23:28:26 -080051 self.camera_id = ""
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080052 self.timestamp = 0
53
54
55def compute_extrinsic(camera_pitch, camera_yaw, T_camera, is_turret):
56 # Compute the extrinsic calibration based on pitch and translation
57 # Includes camera rotation from robot x,y,z to opencv (z, -x, -y)
58
59 # Also, handle extrinsics for the turret
60 # The basic camera pose is relative to the center, base of the turret
61 # TODO<Jim>: Maybe store these to .json files, like with intrinsics?
62 base_cam_ext = CameraExtrinsics()
63 turret_cam_ext = CameraExtrinsics()
64
65 camera_pitch_matrix = np.array(
66 [[np.cos(camera_pitch), 0.0,
67 np.sin(camera_pitch)], [0.0, 1.0, 0.0],
68 [-np.sin(camera_pitch), 0.0,
69 np.cos(camera_pitch)]])
70
71 camera_yaw_matrix = np.array(
72 [[np.cos(camera_yaw), -np.sin(camera_yaw), 0.0],
73 [np.sin(camera_yaw), np.cos(camera_yaw), 0.0], [0.0, 0.0, 1.0]])
74
75 robot_to_camera_rotation = np.array([[0., 0., 1.], [-1, 0, 0], [0, -1.,
76 0]])
77
78 if is_turret:
James Kuszmaul3f3b1d92022-03-13 18:01:37 -070079 # Turret is just an identity matrix in the middle of the robot.
80 base_cam_ext.R = np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0],
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080081 [0.0, 0.0, 1.0]])
82 base_cam_ext.T = np.array([0.0, 0.0, 0.0])
83 turret_cam_ext.R = camera_yaw_matrix @ camera_pitch_matrix @ robot_to_camera_rotation
84 turret_cam_ext.T = T_camera
85 else:
86 base_cam_ext.R = camera_yaw_matrix @ camera_pitch_matrix @ robot_to_camera_rotation
87 base_cam_ext.T = T_camera
88 turret_cam_ext = None
89
90 return base_cam_ext, turret_cam_ext
91
92
93def compute_extrinsic_by_pi(pi_number):
James Kuszmaul3f3b1d92022-03-13 18:01:37 -070094 # Defaults for all cameras
95 camera_pitch = -35.0 * np.pi / 180.0
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080096 camera_yaw = 0.0
James Kuszmaul3f3b1d92022-03-13 18:01:37 -070097 is_turret = True
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080098 # Default camera location to robot origin
99 T = np.array([0.0, 0.0, 0.0])
100
101 if pi_number == "pi1":
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800102 camera_yaw = 90.0 * np.pi / 180.0
Milind Upadhyay0792b3b2022-04-14 21:39:30 -0700103 T = np.array([-11.0 * 0.0254, 5.5 * 0.0254, 29.5 * 0.0254])
James Kuszmaul3f3b1d92022-03-13 18:01:37 -0700104 elif pi_number == "pi2":
105 camera_yaw = 0.0
Milind Upadhyay0792b3b2022-04-14 21:39:30 -0700106 T = np.array([-9.5 * 0.0254, -3.5 * 0.0254, 34.5 * 0.0254])
James Kuszmaul3f3b1d92022-03-13 18:01:37 -0700107 elif pi_number == "pi3":
Austin Schuh463abe22022-04-15 19:15:49 -0700108 camera_yaw = 179.0 * np.pi / 180.0
Milind Upadhyay0792b3b2022-04-14 21:39:30 -0700109 T = np.array([-9.5 * 0.0254, 3.5 * 0.0254, 34.5 * 0.0254])
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800110 elif pi_number == "pi4":
111 camera_yaw = -90.0 * np.pi / 180.0
Milind Upadhyay0792b3b2022-04-14 21:39:30 -0700112 T = np.array([-10.25 * 0.0254, -5.0 * 0.0254, 27.5 * 0.0254])
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800113
114 return compute_extrinsic(camera_pitch, camera_yaw, T, is_turret)
115
116
117def load_camera_definitions():
118 ### CAMERA DEFINITIONS
119 # We only load in cameras that have a calibration file
120 # These are stored in y2022/vision/calib_files
121 #
122 # Or better yet, use //y2020/vision:calibration to calibrate the camera
123 # using a Charuco target board
124
125 camera_list = []
126
127 dir_name = bazel_name_fix('calib_files')
128 if dir_name is not None:
129 glog.debug("Searching for calibration files in " + dir_name)
130 else:
131 glog.fatal("Failed to find calib_files directory")
132
133 for filename in sorted(os.listdir(dir_name)):
134 glog.debug("Inspecting %s", filename)
135 if ("cam-calib-int" in filename
136 or 'calibration' in filename) and filename.endswith(".json"):
137
138 # Extract intrinsics from file
139 calib_file = open(dir_name + "/" + filename, 'r')
140 calib_dict = json.loads(calib_file.read())
141
142 team_number = calib_dict["team_number"]
143 node_name = calib_dict["node_name"]
Jim Ostrowskifec0c332022-02-06 23:28:26 -0800144 camera_id = "UNKNOWN"
145 if "camera_id" in calib_dict:
146 camera_id = calib_dict["camera_id"]
147
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800148 camera_matrix = np.asarray(calib_dict["intrinsics"]).reshape(
149 (3, 3))
150 dist_coeffs = np.asarray(calib_dict["dist_coeffs"]).reshape((1, 5))
151
152 glog.debug("Found calib for " + node_name + ", team #" +
153 str(team_number))
154
155 camera_params = CameraParameters()
156 # TODO: Need to add reading in extrinsic camera parameters from json
157 camera_params.camera_ext, camera_params.turret_ext = compute_extrinsic_by_pi(
158 node_name)
159
160 camera_params.node_name = node_name
161 camera_params.team_number = team_number
Jim Ostrowskifec0c332022-02-06 23:28:26 -0800162 camera_params.camera_id = camera_id
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800163 camera_params.camera_int.camera_matrix = copy.copy(camera_matrix)
164 camera_params.camera_int.dist_coeffs = copy.copy(dist_coeffs)
165 camera_list.append(camera_params)
166
167 return camera_list