mtt_new_msg.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 #include <mtt/mtt.h>
33 
34 bool new_data=false;
35 sensor_msgs::PointCloud2 pointData;
36 
37 void PointsHandler(const sensor_msgs::PointCloud2& msg)
38 {
39  cout<<"Received point cloud"<<endl;
40  pointData=msg;
41  new_data=true;
42 }
43 
44 void PointCloud2ToData(sensor_msgs::PointCloud2& cloud,t_data& data)//this function will convert the point cloud data into a laser scan type structure
45 {
46  pcl::PointCloud<pcl::PointXYZ> PclCloud;
47 
48  pcl::fromROSMsg(cloud,PclCloud);
49 
50 
51  double theta;
52  map<double,pair<uint,double> > grid;
53  map<double,pair<uint,double> >::iterator it;
54 
55  double spacing = 0.5*M_PI/180.;
56 
57  double rightBorder;
58  double leftBorder;
59 
60  double r;
61 
62  cout<<"mtt Input size:"<<PclCloud.points.size()<<endl;
63  //get points into grid to reorder them
64  for(uint i=0;i<PclCloud.points.size();i++)
65  {
66  theta=atan2(PclCloud.points[i].y,PclCloud.points[i].x);
67  r=sqrt(pow(PclCloud.points[i].x,2)+pow(PclCloud.points[i].y,2));
68 
69  //get closest theta, given a predefined precision
70  rightBorder = spacing * ceil(theta/spacing);
71  leftBorder = rightBorder - spacing;
72 
73  if(fabs(rightBorder-theta)<fabs(leftBorder-theta))
74  theta=rightBorder;
75  else
76  theta=leftBorder;
77 
78  if(grid.find(theta)!=grid.end())
79  {
80  if(r<grid[theta].second)//using closest measurement
81  {
82  grid[theta].first=i;
83  grid[theta].second=r;
84  }
85  }else
86  {
87  grid[theta].first=i;
88  grid[theta].second=r;
89  }
90  }
91 
92  uint i=0;
93  for(it=grid.begin();it!=grid.end();it++)
94  {
95  //the map auto orders the theta values
96  data.x[i]=PclCloud.points[(*it).second.first].x;
97  data.y[i]=PclCloud.points[(*it).second.first].y;
98  data.r[i]=sqrt(pow(data.x[i],2)+pow(data.y[i],2));
99  data.t[i]=atan2(data.y[i],data.x[i]);
100 
101  i++;
102  }
103 
104  data.n_points=grid.size();
105  cout<<"mtt Size of data:"<<data.n_points<<endl;
106 }
107 
108 
109 
110 
111 void CreateMarkers(vector<visualization_msgs::Marker>& marker_vector,mtt::TargetList& target_msg,vector<t_listPtr>& list)
112 {
113  static map<pair<string,int>, pair<visualization_msgs::Marker,int> > marker_map;
114  map<pair<string,int>, pair<visualization_msgs::Marker,int> >::iterator it;
115 
116 
117  //limpar o vector todo
118  marker_vector.clear();
119  //colocar todos os elementos em processo de elemincação
120  for(it=marker_map.begin();it!=marker_map.end();it++)
121  it->second.second--;
122 
123 
124  std_msgs::ColorRGBA color;
125  class_colormap colormap("hsv",10, 1, false);
126 
127  visualization_msgs::Marker marker;
128 
129  marker.header.frame_id = pointData.header.frame_id;//target_msg.header.frame_id;
130 // cout<<"fid:"<<target_msg.header.frame_id<<endl;
131 
132  marker.header.stamp = ros::Time::now();
133  marker.ns = "ids";
134  marker.action = visualization_msgs::Marker::ADD;
135 
136  marker.pose.position.z=0.3;
137 
138  marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
139 
140  marker.scale.x = 1.0;
141  marker.scale.y = 1.0;
142  marker.scale.z = 1.0;
143 
144  marker.color.r=0;
145  marker.color.g=0;
146  marker.color.b=0;
147  marker.color.a=1;
148 
149  marker.id=0;
150 
151  for(uint i=0;i<list.size();i++)
152  {
153  if(list[i]->shape.lines.size()!=0)
154  {
155  marker.pose.position.x=list[i]->position.estimated_x;
156  marker.pose.position.y=list[i]->position.estimated_y;
157 
158  marker.text = boost::lexical_cast<string>(list[i]->id);
159 
160  marker.id++;
161 
162  marker_map[make_pair(marker.ns,marker.id) ] = make_pair(marker,1);//isto substitui ou cria o novo marker no map
163  }
164  }
165 
166  marker.pose.position.x=0;
167  marker.pose.position.y=0;
168 
169  marker.text = "origin";
170 
171  marker.id++;
172 
173  marker_map[make_pair(marker.ns,marker.id) ] = make_pair(marker,1);//isto substitui ou cria o novo marker no map
174 
175 
176  // Markers for Line objects
177  marker.type = visualization_msgs::Marker::LINE_STRIP;
178  marker.ns = "objects";
179 
180  marker.pose.position.x=0;
181  marker.pose.position.y=0;
182  marker.pose.position.z=0;
183 
184  marker.scale.x = 0.2;
185  marker.scale.y = 0.1;
186  marker.scale.z = 0.1;
187 
188  for(uint i=0;i<list.size();i++)
189  {
190  marker.color = colormap.color(list[i]->id);
191 
192  geometry_msgs::Point p;
193  p.z = 0.1;
194 
195  marker.points.clear();
196 
197  uint l;
198  for(l=0;l<list[i]->shape.lines.size();l++)
199  {
200  p.x = list[i]->shape.lines[l]->xi;
201  p.y = list[i]->shape.lines[l]->yi;
202 
203  marker.points.push_back(p);
204  }
205 
206  p.x = list[i]->shape.lines[l-1]->xf;
207  p.y = list[i]->shape.lines[l-1]->yf;
208 
209  marker.points.push_back(p);
210 
211  marker.id++;
212 
213  marker_map[make_pair(marker.ns,marker.id) ] = make_pair(marker,1);//isto substitui ou cria o novo marker no map
214  }
215 
216 
217 // // trying to create markers for vectors
218 // marker.type = visualization_msgs::Marker::ARROW;
219 // marker.ns = "velocities";
220 //
221 // marker.pose.position.x=0;
222 // marker.pose.position.y=0;
223 // marker.pose.position.z=0;
224 //
225 // marker.scale.x = 1;
226 // marker.scale.y = 1;
227 // marker.scale.z = 1;
228 //
229 // for(uint i=0;i<list.size();i++)
230 // {
231 // marker.color = colormap.color(list[i]->id);
232 // marker.points.clear();
233 //
234 // marker.points[0].x = list[i]->position.estimated_x;
235 // marker.points[0].y = list[i]->position.estimated_y;
236 // marker.points[0].z = 0;
237 //
238 // marker.points[1].x = list[i]->position.estimated_x + list[i]->velocity.velocity_x;
239 // marker.points[1].y = list[i]->position.estimated_y + list[i]->velocity.velocity_y;
240 // marker.points[1].z = 0;
241 //
242 // marker.id++;
243 //
244 // marker_map[make_pair(marker.ns,marker.id) ] = make_pair(marker,1);//isto substitui ou cria o novo marker no map
245 // }
246 
247 
248  //para o map todo envio tudo, e meto tudo a false
249  //envio todo e tudo o que ainda estiver a false vai com operaração de delete
250  for(it=marker_map.begin();it!=marker_map.end();it++)
251  {
252  if( it->second.second==0 )//se for falso é para apagar
253  it->second.first.action = visualization_msgs::Marker::DELETE;
254  else if(it->second.second<=-1)//já foi apagado
255  {
260  continue;
261  }
262 
263  marker_vector.push_back(it->second.first);
264  }
265 }
266 
267 int main(int argc,char**argv)
268 {
269  mtt::TargetList targetList;
270 
271  t_config config;
272  t_data full_data;
273  t_flag flags;
274 
275  vector<t_clustersPtr> clusters;
276  vector<t_objectPtr> object;
277  vector<t_listPtr> list;
278 
279  visualization_msgs::MarkerArray markersMsg;
280 
281  // Initialize ROS
282  init(argc,argv,"mtt");
283 
284  NodeHandle nh("~");
285 
286  Subscriber subs_points = nh.subscribe("/points", 1000, PointsHandler);
287  Publisher target_publisher = nh.advertise<mtt::TargetList>("/new_targets", 1000);
288  Publisher markers_publisher= nh.advertise<visualization_msgs::MarkerArray>("/new_markers", 1000);
289 // Publisher arrow_publisher= nh.advertise<visualization_msgs::MarkerArray>("/arrows", 1000);
290 // Publisher marker_publisher= nh.advertise<visualization_msgs::Marker>("/marker", 1000);
291 
292  init_flags(&flags); //Inits flags values
293  init_config(&config); //Inits configuration values
294 
295  cout<<"Start to spin"<<endl;
296  Rate r(100);
297  while(ok())
298  {
299  spinOnce();
300  r.sleep();
301 
302  if(!new_data)
303  continue;
304  new_data=false;
305 
306  //Get data from PointCloud2 to full_data
307  PointCloud2ToData(pointData,full_data);
308 
309  //clustering
310  clustering(full_data,clusters,&config,&flags);
311  cout<<"clusters: "<<clusters.size()<<endl;
312  //calc_cluster_props
313  calc_cluster_props(clusters,full_data);
314 
315  //clusters2objects
316  clusters2objects(object,clusters,full_data,config);
317 
318  calc_object_props(object);
319 
320  //AssociateObjects
321  AssociateObjects(list,object,config,flags);
322 
323  //MotionModelsIteration
324  MotionModelsIteration(list,config);
325 
326 // cout<<"Number of targets "<<list.size()<<endl;
327 
328  clean_objets(object);//clean current objects
329 
330  //clear target list array
331  targetList.Targets.clear();
332 
333  //structure to be fed to array
334  mtt::Target target;
335 
336  //build header
337  target.header.stamp = ros::Time::now();
338  target.header.frame_id = pointData.header.frame_id;
339 
340 
341 
342  for(uint i=0;i<list.size();i++)
343  {
344  geometry_msgs::Pose pose;
345  geometry_msgs::Twist vel;
346  geometry_msgs::Point initialpose;
347  geometry_msgs::Point finalpose;
348 
349  //header
350  target.header.seq = list[i]->id;
351  target.id = list[i]->id;
352 
353  //velocity
354  vel.linear.x=list[i]->velocity.velocity_x;
355  vel.linear.y=list[i]->velocity.velocity_y;
356  vel.linear.z=0;
357  target.velocity = vel;
358 
359  //compute orientation based on velocity.
360  double theta = atan2(vel.linear.y,vel.linear.x);
361  geometry_msgs::Quaternion target_orientation =
362  tf::createQuaternionMsgFromYaw(theta);
363 
364  //pose
365  pose.position.x = list[i]->position.estimated_x;
366  pose.position.y = list[i]->position.estimated_y;
367  pose.position.z = 0;
368  pose.orientation = target_orientation;
369  target.pose = pose;
370 
371  //initialpose
372  initialpose.x = list[i]->shape.lines[0]->xi;
373  initialpose.y = list[i]->shape.lines[0]->yi;
374  initialpose.z = 0;
375  target.initialpose = initialpose;
376 
377  //finalpose
378  finalpose.x = list[i]->shape.lines[list[i]->shape.lines.size()-1]->xf;
379  finalpose.y = list[i]->shape.lines[list[i]->shape.lines.size()-1]->yf;
380  finalpose.z = 0;
381  target.finalpose = finalpose;
382 
383  //target size
384  target.size = sqrt(((finalpose.x - initialpose.x) * (finalpose.x - initialpose.x))
385  + ((finalpose.y - initialpose.y) * (finalpose.y - initialpose.y)));
386 
387  //feed array with current target
388  //condition to accept as valit target (procopio change)
389  double velocity = sqrt(pow(vel.linear.x,2)+
390  pow(vel.linear.y,2));
391 
392  if(/*velocity > 0.5 &&*/ velocity < 3.0)
393  targetList.Targets.push_back(target);
394 
395  }
396 
397 
398 
399  target_publisher.publish(targetList);
400 
401  CreateMarkers(markersMsg.markers,targetList,list);
402  markers_publisher.publish(markersMsg);
403 // arrow_publisher.publish(arrow_arrray);
404 
405  flags.fi=false;
406  }
407 
408  return 0;
409 }
void PointsHandler(const sensor_msgs::PointCloud2 &msg)
Definition: mtt_new_msg.cpp:37
void clean_objets(vector< t_objectPtr > &objects)
Frees memory associated with objects.
void CreateMarkers(vector< visualization_msgs::Marker > &marker_vector, mtt::TargetList &target_msg, vector< t_listPtr > &list)
int main(int argc, char **argv)
bool fi
Definition: mtt_common.h:372
double r[2160]
Definition: mtt_common.h:118
double t[2160]
Definition: mtt_common.h:118
double y[2160]
Definition: mtt_common.h:117
void init_flags(t_flag *flags)
Init flags.
double vel
void calc_cluster_props(t_cluster **clusters, int size, t_data *data, t_config *config)
Calculates cluster properties.
void AssociateObjects(vector< t_listPtr > &list, vector< t_objectPtr > &objects, t_config &config, t_flag &flags)
Global include file that combines all local include files for the mtt algorithm.
int n_points
Definition: mtt_common.h:122
void PointCloud2ToData(sensor_msgs::PointCloud2 &cloud, t_data &data)
Definition: mtt_new_msg.cpp:44
void MotionModelsIteration(vector< t_listPtr > &list, t_config &config)
Definition: mtt_kalman.cpp:64
double theta
map< pair< string, int >, pair< visualization_msgs::Marker, int > > marker_map
This structure has all points coordinates.
Definition: mtt_common.h:115
t_cluster ** clustering(t_data *data, int *count, t_config *config, t_flag *flags)
Performs clustering operation.
bool new_data
Definition: mtt_new_msg.cpp:34
bool clusters2objects(vector< t_objectPtr > &objectsPtr, vector< t_clustersPtr > &clusters, t_data &data, t_config &config)
Converts clusters of points into objects using recursive line fitting.
This structure contains global configurations parameters.
Definition: mtt_common.h:137
double x[2160]
Definition: mtt_common.h:117
void init_config(t_config *config)
Init configuration.
void calc_object_props(vector< t_objectPtr > &objects)
Computes object properties, such as centroid and size.
ros::Publisher target_publisher
ros::Publisher markers_publisher
This structure contains global flags parameters.
Definition: mtt_common.h:369
sensor_msgs::PointCloud2 pointData
Definition: mtt_new_msg.cpp:35


mtt
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:18