00001 import rospy
00002 import tf
00003 from numpy import array
00004 import actionlib
00005 from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
00006 from nav_msgs.srv import GetPlan
00007 from geometry_msgs.msg import PoseStamped
00008 from numpy import floor
00009 from numpy.linalg import norm
00010 from numpy import inf
00011
00012 class robot:
00013 goal = MoveBaseGoal()
00014 start = PoseStamped()
00015 end = PoseStamped()
00016
00017 def __init__(self,name):
00018 self.assigned_point=[]
00019 self.name=name
00020 self.global_frame=rospy.get_param('~global_frame','/map')
00021 self.listener=tf.TransformListener()
00022 self.listener.waitForTransform(self.global_frame, name+'/base_link', rospy.Time(0),rospy.Duration(10.0))
00023 cond=0;
00024 while cond==0:
00025 try:
00026 (trans,rot) = self.listener.lookupTransform(self.global_frame, self.name+'/base_link', rospy.Time(0))
00027 cond=1
00028 except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
00029 cond==0
00030 self.position=array([trans[0],trans[1]])
00031 self.assigned_point=self.position
00032 self.client=actionlib.SimpleActionClient(self.name+'/move_base', MoveBaseAction)
00033 self.client.wait_for_server()
00034 robot.goal.target_pose.header.frame_id=self.global_frame
00035 robot.goal.target_pose.header.stamp=rospy.Time.now()
00036
00037 rospy.wait_for_service(self.name+'/move_base_node/NavfnROS/make_plan')
00038 self.make_plan = rospy.ServiceProxy(self.name+'/move_base_node/NavfnROS/make_plan', GetPlan)
00039 robot.start.header.frame_id=self.global_frame
00040 robot.end.header.frame_id=self.global_frame
00041
00042 def getPosition(self):
00043 cond=0;
00044 while cond==0:
00045 try:
00046 (trans,rot) = self.listener.lookupTransform(self.global_frame, self.name+'/base_link', rospy.Time(0))
00047 cond=1
00048 except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
00049 cond==0
00050 self.position=array([trans[0],trans[1]])
00051 return self.position
00052
00053 def sendGoal(self,point):
00054 robot.goal.target_pose.pose.position.x=point[0]
00055 robot.goal.target_pose.pose.position.y=point[1]
00056 robot.goal.target_pose.pose.orientation.w = 1.0
00057 self.client.send_goal(robot.goal)
00058 self.assigned_point=array(point)
00059
00060 def cancelGoal(self):
00061 self.client.cancel_goal()
00062 self.assigned_point=self.getPosition()
00063
00064 def getState(self):
00065 return self.client.get_state()
00066
00067 def makePlan(self,start,end):
00068 robot.start.pose.position.x=start[0]
00069 robot.start.pose.position.y=start[1]
00070 robot.end.pose.position.x=end[0]
00071 robot.end.pose.position.y=end[1]
00072 start=self.listener.transformPose(self.name+'/map', robot.start)
00073 end=self.listener.transformPose(self.name+'/map', robot.end)
00074 plan=self.make_plan(start = start, goal = end, tolerance = 0.0)
00075 return plan.plan.poses
00076
00077
00078 def index_of_point(mapData,Xp):
00079 resolution=mapData.info.resolution
00080 Xstartx=mapData.info.origin.position.x
00081 Xstarty=mapData.info.origin.position.y
00082 width=mapData.info.width
00083 Data=mapData.data
00084 index=int( ( floor((Xp[1]-Xstarty)/resolution)*width)+( floor((Xp[0]-Xstartx)/resolution) ))
00085 return index
00086
00087 def point_of_index(mapData,i):
00088 y=mapData.info.origin.position.y+(i/mapData.info.width)*mapData.info.resolution
00089 x=mapData.info.origin.position.x+(i-(i/mapData.info.width)*(mapData.info.width))*mapData.info.resolution
00090 return array([x,y])
00091
00092
00093 def informationGain(mapData,point,r):
00094 infoGain=0;
00095 index=index_of_point(mapData,point)
00096 r_region=int(r/mapData.info.resolution)
00097 init_index=index-r_region*(mapData.info.width+1)
00098 for n in range(0,2*r_region+1):
00099 start=n*mapData.info.width+init_index
00100 end=start+2*r_region
00101 limit=((start/mapData.info.width)+2)*mapData.info.width
00102 for i in range(start,end+1):
00103 if (i>=0 and i<limit and i<len(mapData.data)):
00104 if(mapData.data[i]==-1 and norm(array(point)-point_of_index(mapData,i))<=r):
00105 infoGain+=1
00106 return infoGain*(mapData.info.resolution**2)
00107
00108
00109 def discount(mapData,assigned_pt,centroids,infoGain,r):
00110 index=index_of_point(mapData,assigned_pt)
00111 r_region=int(r/mapData.info.resolution)
00112 init_index=index-r_region*(mapData.info.width+1)
00113 for n in range(0,2*r_region+1):
00114 start=n*mapData.info.width+init_index
00115 end=start+2*r_region
00116 limit=((start/mapData.info.width)+2)*mapData.info.width
00117 for i in range(start,end+1):
00118 if (i>=0 and i<limit and i<len(mapData.data)):
00119 for j in range(0,len(centroids)):
00120 current_pt=centroids[j]
00121 if(mapData.data[i]==-1 and norm(point_of_index(mapData,i)-current_pt)<=r and norm(point_of_index(mapData,i)-assigned_pt)<=r):
00122 infoGain[j]-=1
00123 return infoGain
00124
00125
00126 def pathCost(path):
00127 if (len(path)>0):
00128 i=len(path)/2
00129 p1=array([path[i-1].pose.position.x,path[i-1].pose.position.y])
00130 p2=array([path[i].pose.position.x,path[i].pose.position.y])
00131 return norm(p1-p2)*(len(path)-1)
00132 else:
00133 return inf
00134
00135
00136 def unvalid(mapData,pt):
00137 index=index_of_point(mapData,pt)
00138 r_region=5
00139 init_index=index-r_region*(mapData.info.width+1)
00140 for n in range(0,2*r_region+1):
00141 start=n*mapData.info.width+init_index
00142 end=start+2*r_region
00143 limit=((start/mapData.info.width)+2)*mapData.info.width
00144 for i in range(start,end+1):
00145 if (i>=0 and i<limit and i<len(mapData.data)):
00146 if(mapData.data[i]==1):
00147 return True
00148 return False
00149
00150 def Nearest(V,x):
00151 n=inf
00152 i=0
00153 for i in range(0,V.shape[0]):
00154 n1=norm(V[i,:]-x)
00155 if (n1<n):
00156 n=n1
00157 result=i
00158 return result
00159
00160
00161 def Nearest2(V,x):
00162 n=inf
00163 result=0
00164 for i in range(0,len(V)):
00165 n1=norm(V[i]-x)
00166
00167 if (n1<n):
00168 n=n1
00169 return i
00170
00171 def gridValue(mapData,Xp):
00172 resolution=mapData.info.resolution
00173 Xstartx=mapData.info.origin.position.x
00174 Xstarty=mapData.info.origin.position.y
00175
00176 width=mapData.info.width
00177 Data=mapData.data
00178
00179
00180 index=( floor((Xp[1]-Xstarty)/resolution)*width)+( floor((Xp[0]-Xstartx)/resolution) )
00181
00182 if int(index) < len(Data):
00183 return Data[int(index)]
00184 else:
00185 return 100
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
00214
00215
00216
00217
00218
00219
00220
00221