blob: 911418b29b3c0529799d8884cc010b9600baf163 [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:
Ravago Jones5127ccc2022-07-31 16:32:45 -070031
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080032 def __init__(self):
33 self.camera_matrix = []
34 self.dist_coeffs = []
35
36 pass
37
38
39class CameraExtrinsics:
Ravago Jones5127ccc2022-07-31 16:32:45 -070040
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080041 def __init__(self):
42 self.R = []
43 self.T = []
44
45
46class CameraParameters:
Ravago Jones5127ccc2022-07-31 16:32:45 -070047
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080048 def __init__(self):
49 self.camera_int = CameraIntrinsics()
50 self.camera_ext = CameraExtrinsics()
51 self.turret_ext = None
52 self.node_name = ""
53 self.team_number = -1
Jim Ostrowskifec0c332022-02-06 23:28:26 -080054 self.camera_id = ""
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080055 self.timestamp = 0
56
57
58def compute_extrinsic(camera_pitch, camera_yaw, T_camera, is_turret):
59 # Compute the extrinsic calibration based on pitch and translation
60 # Includes camera rotation from robot x,y,z to opencv (z, -x, -y)
61
62 # Also, handle extrinsics for the turret
63 # The basic camera pose is relative to the center, base of the turret
64 # TODO<Jim>: Maybe store these to .json files, like with intrinsics?
65 base_cam_ext = CameraExtrinsics()
66 turret_cam_ext = CameraExtrinsics()
67
68 camera_pitch_matrix = np.array(
69 [[np.cos(camera_pitch), 0.0,
70 np.sin(camera_pitch)], [0.0, 1.0, 0.0],
71 [-np.sin(camera_pitch), 0.0,
72 np.cos(camera_pitch)]])
73
74 camera_yaw_matrix = np.array(
75 [[np.cos(camera_yaw), -np.sin(camera_yaw), 0.0],
76 [np.sin(camera_yaw), np.cos(camera_yaw), 0.0], [0.0, 0.0, 1.0]])
77
78 robot_to_camera_rotation = np.array([[0., 0., 1.], [-1, 0, 0], [0, -1.,
79 0]])
80
81 if is_turret:
James Kuszmaul3f3b1d92022-03-13 18:01:37 -070082 # Turret is just an identity matrix in the middle of the robot.
83 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 -080084 [0.0, 0.0, 1.0]])
85 base_cam_ext.T = np.array([0.0, 0.0, 0.0])
86 turret_cam_ext.R = camera_yaw_matrix @ camera_pitch_matrix @ robot_to_camera_rotation
87 turret_cam_ext.T = T_camera
88 else:
89 base_cam_ext.R = camera_yaw_matrix @ camera_pitch_matrix @ robot_to_camera_rotation
90 base_cam_ext.T = T_camera
91 turret_cam_ext = None
92
93 return base_cam_ext, turret_cam_ext
94
95
Milind Upadhyay37f6fe82022-04-14 23:00:44 -070096def compute_extrinsic_by_pi(pi_number, team_number):
James Kuszmaul3f3b1d92022-03-13 18:01:37 -070097 # Defaults for all cameras
98 camera_pitch = -35.0 * np.pi / 180.0
Jim Ostrowski007e2ea2022-01-30 13:13:26 -080099 camera_yaw = 0.0
James Kuszmaul3f3b1d92022-03-13 18:01:37 -0700100 is_turret = True
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800101 # Default camera location to robot origin
102 T = np.array([0.0, 0.0, 0.0])
103
Milind Upadhyay37f6fe82022-04-14 23:00:44 -0700104 if team_number == 971:
105 if pi_number == "pi1":
106 camera_yaw = 90.0 * np.pi / 180.0
107 T = np.array([-11.0 * 0.0254, 5.5 * 0.0254, 29.5 * 0.0254])
108 elif pi_number == "pi2":
109 camera_yaw = 0.0
110 T = np.array([-9.5 * 0.0254, -3.5 * 0.0254, 34.5 * 0.0254])
111 elif pi_number == "pi3":
112 camera_yaw = 179.0 * np.pi / 180.0
113 T = np.array([-9.5 * 0.0254, 3.5 * 0.0254, 34.5 * 0.0254])
114 elif pi_number == "pi4":
115 camera_yaw = -90.0 * np.pi / 180.0
116 T = np.array([-10.25 * 0.0254, -5.0 * 0.0254, 27.5 * 0.0254])
117 elif team_number == 9971:
118 if pi_number == "pi1":
Austin Schuh399b1842022-04-16 14:26:10 -0700119 camera_yaw = 178.0 * np.pi / 180.0
Milind Upadhyay37f6fe82022-04-14 23:00:44 -0700120 T = np.array([0.0 * 0.0254, 8.5 * 0.0254, 34.0 * 0.0254])
121 elif pi_number == "pi2":
122 camera_yaw = 0.0
123 T = np.array([-9.0 * 0.0254, -3.5 * 0.0254, 35.5 * 0.0254])
124 elif pi_number == "pi3":
125 camera_yaw = 90.0 * np.pi / 180.0
126 T = np.array([-8.0 * 0.0254, 3.0 * 0.0254, 32.0 * 0.0254])
127 else:
128 glog.fatal("Unknown team number for extrinsics")
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800129
130 return compute_extrinsic(camera_pitch, camera_yaw, T, is_turret)
131
132
133def load_camera_definitions():
134 ### CAMERA DEFINITIONS
135 # We only load in cameras that have a calibration file
136 # These are stored in y2022/vision/calib_files
137 #
138 # Or better yet, use //y2020/vision:calibration to calibrate the camera
139 # using a Charuco target board
140
141 camera_list = []
142
143 dir_name = bazel_name_fix('calib_files')
144 if dir_name is not None:
145 glog.debug("Searching for calibration files in " + dir_name)
146 else:
147 glog.fatal("Failed to find calib_files directory")
148
149 for filename in sorted(os.listdir(dir_name)):
150 glog.debug("Inspecting %s", filename)
151 if ("cam-calib-int" in filename
152 or 'calibration' in filename) and filename.endswith(".json"):
153
154 # Extract intrinsics from file
155 calib_file = open(dir_name + "/" + filename, 'r')
156 calib_dict = json.loads(calib_file.read())
157
158 team_number = calib_dict["team_number"]
159 node_name = calib_dict["node_name"]
Jim Ostrowskifec0c332022-02-06 23:28:26 -0800160 camera_id = "UNKNOWN"
161 if "camera_id" in calib_dict:
162 camera_id = calib_dict["camera_id"]
163
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800164 camera_matrix = np.asarray(calib_dict["intrinsics"]).reshape(
165 (3, 3))
166 dist_coeffs = np.asarray(calib_dict["dist_coeffs"]).reshape((1, 5))
167
168 glog.debug("Found calib for " + node_name + ", team #" +
169 str(team_number))
170
171 camera_params = CameraParameters()
172 # TODO: Need to add reading in extrinsic camera parameters from json
173 camera_params.camera_ext, camera_params.turret_ext = compute_extrinsic_by_pi(
Milind Upadhyay37f6fe82022-04-14 23:00:44 -0700174 node_name, team_number)
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800175
176 camera_params.node_name = node_name
177 camera_params.team_number = team_number
Jim Ostrowskifec0c332022-02-06 23:28:26 -0800178 camera_params.camera_id = camera_id
Jim Ostrowski007e2ea2022-01-30 13:13:26 -0800179 camera_params.camera_int.camera_matrix = copy.copy(camera_matrix)
180 camera_params.camera_int.dist_coeffs = copy.copy(dist_coeffs)
181 camera_list.append(camera_params)
182
183 return camera_list