trajectory_planner_nodelet.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 ***************************************************************************************************/
27 #ifndef _trajectory_planner_nodelet_CPP_
28 #define _trajectory_planner_nodelet_CPP_
29 
37 
38 bool plan_trajectory=false;
39 bool have_plan=false;
40 geometry_msgs::PoseStamped pose_in;
41 geometry_msgs::PoseStamped pose_transformed;
42 
43 std::vector< pcl::PointCloud<pcl::PointXYZ> > pc_v;
44 
45 
51 void set_coordinates(trajectory_planner::coordinates msg)
52 {
53 
54 // cout<<"stat Message received!!!"<<endl;
55  // Change parameters
56  pose_in.pose.position.x=msg.x;
57  pose_in.pose.position.y=msg.y;
58  pose_in.pose.orientation.w=cos(msg.theta/2.0);
59  pose_in.pose.orientation.x=0.0;
60  pose_in.pose.orientation.y=0.0;
61  pose_in.pose.orientation.z=sin(msg.theta/2.0);
62 
63  pose_in.header.frame_id="/world";
64 
65  manage_vt->set_attractor_point(msg.x,msg.y,msg.theta);
66  plan_trajectory=true;
67 }
68 
69 
75 void mtt_callback(mtt::TargetListPC msg)
76 {
77  ROS_INFO("received mtt num obs=%ld num lines first obs =%d frame_id=%s",msg.id.size(), msg.obstacle_lines[0].width, msg.header.frame_id.c_str());
78 
79  //copy to global variable
80  pc_v.erase(pc_v.begin(), pc_v.end());
81  for (size_t i=0; i<msg.id.size(); ++i)
82  {
83  pcl::PointCloud<pcl::PointXYZ> tmp;
84  pcl::fromROSMsg(msg.obstacle_lines[i], tmp);
85  tmp.header.frame_id=msg.header.frame_id;
86 // tmp.header.stamp=msg.header.stamp;
87 
88  ROS_INFO("Obstacle %ld has %ld lines", i, tmp.points.size());
89  pc_v.push_back(tmp);
90  }
91 }
92 
93 
99 vector<double> set_speed_vector(boost::shared_ptr<c_trajectory> t)
100 {
101  vector<double> speed_setted;
102  for(size_t i=0;i<_NUM_NODES_;++i)
103  {
104  if(i < (_NUM_NODES_ - 1))
105  {
106  if((t->arc[i])*(t->arc[i+1])<0.0)
107  {
108  speed_setted.push_back((t->arc[i]/fabs(t->arc[i]))*_SPEED_SAFFETY_); // This is the speed set to reverse/forward or forward/reverse
109  }
110  else
111  {
112  speed_setted.push_back((t->arc[i]/fabs(t->arc[i]))*_SPEED_REQUIRED_);
113  }
114  }
115  else
116  {
117  speed_setted.push_back((t->arc[i]/fabs(t->arc[i]))*_SPEED_REQUIRED_);
118  }
119 
120  }
121  return speed_setted;
122 }
123 
124 
132 int main(int argc, char **argv)
133 {
134  ros::init(argc, argv, "trajectory_planner_nodelet");
135  ros::NodeHandle n("~");
136  p_n=&n;
137 
138  //Define the publishers and subscribers
139  tf::TransformBroadcaster broadcaster;
140  tf::TransformListener listener;
141  p_listener = &listener;
142  ros::Publisher array_pub = n.advertise<visualization_msgs::MarkerArray>( "/array_of_markers", 1 );
143  ros::Subscriber sub = n.subscribe("/msg_coordinates", 1, set_coordinates);
144  ros::Subscriber mtt_sub = n.subscribe("/mtt_targets", 1, mtt_callback);
145  ros::Rate loop_rate(10);
146 
147  // ___________________________________
148  // | |
149  // | Define the trajectories |
150  // |_________________________________|
151  //Declare the traj manager class
153 
154  //initialize attractor point
156  AP.x=-1.0;
157  AP.y=0.0;
158  AP.theta=-M_PI/8;
159  manage_vt->set_attractor_point(AP.x, AP.y, AP.theta);
160 
161 
162  //initialize vehicle description
165 
166  //initialize inter axis distance
167  manage_vt->set_inter_axis_distance(_D_);
168 
169 
170  //Compute the trajectory parameters
171  for (int i=0; i< _NUM_TRAJ_; i++)
172  {
173  vector<double> v_a;
174  vector<double> v_arc;
175  for (int j=0; j<_NUM_NODES_; ++j)
176  {
177  v_a.push_back(M_PI/180.*a[i][j]);
178  v_arc.push_back(arc[i][j]);
179  }
180  manage_vt->create_new_trajectory(v_a,v_arc,v_a);
181  }
182 
183  // Choose trajectory (0 is trajectory 1)
184  int val = 0; // This will be commented
185 
186  if (val>=(int)manage_vt->vt.size())
187  {
188  ROS_ERROR("Chosen trajectory does not exist");
189  exit(0);
190  }
191  manage_vt->set_chosen_traj(val);
192 
193  //create the static visualisation markers
194 
195  commandPublisher = n.advertise<trajectory_planner::traj_info>("/trajectory_information", 1000);
196 
197 
198 
199 
200 
201  while(ros::ok())
202  {
203 
204  if(plan_trajectory==true)
205  {
206  //aqui recebi um comando para defenir uma trajectoria
207 
208  plan_trajectory = false;
209  ROS_INFO("Going to plan a trajectory");
210  // _________________________________
211  //| |
212  //| Set the parking view frame |
213  //|_________________________________|
214  bool have_transform=true;
215  //Set the frame where to draw the trajectories
216  try
217  {
218  p_listener->lookupTransform("/world","/vehicle_odometry",ros::Time(0), transformw);
219  }
220  catch (tf::TransformException ex)
221  {
222  ROS_ERROR("%s",ex.what());
223  have_transform=false;
224  }
225 
226  if (have_transform)
227  {
228  ros::Time time=ros::Time::now();
229  broadcaster.sendTransform(tf::StampedTransform(transformw, time,"/world", "/parking_frame"));
230  ros::spinOnce();
231  broadcaster.sendTransform(tf::StampedTransform(transformw, time+ros::Duration(5),"/world", "/parking_frame"));
232  ros::spinOnce();
233  // cout<<"stat Publishing transform"<<endl;
234 
235  ros::Duration(0.1).sleep();
236 
237  // ___________________________________
238  // | |
239  // | Trajectory evaluation |
240  // |_________________________________|
241 
242  //Transform attractor point to /parking_frame
243  tf::Transformer tt;
244  pose_in.header.stamp=time+ros::Duration(0.1);
245  p_listener->transformPose ("/parking_frame", pose_in, pose_transformed);
246 
247  ROS_INFO("pose_in frame_id=%s pose_transformed frame_id=%s",pose_in.header.frame_id.c_str(), pose_transformed.header.frame_id.c_str());
248  //Set transformed attractor point
249  manage_vt->set_attractor_point(pose_transformed.pose.position.x,pose_transformed.pose.position.y,atan2(2.0*(pose_transformed.pose.orientation.w*pose_transformed.pose.orientation.z),1-(2*(pose_transformed.pose.orientation.z*pose_transformed.pose.orientation.z))));
250 
251 
252  //transform mtt to /parking_frame
253  pcl::PointCloud<pcl::PointXYZ> pct;
254  mtt::TargetListPC msg_transformed;
255 
256 
257 
258  for (size_t i=0; i<pc_v.size(); ++i)
259  {
260 
261  if (i==0)
262  {
263  try
264  {
265 // p_listener->lookupTransform(pc_v[i].header.frame_id,"/parking_frame", pc_v[i].header.stamp, transform_mtt);
266  }
267  catch (tf::TransformException ex)
268  {
269  ROS_ERROR("%s",ex.what());
270  }
271  }
272 
273  ROS_INFO("pc_v[%ld] frame_id=%s pcp frame_id=%s",i, pc_v[i].header.frame_id.c_str(), pct.header.frame_id.c_str());
274  pcl_ros::transformPointCloud(pc_v[i],pct,transform_mtt.inverse());
275 
276  sensor_msgs::PointCloud2 pc_msg;
277  pcl::toROSMsg(pct, pc_msg);
278  pc_msg.header.frame_id = "/parking_frame";
279  pc_msg.header.stamp = ros::Time::now();
280  msg_transformed.obstacle_lines.push_back(pc_msg);
281  }
282  manage_vt->set_obstacles(msg_transformed);
283 
284 
285 
286  // ___________________________________
287  // | |
288  // | Draw |
289  // |_________________________________|
290  manage_vt->create_static_markers();
291 
292 
293  ros::Time st=ros::Time::now();
294 
295  manage_vt->compute_trajectories_scores();
296 
297  cout<<"Compute scores: "<<(ros::Time::now()-st).toSec();
298 
299  ROS_INFO("manage_vt chosen traj= %d",manage_vt->chosen_traj.index);
300 
301 
302  trajectory_planner::traj_info info;
303 
304  manage_vt->get_traj_info_msg_from_chosen(&info);
305 
306  commandPublisher.publish(info);
307 
308 
309  visualization_msgs::MarkerArray marker_array;
310  manage_vt->compute_vis_marker_array(&marker_array);
311  array_pub.publish(marker_array);
312  cout<<"ja size:"<<marker_array.markers.size()<<endl;
313 
314 
315  have_plan=true;
316 
317  }
318  }
319 
320  if (have_plan==true)
321  {
322  // !!!! The previous 'if' must have a weight evaluation !!!! if ... && GlobalScore>=0.74
323 
324  broadcaster.sendTransform(tf::StampedTransform(transformw, ros::Time::now(),"/world", "/parking_frame"));
325  cout<<"stat Publishing transform"<<endl;
326  }
327 
328  //printf("CURRENT NODE-> %d\n",node);
329  //printf("Distance Travelled-> %f\n",base_status.distance_traveled);
330 
331  loop_rate.sleep();
332  ros::spinOnce();
333  }
334 }
335 #endif
int main(int argc, char **argv)
Main code of the nodelet.
vector< double > set_speed_vector(boost::shared_ptr< c_trajectory > t)
Determine the speed vector.
#define _VEHICLE_LENGHT_FRONT_
#define _SPEED_SAFFETY_
double arc[20][9]
ros::Publisher commandPublisher
std::vector< pcl::PointCloud< pcl::PointXYZ > > pc_v
bool plan_trajectory
#define _D_
#define _NUM_TRAJ_
_EXTERN_ c_manage_trajectoryPtr manage_vt
_EXTERN_ tf::TransformListener * p_listener
_EXTERN_ tf::StampedTransform transformw
#define _NUM_NODES_
#define _VEHICLE_LENGHT_BACK_
trajectory_planner::traj_info info
boost::shared_ptr< c_manage_trajectory > c_manage_trajectoryPtr
_EXTERN_ tf::StampedTransform transform_mtt
void mtt_callback(mtt::TargetListPC msg)
Set mtt points.
#define _SPEED_REQUIRED_
ros::NodeHandle * p_n
Definition: APgenerator.cpp:45
void set_coordinates(trajectory_planner::coordinates msg)
Set attractor point coordinates.
#define _VEHICLE_WIDTH_
double a[20][9]
Trajectories definitions.
geometry_msgs::PoseStamped pose_in
geometry_msgs::PoseStamped pose_transformed
#define _VEHICLE_HEIGHT_TOP_
#define _VEHICLE_HEIGHT_BOTTOM_


trajectory_planner
Author(s): Joel Pereira
autogenerated on Mon Mar 2 2015 01:32:54