@@ -414,24 +414,19 @@ void CollisionMonitor::process(const Velocity & cmd_vel_in, const std_msgs::msg:
414414 // Points array collected from different data sources in a robot base frame
415415 std::unordered_map<std::string, std::vector<Point>> sources_collision_points_map;
416416
417- // Fill collision_points array from different data sources
418- for (std::shared_ptr<Source> source : sources_) {
419- std::vector<Point> collision_points;
420- source->getData (curr_time, collision_points);
421- sources_collision_points_map.insert ({source->getSourceName (), collision_points});
422- }
423-
424417 // By default - there is no action
425418 Action robot_action{DO_NOTHING, cmd_vel_in, " " };
426419 // Polygon causing robot action (if any)
427420 std::shared_ptr<Polygon> action_polygon;
428421
429- // Fill collision_points array from different data sources
422+ // Fill collision points array from different data sources
430423 for (std::shared_ptr<Source> source : sources_) {
424+ std::vector<Point> collision_points;
425+ auto iter = sources_collision_points_map.insert ({source->getSourceName (), collision_points});
426+ bool is_source_valid = source->getData (curr_time, iter.first ->second );
427+
431428 if (source->getEnabled ()) {
432- if (!source->getData (curr_time, collision_points) &&
433- source->getSourceTimeout ().seconds () != 0.0 )
434- {
429+ if (!is_source_valid && source->getSourceTimeout ().seconds () != 0.0 ) {
435430 action_polygon = nullptr ;
436431 robot_action.polygon_name = " invalid source" ;
437432 robot_action.action_type = STOP;
@@ -441,34 +436,34 @@ void CollisionMonitor::process(const Velocity & cmd_vel_in, const std_msgs::msg:
441436 break ;
442437 }
443438 }
444- }
445439
446- if (collision_points_marker_pub_->get_subscription_count () > 0 ) {
447- // visualize collision points with markers
448- auto marker_array = std::make_unique<visualization_msgs::msg::MarkerArray>();
449- visualization_msgs::msg::Marker marker;
450- marker.header .frame_id = get_parameter (" base_frame_id" ).as_string ();
451- marker.header .stamp = rclcpp::Time (0 , 0 );
452- marker.ns = " collision_points" ;
453- marker.id = 0 ;
454- marker.type = visualization_msgs::msg::Marker::POINTS;
455- marker.action = visualization_msgs::msg::Marker::ADD;
456- marker.scale .x = 0.02 ;
457- marker.scale .y = 0.02 ;
458- marker.color .r = 1.0 ;
459- marker.color .a = 1.0 ;
460- marker.lifetime = rclcpp::Duration (0 , 0 );
461- marker.frame_locked = true ;
462-
463- for (const auto & point : collision_points) {
464- geometry_msgs::msg::Point p;
465- p.x = point.x ;
466- p.y = point.y ;
467- p.z = 0.0 ;
468- marker.points .push_back (p);
440+ if (collision_points_marker_pub_->get_subscription_count () > 0 ) {
441+ // visualize collision points with markers
442+ auto marker_array = std::make_unique<visualization_msgs::msg::MarkerArray>();
443+ visualization_msgs::msg::Marker marker;
444+ marker.header .frame_id = get_parameter (" base_frame_id" ).as_string ();
445+ marker.header .stamp = rclcpp::Time (0 , 0 );
446+ marker.ns = " collision_points" ;
447+ marker.id = 0 ;
448+ marker.type = visualization_msgs::msg::Marker::POINTS;
449+ marker.action = visualization_msgs::msg::Marker::ADD;
450+ marker.scale .x = 0.02 ;
451+ marker.scale .y = 0.02 ;
452+ marker.color .r = 1.0 ;
453+ marker.color .a = 1.0 ;
454+ marker.lifetime = rclcpp::Duration (0 , 0 );
455+ marker.frame_locked = true ;
456+
457+ for (const auto & point : iter.first ->second ) {
458+ geometry_msgs::msg::Point p;
459+ p.x = point.x ;
460+ p.y = point.y ;
461+ p.z = 0.0 ;
462+ marker.points .push_back (p);
463+ }
464+ marker_array->markers .push_back (marker);
465+ collision_points_marker_pub_->publish (std::move (marker_array));
469466 }
470- marker_array->markers .push_back (marker);
471- collision_points_marker_pub_->publish (std::move (marker_array));
472467 }
473468
474469 for (std::shared_ptr<Polygon> polygon : polygons_) {
@@ -483,37 +478,17 @@ void CollisionMonitor::process(const Velocity & cmd_vel_in, const std_msgs::msg:
483478 // Update polygon coordinates
484479 polygon->updatePolygon (cmd_vel_in);
485480
486- // Get collision points for sources associated to polygon
487- std::vector<std::string> sources_names = polygon->getSourcesNames ();
488- std::vector<Point> collision_points;
489- for (auto source_name : sources_names) {
490- try {
491- // Get vector for source
492- auto source_collision_points = sources_collision_points_map.at (source_name);
493- // Concatenate vectors
494- collision_points.insert (
495- collision_points.end (),
496- source_collision_points.begin (),
497- source_collision_points.end ());
498- } catch (std::exception & e) {
499- RCLCPP_ERROR_STREAM_THROTTLE (
500- get_logger (),
501- *get_clock (),
502- 1000 ,
503- " Observation source [" << source_name <<
504- " ] configured for polygon [" << polygon->getName () << " ] is not defined!" );
505- }
506- }
507-
508481 const ActionType at = polygon->getActionType ();
509482 if (at == STOP || at == SLOWDOWN || at == LIMIT) {
510483 // Process STOP/SLOWDOWN for the selected polygon
511- if (processStopSlowdownLimit (polygon, collision_points, cmd_vel_in, robot_action)) {
484+ if (processStopSlowdownLimit (
485+ polygon, sources_collision_points_map, cmd_vel_in, robot_action))
486+ {
512487 action_polygon = polygon;
513488 }
514489 } else if (at == APPROACH) {
515490 // Process APPROACH for the selected polygon
516- if (processApproach (polygon, collision_points , cmd_vel_in, robot_action)) {
491+ if (processApproach (polygon, sources_collision_points_map , cmd_vel_in, robot_action)) {
517492 action_polygon = polygon;
518493 }
519494 }
@@ -535,15 +510,15 @@ void CollisionMonitor::process(const Velocity & cmd_vel_in, const std_msgs::msg:
535510
536511bool CollisionMonitor::processStopSlowdownLimit (
537512 const std::shared_ptr<Polygon> polygon,
538- const std::vector<Point> & collision_points ,
513+ const std::unordered_map<std::string, std:: vector<Point>> & sources_collision_points_map ,
539514 const Velocity & velocity,
540515 Action & robot_action) const
541516{
542517 if (!polygon->isShapeSet ()) {
543518 return false ;
544519 }
545520
546- if (polygon->getPointsInside (collision_points ) >= polygon->getMinPoints ()) {
521+ if (polygon->getPointsInside (sources_collision_points_map ) >= polygon->getMinPoints ()) {
547522 if (polygon->getActionType () == STOP) {
548523 // Setting up zero velocity for STOP model
549524 robot_action.polygon_name = polygon->getName ();
@@ -590,7 +565,7 @@ bool CollisionMonitor::processStopSlowdownLimit(
590565
591566bool CollisionMonitor::processApproach (
592567 const std::shared_ptr<Polygon> polygon,
593- const std::vector<Point> & collision_points ,
568+ const std::unordered_map<std::string, std:: vector<Point>> & sources_collision_points_map ,
594569 const Velocity & velocity,
595570 Action & robot_action) const
596571{
@@ -599,7 +574,7 @@ bool CollisionMonitor::processApproach(
599574 }
600575
601576 // Obtain time before a collision
602- const double collision_time = polygon->getCollisionTime (collision_points , velocity);
577+ const double collision_time = polygon->getCollisionTime (sources_collision_points_map , velocity);
603578 if (collision_time >= 0.0 ) {
604579 // If collision will occurr, reduce robot speed
605580 const double change_ratio = collision_time / polygon->getTimeBeforeCollision ();
0 commit comments