debug_obstacle_distance_node.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include <string>
00019 #include <map>
00020 #include <ros/ros.h>
00021 #include <visualization_msgs/MarkerArray.h>
00022 #include "cob_control_msgs/ObstacleDistance.h"
00023 #include "cob_control_msgs/ObstacleDistances.h"
00024 
00025 class DebugObstacleDistance
00026 {
00027     ros::NodeHandle nh_;
00028     ros::Publisher marker_pub_;
00029     ros::Subscriber obstacle_distances_sub_;
00030 
00031     std::string chain_base_link_;
00032 
00033 public:
00034     int init()
00035     {
00036         if (!nh_.getParam("chain_base_link", this->chain_base_link_))
00037         {
00038             ROS_ERROR("Failed to get parameter \"chain_base_link\".");
00039             return -1;
00040         }
00041 
00042         marker_pub_ = this->nh_.advertise<visualization_msgs::MarkerArray>("obstacle_distance/distance_markers", 1, true);
00043         obstacle_distances_sub_ = this->nh_.subscribe("obstacle_distance", 1, &DebugObstacleDistance::obstacleDistancesCallback, this);
00044 
00045         return 0;
00046     }
00047 
00048 
00049     void obstacleDistancesCallback(const cob_control_msgs::ObstacleDistances::ConstPtr& msg)
00050     {
00051         visualization_msgs::MarkerArray marker_array;
00052         std::map<std::string, cob_control_msgs::ObstacleDistance> relevant_obstacle_distances;
00053 
00054         for (uint32_t i = 0; i < msg->distances.size(); i++)
00055         {
00056             const std::string id = msg->distances[i].link_of_interest;
00057             if (relevant_obstacle_distances.count(id) > 0)
00058             {
00059                 if (relevant_obstacle_distances[id].distance > msg->distances[i].distance)
00060                 {
00061                     relevant_obstacle_distances[id] = msg->distances[i];
00062                 }
00063             }
00064             else
00065             {
00066                 relevant_obstacle_distances[id] = msg->distances[i];
00067             }
00068         }
00069 
00070         for (std::map<std::string, cob_control_msgs::ObstacleDistance>::const_iterator it = relevant_obstacle_distances.begin();
00071                 it != relevant_obstacle_distances.end(); ++it)
00072         {
00073             // show distance vector as arrow
00074             visualization_msgs::Marker marker_vector;
00075             marker_vector.type = visualization_msgs::Marker::ARROW;
00076             marker_vector.lifetime = ros::Duration(0.5);
00077             marker_vector.action = visualization_msgs::Marker::ADD;
00078             marker_vector.ns = it->first;
00079             marker_vector.id = 42;
00080             marker_vector.header.frame_id = chain_base_link_;
00081 
00082             marker_vector.scale.x = 0.01;
00083             marker_vector.scale.y = 0.05;
00084 
00085             geometry_msgs::Point start;
00086             start.x = it->second.nearest_point_obstacle_vector.x;
00087             start.y = it->second.nearest_point_obstacle_vector.y;
00088             start.z = it->second.nearest_point_obstacle_vector.z;
00089 
00090             geometry_msgs::Point end;
00091             end.x = it->second.nearest_point_frame_vector.x;
00092             end.y = it->second.nearest_point_frame_vector.y;
00093             end.z = it->second.nearest_point_frame_vector.z;
00094 
00095             marker_vector.color.a = 1.0;
00096             marker_vector.color.g = 1.0;
00097 
00098             marker_vector.points.push_back(start);
00099             marker_vector.points.push_back(end);
00100             marker_array.markers.push_back(marker_vector);
00101 
00102 
00103             // show distance as text
00104             visualization_msgs::Marker marker_distance;
00105             marker_distance.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
00106             marker_distance.lifetime = ros::Duration(0.5);
00107             marker_distance.action = visualization_msgs::Marker::ADD;
00108             marker_distance.ns = it->first;
00109             marker_distance.id = 69;
00110             marker_distance.header.frame_id = chain_base_link_;
00111             marker_distance.text = boost::lexical_cast<std::string>(boost::format("%.3f") % it->second.distance);
00112 
00113             marker_distance.scale.x = 0.1;
00114             marker_distance.scale.y = 0.1;
00115             marker_distance.scale.z = 0.1;
00116 
00117             marker_distance.color.a = 1.0;
00118             // marker_distance.color.r = 1.0;
00119             // marker_distance.color.g = 1.0;
00120             // marker_distance.color.b = 1.0;
00121             marker_distance.color.r = 0.0;
00122             marker_distance.color.g = 0.0;
00123             marker_distance.color.b = 0.0;
00124 
00125             marker_distance.pose.position.x = it->second.nearest_point_frame_vector.x;
00126             marker_distance.pose.position.y = it->second.nearest_point_frame_vector.y + 0.05;
00127             marker_distance.pose.position.z = it->second.nearest_point_frame_vector.z;
00128 
00129             marker_array.markers.push_back(marker_distance);
00130         }
00131 
00132         this->marker_pub_.publish(marker_array);
00133     }
00134 };
00135 
00136 
00137 
00138 int main(int argc, char** argv)
00139 {
00140     ros::init(argc, argv, "debug_obstacle_distance_node");
00141 
00142     DebugObstacleDistance dod;
00143     if (dod.init() != 0)
00144     {
00145         ROS_ERROR("Failed to initialize DebugDistanceManager.");
00146         return -1;
00147     }
00148 
00149     ros::spin();
00150 }
00151 


cob_obstacle_distance
Author(s): Marco Bezzon
autogenerated on Thu Jun 6 2019 21:19:14