00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00035 #include <driver_monitoring/class_colision_time.h>
00036
00037
00038
00039 visualization_msgs::Marker mark_cluster(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster, std::string ns ,int id, float r, float g, float b);
00040
00041 visualization_msgs::Marker mark_side_cluster(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster, std::string ns ,int id, float r, float g, float b);
00042
00043 visualization_msgs::Marker generic_text(float x, float y, float z , float r, float g, float b, float scale, std::string txt, std::string frm_id, std::string ns);
00044
00045
00046 ros::Publisher points_pub;
00047
00048 ros::Publisher marker_fronts_publisher;
00049 ros::Publisher marker_front_text_publisher;
00050
00051
00052
00053 ros::Publisher marker_side_publisher;
00054 ros::Publisher marker_side_text_publisher;
00055 ros::Publisher marker_side_text_blink_publisher;
00056 ros::Publisher marker_generic_text_blink_left_publisher;
00057
00058
00059
00060
00061 tf::TransformListener *listener_center_bumper_ptr;
00062 class_colision_time::TYPE_msg_mtt obstacle_zone_front;
00063 class_colision_time::TYPE_msg_mtt obstacle_left;
00064 class_colision_time::TYPE_msg_partial partial_msg;
00065 class_colision_time::TYPE_msg_velocity velocity_msg;
00066 class_colision_time::TYPE_msg_plc plc_msg;
00067 class_colision_time::TYPE_msg_bags colision_time;
00068
00069
00077 int cria_txt(int x)
00078 {
00079 FILE *fp;
00080 string nane_file;
00081
00082 nane_file.append("../");
00083 nane_file.append(colision_time.name_file);
00084 nane_file.erase(nane_file.end()-4, nane_file.end());
00085 nane_file.append(".txt");
00086 cout<< "msg_save:"<< colision_time.name_file<<endl;
00087 fp = fopen(nane_file.c_str(),"a+");
00088
00089
00090
00091 if (fp==NULL)
00092 {
00093
00094 cout<<"cannot open file: " << nane_file.c_str() <<endl;
00095
00096 }
00097 if (x==1){
00098 fprintf(fp,"%s \t %s \t %f \t %f \t %f \n", colision_time.name_file.c_str(), colision_time.sit_type_1.c_str(),colision_time.sit_begin_1, colision_time.duration_1, colision_time.sit_end_1);
00099 cout<<"cria txt 1"<<endl;
00100 }
00101 if (x==2){
00102 fprintf(fp,"%s \t %s \t %f \t %f \t %f \n", colision_time.name_file.c_str(), colision_time.sit_type_2.c_str(),colision_time.sit_begin_2, colision_time.duration_2, colision_time.sit_end_2);
00103 cout<<"cria txt 2"<<endl;
00104 }
00105 if (x==3){
00106 fprintf(fp,"%s \t %s \t %f \t %f \t %f \n", colision_time.name_file.c_str(), colision_time.sit_type_3.c_str(),colision_time.sit_begin_3, colision_time.duration_3, colision_time.sit_end_3);
00107 cout<<"cria txt 3"<<endl;
00108 }
00109 if (x==4){
00110 fprintf(fp,"%s \t %s \t %f \t %f \t %f \n", colision_time.name_file.c_str(), colision_time.sit_type_4.c_str(),colision_time.sit_begin_4, colision_time.duration_4, colision_time.sit_end_4);
00111 cout<<"cria txt 4"<<endl;
00112 }
00113
00114
00115 fclose(fp);
00116
00117 return 0;
00118 };
00119
00120
00126 void initialization_variables(void)
00127 {
00128 colision_time.internal_1=0;
00129 colision_time.internal_2=0;
00130 colision_time.internal_3=0;
00131 colision_time.internal_4=0;
00132 }
00133
00139 void begin_situation(void)
00140 {
00141
00142
00143
00144
00145
00146 if(colision_time.sit_1==1 && colision_time.internal_1==0)
00147 {
00148
00149 colision_time.lst_obj_sit_1=ros::Time::now().toSec();
00150 colision_time.sit_begin_1=ros::Time::now().toSec();
00151 colision_time.internal_1=1;
00152 }
00153 if (colision_time.sit_1==1 && colision_time.internal_1==1)
00154 {colision_time.lst_obj_sit_1=ros::Time::now().toSec(); }
00155
00156 if(ros::Time::now().toSec()-colision_time.lst_obj_sit_1<1 )
00157 {
00158 colision_time.sit_1=1;
00159
00160 }else
00161 {
00162 if(colision_time.sit_1==0 && colision_time.internal_1==1)
00163 {
00164 colision_time.sit_type_1="object detected--> vehicle behind the car";
00165
00166 colision_time.duration_1=colision_time.bag_time-colision_time.sit_begin_1;
00167 colision_time.sit_end_1=colision_time.bag_time;
00168
00169
00170 colision_time.internal_1=0;
00171 cria_txt(1);
00172
00173 }
00174 }
00175
00176
00177 if(colision_time.sit_2==1 && colision_time.internal_2==0)
00178 {
00179
00180 colision_time.lst_obj_sit_2=ros::Time::now().toSec();
00181 colision_time.sit_begin_2=ros::Time::now().toSec();
00182 colision_time.internal_2=1;
00183 }
00184 if (colision_time.sit_2==1 && colision_time.internal_2==1)
00185 {colision_time.lst_obj_sit_2=ros::Time::now().toSec(); }
00186
00187 if(ros::Time::now().toSec()-colision_time.lst_obj_sit_2<1 )
00188 {
00189 colision_time.sit_2=1;
00190
00191 }else
00192 {
00193 if(colision_time.sit_2==0 && colision_time.internal_2==1)
00194 {
00195 colision_time.sit_type_2="object detected--> Colision Risk";
00196
00197 colision_time.duration_2=colision_time.bag_time-colision_time.sit_begin_2;
00198 colision_time.sit_end_2=colision_time.bag_time;
00199
00200
00201 colision_time.internal_2=0;
00202 cria_txt(2);
00203 cout<<"cria txt 2"<<endl;
00204 }
00205 }
00206
00207 if(colision_time.sit_3==1 && colision_time.internal_3==0)
00208 {
00209
00210 colision_time.lst_obj_sit_3=ros::Time::now().toSec();
00211 colision_time.sit_begin_3=ros::Time::now().toSec();
00212 colision_time.internal_3=1;
00213 }
00214 if (colision_time.sit_3==1 && colision_time.internal_3==1)
00215 {colision_time.lst_obj_sit_3=ros::Time::now().toSec(); }
00216
00217 if(ros::Time::now().toSec()-colision_time.lst_obj_sit_3<1 )
00218 {
00219 colision_time.sit_3=1;
00220
00221 }else
00222 {
00223 if(colision_time.sit_3==0 && colision_time.internal_3==1)
00224 {
00225 colision_time.sit_type_3="object detected--> Forgot to blink left!";
00226
00227 colision_time.duration_3=colision_time.bag_time-colision_time.sit_begin_3;
00228 colision_time.sit_end_3=colision_time.bag_time;
00229
00230
00231 colision_time.internal_3=0;
00232 cria_txt(3);
00233 cout<<"cria txt 3"<<endl;
00234 }
00235 }
00236
00237 if(colision_time.sit_4==1 && colision_time.internal_4==0)
00238 {
00239
00240 colision_time.lst_obj_sit_4=ros::Time::now().toSec();
00241 colision_time.sit_begin_4=ros::Time::now().toSec();
00242 colision_time.internal_4=1;
00243 }
00244 if (colision_time.sit_4==1 && colision_time.internal_4==1)
00245 {colision_time.lst_obj_sit_4=ros::Time::now().toSec(); }
00246
00247 if(ros::Time::now().toSec()-colision_time.lst_obj_sit_4<1 )
00248 {
00249 colision_time.sit_4=1;
00250
00251 }else
00252 {
00253 if(colision_time.sit_4==0 && colision_time.internal_4==1)
00254 {
00255 colision_time.sit_type_4="object detected--> Forgot to blink right";
00256
00257 colision_time.duration_4=colision_time.bag_time-colision_time.sit_begin_4;
00258 colision_time.sit_end_4=colision_time.bag_time;
00259
00260
00261 colision_time.internal_4=0;
00262 cria_txt(4);
00263 cout<<"cria txt 4 "<<endl;
00264 }
00265 }
00266
00267 };
00268
00269
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00295
00296
00297
00298
00299
00300
00301
00307
00308
00309
00310
00311
00312
00313
00319 void topic_callback_rosout_cool(rosgraph_msgs::Log rosout_msg)
00320 {
00321
00322 if (rosout_msg.msg[0]=='O' && rosout_msg.msg[1]=='p'&& rosout_msg.msg[2]=='e'&& rosout_msg.msg[6]=='g')
00323 {
00324 char name_file[1024];
00325 sscanf(rosout_msg.msg.c_str(),"%*s %s",name_file);
00326 colision_time.name_file= name_file;
00327
00328
00329
00330 }
00331 };
00332
00333
00344 void topic_callback(const mtt::TargetListPC::Ptr& msg)
00345 {
00346 colision_time.bag_time=ros::Time::now().toSec();
00347 colision_time.sit_1=0;
00348 colision_time.sit_2=0;
00349 colision_time.sit_3=0;
00350 colision_time.sit_4=0;
00351
00352
00353
00354
00355 PointCloud<PointXYZ> velocity;
00356 PointCloud<PointXYZ> velocity_1;
00357 PointCloud<PointXYZ> position_1;
00358 PointCloud<PointXYZ> position;
00359
00360 pcl::PCLPointCloud2 pcl_pc;
00361 pcl_conversions::toPCL(msg->velocity, pcl_pc);
00362 pcl::fromPCLPointCloud2(pcl_pc, velocity_1);
00363
00364 pcl_conversions::toPCL(msg->position, pcl_pc);
00365 pcl::fromPCLPointCloud2(pcl_pc, position_1);
00366
00367
00368
00369
00370 tf::StampedTransform transform;
00371 try
00372 {
00373 listener_center_bumper_ptr->lookupTransform("/atc/vehicle/center_bumper", msg->header.frame_id, ros::Time(0), transform);
00374 }
00375 catch (tf::TransformException ex)
00376 {
00377 ROS_ERROR("%s",ex.what());
00378 }
00379
00380 pcl_ros::transformPointCloud (position_1,position,transform);
00381 pcl_ros::transformPointCloud (velocity_1,velocity,transform);
00382 position.header.frame_id="/atc/vehicle/center_bumper";
00383 velocity.header.frame_id="/atc/vehicle/center_bumper";
00384
00385 PointCloud<PointXYZ>::Ptr front_zone (new PointCloud<PointXYZ> ());
00386 front_zone->header.frame_id="/atc/vehicle/center_bumper";
00387
00388
00389 PointCloud<PointXYZ>::Ptr left_zone (new PointCloud<PointXYZ> ());
00390 left_zone->header.frame_id="/atc/vehicle/center_bumper";
00391
00392
00393 PointXYZ point;
00394
00395 for (int i=0; i<(int)position.size(); ++i)
00396 {
00397 point=position.points[i];
00398
00399 if (point.x>0 && point.x<40 && point.y>-1 && point.y<1)
00400 {
00401 front_zone->points.push_back(point);
00402 obstacle_zone_front.id=msg->id[i];
00403 obstacle_zone_front.x=point.x;
00404 obstacle_zone_front.y=point.y;
00405 obstacle_zone_front.z=point.z;
00406 obstacle_zone_front.v_x=velocity[i].x;
00407 obstacle_zone_front.v_y=velocity[i].y;
00408 obstacle_zone_front.v_z=velocity[i].z;
00409 }
00410
00411 if (point.x>-40 && point.x<-0.5 && point.y>1.2 && point.y<3.5)
00412 {
00413 left_zone->points.push_back(point);
00414 obstacle_left.id=msg->id[i];
00415 obstacle_left.x=point.x;
00416 obstacle_left.y=point.y;
00417 obstacle_left.z=point.z;
00418 obstacle_left.v_x=velocity[i].x;
00419 obstacle_left.v_y=velocity[i].y;
00420 obstacle_left.v_z=velocity[i].z;
00421 }
00422
00423 }
00424
00425 sensor_msgs::PointCloud2 ptc_front;
00426 toROSMsg(*front_zone,ptc_front);
00427 ptc_front.header.frame_id="/atc/vehicle/center_bumper";
00428 points_pub.publish(ptc_front);
00429
00430
00431 sensor_msgs::PointCloud2 ptc_side;
00432 toROSMsg(*left_zone,ptc_side);
00433 ptc_side.header.frame_id="/atc/vehicle/center_bumper";
00434 points_pub.publish(ptc_side);
00435
00436
00437
00441
00442
00443 visualization_msgs::Marker marker_front=mark_cluster(front_zone, "front_cube" ,0, 0, 0, 1);
00444 marker_fronts_publisher.publish(marker_front);
00445
00446
00447
00448 float velocity_car_front;
00449 float time_car_front;
00450 velocity_car_front=obstacle_zone_front.v_x+velocity_msg.velocity;
00451 time_car_front=obstacle_zone_front.x/obstacle_zone_front.v_x;
00452
00453 cout<<"V Atlascar ->"<< velocity_msg.velocity<<endl;
00454 cout<<"V objecto->"<< obstacle_zone_front.v_x<< endl;
00455 cout<<"V total ->"<< velocity_car_front <<endl;
00456 cout<<"eime total ->"<<time_car_front <<endl;
00457
00458 if(time_car_front>-4 && time_car_front< 0)
00459 {
00460 colision_time.sit_2=1;
00461 string msg_text="Colision Risk!!";
00462 visualization_msgs::Marker marker_front_text_front=generic_text( obstacle_zone_front.x , obstacle_zone_front.y, obstacle_zone_front.z , 1, 0, 0, 1, msg_text , "/atc/vehicle/center_bumper", "front_warning");
00463
00464 marker_front_text_publisher.publish(marker_front_text_front);
00465
00466
00467
00468
00469 }
00470
00471
00472
00476
00477
00478
00479 visualization_msgs::Marker marker_side=mark_side_cluster(left_zone, "side_cube" ,0, 0, 0, 1);
00480 marker_side_publisher.publish(marker_side);
00481
00482
00486
00487
00488
00489 long int exist_points_side=left_zone->points.size();
00490 if(exist_points_side>0)
00491 {
00492 colision_time.sit_1=1;
00493
00494 string msg_text_side="OBJECT DETECTED";
00495 visualization_msgs::Marker marker_side_text =generic_text( 0 , obstacle_left.y, 2 , 0, 0, 0.487, 1, msg_text_side ,"/atc/vehicle/center_bumper","cannot_pass");
00496
00497 marker_side_text_publisher.publish(marker_side_text);
00498 }
00499
00503
00504 if( partial_msg.lights_left==0 && plc_msg.steering_wheel<(-0.315) )
00505 {
00506 colision_time.sit_3=1;
00507 string msg_blink_side="Forgot to blink left!";
00508
00509 visualization_msgs::Marker marker_side_text_left_blink= generic_text( 3 , 6, 2 , 0, 0, 1, 1, msg_blink_side ,"/atc/vehicle/center_bumper","Blink_left");
00510
00511 marker_generic_text_blink_left_publisher.publish(marker_side_text_left_blink);
00512
00513 }
00514
00518
00519
00520 if( partial_msg.lights_right==0 && plc_msg.steering_wheel>(-0.275) && plc_msg.steering_wheel!=0 )
00521 {
00522 colision_time.sit_4=1;
00523 string msg_blink_side="Forgot to blink right!";
00524
00525 visualization_msgs::Marker marker_side_text_right_blink= generic_text(3 , -6, 2 , 0, 0, 1, 1, msg_blink_side ,"/atc/vehicle/center_bumper","Blink_right");
00526
00527 marker_side_text_blink_publisher.publish(marker_side_text_right_blink);
00528 }
00529
00530 begin_situation();
00531
00532
00533
00534 obstacle_zone_front.v_x =0;
00535 };
00536
00537
00538
00544 int main (int argc, char **argv)
00545 {
00546 ROS_INFO("Starting colision time node.");
00547
00548 ros::init(argc,argv,"colision_time");
00549 ros::NodeHandle n;
00550
00551 initialization_variables();
00552
00553
00554
00555 tf::TransformListener listener_center_bumper;
00556 listener_center_bumper_ptr=&listener_center_bumper;
00557
00558
00559 ros::Subscriber sub = n.subscribe("/mtt/trck/targets", 1, topic_callback);
00560
00561
00562
00563 ros::Subscriber sub_rosout =n.subscribe("/rosout_agg", 1, topic_callback_rosout_cool);
00564
00565
00566 ROS_ERROR("AtlascarPartialStatus and AtlascarVelocityStatus no longer avaliable, please correct!!");
00567
00568
00569
00570
00571
00572
00573
00574 points_pub = n.advertise<sensor_msgs::PointCloud2>("/atc/monitoring/danger_zone", 1);
00575
00576
00577 marker_fronts_publisher= n.advertise<visualization_msgs::Marker>("/prcp/front_mrk", 1000);
00578
00579
00580 marker_side_publisher= n.advertise<visualization_msgs::Marker>("/prcp/side_mrk", 1000);
00581
00582
00583 marker_front_text_publisher= n.advertise<visualization_msgs::Marker>("/prcp/front_text_mrk", 1000);
00584
00585
00586 marker_side_text_publisher= n.advertise<visualization_msgs::Marker>("/prcp/side_text_mrk", 1000);
00587
00588
00589
00590 marker_side_text_blink_publisher= n.advertise<visualization_msgs::Marker>("/prcp/side_text_blink_right_mrk", 1000);
00591
00592
00593 marker_generic_text_blink_left_publisher= n.advertise<visualization_msgs::Marker>("/prcp/side_text_blink_left_mrk", 1000);
00594
00595
00596
00597 ros::spin();
00598
00599 return 1;
00600 }
00601
00602
00603
00618 visualization_msgs::Marker generic_text(float x, float y, float z , float r,
00619 float g, float b, float scale, std::string txt, std::string frm_id, std::string
00620 ns)
00621 {
00622 uint32_t shape = visualization_msgs::Marker::TEXT_VIEW_FACING;
00623 visualization_msgs::Marker marker_front;
00624
00625 marker_front.header.frame_id = frm_id;
00626 marker_front.header.stamp = ros::Time::now();
00627
00628
00629 marker_front.ns = ns;
00630 marker_front.id = 0;
00631 marker_front.type = shape;
00632 marker_front.action = visualization_msgs::Marker::ADD;
00633
00634 marker_front.pose.position.x = x;
00635 marker_front.pose.position.y = y;
00636 marker_front.pose.position.z = z;
00637 marker_front.scale.z = scale;
00638
00639
00640
00641
00642 marker_front.text=txt;
00643
00644 marker_front.color.r = r;
00645 marker_front.color.g = g;
00646 marker_front.color.b = b;
00647 marker_front.color.a = 0.8;
00648
00649 marker_front.lifetime = ros::Duration(0.4);
00650 return marker_front;
00651 }
00652
00653
00654
00655
00656
00657
00668 visualization_msgs::Marker mark_cluster(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster, std::string ns ,int id, float r, float g, float b)
00669 {
00670 Eigen::Vector4f centroid;
00671 Eigen::Vector4f min;
00672 Eigen::Vector4f max;
00673
00674 pcl::compute3DCentroid (*cloud_cluster, centroid);
00675 pcl::getMinMax3D (*cloud_cluster, min, max);
00676
00677 uint32_t shape = visualization_msgs::Marker::CUBE;
00678 visualization_msgs::Marker marker_front;
00679 marker_front.header.frame_id = cloud_cluster->header.frame_id;
00680 marker_front.header.stamp = ros::Time::now();
00681
00682 marker_front.ns = ns;
00683 marker_front.id = id;
00684 marker_front.type = shape;
00685 marker_front.action = visualization_msgs::Marker::ADD;
00686
00687
00688
00689 marker_front.pose.position.x = min[0];
00690 marker_front.pose.position.y = centroid[1];
00691 marker_front.pose.position.z = centroid[2];
00692 marker_front.pose.orientation.x = 0.0;
00693 marker_front.pose.orientation.y = 0.0;
00694 marker_front.pose.orientation.z = 0.0;
00695 marker_front.pose.orientation.w = 1.0;
00696
00697 marker_front.scale.x = 1;
00698 marker_front.scale.y = 1;
00699 marker_front.scale.z = 1;
00700
00701 marker_front.color.r = r;
00702 marker_front.color.g = g;
00703 marker_front.color.b = b;
00704 marker_front.color.a = 0.5;
00705
00706
00707 marker_front.lifetime = ros::Duration(0.3);
00708 return marker_front;
00709 }
00710
00711
00712
00713
00714
00725 visualization_msgs::Marker mark_side_cluster(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster, std::string ns ,int id, float r, float g, float b)
00726 {
00727 Eigen::Vector4f centroid;
00728 Eigen::Vector4f min;
00729 Eigen::Vector4f max;
00730
00731 pcl::compute3DCentroid (*cloud_cluster, centroid);
00732 pcl::getMinMax3D (*cloud_cluster, min, max);
00733
00734 uint32_t shape = visualization_msgs::Marker::CUBE;
00735 visualization_msgs::Marker marker_side;
00736 marker_side.header.frame_id = cloud_cluster->header.frame_id;
00737 marker_side.header.stamp = ros::Time::now();
00738
00739 marker_side.ns = ns;
00740 marker_side.id = id;
00741 marker_side.type = shape;
00742 marker_side.action = visualization_msgs::Marker::ADD;
00743
00744
00745
00746 marker_side.pose.position.x = max[0];
00747 marker_side.pose.position.y = centroid[1];
00748 marker_side.pose.position.z = centroid[2];
00749 marker_side.pose.orientation.x = 0.0;
00750 marker_side.pose.orientation.y = 0.0;
00751 marker_side.pose.orientation.z = 0.0;
00752 marker_side.pose.orientation.w = 1.0;
00753
00754 marker_side.scale.x = 1;
00755 marker_side.scale.y = 1;
00756 marker_side.scale.z = 1;
00757
00758 marker_side.color.r = r;
00759 marker_side.color.g = g;
00760 marker_side.color.b = b;
00761 marker_side.color.a = 1;
00762
00763
00764 marker_side.lifetime = ros::Duration(0.3);
00765 return marker_side;
00766 }
00767