ISAAC  0.2.11
Flight software for the ISAAC project, adding functionality to the Astrobee robot, operating inside the International Space Station.
All Classes Functions Variables Pages
camera_projection.h
1 /* Copyright (c) 2021, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The "ISAAC - Integrated System for Autonomous and Adaptive Caretaking
7  * platform" software is licensed under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with the
9  * License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16  * License for the specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef INSPECTION_CAMERA_PROJECTION_H_
21 #define INSPECTION_CAMERA_PROJECTION_H_
22 
23 // Standard ROS includes
24 #include <nodelet/nodelet.h>
25 #include <pluginlib/class_list_macros.h>
26 #include <ros/ros.h>
27 
28 // TF2 support
29 #include <tf2_ros/transform_listener.h>
30 #include <tf2_geometry_msgs/tf2_geometry_msgs.h>
31 
32 // Shared project includes
33 #include <ff_common/ff_names.h>
34 #include <msg_conversions/msg_conversions.h>
35 #include <config_reader/config_reader.h>
36 #include <camera/camera_model.h>
37 #include <isaac_util/isaac_names.h>
38 
39 // Point Cloud
40 #include <sensor_msgs/PointCloud2.h>
41 #include <sensor_msgs/point_cloud_conversion.h>
42 
43 // Software messages
44 #include <visualization_msgs/MarkerArray.h>
45 #include <geometry_msgs/PoseArray.h>
46 
47 // Eigen for math
48 #include <Eigen/Dense>
49 
50 // C++ headers
51 #include <vector>
52 #include <string>
53 #include <map>
54 
58 namespace inspection {
59 
60 /*
61  This class provides camera functionality that allows us to project the 3D point into
62  the camera frame and the other way around. It automatically reads the camera parameters
63  from the config files based on the camera name, such that no setup is necessary.
64 */
65 class CameraView : public camera::CameraModel {
66  public:
67  // Constructor for Camera View taking in inputs:
68  // cam_name: name of the camera that it used to read from config file the parameters
69  // f: far clip, maximum camera distance (m)
70  // n: near clip, minimum camera distance (m)
71  // cam_transform: transform from body->camera, useful for offline applications where tf is not available
72  CameraView(const std::string& cam_name, const camera::CameraParameters& params, const float f = 2.0,
73  const float n = 0.19, const geometry_msgs::Transform::ConstPtr cam_transform = NULL);
74 
75  // Gets the points x y where the point is in the image. If outside the image, then it will return false
76  // If the robot pose is not specified, it's considered to be the current one
77  bool GetCamXYFromPoint(const Eigen::Affine3d robot_pose, const Eigen::Vector3d point, int &x, int &y);
78  bool GetCamXYFromPoint(const Eigen::Vector3d point, int &x, int &y);
79 
80  // Get 3D point from camera pixel location and point cloud
81  bool GetPointFromXYD(const sensor_msgs::PointCloud2 pCloud, const int u, const int v, geometry_msgs::Point &point);
82 
83  // Get the distance from the camera to the target using depth camera information
84  double GetDistanceFromTarget(const geometry_msgs::Pose point, std::string depth_cam_name,
85  double size_x, double size_y);
86 
87  // Draw the camera frustum using a marker array for rviz visualization
88  void DrawCameraFrustum(const geometry_msgs::Pose robot_pose, ros::Publisher &publisher);
89 
90  bool debug_ = false;
91  float f_;
92  float n_;
93 
94  protected:
95  // Checks if a point is inside a poligon, in this case with 4 sides.
96  bool InsideTarget(std::vector<int> vert_x, std::vector<int> vert_y, int test_x, int test_y);
97  // Define the projection matrix based on camera parameters using the pinhole model
98  // bool SetProjectionMatrix(Eigen::Matrix3d cam_mat);
99 
100  private:
101  std::string cam_name_;
102  config_reader::ConfigReader cfg_cam_;
103  int W_, H_;
104  float fx_, fy_;
105  Eigen::Matrix4d P_;
106 
107  tf2_ros::Buffer tf_buffer_;
108  std::shared_ptr<tf2_ros::TransformListener> tf_listener_;
109 
110  Eigen::Affine3d tf_body_to_cam_;
111 
112  public:
113  // This fixes the Eigen aligment issue
114  // http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html
115  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
116 };
117 
118 
119 } // namespace inspection
120 #endif // INSPECTION_CAMERA_PROJECTION_H_
inspection::CameraView
Definition: camera_projection.h:65