client.cpp
Go to the documentation of this file.
00001 
00002 #include "yocs_ar_pair_tracking/client.hpp"
00003 
00004 namespace yocs {
00005 
00006 ARPairTrackingClient::ARPairTrackingClient()
00007 {
00008   ros::NodeHandle nh("");
00009   global_frame_ = std::string("map");
00010   global_marker_prefix_ = std::string("global_marker");
00011   target_frame_postfix_ = std::string("target");
00012 
00013   pub_update_ar_pair_ = nh.advertise<yocs_msgs::ARPairList>("update_ar_pairs", 1, true); 
00014   sub_global_marker_  = nh.subscribe<ar_track_alvar_msgs::AlvarMarkers>("global_markers", 1, &ARPairTrackingClient::globalMarkersCB, this);
00015 
00016   tf_broadcast_freq_ = 5.0;
00017   boost::thread(&ARPairTrackingClient::broadcastMarkersTF, this);
00018  
00019   baseline_ = 0.26;
00020   target_offset_ = 0.5;
00021 }
00022 
00023 ARPairTrackingClient::~ARPairTrackingClient()
00024 {
00025 }
00026 
00027 void ARPairTrackingClient::globalMarkersCB(const ar_track_alvar_msgs::AlvarMarkers::ConstPtr& msg)
00028 {
00029   global_markers_ = *msg;
00030   createMirrorMarkers();
00031 
00032   ROS_INFO("%lu global marker pose(s) received", global_markers_.markers.size());
00033 
00034   notifyARPairTracker();
00035 }
00036 
00037 void ARPairTrackingClient::createMirrorMarkers()
00038 {
00039   for (unsigned int i = 0; i < global_markers_.markers.size(); i++)
00040   {
00041     // Compensate the vertical alignment of markers and put at ground level to adopt navistack goals format
00042     tf::Transform tf(tf::createQuaternionFromYaw(tf::getYaw(global_markers_.markers[i].pose.pose.orientation) - M_PI/2.0),
00043                      tf::Vector3(global_markers_.markers[i].pose.pose.position.x, global_markers_.markers[i].pose.pose.position.y, 0.0));
00044     tf::StampedTransform marker_gb(tf, ros::Time::now(), global_frame_, "KKKK");
00045 
00046     // Half turn and translate to put goal at some distance in front of the marker
00047     tf::Transform in_front(tf::createQuaternionFromYaw(M_PI),
00048     tf::Vector3(1.0, 0.0, 0.0));
00049     marker_gb *= in_front;
00050 
00051     ar_track_alvar_msgs::AlvarMarker kk; 
00052     mtk::tf2pose(marker_gb, kk.pose);
00053     global_markers_mirrors_.markers.push_back(kk);
00054 
00055     ROS_DEBUG("Marker %d: %s", global_markers_.markers[i].id, mtk::pose2str(global_markers_.markers[i].pose.pose));
00056 
00057     // TODO: In the current annotation design, it is impossible to record both left and right id of ar marker. 
00058   }
00059 }
00060 
00061 void ARPairTrackingClient::notifyARPairTracker()
00062 {
00063   unsigned int i;
00064   yocs_msgs::ARPairList l;
00065 
00066   for(i = 0; i < global_markers_.markers.size(); i ++)
00067   {
00068     ar_track_alvar_msgs::AlvarMarker m = global_markers_.markers[i];
00069     yocs_msgs::ARPair p;
00070     char frame[32];
00071     sprintf(frame, "%s_%d_%s",global_marker_prefix_.c_str(), m.id, target_frame_postfix_.c_str());
00072     
00073     p.left_id = m.id;
00074     p.right_id = m.id - 3;
00075     p.baseline = baseline_;
00076     p.target_offset = target_offset_;
00077     p.target_frame = frame; 
00078 
00079     l.pairs.push_back(p);
00080   }
00081   
00082   pub_update_ar_pair_.publish(l);
00083 }
00084 
00085 
00086 void ARPairTrackingClient::broadcastMarkersTF()
00087 {
00088   ros::Rate rate(tf_broadcast_freq_);
00089 
00090   // TODO semantic map is not broadcasting this already?  only docking base no
00091   while (ros::ok())
00092   {
00093     publishMarkerTFs(global_marker_prefix_,global_markers_);
00094     publishTargetTFs(global_marker_prefix_,global_markers_);
00095     publishMarkerTFs("mirror",global_markers_mirrors_);
00096     rate.sleep();
00097   }
00098 
00099 }
00100 
00101 void ARPairTrackingClient::publishTargetTFs(const std::string prefix, const ar_track_alvar_msgs::AlvarMarkers& markers)
00102 {
00103   char parent_frame[32];
00104   char child_frame[32];
00105   tf::StampedTransform tf;
00106   tf.stamp_ = ros::Time::now();
00107 
00108   for (unsigned int i = 0; i <markers.markers.size(); i++)
00109   {
00110     sprintf(parent_frame, "%s_%d", prefix.c_str(), markers.markers[i].id);
00111     sprintf(child_frame, "%s_%s", parent_frame, target_frame_postfix_.c_str());
00112 
00113     tf::Transform tf(tf::createQuaternionFromRPY(M_PI,0,0),
00114                      tf::Vector3(0, 0, target_offset_));
00115     tf::StampedTransform target(tf, ros::Time::now(), parent_frame, child_frame);
00116 
00117     tf_brcaster_.sendTransform(target);
00118   }
00119 }
00120 
00121 void ARPairTrackingClient::publishMarkerTFs(const std::string prefix, const ar_track_alvar_msgs::AlvarMarkers& markers)
00122 {
00123   char child_frame[32];
00124   tf::StampedTransform tf;
00125   tf.stamp_ = ros::Time::now();
00126 
00127   for (unsigned int i = 0; i <markers.markers.size(); i++)
00128   {
00129     sprintf(child_frame, "%s_%d", prefix.c_str(), markers.markers[i].id);
00130     mtk::pose2tf(markers.markers[i].pose, tf);
00131     tf.child_frame_id_ = child_frame;
00132     tf.stamp_ = ros::Time::now();
00133     tf_brcaster_.sendTransform(tf);
00134   }
00135 }
00136 
00137 
00138 
00139 }
00140 
00141 int main(int argc, char** argv)
00142 {
00143   ros::init(argc, argv, "ar_pair_client");
00144   yocs::ARPairTrackingClient client;
00145 
00146   ros::spin();
00147 }


yocs_ar_pair_tracking
Author(s): Daniel Stonier, Jihoon Lee, Jorge Santos Simon
autogenerated on Thu Jun 6 2019 21:47:30