$search
00001 00061 //################## 00062 //#### includes #### 00063 00064 // standard includes 00065 //-- 00066 00067 // ROS includes 00068 #include <ros/ros.h> 00069 #include <tf/transform_listener.h> 00070 #include <visualization_msgs/Marker.h> 00071 #include <cob_3d_mapping_msgs/GetFieldOfView.h> 00072 #include <Eigen/Core> 00073 //#include "cob_3d_mapping_common/reconfigureable_node.h" 00074 #include <dynamic_reconfigure/server.h> 00075 #include <cob_3d_mapping_common/field_of_viewConfig.h> 00076 00077 // external includes 00078 #include <boost/timer.hpp> 00079 00080 00081 using namespace tf; 00082 00083 //#################### 00084 //#### node class #### 00085 class FieldOfView 00086 { 00087 public: 00088 // Constructor 00089 FieldOfView() 00090 { 00091 config_server_.setCallback(boost::bind(&FieldOfView::dynReconfCallback, this, _1, _2)); 00092 fov_marker_pub_ = n_.advertise<visualization_msgs::Marker>("fov_marker",10); 00093 get_fov_srv_ = n_.advertiseService("get_fov", &FieldOfView::srvCallback_GetFieldOfView, this); 00094 /*setSensorFoV_hor(cob_3d_mapping_common::field_of_viewConfig::__getDefault__().sensor_fov_hor_angle); 00095 setSensorFoV_ver(cob_3d_mapping_common::field_of_viewConfig::__getDefault__().sensor_fov_ver_angle); 00096 setSensorMaxRange(cob_3d_mapping_common::field_of_viewConfig::__getDefault__().sensor_max_range);*/ 00097 //camera_frame_ = std::string(/*"/base_kinect_rear_link"*/"/head_cam3d_link"); 00098 computeFieldOfView(); 00099 00100 //setReconfigureCallback2(boost::bind(&callback, this, _1, _2), boost::bind(&callback_get, this, _1)); 00101 } 00102 00103 00104 // Destructor 00105 ~FieldOfView() 00106 { 00108 } 00109 00110 void dynReconfCallback(cob_3d_mapping_common::field_of_viewConfig &config, uint32_t level) 00111 { 00112 setSensorFoV_hor(config.sensor_fov_hor_angle); 00113 setSensorFoV_ver(config.sensor_fov_ver_angle); 00114 setSensorMaxRange(config.sensor_max_range); 00115 camera_frame_ = config.camera_frame; 00116 00117 //new settings -> recalculate 00118 computeFieldOfView(); 00119 } 00120 00132 /*static void callback(FieldOfView *fov, cob_3d_mapping_common::field_of_viewConfig &config, uint32_t level) 00133 { 00134 if(!fov) 00135 return; 00136 00137 boost::mutex::scoped_lock l(fov->m_mutex); 00138 00139 if(level&1) //hor changed 00140 fov->setSensorFoV_hor(config.sensor_fov_hor_angle); 00141 if(level&2) //ver changed 00142 fov->setSensorFoV_ver(config.sensor_fov_ver_angle); 00143 if(level&4) //range changed 00144 fov->setSensorMaxRange(config.sensor_max_range); 00145 00146 //new settings -> recalculate 00147 fov->computeFieldOfView(); 00148 } 00149 00150 // callback for dynamic reconfigure 00151 static void callback_get(FieldOfView *fov, cob_3d_mapping_common::field_of_viewConfig &config) 00152 { 00153 if(!fov) 00154 return; 00155 00156 boost::mutex::scoped_lock l(fov->m_mutex); 00157 00158 config.sensor_fov_hor_angle = fov->sensor_fov_hor_; 00159 config.sensor_fov_ver_angle = fov->sensor_fov_ver_; 00160 config.sensor_max_range = fov->sensor_max_range_; 00161 }*/ 00162 00163 void setSensorFoV_hor(double val) { 00164 sensor_fov_hor_ = val*M_PI/180; 00165 } 00166 00167 void setSensorFoV_ver(double val) { 00168 sensor_fov_ver_ = val*M_PI/180; 00169 } 00170 00171 void setSensorMaxRange(double val) { 00172 sensor_max_range_ = val; 00173 } 00174 00182 void computeFieldOfView() 00183 { 00184 //don't lock 00185 double fovHorFrac = sensor_fov_hor_/2; 00186 double fovVerFrac = sensor_fov_ver_/2; 00187 00188 vec_a_(0) = tan(fovHorFrac)*sensor_max_range_; 00189 vec_a_(1) = -tan(fovVerFrac)*sensor_max_range_; 00190 vec_a_(2) = sensor_max_range_; 00191 00192 vec_b_(0) = -vec_a_(0); 00193 vec_b_(1) = vec_a_(1); 00194 vec_b_(2) = sensor_max_range_; 00195 00196 vec_c_(0) = -vec_a_(0); 00197 vec_c_(1) = -vec_a_(1); 00198 vec_c_(2) = sensor_max_range_; 00199 00200 vec_d_(0) = vec_a_(0); 00201 vec_d_(1) = -vec_a_(1); 00202 vec_d_(2) = sensor_max_range_; 00203 00204 /*std::cout << "a: " << vec_a << std::endl; 00205 std::cout << "b: " << vec_b << std::endl; 00206 std::cout << "c: " << vec_c << std::endl; 00207 std::cout << "d: " << vec_d << std::endl;*/ 00208 00210 n_up_ = tf::Point(0,vec_a_(2)*vec_b_(0) - vec_a_(0)*vec_b_(2),vec_a_(0)*vec_b_(1) - vec_a_(1)*vec_b_(0)); 00211 00213 n_down_ = tf::Point(0, -n_up_[1], n_up_[2]); 00214 00216 n_left_ = tf::Point(vec_b_(1)*vec_c_(2) - vec_b_(2)*vec_c_(1), 0, vec_b_(0)*vec_c_(1) - vec_b_(1)*vec_c_(0)); 00217 00219 n_right_ = tf::Point(-n_left_[0], 0, n_left_[2]); 00220 00221 /*std::cout << "n_up: " << n_up << std::endl; 00222 std::cout << "n_down: " << n_down << std::endl; 00223 std::cout << "n_right: " << n_right << std::endl; 00224 std::cout << "n_left: " << n_left << std::endl;*/ 00225 00226 } 00227 00228 00239 void transformFOV(ros::Time stamp, std::string target_frame) 00240 { 00241 //std::string target_frame = std::string("/map"); 00242 StampedTransform transform; 00243 try 00244 { 00245 tf_listener_.waitForTransform(target_frame, camera_frame_, stamp, ros::Duration(0.1)); 00246 tf_listener_.lookupTransform(target_frame, camera_frame_, stamp/*ros::Time(0)*/, transform); 00247 //tf::Point n_up(n_up_(0),n_up_(1),n_up_(2)); 00248 tf::Stamped<tf::Point> stamped_n_up(n_up_,transform.stamp_,camera_frame_); 00249 tf::Stamped<tf::Point> stamped_n_up_t; 00250 try{ 00251 tf_listener_.transformPoint(target_frame,stamped_n_up, stamped_n_up_t); 00252 } 00253 catch (tf::TransformException ex){ 00254 ROS_ERROR("%s",ex.what()); 00255 } 00256 00257 n_up_t_[0] = stamped_n_up_t.x(); 00258 n_up_t_[1] = stamped_n_up_t.y(); 00259 n_up_t_[2] = stamped_n_up_t.z(); 00260 00261 //tf::Point n_down(n_down_(0),n_down_(1),n_down_(2)); 00262 tf::Stamped<tf::Point> stamped_n_down(n_down_,transform.stamp_,camera_frame_); 00263 tf::Stamped<tf::Point> stamped_n_down_t; 00264 try{ 00265 tf_listener_.transformPoint(target_frame,stamped_n_down, stamped_n_down_t); 00266 } 00267 catch (tf::TransformException ex){ 00268 ROS_ERROR("%s",ex.what()); 00269 } 00270 n_down_t_[0] = stamped_n_down_t.x(); 00271 n_down_t_[1] = stamped_n_down_t.y(); 00272 n_down_t_[2] = stamped_n_down_t.z(); 00273 00274 //tf::Point n_right(n_right_(0),n_right_(1),n_right_(2)); 00275 tf::Stamped<tf::Point> stamped_n_right(n_right_,transform.stamp_,camera_frame_); 00276 tf::Stamped<tf::Point> stamped_n_right_t; 00277 try{ 00278 tf_listener_.transformPoint(target_frame,stamped_n_right, stamped_n_right_t); 00279 } 00280 catch (tf::TransformException ex){ 00281 ROS_ERROR("%s",ex.what()); 00282 } 00283 n_right_t_[0] = stamped_n_right_t.x(); 00284 n_right_t_[1] = stamped_n_right_t.y(); 00285 n_right_t_[2] = stamped_n_right_t.z(); 00286 00287 //tf::Point n_left(n_left_(0),n_left_(1),n_left_(2)); 00288 tf::Stamped<tf::Point> stamped_n_left(n_left_,transform.stamp_,camera_frame_); 00289 tf::Stamped<tf::Point> stamped_n_left_t; 00290 try{ 00291 tf_listener_.transformPoint(target_frame,stamped_n_left, stamped_n_left_t); 00292 } 00293 catch (tf::TransformException ex){ 00294 ROS_ERROR("%s",ex.what()); 00295 } 00296 n_left_t_[0] = stamped_n_left_t.x(); 00297 n_left_t_[1] = stamped_n_left_t.y(); 00298 n_left_t_[2] = stamped_n_left_t.z(); 00299 00300 tf::Point n_origin(0,0,0); 00301 tf::Stamped<tf::Point> stamped_n_origin(n_origin,transform.stamp_,camera_frame_); 00302 tf::Stamped<tf::Point> stamped_n_origin_t; 00303 try{ 00304 tf_listener_.transformPoint(target_frame,stamped_n_origin, stamped_n_origin_t); 00305 } 00306 catch (tf::TransformException ex){ 00307 ROS_ERROR("%s",ex.what()); 00308 } 00309 n_origin_t_[0] = stamped_n_origin_t.x(); 00310 n_origin_t_[1] = stamped_n_origin_t.y(); 00311 n_origin_t_[2] = stamped_n_origin_t.z(); 00312 00313 visualization_msgs::Marker marker = generateMarker(target_frame, transform.stamp_); 00314 fov_marker_pub_.publish(marker); 00315 } 00316 catch (tf::TransformException ex) 00317 { 00318 ROS_ERROR("%s",ex.what()); 00319 } 00320 } 00321 00322 00333 visualization_msgs::Marker generateMarker(std::string& target_frame, ros::Time& stamp) 00334 { 00335 tf::Pose marker_pose(btQuaternion(0,0,0,1),btVector3(0,0.5,0)); 00336 tf::Stamped<tf::Pose> stamped_marker_pose(marker_pose, stamp, camera_frame_); 00337 tf::Stamped<tf::Pose> stamped_marker_pose_t; 00338 try{ 00339 tf_listener_.transformPose(target_frame, stamped_marker_pose, stamped_marker_pose_t); 00340 } 00341 catch (tf::TransformException ex){ 00342 ROS_ERROR("%s",ex.what()); 00343 } 00344 visualization_msgs::Marker marker; 00345 marker.header.frame_id = camera_frame_;//target_frame; 00346 marker.header.stamp = stamp; 00347 geometry_msgs::Pose pose_msg; 00348 tf::poseTFToMsg(stamped_marker_pose_t, pose_msg); 00349 //marker.pose = pose_msg; 00350 marker.action = visualization_msgs::Marker::ADD; 00351 marker.type = visualization_msgs::Marker::LINE_LIST; 00352 marker.lifetime = ros::Duration(); 00353 marker.scale.x = 0.01; 00354 marker.points.resize(16); 00355 00356 00357 marker.points[0].x = 0; 00358 marker.points[0].y = 0; 00359 marker.points[0].z = 0; 00360 00361 marker.points[1].x = vec_a_(0); 00362 marker.points[1].y = vec_a_(1); 00363 marker.points[1].z = vec_a_(2); 00364 00365 marker.points[2].x = 0; 00366 marker.points[2].y = 0; 00367 marker.points[2].z = 0; 00368 00369 marker.points[3].x = vec_b_(0); 00370 marker.points[3].y = vec_b_(1); 00371 marker.points[3].z = vec_b_(2); 00372 00373 marker.points[4].x = 0; 00374 marker.points[4].y = 0; 00375 marker.points[4].z = 0; 00376 00377 marker.points[5].x = vec_c_(0); 00378 marker.points[5].y = vec_c_(1); 00379 marker.points[5].z = vec_c_(2); 00380 00381 marker.points[6].x = 0; 00382 marker.points[6].y = 0; 00383 marker.points[6].z = 0; 00384 00385 marker.points[7].x = vec_d_(0); 00386 marker.points[7].y = vec_d_(1); 00387 marker.points[7].z = vec_d_(2); 00388 00389 marker.points[8].x = vec_a_(0); 00390 marker.points[8].y = vec_a_(1); 00391 marker.points[8].z = vec_a_(2); 00392 00393 marker.points[9].x = vec_b_(0); 00394 marker.points[9].y = vec_b_(1); 00395 marker.points[9].z = vec_b_(2); 00396 00397 marker.points[10].x = vec_b_(0); 00398 marker.points[10].y = vec_b_(1); 00399 marker.points[10].z = vec_b_(2); 00400 00401 marker.points[11].x = vec_c_(0); 00402 marker.points[11].y = vec_c_(1); 00403 marker.points[11].z = vec_c_(2); 00404 00405 marker.points[12].x = vec_c_(0); 00406 marker.points[12].y = vec_c_(1); 00407 marker.points[12].z = vec_c_(2); 00408 00409 marker.points[13].x = vec_d_(0); 00410 marker.points[13].y = vec_d_(1); 00411 marker.points[13].z = vec_d_(2); 00412 00413 marker.points[14].x = vec_d_(0); 00414 marker.points[14].y = vec_d_(1); 00415 marker.points[14].z = vec_d_(2); 00416 00417 marker.points[15].x = vec_a_(0); 00418 marker.points[15].y = vec_a_(1); 00419 marker.points[15].z = vec_a_(2); 00420 00421 marker.color.r = 1.0; 00422 marker.color.g = 0.0; 00423 marker.color.b = 0.0; 00424 marker.color.a = 1.0; 00425 00426 return marker; 00427 } 00428 00429 00430 00441 bool srvCallback_GetFieldOfView(cob_3d_mapping_msgs::GetFieldOfView::Request &req, 00442 cob_3d_mapping_msgs::GetFieldOfView::Response &res) 00443 { 00444 boost::mutex::scoped_lock l(m_mutex); 00445 00446 //ROS_INFO("FieldOfView Trigger"); 00447 transformFOV(req.stamp, req.target_frame); 00448 geometry_msgs::Point n_msg; 00449 pointTFToMsg(n_up_t_, n_msg); 00450 res.fov.points.push_back(n_msg); 00451 pointTFToMsg(n_down_t_, n_msg); 00452 res.fov.points.push_back(n_msg); 00453 pointTFToMsg(n_right_t_, n_msg); 00454 res.fov.points.push_back(n_msg); 00455 pointTFToMsg(n_left_t_, n_msg); 00456 res.fov.points.push_back(n_msg); 00457 pointTFToMsg(n_origin_t_, n_msg); 00458 res.fov.points.push_back(n_msg); 00459 00460 return true; 00461 } 00462 00463 protected: 00464 ros::NodeHandle n_; 00465 ros::Subscriber point_cloud_sub_; 00466 ros::Publisher point_cloud_pub_; 00467 ros::Publisher fov_marker_pub_; 00468 ros::ServiceServer get_fov_srv_; 00469 ros::Time stamp_; 00470 00471 TransformListener tf_listener_; 00472 dynamic_reconfigure::Server<cob_3d_mapping_common::field_of_viewConfig> config_server_; 00473 00474 double sensor_fov_hor_; 00475 double sensor_fov_ver_; 00476 double sensor_max_range_; 00477 std::string camera_frame_; 00478 00479 Eigen::Vector3d vec_a_; 00480 Eigen::Vector3d vec_b_; 00481 Eigen::Vector3d vec_c_; 00482 Eigen::Vector3d vec_d_; 00483 00484 tf::Point n_up_; 00485 tf::Point n_down_; 00486 tf::Point n_right_; 00487 tf::Point n_left_; 00488 00489 tf::Point n_up_t_; 00490 tf::Point n_down_t_; 00491 tf::Point n_right_t_; 00492 tf::Point n_left_t_; 00493 tf::Point n_origin_t_; 00494 00495 boost::mutex m_mutex; 00496 00497 }; 00498 00499 00500 int main (int argc, char** argv) 00501 { 00502 ros::init (argc, argv, "field_of_view"); 00503 00504 FieldOfView fov; 00505 00506 ros::Rate loop_rate(10); 00507 while (ros::ok()) 00508 { 00509 ros::spinOnce (); 00510 //fov.transformNormals(); 00511 loop_rate.sleep(); 00512 } 00513 } 00514 00515 00516