find_velodyne_points.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <cstdio>
3 #include <math.h>
4 #include <algorithm>
5 #include <map>
6 
7 #include "opencv2/opencv.hpp"
8 
9 #include <ros/ros.h>
10 #include <cv_bridge/cv_bridge.h>
13 #include <sensor_msgs/PointCloud2.h>
14 #include <sensor_msgs/Image.h>
15 #include <sensor_msgs/CameraInfo.h>
17 
21 
22 #include <pcl_ros/point_cloud.h>
23 #include <boost/foreach.hpp>
25 #include <velodyne_pcl/point_types.h>
26 //#include <velodyne_pointcloud/point_types.h>
27 #include <pcl/common/eigen.h>
28 #include <pcl/common/transforms.h>
29 #include <pcl/filters/passthrough.h>
30 
34 
35 #include "lidar_camera_calibration/marker_6dof.h"
36 
37 using namespace cv;
38 using namespace std;
39 using namespace ros;
40 using namespace message_filters;
41 using namespace pcl;
42 
43 
46 
47 
49 
50 pcl::PointCloud <myPointXYZRID> point_cloud;
51 Hesai::PointCloud point_cloud_hesai;
52 
53 Eigen::Quaterniond qlidarToCamera;
54 Eigen::Matrix3d lidarToCamera;
55 
56 
57 void callback_noCam(const sensor_msgs::PointCloud2ConstPtr &msg_pc,
58  const lidar_camera_calibration::marker_6dof::ConstPtr &msg_rt) {
59  ROS_INFO_STREAM("Velodyne scan received at " << msg_pc->header.stamp.toSec());
60  ROS_INFO_STREAM("marker_6dof received at " << msg_rt->header.stamp.toSec());
61 
62  // Loading Velodyne point cloud_sub
63  if (config.lidar_type == 0) // velodyne lidar
64  {
65  fromROSMsg(*msg_pc, point_cloud);
66  } else if (config.lidar_type == 1) //hesai lidar
67  {
68  fromROSMsg(*msg_pc, point_cloud_hesai);
70  }
71 
72  point_cloud = transform(point_cloud, config.initialTra[0], config.initialTra[1], config.initialTra[2],
73  config.initialRot[0], config.initialRot[1], config.initialRot[2]);
74 
75  //Rotation matrix to transform lidar point cloud to camera's frame
76 
77  qlidarToCamera = Eigen::AngleAxisd(config.initialRot[2], Eigen::Vector3d::UnitZ())
78  * Eigen::AngleAxisd(config.initialRot[1], Eigen::Vector3d::UnitY())
79  * Eigen::AngleAxisd(config.initialRot[0], Eigen::Vector3d::UnitX());
80 
81  lidarToCamera = qlidarToCamera.matrix();
82 
83  std::cout << "\n\nInitial Rot" << lidarToCamera << "\n";
85  // x := x, y := -z, z := y
86 
87  cv::Mat temp_mat(config.s, CV_8UC3);
88  pcl::PointCloud <pcl::PointXYZ> retval = *(toPointsXYZ(point_cloud));
89 
90  std::vector<float> marker_info;
91 
92  for (std::vector<float>::const_iterator it = msg_rt->dof.data.begin(); it != msg_rt->dof.data.end(); ++it) {
93  marker_info.push_back(*it);
94  std::cout << *it << " ";
95  }
96  std::cout << "\n";
97 
98  bool no_error = getCorners(temp_mat, retval, config.P, config.num_of_markers, config.MAX_ITERS);
99  if (no_error) {
100  find_transformation(marker_info, config.num_of_markers, config.MAX_ITERS, lidarToCamera);
101  }
102 }
103 
104 void callback(const sensor_msgs::CameraInfoConstPtr &msg_info,
105  const sensor_msgs::PointCloud2ConstPtr &msg_pc,
106  const lidar_camera_calibration::marker_6dof::ConstPtr &msg_rt) {
107 
108  ROS_INFO_STREAM("Camera info received at " << msg_info->header.stamp.toSec());
109  ROS_INFO_STREAM("Velodyne scan received at " << msg_pc->header.stamp.toSec());
110  ROS_INFO_STREAM("marker_6dof received at " << msg_rt->header.stamp.toSec());
111 
112  float p[12];
113  float *pp = p;
114  for (boost::array<double, 12ul>::const_iterator i = msg_info->P.begin(); i != msg_info->P.end(); i++) {
115  *pp = (float) (*i);
116  pp++;
117  }
118  cv::Mat(3, 4, CV_32FC1, &p).copyTo(projection_matrix);
119 
120  // Loading Velodyne point cloud_sub
121  if (config.lidar_type == 0) // velodyne lidar
122  {
123  fromROSMsg(*msg_pc, point_cloud);
124  } else if (config.lidar_type == 1) // hesai lidar
125  {
126  fromROSMsg(*msg_pc, point_cloud_hesai);
128  }
129 
130  point_cloud = transform(point_cloud, config.initialTra[0], config.initialTra[1], config.initialTra[2],
131  config.initialRot[0], config.initialRot[1], config.initialRot[2]);
132 
133  //Rotation matrix to transform lidar point cloud to camera's frame
134 
135  qlidarToCamera = Eigen::AngleAxisd(config.initialRot[2], Eigen::Vector3d::UnitZ())
136  * Eigen::AngleAxisd(config.initialRot[1], Eigen::Vector3d::UnitY())
137  * Eigen::AngleAxisd(config.initialRot[0], Eigen::Vector3d::UnitX());
138 
139  lidarToCamera = qlidarToCamera.matrix();
140 
142  // x := x, y := -z, z := y
143 
144  cv::Mat temp_mat(config.s, CV_8UC3);
145  pcl::PointCloud <pcl::PointXYZ> retval = *(toPointsXYZ(point_cloud));
146 
147  std::vector<float> marker_info;
148 
149  for (std::vector<float>::const_iterator it = msg_rt->dof.data.begin(); it != msg_rt->dof.data.end(); ++it) {
150  marker_info.push_back(*it);
151  std::cout << *it << " ";
152  }
153  std::cout << "\n";
154 
155  getCorners(temp_mat, retval, projection_matrix, config.num_of_markers, config.MAX_ITERS);
156  find_transformation(marker_info, config.num_of_markers, config.MAX_ITERS, lidarToCamera);
157 }
158 
159 
160 int main(int argc, char **argv) {
161  readConfig();
162  ros::init(argc, argv, "find_transform");
163 
164  ros::NodeHandle n;
165 
166  if (config.useCameraInfo) {
167  ROS_INFO_STREAM("Reading CameraInfo from topic");
168  n.getParam("/lidar_camera_calibration/camera_info_topic", CAMERA_INFO_TOPIC);
169  n.getParam("/lidar_camera_calibration/velodyne_topic", VELODYNE_TOPIC);
170 
173  message_filters::Subscriber <lidar_camera_calibration::marker_6dof> rt_sub(n, "lidar_camera_calibration_rt", 1);
174 
175  std::cout << "done1\n";
176 
178  Synchronizer <MySyncPolicy> sync(MySyncPolicy(10), info_sub, cloud_sub, rt_sub);
179  sync.registerCallback(boost::bind(&callback, _1, _2, _3));
180 
181  ros::spin();
182  } else {
183  ROS_INFO_STREAM("Reading CameraInfo from configuration file");
184  n.getParam("/lidar_camera_calibration/velodyne_topic", VELODYNE_TOPIC);
185 
187  message_filters::Subscriber <lidar_camera_calibration::marker_6dof> rt_sub(n, "lidar_camera_calibration_rt", 1);
188 
190  Synchronizer <MySyncPolicy> sync(MySyncPolicy(10), cloud_sub, rt_sub);
191  sync.registerCallback(boost::bind(&callback_noCam, _1, _2));
192 
193  ros::spin();
194  }
195 
196  return EXIT_SUCCESS;
197 }
pcl::PointCloud< myPointXYZRID > intensityByRangeDiff(pcl::PointCloud< myPointXYZRID > point_cloud, config_settings config)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
void callback_noCam(const sensor_msgs::PointCloud2ConstPtr &msg_pc, const lidar_camera_calibration::marker_6dof::ConstPtr &msg_rt)
Hesai::PointCloud point_cloud_hesai
pcl::PointCloud< pcl::PointXYZ > * toPointsXYZ(pcl::PointCloud< myPointXYZRID > point_cloud)
pcl::PointCloud< myPointXYZRID >::Ptr toMyPointXYZRID(const Hesai::PointCloud &point_cloud_ptr)
ROSCPP_DECL void spin(Spinner &spinner)
pcl::PointCloud< myPointXYZRID > transform(pcl::PointCloud< myPointXYZRID > pc, float x, float y, float z, float rot_x, float rot_y, float rot_z)
pcl::PointCloud< myPointXYZRID > point_cloud
Mat projection_matrix
Connection registerCallback(C &callback)
string VELODYNE_TOPIC
int main(int argc, char **argv)
Eigen::Quaterniond qlidarToCamera
string CAMERA_INFO_TOPIC
void readConfig()
void callback(const sensor_msgs::CameraInfoConstPtr &msg_info, const sensor_msgs::PointCloud2ConstPtr &msg_pc, const lidar_camera_calibration::marker_6dof::ConstPtr &msg_rt)
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const
Eigen::Matrix3d lidarToCamera
config
bool getCorners(cv::Mat img, pcl::PointCloud< pcl::PointXYZ > scan, cv::Mat P, int num_of_markers, int MAX_ITERS)
Definition: Corners.cpp:31
void find_transformation(std::vector< float > marker_info, int num_of_marker_in_config, int MAX_ITERS, Eigen::Matrix3d lidarToCamera)
Definition: Find_RT.h:289


lidar_camera_calibration
Author(s):
autogenerated on Sat Feb 6 2021 03:39:37