tracking.cpp
Go to the documentation of this file.
1 /*
2  * ar_marker_processor.hpp
3  *
4  * LICENSE : BSD - https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
5  *
6  * Created on: Apr 6, 2013
7  * Author: jorge
8  * Modified on: Dec, 2013
9  * Jihoon Lee
10  */
11 
13 
14 namespace yocs
15 {
16 
18  init();
19 }
20 
22 
24 {
25  ros::NodeHandle nh, pnh("~");
26 
27  // Parameters
35 
36  if (nh.getParam("ar_track_alvar/max_frequency", ar_tracker_freq_) == false)
37  {
39  ROS_WARN("Cannot get AR tracker frequency; using default value (%f)", ar_tracker_freq_);
40  ROS_WARN("Confidence evaluation can get compromised if this is not the right value!");
41  }
42 
43  sub_ar_markers_ = nh.subscribe("ar_track_alvar/ar_pose_marker", 1, &ARMarkerTracking::arPoseMarkersCB, this);
44 
45  // There are 18 different markers
47 
48  return true;
49 }
50 
51 void ARMarkerTracking::arPoseMarkersCB(const ar_track_alvar_msgs::AlvarMarkers::ConstPtr& msg)
52 {
53  // TODO: use confidence to incorporate covariance to global poses
54  std::stringstream ss;
55  for (unsigned int i = 0; i < msg->markers.size(); i++) {
56  ss << " " << msg->markers[i].id;
57  }
58  ss << " ";
59  if(msg->markers.size() > 0) {
60  ROS_DEBUG_STREAM("AR Marker Tracking : received markers [" << ss.str() << "]");
61  }
62 
63  // Maintain markers
65  spotted_markers_ = *msg;
67 }
68 
69 void ARMarkerTracking::maintainTrackedMarkers(const ar_track_alvar_msgs::AlvarMarkers::ConstPtr& msg,std::vector<TrackedMarker>& tracked_markers)
70 {
71  // Make thresholds relative to the tracking frequency (as it can be dynamically changed)
72  int obs_list_max_size = (int)round(max_tracking_time_*ar_tracker_freq_);
73  double max_valid_d_inc = max_valid_d_inc_ / ar_tracker_freq_;
74  double max_valid_h_inc = max_valid_h_inc_ / ar_tracker_freq_;
75 
76  for (unsigned int i = 0; i < msg->markers.size(); i++)
77  {
78  if (msg->markers[i].id >= tracked_markers.size())
79  {
80  // A recognition error from Alvar markers tracker
81  ROS_WARN("Discarding AR marker with unrecognized id (%d)", msg->markers[i].id);
82  continue;
83  }
84  //ROS_INFO("Maintaining marker id = %d",msg->markers[i].id);
85 
86  TrackedMarker& marker = tracked_markers[msg->markers[i].id];
87 
88  // Confidence evaluation
89  maintainTrackedMarker(marker, msg->markers[i], obs_list_max_size, max_valid_d_inc, max_valid_h_inc);
90  }
91 }
92 
93 
94 void ARMarkerTracking::maintainTrackedMarker(TrackedMarker& marker,const ar_track_alvar_msgs::AlvarMarker& msgMarker, const int obs_list_max_size, const double max_valid_d_inc, const double max_valid_h_inc)
95 {
96  marker.distance = mtk::distance3D(msgMarker.pose.pose);
97  marker.distance2d = mtk::distance2D(msgMarker.pose.pose.position.x, msgMarker.pose.pose.position.z, 0,0);
98  marker.heading = tf::getYaw(msgMarker.pose.pose.orientation) + M_PI/2.0;
99 
100  int position = 0;
101  ros::Time now = ros::Time::now();
102  geometry_msgs::PoseStamped prev = msgMarker.pose;
103  for (ObsList::iterator it = marker.obs_list_.begin(); it != marker.obs_list_.end(); ++it)
104  {
105  double age = (now - it->header.stamp).toSec();
106  if (age > ((position + 1)/ar_tracker_freq_) + 1.5)
107  {
108  int s0 = marker.obs_list_.size();
109  marker.obs_list_.erase(it, marker.obs_list_.end());
110  int s1 = marker.obs_list_.size();
111  ROS_DEBUG("%d observations discarded (first one with position %d in the list) for being %f seconds old ( > %f)", s0 - s1, position, age, ((position + 1)/ar_tracker_freq_) + 1.5);
112  break;
113  }
114 
115  if ((mtk::distance3D(prev.pose, it->pose) > max_valid_d_inc) || (std::abs(mtk::minAngle(prev.pose, it->pose)) > max_valid_h_inc))
116  {
117  // Incoherent observation; stop going over the list and use current position value to fill confidence values
118  ROS_DEBUG("%d BREAK at %d %f %f %f %f %f", msgMarker.id, position, mtk::distance3D(prev.pose, it->pose), mtk::minAngle(prev.pose, it->pose), max_valid_d_inc, max_valid_h_inc, ar_tracker_freq_);
119  break;
120  }
121 
122  prev = *it;
123  position++;
124  }
125 
126  marker.conf_distance = marker.distance <= min_penalized_dist_ ? 1.0
127  : marker.distance >= max_reliable_dist_ ? 0.0
128  : 1.0 - std::pow((marker.distance - min_penalized_dist_) / (max_reliable_dist_ - min_penalized_dist_), 2);
129  marker.conf_heading = std::abs(marker.heading) <= min_penalized_head_ ? 1.0
130  : std::abs(marker.heading) >= max_reliable_head_ ? 0.0
131  : 1.0 - std::pow((std::abs(marker.heading) - min_penalized_head_) / (max_reliable_head_ - min_penalized_head_), 2);
132  marker.stability = marker.obs_list_.size() == 0 ? 0.0 : std::sqrt((double)position/(double)marker.obs_list_.size());
133  marker.persistence = std::sqrt((double)marker.obs_list_.size() / (double)obs_list_max_size);
134  marker.confidence = marker.conf_distance * marker.stability * marker.persistence; // * marker.conf_heading I don't understand how conf_heading works. it always <=-1.4 or 1.4 >= disable it until becomes necessary
135  marker.obs_list_.insert(marker.obs_list_.begin(), msgMarker.pose);
136  marker.obs_list_.begin()->header = msgMarker.header; // bloody alvar tracker doesn't fill pose's header
137  if (marker.obs_list_.size() > obs_list_max_size)
138  marker.obs_list_.pop_back();
139 
140  ROS_DEBUG_STREAM(msgMarker.id << "\n" << marker);
141 }
142 
144 {
145  ros::spin();
146 // // Broadcasts the spotted ar markers tf
147 // if (tf_broadcast_freq_ > 0.0)
148 // {
149 // ros::Rate rate(tf_broadcast_freq_);
150 //
151 // while(ros::ok())
152 // {
153 // rate.sleep();
154 // ros::spinOnce();
155 // }
156 // }
157 // else {
158 // while(ros::ok())
159 // {
160 // ros::Duration(0.5).sleep();
161 // ros::spinOnce();
162 // }
163 // }
164 }
165 
166 } // yocs
void maintainTrackedMarkers(const ar_track_alvar_msgs::AlvarMarkers::ConstPtr &msg, std::vector< TrackedMarker > &tracked_markers)
Definition: tracking.cpp:69
virtual ~ARMarkerTracking()
Definition: tracking.cpp:21
void maintainTrackedMarker(TrackedMarker &marker, const ar_track_alvar_msgs::AlvarMarker &msgMarker, const int obs_list_max_size, const double max_valid_d_inc, const double max_valid_h_inc)
Definition: tracking.cpp:94
ar_track_alvar_msgs::AlvarMarkers spotted_markers_
Definition: tracking.hpp:175
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
static double getYaw(const Quaternion &bt_q)
std::vector< TrackedMarker > tracked_markers_
Definition: tracking.hpp:174
#define ROS_WARN(...)
void arPoseMarkersCB(const ar_track_alvar_msgs::AlvarMarkers::ConstPtr &msg)
Definition: tracking.cpp:51
ROSCPP_DECL void spin(Spinner &spinner)
double distance2D(double ax, double ay, double bx, double by)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
#define ROS_DEBUG_STREAM(args)
ros::Subscriber sub_ar_markers_
Definition: tracking.hpp:176
virtual void customCB(const ar_track_alvar_msgs::AlvarMarkers &spotted_markers, const std::vector< TrackedMarker > &tracked_markers)
Definition: tracking.hpp:136
bool getParam(const std::string &key, std::string &s) const
static Time now()
double minAngle(geometry_msgs::Quaternion a, geometry_msgs::Quaternion b)
double distance3D(double ax, double ay, double az, double bx, double by, double bz)
#define ROS_DEBUG(...)


yocs_ar_marker_tracking
Author(s): Daniel Stonier, Jorge Santos
autogenerated on Mon Jun 10 2019 15:53:43