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 import roslib; roslib.load_manifest('semanticmodel')
00039 import rospy
00040 import semanticmodel.plane_data_association as pd
00041 import threading
00042 from semanticmodel import msg
00043 from semanticmodel import srv
00044 import visualization_msgs.msg as vm
00045 import time
00046 import math
00047 import logging
00048 import sys
00049
00050
00051
00052 LOG = logging.getLogger('world_model.planes')
00053
00054
00055
00056 class PlaneSetEstimator:
00057 "Holds the ros independent logic for the plane set estimator"
00058
00059 def __init__(self, max_disp):
00060 "Initialize estimator"
00061 self.max_disp = max_disp
00062 self.planes = {}
00063 LOG.info('Initialized estimator')
00064
00065
00066 def process_plane(self, bp):
00067 "Combine with an existing plane or add a new one"
00068 int_planes = [id for id, existing_plane in self.planes.items()
00069 if pd.intersect(bp, existing_plane, self.max_disp)]
00070 LOG.debug("Intersecting planes are {0}".format(int_planes))
00071 if int_planes:
00072 self.planes = pd.merge_planes(self.planes, bp, self.max_disp)
00073 else:
00074 LOG.debug("Adding new plane {0}".format(len(self.planes)))
00075 self.planes[len(self.planes)] = bp
00076 LOG.debug("Done processing plane")
00077
00078
00079 def get_containing_plane(self, bp):
00080 int_planes = [pl for id, pl in self.planes.items()
00081 if pd.intersect(bp, pl, self.max_disp)]
00082 LOG.debug("Intersecting planes are {0}".format(int_planes))
00083 combined_plane = bp
00084 for p in int_planes:
00085 combined_plane = pd.combine(combined_plane, p)
00086 LOG.debug("Combined plane is now {0}".format(combined_plane))
00087 return combined_plane
00088
00089
00090
00091 class Node:
00092 "Ros wrapper for the plane state estimator"
00093
00094 def __init__(self):
00095 "Set up ros comms and initialize actual estimator"
00096 self.lock = threading.Lock()
00097 self.pe = PlaneSetEstimator(rospy.get_param("~max_plane_displacement", 0.1))
00098 with self.lock:
00099 self.sub = rospy.Subscriber('plane', msg.Plane, self.process_plane)
00100 self.pub = rospy.Publisher('planes', msg.Planes)
00101 self.vis_pub = rospy.Publisher('visualization_marker', vm.Marker)
00102 self.srv = rospy.Service('get_containing_plane', srv.PlaneExchange,
00103 self.get_containing_plane)
00104 LOG.info('Initialized node')
00105
00106 def process_plane(self, p):
00107 LOG.debug("Processing plane")
00108 bp = pd.BoundedPlane(m)
00109 with self.lock:
00110 self.pe.process_plane(bp)
00111
00112 def get_containing_plane(self, req):
00113 bp = pd.BoundedPlane(req.in_plane)
00114 LOG.debug("Getting containing plane for {0}".format(bp))
00115 with self.lock:
00116 return pd.to_plane_msg(self.pe.get_containing_plane(bp))
00117
00118 def spin(self):
00119 "Publish current estimate at fixed rate"
00120 r = rospy.Rate(0.3)
00121 while not rospy.is_shutdown():
00122 time.sleep(2.0)
00123
00124 with self.lock:
00125 planes = self.pe.planes
00126 self.pub.publish(map(pd.to_plane_msg, planes.values()))
00127 if len(planes)>0:
00128 self.vis_pub.publish(pd.to_marker(planes.values(), rospy.Time.now()))
00129
00130
00131
00132 def main():
00133 rospy.init_node('plane_set_estimator')
00134 node = Node()
00135 node.spin()
00136
00137 if __name__ == "__main__":
00138 main()