#!/usr/bin/python3importnumpyasnpimportrospyimportrospkgfromstd_msgs.msgimportStringfromutilities.msgimportCalibrationMatricesimportre# Expression to find Ouster window_startpattern_ouster_window_start=r"int\s+window_start\s+=\s+(\d+)"# Expression to find Ouster window_widthpattern_ouster_window_width=r"int\s+window_width\s+=\s+(\d+)"# Expression to find Ouster limit_lines (64 - value)pattern_ouster_limit_lines=r"return\s+ls.h\s+-\s+(\d+)"# Expression to fin limit_lines env variablepattern_mission_machine_limit_lines=r'<arg name="LIMIT_LINES" value="(\d+)"/>'
[docs]classParameterDumper:def__init__(self)->None:self.exclude=['roslaunch','rosdistro','rosversion','/zed2i/zed2i_description','run_id']self.param_pub=rospy.Publisher('parameters',String,queue_size=10)self.matrices_available=rospy.has_param('/local_mapping/matrices_subdirectory')ifself.matrices_availableisTrue:rospack=rospkg.RosPack()matrices_subdirectory=rospy.get_param('/local_mapping/matrices_subdirectory')matrices_path=f'{rospack.get_path("local_mapping")}/scripts/matrices/{matrices_subdirectory}'self.extrinsic_matrix_left=np.load(f'{matrices_path}/extrinsic_matrix_left.npy')self.extrinsic_matrix_right=np.load(f'{matrices_path}/extrinsic_matrix_right.npy')self.intrinsic_matrix_left=np.load(f'{matrices_path}/intrinsic_matrix_left.npy')self.intrinsic_matrix_right=np.load(f'{matrices_path}/intrinsic_matrix_right.npy')try:self.lidar_to_camera_matrix=np.load(f'{matrices_path}/lidar_to_camera.npy')except:rospy.loginfo('[Parameter Dumper] No lidar to camera matrix found. Assuming Sensorfusion is not used.')self.matrices_pub=rospy.Publisher('calibration_matrices',CalibrationMatrices,queue_size=10)
[docs]defdump_parameters(self):param_names=rospy.get_param_names()ouster_params=self.get_ouster_params()dump_params_msg=String()forparam_nameinparam_names:ifrospy.get_param(param_name)andnotany(substringinparam_nameforsubstringinself.exclude):param_value=rospy.get_param(param_name)dump_params_msg.data+=f'{param_name}: {param_value}\n'dump_params_msg.data+=ouster_paramsself.param_pub.publish(dump_params_msg)rospy.loginfo('[Parameter Dumper] Saved parameters to rosbag.')
[docs]defdump_calibration_matrices(self):matrix_msg=CalibrationMatrices()matrix_msg.extrinsic_matrix_left=self.extrinsic_matrix_left.flatten().tolist()matrix_msg.extrinsic_matrix_right=self.extrinsic_matrix_right.flatten().tolist()matrix_msg.intrinsic_matrix_left=self.intrinsic_matrix_left.flatten().tolist()matrix_msg.intrinsic_matrix_right=self.intrinsic_matrix_right.flatten().tolist()ifhasattr(self,'lidar_to_camera_matrix'):matrix_msg.extrinsic_matrix_lidar_to_camera=self.lidar_to_camera_matrix.flatten().tolist()else:matrix_msg.extrinsic_matrix_lidar_to_camera=[]self.matrices_pub.publish(matrix_msg)rospy.loginfo('[Parameter Dumper] Saved calibration matrices to rosbag.')
[docs]defget_ouster_params(self):try:rospack=rospkg.RosPack()driver_path=rospack.get_path("ouster_ros")+"/src/point_cloud_compose.h"withopen(driver_path,"r")asf:point_cloud_compose=f.read()window_start=re.search(pattern_ouster_window_start,point_cloud_compose).group(1)window_width=re.search(pattern_ouster_window_width,point_cloud_compose).group(1)limit_lines_subtract=re.search(pattern_ouster_limit_lines,point_cloud_compose).group(1)limit_lines=64-int(limit_lines_subtract)# Convert reduced number of lines to total number of linesmission_machine_path=rospack.get_path("as_missions")+"/launch/mission_machine.launch"withopen(mission_machine_path,"r")asf:mission_machine=f.read()limit_lines_m=re.search(pattern_mission_machine_limit_lines,mission_machine).group(1)ifint(limit_lines_m)>64orint(limit_lines_m)<0:rospy.logwarn(f'[Parameter Dumper] Invalid limit_lines value in mission_machine.launch: {limit_lines_m}')else:limit_lines=limit_lines_mreturnf"window_start = {window_start}\nwindow_width = {window_width}\nlimit_lines = {limit_lines}\n"exceptExceptionase:rospy.logwarn(f'[Parameter Dumper] Could not find Ouster parameters: {e}')returnf"No Ouster parameters found."
if__name__=='__main__':rospy.init_node('dump_parameters')dumper=ParameterDumper()rospy.sleep(5)# wait 5 seconds until all nodes are starteddumper.dump_parameters()ifdumper.matrices_availableisTrue:dumper.dump_calibration_matrices()else:rospy.logwarn('[Parameter_Dumper] Did not save calibration matrices, because the subdirectory was not \available as a ROS parameter.')