$search
00001 /* 00002 * visualiser.cpp 00003 * 00004 * Created on: May 7, 2012 00005 * Author: sdries 00006 */ 00007 00008 00009 #include "ros/ros.h" 00010 #include "tf/transform_listener.h" 00011 00012 // World model messages 00013 #include "wire_msgs/WorldEvidence.h" 00014 #include "wire_msgs/WorldState.h" 00015 00016 // Visualizer class 00017 #include "Visualizer.h" 00018 00019 // Conversions 00020 #include "problib/conversions.h" 00021 00022 using namespace std; 00023 00025 00026 // Visualizer instances for world model input and output 00027 Visualizer world_evidence_visualizer_; 00028 Visualizer world_state_visualizer_; 00029 00030 // Marker publishers 00031 ros::Publisher world_evidence_marker_pub_; 00032 ros::Publisher world_state_marker_pub_; 00033 00034 // Marker frame 00035 string marker_frame_ = ""; 00036 const string DEFAULT_MARKER_FRAME = "/map"; 00037 00038 00039 /* 00040 * World evidence callback. Creates a marker for each of the world model objects 00041 */ 00042 void worldEvidenceCallback(const wire_msgs::WorldEvidence::ConstPtr& msg) { 00043 00044 // ID for marker administration 00045 long ID = 0; 00046 00047 // Iterate over all elements of evidence 00048 for (vector<wire_msgs::ObjectEvidence>::const_iterator it = msg->object_evidence.begin(); it != msg->object_evidence.end(); ++it) { 00049 00050 // Get current evidence 00051 const wire_msgs::ObjectEvidence& obj_ev = *it; 00052 00053 // Create marker 00054 visualization_msgs::MarkerArray markers_msg; 00055 world_evidence_visualizer_.createMarkers(msg->header, ID++, obj_ev.properties, markers_msg.markers, marker_frame_); 00056 00057 // Publish marker 00058 world_evidence_marker_pub_.publish(markers_msg); 00059 00060 } 00061 } 00062 00063 00064 /* 00065 * World state callback. Creates a marker for each detection. 00066 */ 00067 void worldStateCallback(const wire_msgs::WorldState::ConstPtr& msg) { 00068 00069 00070 // Iterate over world state objects 00071 for (vector<wire_msgs::ObjectState>::const_iterator it = msg->objects.begin(); it != msg->objects.end(); ++it) { 00072 00073 // Get current object 00074 const wire_msgs::ObjectState& obj = *it; 00075 00076 // Create marker 00077 visualization_msgs::MarkerArray markers_msg; 00078 world_state_visualizer_.createMarkers(msg->header, obj.ID, obj.properties, markers_msg.markers, marker_frame_); 00079 00080 // Publish marker 00081 world_state_marker_pub_.publish(markers_msg); 00082 00083 } 00084 } 00085 00086 00087 /* 00088 * Loads the marker frame from the parameter server. Uses the default frame is it is not defined. 00089 */ 00090 bool getMarkerFrame(ros::NodeHandle& n) { 00091 00092 // Get marker frame from the parameter server 00093 XmlRpc::XmlRpcValue marker_frame; 00094 if (!n.getParam("marker_frame", marker_frame)) { 00095 ROS_WARN("No marker_frame defined, using default frame"); 00096 return false; 00097 } 00098 00099 // Check if the marker frame is of type string 00100 if (marker_frame.getType() != XmlRpc::XmlRpcValue::TypeString) { 00101 ROS_ERROR("Parameter marker_frame must be of type string, using default frame"); 00102 return false; 00103 } 00104 00105 // Store marker frame 00106 marker_frame_ = (string)marker_frame; 00107 00108 return true; 00109 } 00110 00111 00112 00113 int main(int argc, char **argv) { 00114 ros::init(argc, argv, "wire_viz"); 00115 ros::NodeHandle n("~"); 00116 00117 // Set parameters for visualizers 00118 if (!world_evidence_visualizer_.setParameters(n, "world_evidence")) { 00119 return 0; 00120 } 00121 if (!world_state_visualizer_.setParameters(n, "world_state")) { 00122 return 0; 00123 } 00124 00125 // Set tf listeners 00126 tf::TransformListener tf_listener; 00127 world_evidence_visualizer_.setTFListener(&tf_listener); 00128 world_state_visualizer_.setTFListener(&tf_listener); 00129 00130 // Get marker frame from parameter server 00131 if (!getMarkerFrame(n)) marker_frame_ = DEFAULT_MARKER_FRAME; 00132 00133 // Subscribe to world evidence and world state topics 00134 ros::Subscriber sub_world_ev = n.subscribe("/world_evidence", 10, &worldEvidenceCallback); 00135 ros::Subscriber sub_world_st = n.subscribe("/world_state", 10, &worldStateCallback); 00136 00137 // Advertise to marker topics 00138 world_evidence_marker_pub_ = n.advertise<visualization_msgs::MarkerArray>("/visualization_markers/world_evidence", 10); 00139 world_state_marker_pub_ = n.advertise<visualization_msgs::MarkerArray>("/visualization_markers/world_state", 10); 00140 00141 ros::spin(); 00142 return 0; 00143 }