-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint_cloud_tracker.cpp
255 lines (254 loc) · 11.5 KB
/
point_cloud_tracker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "pcl_tutorial_ros/point_cloud_tracker.h"
namespace pct {
void PointCloudTracker::addVector2UnitVector4PlaneAngle ( const geometry_msgs::Point& vec_a, const geometry_msgs::Point& vec_b,
const std::string &plane, geometry_msgs::Point* added_vec ) {
double angle;
geometry_msgs::Point uni_vector;
if ( plane == "xy" ) {
angle = atan2 ( vec_a.z - vec_b.z, vec_a.x - vec_b.x );
uni_vector.x += cos ( angle );
uni_vector.y += sin ( angle );
} else if ( plane == "yz" ) {
angle = atan2 ( vec_a.y - vec_b.y, vec_a.x - vec_b.x );
uni_vector.x += cos ( angle );
uni_vector.y += sin ( angle );
} else {
ROS_ERROR( "Please set the plane correctly" );
}
added_vec->x += uni_vector.x;
added_vec->y += uni_vector.y;
return;
}
geometry_msgs::Point PointCloudTracker::clacAverageSpatialVectorOrientation ( geometry_msgs::Point& vec_xy, geometry_msgs::Point& vec_yz, const double length ) {
geometry_msgs::Point vec_orien;
vec_xy.x = vec_xy.x / length;
vec_xy.y = vec_xy.y / length;
vec_yz.x = vec_yz.x / length;
vec_yz.y = vec_yz.y / length;
vec_orien.y = std::atan2 ( vec_xy.y, vec_xy.x );
vec_orien.z = std::atan2 ( vec_yz.y, vec_yz.x );
return vec_orien;
}
PointCloudTracker::PointCloudTracker ( ) {
// ParticleFilterTracker :
std::vector<double> default_step_covariance = std::vector<double> (6, 0.015 * 0.015);
default_step_covariance[3] *= 40.0;
default_step_covariance[4] *= 40.0;
default_step_covariance[5] *= 40.0;
std::vector<double> initial_noise_covariance = std::vector<double> (6, 0.00001);
std::vector<double> default_initial_mean = std::vector<double> (6, 0.0);
pcl::tracking::KLDAdaptiveParticleFilterOMPTracker<PointT, ParticleT> *tracker
(new pcl::tracking::KLDAdaptiveParticleFilterOMPTracker<PointT, ParticleT> (8));
//Set the size of one particle
ParticleT bin_size;
bin_size.x = 0.1f;
bin_size.y = 0.1f;
bin_size.z = 0.1f;
bin_size.roll = 0.1f;
bin_size.pitch = 0.1f;
bin_size.yaw = 0.1f;
// Set all parameters for KLDAdaptiveParticleFilterOMPTracker
tracker->setMaximumParticleNum(1000);
tracker->setDelta(0.99);
tracker->setEpsilon(0.2);
tracker->setBinSize(bin_size);
// Set all parameters for ParticleFilter
tracker_ = tracker;
tracker_->setTrans (Eigen::Affine3f::Identity ());
tracker_->setStepNoiseCovariance (default_step_covariance);
tracker_->setInitialNoiseCovariance (initial_noise_covariance);
tracker_->setInitialNoiseMean (default_initial_mean);
tracker_->setIterationNum (1);
tracker_->setParticleNum (600);
tracker_->setResampleLikelihoodThr(0.00);
tracker_->setUseNormal (false);
//Setup coherence object for tracking
pcl::tracking::ApproxNearestPairPointCloudCoherence<PointT>::Ptr coherence
(new pcl::tracking::ApproxNearestPairPointCloudCoherence<PointT>);
pcl::tracking::DistanceCoherence<PointT>::Ptr distance_coherence
(new pcl::tracking::DistanceCoherence<PointT>);
coherence->addPointCoherence (distance_coherence);
pcl::search::Octree<PointT>::Ptr search (new pcl::search::Octree<PointT> (0.05));
coherence->setSearchMethod (search);
coherence->setMaximumDistance (0.05);
tracker_->setCloudCoherence (coherence);
// allocate Memory :
cloud_tracked_target_.reset ( new PointCloud() );
// target_locus_ clear :
target_locus_.clear();
}
// Set Particle Filter :
bool PointCloudTracker::setTrackTarget ( const PointCloud::Ptr ref_cloud, const geometry_msgs::Point& ref_pt ) {
try {
Eigen::Affine3f trans = Eigen::Affine3f::Identity();
trans.translation ().matrix () = Eigen::Vector3f ( ref_pt.x, ref_pt.y, ref_pt.z );
pcl::transformPointCloud<PointT> ( *ref_cloud, *cloud_tracked_target_, trans.inverse() );
tracker_->resetTracking();
tracker_->setReferenceCloud ( cloud_tracked_target_ );
tracker_->setTrans ( trans );
return true;
} catch ( std::exception& ex ) {
ROS_ERROR("%s", ex.what());
return false;
}
}
// change track target :
bool PointCloudTracker::changeTrackTarget ( const PointCloud::Ptr ref_cloud, const geometry_msgs::Point& ref_pt ) {
try {
Eigen::Affine3f trans = Eigen::Affine3f::Identity();
trans.translation ().matrix () = Eigen::Vector3f ( ref_pt.x, ref_pt.y, ref_pt.z );
pcl::transformPointCloud<PointT> ( *ref_cloud, *cloud_tracked_target_, trans.inverse() );
tracker_->setReferenceCloud ( cloud_tracked_target_ );
tracker_->setTrans ( trans );
return true;
} catch ( std::exception& ex ) {
ROS_ERROR("%s", ex.what());
return false;
}
}
// Get Particle Filter Tracking Result :
bool PointCloudTracker::getTrackResult ( const PointCloud::Ptr input_cloud, PointCloud::Ptr output_cloud, geometry_msgs::Pose* output_pose ) {
try {
tracker_->setInputCloud ( input_cloud );
tracker_->compute ( );
ParticleT pf_result = tracker_->getResult ();
if (!std::isfinite (pf_result.x) ||
!std::isfinite (pf_result.y) ||
!std::isfinite (pf_result.z)) {
ROS_ERROR( "Tracking failed : TrackResult is Nan data." );
output_pose->position.x = 0.0;
output_pose->position.y = 0.0;
output_pose->position.z = 0.0;
output_pose->orientation.x = 0.0;
output_pose->orientation.y = 0.0;
output_pose->orientation.z = 0.0;
return false;
} else {
// Get Target PointCloud :
Eigen::Affine3f transformation = tracker_->toEigenMatrix ( pf_result );
pcl::transformPointCloud<PointT> ( *(tracker_->getReferenceCloud ()), *output_cloud, transformation );
// Get Target Pose :
output_pose->position.x = pf_result.x;
output_pose->position.y = pf_result.y;
output_pose->position.z = pf_result.z;
tf::Quaternion quat = tf::createQuaternionFromRPY( pf_result.roll, pf_result.pitch, pf_result.yaw );
quaternionTFToMsg( quat, output_pose->orientation );
// add locus :
target_locus_.push_back( output_pose->position );
if ( target_locus_.size () > 10 ) target_locus_.erase ( target_locus_.begin( ) );
return true;
}
} catch ( std::exception& ex ) {
ROS_ERROR("%s", ex.what());
return false;
}
}
// Get Particle Filter Tracking Result :
bool PointCloudTracker::getTrackResult ( const PointCloud::Ptr input_cloud, PointCloud::Ptr output_cloud, geometry_msgs::Point* output_pt ) {
try {
tracker_->setInputCloud ( input_cloud );
tracker_->compute ( );
ParticleT pf_result = tracker_->getResult ();
if (!std::isfinite (pf_result.x) ||
!std::isfinite (pf_result.y) ||
!std::isfinite (pf_result.z)) {
ROS_ERROR( "Tracking failed : TrackResult is Nan data." );
output_pt->x = 0.0;
output_pt->y = 0.0;
output_pt->z = 0.0;
return false;
} else {
// Get Target PointCloud :
Eigen::Affine3f transformation = tracker_->toEigenMatrix ( pf_result );
pcl::transformPointCloud<PointT> ( *(tracker_->getReferenceCloud ()), *output_cloud, transformation );
// Get Target Pose :
output_pt->x = pf_result.x;
output_pt->y = pf_result.y;
output_pt->z = pf_result.z;
// add locus :
target_locus_.push_back( *output_pt );
if ( target_locus_.size () > 10 ) target_locus_.erase ( target_locus_.begin( ) );
return true;
}
} catch ( std::exception& ex ) {
ROS_ERROR("%s", ex.what());
return false;
}
}
// Get the current particles :
bool PointCloudTracker::getParticles ( PointCloud::Ptr output_cloud ) {
try {
ParticleFilter::PointCloudStatePtr particles = tracker_->getParticles ();
PointCloud::Ptr tmp ( new PointCloud() );
//Set pointCloud with particle's points
for( auto &particle : particles->points ) {
PointT point;
point.x = particle.x;
point.y = particle.y;
point.z = particle.z;
tmp->points.push_back (point);
}
*output_cloud = *tmp;
return true;
} catch ( std::exception& ex ) {
ROS_ERROR("%s", ex.what());
return false;
}
}
// Get the current target motion(3D ver) :
bool PointCloudTracker::getTargetMotion3D ( double* distance, geometry_msgs::Point* orientation ) {
try {
double length = 0.0;
double ave_vec_mag = 0.0;
geometry_msgs::Point some_vec_xy;
geometry_msgs::Point some_vec_yz;
geometry_msgs::Point pre_val;
for ( auto& locus : target_locus_ ) {
if ( length == 0.0 ) {
pre_val = locus;
} else {
ave_vec_mag += calcSpatialVectorMagnitude ( locus, pre_val );
addVector2UnitVector4PlaneAngle ( locus, pre_val, "xy", &some_vec_xy );
addVector2UnitVector4PlaneAngle ( locus, pre_val, "yz", &some_vec_yz );
pre_val = locus;
}
length ++;
}
*distance = ave_vec_mag / length;
*orientation = clacAverageSpatialVectorOrientation ( some_vec_xy, some_vec_yz, length );
return true;
} catch ( std::exception& ex ) {
ROS_ERROR("%s", ex.what());
return false;
}
}
// Get the current target motion(2D ver) :
bool PointCloudTracker::getTargetMotion2D ( double* distance, double* orientation ) {
try {
double length = 0.0;
double ave_vec_mag = 0.0;
geometry_msgs::Point vector_ave;
geometry_msgs::Point pre_val;
for ( auto& locus : target_locus_ ) {
if ( length == 0.0 ) {
pre_val = locus;
} else {
ave_vec_mag += hypotf ( locus.x - pre_val.x, locus.y - pre_val.y );
double angle = atan2 ( locus.y - pre_val.y, locus.x - pre_val.x );
vector_ave.x += cos ( angle );
vector_ave.y += sin ( angle );
pre_val = locus;
}
length ++;
}
vector_ave.x = vector_ave.x / length;
vector_ave.y = vector_ave.y / length;
*distance = ave_vec_mag / length;
*orientation = atan2 ( vector_ave.y, vector_ave.x );
return true;
} catch ( std::exception& ex ) {
ROS_ERROR("%s", ex.what());
return false;
}
}
}