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
00061
00062
00063
00064 import rospy
00065 import unique_id
00066
00067 import warehouse_ros as wr
00068
00069 from nav_msgs.msg import *
00070 from nav_msgs.srv import *
00071
00072 from world_canvas_msgs.msg import *
00073 from world_canvas_msgs.srv import *
00074 from world_canvas_utils.serialization import *
00075
00076
00077 class MapManager:
00078
00079
00080
00081
00082
00083 def __init__(self):
00084
00085 self.map_collection = wr.MessageCollection('world_canvas', 'maps', OccupancyGrid)
00086 self.map_collection.ensure_index('uuid', unique=True)
00087
00088
00089 self.list_maps_srv = rospy.Service('list_maps', ListMaps, self.list_maps)
00090 self.publish_map_srv = rospy.Service('publish_map', PublishMap, self.publish_map)
00091 self.delete_map_srv = rospy.Service('delete_map', DeleteMap, self.delete_map)
00092 self.rename_map_srv = rospy.Service('rename_map', RenameMap, self.rename_map)
00093 self.save_map_srv = rospy.Service('save_map', SaveMap, self.save_map)
00094 self.dynamic_map_srv = rospy.Service('dynamic_map', GetMap, self.dynamic_map)
00095
00096
00097 self.map_subscriber = rospy.Subscriber('map', OccupancyGrid, self.on_map_received, queue_size=1)
00098
00099
00100 self.rec_session = RecSession(self)
00101
00102
00103 self.map_publisher = rospy.Publisher('map', OccupancyGrid, latch=True, queue_size=1)
00104
00105 try:
00106 self.last_map = rospy.get_param('~last_map_id')
00107 map = self.lookup_map(self.last_map)
00108 if map is None:
00109 rospy.logerr("Invalid last_map_id: %s" % str(self.last_map))
00110 else:
00111 self.map_publisher.publish(map)
00112 except KeyError:
00113 self.last_map = None
00114
00115 rospy.loginfo("Map manager : initialized.")
00116
00117
00118
00119
00120
00121
00122 def list_maps(self, request):
00123 rospy.logdebug("Service call : list_maps");
00124
00125 response = ListMapsResponse()
00126
00127 all_maps = self.map_collection.query({}, metadata_only=True, sort_by='creation_time', ascending=False)
00128
00129
00130 while True:
00131 try:
00132 map_md = all_maps.next()
00133 rospy.logdebug("Add map to result list: %s" % map_md)
00134
00135
00136 new_entry = MapListEntry()
00137 new_entry.name = map_md.get('name', '')
00138 new_entry.date = map_md['creation_time']
00139 new_entry.session_id = map_md['session_id']
00140 new_entry.map_id = map_md['uuid']
00141
00142 response.map_list.append(new_entry)
00143 except StopIteration:
00144 break
00145
00146 return response
00147
00148
00149 def lookup_map(self, uuid):
00150 rospy.logdebug("Load map %s" % uuid)
00151 matching_maps = self.map_collection.query({'uuid': {'$in': [uuid]}})
00152 try:
00153 return matching_maps.next()[0]
00154 except StopIteration:
00155 rospy.logerr("No map found for uuid %s" % uuid)
00156 return None
00157
00158
00159 def publish_map(self, request):
00160 rospy.logdebug("Service call : publish_map %s" % request.map_id)
00161 response = PublishMapResponse()
00162
00163 map = self.lookup_map(request.map_id)
00164 if map is None:
00165 rospy.logerr("Invalid map id: %s" % str(request.map_id))
00166 return None
00167 else:
00168 self.last_map = request.map_id
00169 rospy.set_param('~last_map_id', self.last_map)
00170 self.map_publisher.publish(map)
00171
00172 return response
00173
00174 def delete_map(self, request):
00175 rospy.logdebug("Service call : delete map %s" % request.map_id)
00176 response = DeleteMapResponse()
00177
00178 if rospy.has_param('~last_map_id') and rospy.get_param('~last_map_id') == request.map_id:
00179 rospy.delete_param('~last_map_id')
00180 if self.last_map == request.map_id:
00181 self.last_map = None
00182
00183 if self.map_collection.remove({'uuid': {'$in': [request.map_id]}}) == 0:
00184 return None
00185
00186 return response
00187
00188 def rename_map(self, request):
00189 rospy.logdebug("Service call : rename map %s as %s" % (request.map_id, request.new_name))
00190 response = RenameMapResponse()
00191
00192 map_metadata = self.get_metadata(request.map_id)
00193 if map_metadata is None:
00194 return None
00195
00196 map_metadata['name'] = request.new_name
00197 self.map_collection.update(map_metadata)
00198 return response
00199
00200 def dynamic_map(self, request):
00201 rospy.logdebug("Service call : get last map (%s)" % self.last_map)
00202 response = GetMapResponse()
00203
00204 if self.last_map is None:
00205 return None
00206
00207 map = self.lookup_map(self.last_map)
00208 if map is None:
00209 return None
00210
00211 response.map = map;
00212 return response
00213
00214 def on_map_received(self, map_msg):
00215 self.rec_session.map = map_msg
00216
00217 if not self.rec_session.auto_save:
00218 rospy.logdebug("Map received but auto-save map is off")
00219 else:
00220 self.rec_session.save()
00221
00222 def save_map(self, request):
00223 rospy.logdebug("Service call : save current map as %s" % request.map_name)
00224 response = SaveMapResponse()
00225
00226 if self.rec_session.save(request.map_name) == False:
00227 return None
00228
00229 return response
00230
00231 def get_metadata(self, uuid):
00232
00233 matching_maps = self.map_collection.query({'uuid': {'$in': [uuid]}}, True)
00234 try:
00235 return matching_maps.next()
00236 except StopIteration:
00237 rospy.logerr("Map %s not found" % uuid)
00238 return None
00239
00240
00241 class RecSession:
00242 '''
00243 Keep track of incoming maps (through 'map' topic) so we can save automatically (if parameter
00244 auto_save_map is set as true) or under demand (through save_map service).
00245 To avoid spaming the database as on map_store/map_saver, we use the same metadata (with the
00246 same uuid) so we instead overwrite the current session map with new data.
00247 See https://github.com/ros-planning/map_store/issues/4 for more details.
00248 '''
00249 def __init__(self, parent):
00250
00251 self.parent = parent
00252 self.map = None
00253
00254
00255
00256 self.metadata = {'uuid': str(unique_id.fromRandom()),
00257 'session_id': str(rospy.get_time())
00258 }
00259 self.auto_save = rospy.get_param('~auto_save_map', False)
00260 self.map_saved = False
00261
00262 def save(self, name=None):
00263 if name is not None:
00264 self.metadata['name'] = name
00265
00266 if self.map is None:
00267 rospy.logerr("No map received so far! Nothing to save")
00268 return False
00269
00270 if not self.map_saved:
00271 try:
00272 self.parent.map_collection.insert(self.map, self.metadata, safe=True)
00273 self.metadata = self.parent.get_metadata(self.metadata['uuid'])
00274 if self.metadata is None:
00275
00276 rospy.logerr("Map %s not found just after inserting it???" % request.map_id)
00277 self.map_saved = True
00278 except Exception as e:
00279
00280
00281 rospy.logerr("Insert map failed: %s" % str(e))
00282 return False
00283
00284 rospy.logdebug("Saved map %d by %d @ %f%s" % (self.map.info.width, self.map.info.height,
00285 self.map.info.resolution, ' as ' + name if name else ''))
00286 else:
00287 self.metadata['creation_time'] = rospy.Time.now().to_sec()
00288 self.parent.map_collection.update(self.metadata, msg=self.map)
00289 rospy.logdebug("Updated map %d by %d @ %f%s" % (self.map.info.width, self.map.info.height,
00290 self.map.info.resolution, ' as ' + name if name else ''))
00291 return True