# # Include files # include "taskallocate/graphs/shapes_P.bzz" include "taskallocate/graphs/shapes_O.bzz" include "taskallocate/graphs/shapes_L.bzz" include "taskallocate/graphs/shapes_Y.bzz" ROBOT_RADIUS = 50 ROBOT_DIAMETER = 2.0*ROBOT_RADIUS ROBOT_SAFETYDIST = 2.0*ROBOT_DIAMETER ROOT_ID = 2 graph_id = 3 graph_loop = 1 # # Global variables # # # Save message from all neighours #the indexes are as 1,2,3..., while each value is a table that store the information of a neighbour robot m_MessageState={}#store received neighbour message m_MessageLabel={}#store received neighbour message m_MessageReqLabel={}#store received neighbour message m_MessageReqID={}#store received neighbour message m_MessageResponse={}#store received neighbour message m_MessageRange={}#store received neighbour message m_MessageBearing={}#store received neighbour message m_neighbourCount=0#used to cunt neighbours #Save message from one neighbour #the indexes are as State(received state),Label(received Label),ReqLabel,ReqID,Response,Range,Bearing m_receivedMessage={.State=s2i("GRAPH_FREE"),.Label=0,.ReqLabel=0,.ReqID=0,.Response=r2i("REQ_NONE"),.Range=0,.Bearing=0} # #Save the message to send #The keys of the talbe is State(current state),Label(current Label),ReqLabel(requested Label),ReqID(request id),Response(reply message{REQ_NONE,REQ_GRANTED,REQ_RESEND}) m_selfMessage={.State=s2i("GRAPH_FREE"),.Label=0,.ReqLabel=0,.ReqID=0,.Response=r2i("REQ_NONE")} #navigation vector m_navigation={.x=0,.y=0} #Current label being requested or chosen (-1 when none) m_nLabel=-1 m_messageID={} repeat_assign=0 assign_label=-1 assign_id=-1 m_gotjoinedparent = 0 #neighbor distance to lock the current pattern lock_neighbor_id={} lock_neighbor_dis={} #Label request id m_unRequestId=0 #Global bias, used to map local coordinate to global coordinate m_bias=0 #Vector to predecessor,range is the distance between robots, bearing is the angle of pred wrt self in local coordinate of self, globalbearing is the angle of self wrt pred in global coordinate m_cMeToPred={.Range=0.0,.Bearing=0.0,.GlobalBearing=0.0} #Counter to wait for something to happen m_unWaitCount=0 #Number of steps to wait before looking for a free label m_unLabelSearchWaitTime=0 #Number of steps to wait for an answer to be received m_unResponseTimeThreshold=0 #Number of steps to wait until giving up joining m_unJoiningLostPeriod=0 #Tolerance distance to a target location m_fTargetDistanceTolerance=0 # virtual stigmergy for the LOCK barrier. m_lockstig = 1 # Lennard-Jones parameters, may need change EPSILON_GRAPH = 4000 #13.5 the LJ parameter for other robots # #return the number of value in table # function count(table,value){ var number=0 var i=0 while(i=0){ pon=0 } else{ pon=1 } var b=math.abs(send_table.Bearing) send_value=r_id*1000+pon*100+b return send_value } # #unpack message # function unpackmessage(recv_value){ var wan=(recv_value-recv_value%1000000)/1000000 recv_value=recv_value-wan*1000000 var qian=(recv_value-recv_value%100000)/100000 recv_value=recv_value-qian*100000 var bai=(recv_value-recv_value%10000)/10000 recv_value=recv_value-bai*10000 var shi=(recv_value-recv_value%100)/100 recv_value=recv_value-shi*100 var ge=recv_value var return_table={.State=0.0,.Label=0.0,.ReqLabel=0.0,.ReqID=0.0,.Response=0.0} return_table.State=wan return_table.Label=qian return_table.ReqLabel=bai return_table.ReqID=shi return_table.Response=ge return return_table } # #unpack guide message # function unpack_guide_msg(recv_value){ #log(id,"I pass value=",recv_value) var qian=(recv_value-recv_value%1000)/1000 recv_value=recv_value-qian*1000 var bai=(recv_value-recv_value%100)/100 recv_value=recv_value-bai*100 var b=recv_value var return_table={.Label=0.0,.Bearing=0.0} return_table.Label=qian if(bai==1){ b=b*-1.0 } return_table.Bearing=b return return_table } # #get the target distance to neighbr nei_id # function target4label(nei_id){ var return_val="miss" var i=0 while(i0) and (m_vecNodes[i].State=="ASSIGNING")){ m_vecNodes[i].StateAge=m_vecNodes[i].StateAge-1 if(m_vecNodes[i].StateAge==0) m_vecNodes[i].State="UNASSIGNED" } i=i+1 } } # # Do free # function DoFree() { if(BVMSTATE!="GRAPH_FREE"){ BVMSTATE="GRAPH_FREE" m_unWaitCount=m_unLabelSearchWaitTime m_selfMessage.State=s2i(BVMSTATE) }else{ UpdateGraph() m_selfMessage.State=s2i(BVMSTATE) #wait for a while before looking for a Label if(m_unWaitCount>0) m_unWaitCount=m_unWaitCount-1 #find a set of joined robots var setJoinedLabels={} var setJoinedIndexes={} var i=0 var j=0 while(i0){ m_nLabel=unFoundLabel DoAsking() return } #set message m_selfMessage.State=s2i(BVMSTATE) BroadcastGraph() } } # #Do asking # function DoAsking(){ if(BVMSTATE!="GRAPH_ASKING"){ BVMSTATE="GRAPH_ASKING" #math.rng.setseed(id) m_unRequestId=id#math.rng.uniform(0,10) #don't know why the random numbers are the same, add id to make the ReqID different m_selfMessage.State=s2i(BVMSTATE) m_selfMessage.ReqLabel=m_nLabel m_selfMessage.ReqID=m_unRequestId m_unWaitCount=m_unResponseTimeThreshold }else { UpdateGraph() #look for response from predecessor var i=0 var psResponse=-1 while(im_MessageRange[mapRequests[i]]) ReqIndex=i i=i+1 } if(repeat_assign==0){ #get the best index, whose ReqLabel and Reqid are ReqLabel=m_MessageReqLabel[mapRequests[ReqIndex]] var ReqID=m_MessageReqID[mapRequests[ReqIndex]] assign_label=ReqLabel assign_id=ReqID repeat_assign=1 } m_selfMessage.ReqLabel=assign_label m_selfMessage.ReqID=assign_id m_selfMessage.Response=r2i("REQ_GRANTED") #m_vecNodes[ReqLabel].State="ASSIGNING" log("Label=",assign_label) log("ID=",assign_id) m_vecNodes[ReqLabel].StateAge=m_unJoiningLostPeriod } #lost pred, wait for some time and transit to free if(seenPred==0){ m_unWaitCount=m_unWaitCount-1 if(m_unWaitCount==0){ DoFree() return } } # using JOINED as the resume state kinds of just reset the barrier timeout, should be IDLE/ALLOCATE... barrier_wait(ROBOTS, "GRAPH_LOCK", "GRAPH_JOINED", -1) BroadcastGraph() } } # #Do Lock # timeout_graph = 40 function DoLock() { #[transition to lock... BVMSTATE="GRAPH_LOCK" m_selfMessage.State=s2i(BVMSTATE) m_selfMessage.Label=m_nLabel m_vecNodes[m_nLabel].State="ASSIGNED" #record neighbor distance lock_neighbor_id={} lock_neighbor_dis={} var i=0 while(i