marker_manager.cpp
Go to the documentation of this file.
00001 #include <generic_control_toolbox/marker_manager.hpp>
00002 
00003 namespace generic_control_toolbox
00004 {
00005 MarkerManager::MarkerManager() { n_ = ros::NodeHandle("~"); }
00006 
00007 MarkerManager::~MarkerManager() {}
00008 
00009 bool MarkerManager::addMarkerGroup(const std::string &group_key,
00010                                    const std::string &topic_name)
00011 {
00012   if (marker_map_.find(group_key) != marker_map_.end())
00013   {
00014     ROS_ERROR_STREAM("MarkerManager: Tried to add already existing group "
00015                      << group_key);
00016     return false;
00017   }
00018 
00019   std::shared_ptr<
00020       realtime_tools::RealtimePublisher<visualization_msgs::MarkerArray> >
00021       rt_pub(new realtime_tools::RealtimePublisher<
00022              visualization_msgs::MarkerArray>(n_, topic_name, 1));
00023   std::map<std::string, int> m;
00024 
00025   marker_pub_[group_key] = rt_pub;
00026   marker_map_[group_key] = m;
00027   marker_array_[group_key] = visualization_msgs::MarkerArray();
00028   return true;
00029 }
00030 
00031 bool MarkerManager::addMarker(const std::string &group_key,
00032                               const std::string &marker_name,
00033                               const std::string &ns,
00034                               const std::string &frame_id, MarkerType type)
00035 {
00036   if (marker_map_.find(group_key) == marker_map_.end())
00037   {
00038     return false;
00039   }
00040 
00041   int id;
00042   if (getMarkerId(group_key, marker_name, id))
00043   {
00044     ROS_ERROR_STREAM("MarkerManager: Tried to add marker "
00045                      << marker_name << " that is already on the marker array");
00046     return false;
00047   }
00048 
00049   visualization_msgs::Marker new_marker;
00050   new_marker.header.frame_id = frame_id;
00051   new_marker.header.stamp = ros::Time();
00052   new_marker.ns = ns;
00053 
00054   if (type == sphere)
00055   {
00056     new_marker.type = new_marker.SPHERE;
00057   }
00058   else
00059   {
00060     new_marker.type = new_marker.ARROW;
00061   }
00062 
00063   new_marker.action = new_marker.ADD;
00064   new_marker.scale.x = 0.01;
00065   new_marker.scale.y = 0.01;
00066   new_marker.scale.z = 0.01;
00067   new_marker.lifetime = ros::Duration(0);
00068   new_marker.frame_locked = false;
00069   new_marker.color.r = 1.0;
00070   new_marker.color.a = 1.0;
00071   new_marker.pose.orientation.w = 1.0;
00072 
00073   int max_id = -1;
00074   for (auto const &entry : marker_map_.at(group_key))
00075   {
00076     if (entry.second > max_id)
00077     {
00078       max_id = entry.second;
00079     }
00080   }
00081 
00082   new_marker.id = max_id + 1;
00083 
00084   marker_map_.at(group_key)[marker_name] = max_id + 1;
00085   marker_array_.at(group_key).markers.push_back(new_marker);
00086 
00087   return true;
00088 }
00089 
00090 bool MarkerManager::setMarkerColor(const std::string &group_key,
00091                                    const std::string &marker_name, double r,
00092                                    double g, double b)
00093 {
00094   if (marker_map_.find(group_key) == marker_map_.end())
00095   {
00096     return false;
00097   }
00098 
00099   int id;
00100   if (!getMarkerId(group_key, marker_name, id))
00101   {
00102     return false;
00103   }
00104 
00105   marker_array_.at(group_key).markers[id].color.r = r;
00106   marker_array_.at(group_key).markers[id].color.g = g;
00107   marker_array_.at(group_key).markers[id].color.b = b;
00108   return true;
00109 }
00110 
00111 bool MarkerManager::setMarkerScale(const std::string &group_key,
00112                                    const std::string &marker_name, double x,
00113                                    double y, double z)
00114 {
00115   if (marker_map_.find(group_key) == marker_map_.end())
00116   {
00117     return false;
00118   }
00119 
00120   int id;
00121   if (!getMarkerId(group_key, marker_name, id))
00122   {
00123     return false;
00124   }
00125 
00126   marker_array_.at(group_key).markers[id].scale.x = x;
00127   marker_array_.at(group_key).markers[id].scale.y = y;
00128   marker_array_.at(group_key).markers[id].scale.z = z;
00129   return true;
00130 }
00131 
00132 bool MarkerManager::setMarkerPoints(const std::string &group_key,
00133                                     const std::string &marker_name,
00134                                     const Eigen::Vector3d &initial_point,
00135                                     const Eigen::Vector3d &final_point)
00136 {
00137   if (marker_map_.find(group_key) == marker_map_.end())
00138   {
00139     return false;
00140   }
00141 
00142   int id;
00143   if (!getMarkerId(group_key, marker_name, id))
00144   {
00145     return false;
00146   }
00147 
00148   geometry_msgs::Point point;
00149   marker_array_.at(group_key).markers[id].points.clear();
00150   tf::pointEigenToMsg(initial_point, point);
00151   marker_array_.at(group_key).markers[id].points.push_back(point);
00152   tf::pointEigenToMsg(final_point, point);
00153   marker_array_.at(group_key).markers[id].points.push_back(point);
00154   return true;
00155 }
00156 
00157 bool MarkerManager::setMarkerPose(const std::string &group_key,
00158                                   const std::string &marker_name,
00159                                   const Eigen::Affine3d &pose)
00160 {
00161   if (marker_map_.find(group_key) == marker_map_.end())
00162   {
00163     return false;
00164   }
00165 
00166   int id;
00167   if (!getMarkerId(group_key, marker_name, id))
00168   {
00169     return false;
00170   }
00171 
00172   tf::poseEigenToMsg(pose, marker_array_.at(group_key).markers[id].pose);
00173   return true;
00174 }
00175 
00176 void MarkerManager::publishMarkers()
00177 {
00178   for (auto const &publisher : marker_pub_)
00179   {
00180     if (publisher.second->trylock())
00181     {
00182       publisher.second->msg_ = marker_array_.at(publisher.first);
00183       publisher.second->unlockAndPublish();
00184     }
00185   }
00186 }
00187 
00188 bool MarkerManager::getMarkerId(const std::string &group_key,
00189                                 const std::string &marker_name, int &id) const
00190 {
00191   if (marker_map_.find(group_key) == marker_map_.end())
00192   {
00193     return false;
00194   }
00195 
00196   for (auto const &it : marker_map_.at(group_key))
00197   {
00198     if (it.first == marker_name)
00199     {
00200       for (unsigned long i = 0; i < marker_array_.at(group_key).markers.size();
00201            i++)
00202       {
00203         if (marker_array_.at(group_key).markers[i].id == it.second)
00204         {
00205           id = it.second;
00206           return true;
00207         }
00208       }
00209     }
00210   }
00211 
00212   ROS_ERROR_STREAM("MarkerManager: Marker name " << marker_name
00213                                                  << " not found");
00214   return false;
00215 }
00216 }  // namespace generic_control_toolbox


generic_control_toolbox
Author(s): diogo
autogenerated on Thu Jun 6 2019 18:02:57