waypoint_provider.cpp
Go to the documentation of this file.
00001 /*
00002    Way point Provider
00003 
00004    inspired by yocs_waypoints_navi
00005 
00006    LICENSE : BSD - https://raw.github.com/yujinrobot/yujin_ocs/license/LICENSE
00007 
00008    Author : Jihoon Lee
00009    Date   : Dec 2013
00010  */
00011 
00012 #include "yocs_waypoint_provider/waypoint_provider.hpp"
00013 
00014 namespace yocs {
00015   WaypointProvider::WaypointProvider(ros::NodeHandle& n,
00016                                      yocs_msgs::WaypointList& wps,
00017                                      yocs_msgs::TrajectoryList& trajs) : nh_(n)
00018   {
00019 
00020     // setup pub
00021     waypoints_pub_ = nh_.advertise<yocs_msgs::WaypointList>("waypoints", 5, true);
00022     trajectories_pub_ = nh_.advertise<yocs_msgs::TrajectoryList>("trajectories", 5, true);
00023     waypoints_marker_pub_ = nh_.advertise<visualization_msgs::MarkerArray>("waypoint_markers", 5, true);
00024     trajectory_marker_pub_ = nh_.advertise<visualization_msgs::MarkerArray>("trajectory_markers", 5, true);
00025     // setup srv server
00026     waypoints_srv_ = nh_.advertiseService("request_waypoints", &WaypointProvider::processWaypointsService, this);
00027 
00028     waypoints_ = wps;
00029     trajectories_ = trajs;
00030     generateWaypointMarkers(waypoints_, waypoint_markers_);
00031     generateTrajectoryMarkers(trajectories_, trajectory_markers_);
00032 
00033     marker_index_ = 1000;
00034     label_index_ = 2000;
00035   }
00036 
00037 
00038   WaypointProvider::~WaypointProvider() {}
00039 
00040   bool WaypointProvider::processWaypointsService(yocs_msgs::WaypointListService::Request& request,
00041                                                  yocs_msgs::WaypointListService::Response& response)
00042   {
00043     ROS_INFO("Waypoint Manager : Received request");
00044     if(!initialized_) // return false if node is not initialized with points
00045     {
00046       response.success = false;
00047     }
00048     else {
00049       response.waypoints = this->waypoints_;
00050       response.success = true;
00051     }
00052     return true;
00053   }
00054 
00055   void WaypointProvider::generateWaypointMarkers(const yocs_msgs::WaypointList& wps,
00056                                                  visualization_msgs::MarkerArray& wp_viz)
00057   {
00058     wp_viz.markers.clear();
00059 
00060     unsigned int i;
00061 
00062     for(i = 0; i < wps.waypoints.size(); i++)
00063     {
00064       visualization_msgs::Marker marker;
00065       visualization_msgs::Marker label;
00066 
00067       createMarkerArrow(i, wps.waypoints[i], marker);
00068       createMarkerLabel(wps.waypoints[i].header.frame_id,
00069                         i,
00070                         "waypoint_labels",
00071                         wps.waypoints[i].name,
00072                         wps.waypoints[i].pose,
00073                         label);
00074 
00075       wp_viz.markers.push_back(marker);
00076       wp_viz.markers.push_back(label);
00077     }
00078   }
00079 
00080   void WaypointProvider::generateTrajectoryMarkers(const yocs_msgs::TrajectoryList& trajs,
00081                                                    visualization_msgs::MarkerArray& traj_markers)
00082   {
00083     traj_markers.markers.clear();
00084     for(unsigned int traj = 0; traj < trajs.trajectories.size(); ++traj)
00085     {
00086       visualization_msgs::Marker marker;
00087       createMarkerLineStrip(traj, trajs.trajectories[traj], marker);
00088       traj_markers.markers.push_back(marker);
00089       // no label, since way points with labels are already generated by for the waypoint marker publisher
00090     }
00091   }
00092 
00093   void WaypointProvider::createMarkerArrow(const int i,
00094                                            const yocs_msgs::Waypoint& wp,
00095                                            visualization_msgs::Marker& marker)
00096   {
00097     marker.header.frame_id = wp.header.frame_id;
00098     marker.header.stamp = ros::Time();
00099     marker.ns = "waypoints";
00100     marker.id = i + marker_index_;
00101     marker.pose = wp.pose;
00102     marker.type = visualization_msgs::Marker::ARROW;
00103     marker.action = visualization_msgs::Marker::ADD;
00104     marker.color.r = 0.0f;
00105     marker.color.g = 1.0f;
00106     marker.color.b = 0.0f;
00107     marker.color.a = 1.0f;
00108     marker.scale.x = 0.1;
00109     marker.scale.y = 0.1;
00110     marker.scale.z = 0.1;
00111 
00112   }
00113 
00114   void WaypointProvider::createMarkerLabel(const std::string frame_id,
00115                                            const int id,
00116                                            const std::string ns,
00117                                            const std::string wp_name,
00118                                            const geometry_msgs::Pose wp_pose,
00119                                            visualization_msgs::Marker& marker)
00120   {
00121     marker.header.frame_id = frame_id;
00122     marker.header.stamp = ros::Time();
00123     marker.ns = ns;
00124     marker.id = id + label_index_;
00125     marker.scale.x = 0.1;
00126     marker.scale.y = 0.1;
00127     marker.scale.z = 0.1;
00128     marker.color.r = 1.0f;
00129     marker.color.g = 1.0f;
00130     marker.color.b = 1.0f;
00131     marker.color.a = 1.0f;
00132     marker.pose = wp_pose;
00133     marker.pose.position.z = marker.pose.position.z + marker.scale.z / 2.0 + 0.05;  // just above the marker
00134     marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
00135     marker.action = visualization_msgs::Marker::ADD;
00136     marker.text = wp_name;
00137   }
00138 
00139   void WaypointProvider::createMarkerLineStrip(const int i,
00140                                                const yocs_msgs::Trajectory& traj,
00141                                                visualization_msgs::Marker& marker)
00142   {
00143     marker.header.frame_id = traj.waypoints[0].header.frame_id;
00144     marker.header.stamp = ros::Time();
00145     marker.ns = "trajectories";
00146     marker.id = i + marker_index_;
00147     marker.pose = geometry_msgs::Pose();
00148     marker.type = visualization_msgs::Marker::LINE_STRIP;
00149     marker.action = visualization_msgs::Marker::ADD;
00150     marker.color.r = 0.0f;
00151     marker.color.g = 0.0f;
00152     marker.color.b = 1.0f;
00153     marker.color.a = 1.0f;
00154     marker.scale.x = 0.1;
00155     marker.scale.y = 0.1;
00156     marker.scale.z = 0.1;
00157     for(unsigned int wp = 0; wp < traj.waypoints.size(); ++wp)
00158     {
00159       marker.points.push_back(traj.waypoints[wp].pose.position);
00160       marker.colors.push_back(marker.color);
00161     }
00162   }
00163 
00164   void WaypointProvider::spin() {
00165     waypoints_pub_.publish(waypoints_);
00166     trajectories_pub_.publish(trajectories_);
00167     waypoints_marker_pub_.publish(waypoint_markers_);
00168     trajectory_marker_pub_.publish(trajectory_markers_);
00169     initialized_ = true;
00170     ros::spin();
00171   }
00172 }


yocs_waypoint_provider
Author(s): Jihoon Lee
autogenerated on Thu Jun 6 2019 21:47:44