Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #include <mongo_ros/message_collection.h>
00061 #include <ros/ros.h>
00062 #include <visualization_msgs/MarkerArray.h>
00063 #include <world_canvas_msgs/Annotation.h>
00064 #include <world_canvas_msgs/AnnotationData.h>
00065
00066 #include <world_canvas_msgs/LoadAnnotationsData.h>
00067 #include <world_canvas_msgs/SaveAnnotationsData.h>
00068
00069 #include <string>
00070 #include <sstream>
00071 #include <exception>
00072 #include <uuid/uuid.h>
00073
00074 namespace mr=mongo_ros;
00075
00076 mr::MessageCollection<world_canvas_msgs::Annotation> *anns_collection;
00077 mr::MessageCollection<world_canvas_msgs::AnnotationData> *data_collection;
00078
00079 ros::Publisher map_pub;
00080 ros::Publisher markers_pub;
00081
00082 visualization_msgs::MarkerArray markers_array;
00083
00084
00085
00086 typedef std::vector<mr::MessageWithMetadata<world_canvas_msgs::Annotation>::ConstPtr> AnnsVector;
00087 typedef std::vector<mr::MessageWithMetadata<world_canvas_msgs::AnnotationData>::ConstPtr> DataVector;
00088
00089 std::string uuid2str(unsigned char* pUuid) {
00090 uuid_t uuid;
00091 memcpy(uuid, pUuid, sizeof(uuid_t));
00092 char uuid_string[37];
00093 uuid_unparse_lower(uuid, uuid_string);
00094 return std::string(uuid_string);
00095 }
00096
00097
00098
00099
00100
00101
00102
00103 void clearMarkers()
00104 {
00105 for (int i = 0; i < markers_array.markers.size(); ++i)
00106 {
00107 markers_array.markers[i].action = visualization_msgs::Marker::DELETE;
00108 }
00109
00110 if (markers_array.markers.size() > 0)
00111 {
00112 markers_pub.publish(markers_array);
00113 markers_array.markers.clear();
00114 }
00115 }
00116
00117 visualization_msgs::Marker makeMarker(int id, const world_canvas_msgs::Annotation& ann)
00118 {
00119 std::stringstream name; name << ann.type << '/' << ann.name;
00120
00121 visualization_msgs::Marker marker;
00122 marker.header.frame_id = ann.pose.header.frame_id;
00123 marker.header.stamp = ros::Time::now();
00124 marker.scale = ann.size;
00125 marker.color = ann.color;
00126 marker.ns = name.str();
00127 marker.id = id;
00128 marker.pose = ann.pose.pose.pose;
00129 marker.type = ann.shape;
00130 marker.action = visualization_msgs::Marker::ADD;
00131
00132
00133
00134 marker.pose.position.z += marker.scale.z/2.0;
00135
00136 return marker;
00137 }
00138
00139 visualization_msgs::Marker makeLabel(const visualization_msgs::Marker& marker)
00140 {
00141 visualization_msgs::Marker label = marker;
00142 label.id = marker.id + 1000000;
00143 label.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
00144 label.pose.position.z = marker.pose.position.z + marker.scale.z/2.0 + 0.1;
00145 label.text = marker.ns;
00146 label.scale.x = label.scale.y = label.scale.z = 0.12;
00147
00148
00149 return label;
00150 }
00151
00152
00153
00154
00155
00156
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 bool loadAnnotationsData(world_canvas_msgs::LoadAnnotationsData::Request &request,
00214 world_canvas_msgs::LoadAnnotationsData::Response &response)
00215 {
00216 try
00217 {
00218 AnnsVector matching_anns =
00219 anns_collection->pullAllResults(mr::Query("map_uuid", request.map_uuid));
00220
00221 if (matching_anns.size() == 0)
00222 {
00223 ROS_INFO("No annotations found for map '%s'; we don't consider this an error",
00224 request.map_uuid.c_str());
00225 response.result = true;
00226 return true;
00227 }
00228
00229 DataVector matching_data =
00230 data_collection->pullAllResults(mr::Query("map_uuid", request.map_uuid));
00231
00232 if (matching_anns.size() != matching_data.size())
00233 {
00234
00235
00236 ROS_ERROR("Pulled annotations and associated data don't match (%lu != %lu)",
00237 matching_anns.size(), matching_data.size());
00238 response.message = "Pulled annotations and associated data don't match";
00239 response.result = false;
00240 return false;
00241 }
00242
00243 response.annotations.reserve(matching_anns.size());
00244 response.data.reserve(matching_data.size());
00245 for (int i = 0; i < matching_anns.size(); ++i)
00246 {
00247 response.annotations.push_back(*matching_anns[i]);
00248 response.data.push_back(*matching_data[i]);
00249 }
00250
00251 ROS_INFO("%lu annotations loaded", matching_anns.size());
00252 response.result = true;
00253 return true;
00254 }
00255 catch(const std::exception &e) {
00256 ROS_ERROR("Error during query: %s", e.what());
00257 response.message = e.what();
00258 response.result = false;
00259 return false;
00260 }
00261 }
00262
00263 bool saveAnnotationsData(world_canvas_msgs::SaveAnnotationsData::Request &request,
00264 world_canvas_msgs::SaveAnnotationsData::Response &response)
00265 {
00266 for (int i = 0; i < request.annotations.size(); ++i)
00267 {
00268 world_canvas_msgs::Annotation annotation = request.annotations[i];
00269 world_canvas_msgs::AnnotationData data = request.data[i];
00271 std::string annotation_id = uuid2str(annotation.id.uuid.c_array());
00272 mr::Metadata metadata = mr::Metadata("map_uuid", annotation.map_uuid,
00274 "id", annotation_id);
00275
00276
00277
00278
00279
00280
00281
00282 ROS_DEBUG("Saving annotation %s for map %s", annotation_id.c_str(), annotation.map_uuid.c_str());
00283 try
00284 {
00286 anns_collection->removeMessages(mr::Query("id", annotation_id));
00287 anns_collection->insert(annotation, metadata);
00288 data_collection->removeMessages(mr::Query("id", annotation_id));
00289 data_collection->insert(data, metadata);
00290 }
00291 catch (mongo::DBException& e)
00292 {
00293 ROS_ERROR("Error during saving: %s", e.what());
00294 response.message = e.what();
00295 response.result = false;
00296 return false;
00297 }
00298 }
00299
00300 ROS_INFO("%lu annotations saved", request.annotations.size());
00301 response.result = true;
00302 return true;
00303 }
00304
00305
00306 int main (int argc, char** argv)
00307 {
00308 ros::init(argc, argv, "annotations_server");
00309 ros::NodeHandle nh;
00310
00311 anns_collection = new mr::MessageCollection<world_canvas_msgs::Annotation> ("world_canvas", "annotations");
00312 anns_collection->ensureIndex("id");
00313
00314 data_collection = new mr::MessageCollection<world_canvas_msgs::AnnotationData> ("world_canvas", "annotations_data");
00315 data_collection->ensureIndex("id");
00316
00317
00318 markers_pub = nh.advertise<visualization_msgs::MarkerArray> ("visual_markers", 1, true);
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 ros::ServiceServer load_data_srv = nh.advertiseService("load_annotations_data", loadAnnotationsData);
00342 ros::ServiceServer save_data_srv = nh.advertiseService("save_annotations_data", saveAnnotationsData);
00343
00344
00345
00346
00347
00348 ROS_DEBUG("Annotations server running");
00349
00350 ros::spin();
00351
00352 delete anns_collection;
00353 delete data_collection;
00354
00355 return 0;
00356 }