Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib2
- import time
- import copy
- import math
- import libdw.sm as sm
- import libdw.sonarDist as sonarDist
- import libdw.util as util
- import libdw.search as s
- import libdw.gfx as gfx
- from soar.io import io
- sonarPoses = [util.Pose(0.073, 0.105, 90 * math.pi / 180),
- util.Pose(0.13, 0.078, 41 * math.pi / 180),
- util.Pose(0.154, 0.03, 15 * math.pi / 180),
- util.Pose(0.154, -0.03, -15 * math.pi / 180),
- util.Pose(0.13, -0.078, -41 * math.pi / 180),
- util.Pose(0.073, -0.105, -90 * math.pi / 180)]
- sonarMax = 1.5
- def getDistanceRight(sonarValues):
- return getDistanceRightAndAngle(sonarValues)[0]
- def getDistanceLeft(sonarValues):
- return getDistanceLeftAndAngle(sonarValues)[0]
- def getDistanceRightAndAngle(sonarValues):
- hits = []
- for spose, d in zip(sonarPoses, sonarValues):
- if d < sonarMax:
- hits.append((spose.x + d * math.cos(spose.theta), spose.y + d * math.sin(spose.theta)))
- else:
- hits.append(None)
- return distAndAngle(hits[4], hits[5])
- def getDistanceLeftAndAngle(sonarValues):
- hits = []
- for spose, d in zip(sonarPoses, sonarValues):
- if d < sonarMax:
- hits.append((spose.x + d * math.cos(spose.theta), spose.y + d * math.sin(spose.theta)))
- else:
- hits.append(None)
- return distAndAngle(hits[1], hits[0])
- def sonarHit(distance, sonarPose, robotPose):
- return robotPose.transformPoint(sonarPose.transformPoint(util.Point(distance, 0)))
- def distAndAngle(h0, h1):
- if h0 and h1:
- linex, liney, lined = line(h0, h1)
- return (abs(lined), math.pi / 2 - math.atan2(liney, linex))
- elif h0:
- hx, hy = h0
- return (math.sqrt(hx * hx + hy * hy), None)
- elif h1:
- hx, hy = h1
- return (math.sqrt(hx * hx + hy * hy), None)
- else:
- return (sonarMax, None)
- return None
- def line(h0, h1):
- h0x, h0y = h0
- h1x, h1y = h1
- dx = h1x - h0x
- dy = h1y - h0y
- mag = math.sqrt(dx * dx + dy * dy)
- nx = dy / mag
- ny = -dx / mag
- d = nx * h0x + ny * h0y
- return (nx, ny, d)
- #-------------------------------------------------------------------------------
- #this script creates a botlog.txt file in the same folder as the script if it does not exist.
- #instructions are contained in (botnamehere).trip
- #call (botnamehere).arrival() when bot reaches destination. this deletes the instruction entry from the instruction list and outputs to the log.
- #call (botnamehere).homecoming() when bot reaches X. this outputs to the log.
- inputUrl = 'http://people.sutd.edu.sg/~oka_kurniawan/10_009/y2014/2d/tests/level2_1.inp'
- logFile = 'botlog.txt'
- class bot:
- def __init__(self, url):
- self.continueMovement = True
- self.trip = [['X', None]]
- for line in urllib2.urlopen(url):
- for x in xrange(-(-int(line.strip('\n').split(' ')[1]) // 6)):
- if x+1 < (-(-int(line.strip('\n').split(' ')[1]) // 6)):
- self.trip += [[line.strip('\n').split(' ')[0]] + [6]] + [['X', None]]
- if x+1 == (-(-int(line.strip('\n').split(' ')[1]) // 6)):
- self.trip += [[line.strip('\n').split(' ')[0]] + [int(line.strip('\n').split(' ')[1]) % 6 if int(line.strip('\n').split(' ')[1]) % 6 != 0 else 6]] + [['X', None]]
- def __str__(self):
- return d
- def arrival(self):
- if self.trip[0][0] == 'X':
- if len(self.trip) == 1:
- desc = 'Arrived at X. Welcome home!'
- self.continueMovement = False
- else:
- self.trip.pop(0)
- desc = 'Arrived at X. Shipment received!'
- with open(logFile, 'a') as outf:
- outf.write('\n' + time.strftime('<%H:%M:%S> || <%d-%m-%Y> || ', time.localtime()) + desc)
- else:
- if len(self.trip) == 0:
- return
- desc = 'Arrived at %s. %i bottles delivered!' % (self.trip[0][0], self.trip[0][1])
- self.trip.pop(0)
- with open(logFile, 'a') as outf:
- outf.write('\n' + time.strftime('<%H:%M:%S> || <%d-%m-%Y> || ', time.localtime()) + desc)
- botbot = bot(inputUrl)
- #-------------------------------------------------------------------------------
- class SearchL3(sm.SM):
- startState='H'
- #legalInputs=['N','S','W','E']
- legalInputs=[0,1,2,3]
- mapl3={'H':[None,None,None,'T1'],
- 'T1':['T4',None,'H','T2'],
- 'T2':['T3',None,'T1','X'],
- 'T3':['B','T2','T4','A'],
- 'T4':['C','T1','D','T3'],
- 'A':[None,None,'T3',None],
- 'B':[None,'T3',None,None],
- 'C':[None,'T4',None,None],
- 'D':[None,None,None,'T4'],
- 'X':[None,None,'T2',None]}
- def __init__(self,goal):
- self.goal = goal
- def nextState(self,state,action):
- return self.mapl3[state][action]
- def getNextValues(self,state,action):
- nextState=self.nextState(state,action)
- return (nextState,nextState)
- def done(self,state):
- return state==self.goal
- #-------------------------------------------------------------------------------
- y='H'
- a=lambda x,y:s.smSearch(SearchL3(x),initialState=y,depthFirst=False,DP=False)
- class State():
- amigo=None
- n=a(botbot.trip[0][0],y)
- def start(self):
- self.n.pop(0)
- return ('straight', 0, 0)
- def end(self):
- if botbot.continueMovement == False:
- self.amigo = ('stop', None, 0)
- return self.amigo
- self.amigo = ('end', None, 0)
- y=self.n[0][1]
- botbot.arrival()
- self.n=a(botbot.trip[0][0],y)
- self.n.pop(0)
- return self.amigo
- def junction(self):
- if self.n[0][0] == 3:
- if self.n[1][0] == 0:
- self.amigo = ('turnLeft', turnTimeStep, 0)
- self.n.pop(0)
- pass
- elif self.n[1][0] == 1:
- self.amigo = ('turnRight', turnTimeStep, 0)
- self.n.pop(0)
- pass
- elif self.n[1][0] == 3:
- self.amigo = ('junctionStraight', junctionTolerance, 0)
- self.n.pop(0)
- pass
- elif self.n[0][0] == 2:
- if self.n[1][0] == 0:
- self.amigo = ('turnRight', turnTimeStep, 0)
- self.n.pop(0)
- pass
- elif self.n[1][0] == 1:
- self.amigo = ('turnLeft', turnTimeStep, 0)
- self.n.pop(0)
- pass
- elif self.n[1][0] == 2:
- self.amigo = ('junctionStraight', junctionTolerance, 0)
- self.n.pop(0)
- pass
- elif self.n[0][0] == 0:
- if self.n[1][0] == 2:
- self.amigo = ('turnLeft', turnTimeStep, 0)
- self.n.pop(0)
- pass
- elif self.n[1][0] == 3:
- self.amigo = ('turnRight', turnTimeStep, 0)
- self.n.pop(0)
- pass
- elif self.n[1][0] == 0:
- self.amigo = ('junctionStraight', junctionTolerance, 0)
- self.n.pop(0)
- pass
- elif self.n[0][0] == 1:
- if self.n[1][0] == 2:
- self.amigo = ('turnRight', turnTimeStep, 0)
- self.n.pop(0)
- pass
- elif self.n[1][0] == 3:
- self.amigo = ('turnLeft', turnTimeStep, 0)
- self.n.pop(0)
- pass
- elif self.n[1][0] == 1:
- self.amigo = ('junctionStraight', junctionTolerance, 0)
- self.n.pop(0)
- pass
- return self.amigo
- def obstacleLeft(self): #optional
- return 'obstacleLeft'
- pass
- def obstacleRight(self):
- return 'obstacleRight'
- class Sensor(sm.SM):
- def getNextValues(self, state, inp):
- R = getDistanceRightAndAngle(inp.sonars)
- L = getDistanceLeftAndAngle(inp.sonars)
- frontRead=(inp.sonars[2]+inp.sonars[3])/2
- xrightRead=inp.sonars[5]
- xleftRead=inp.sonars[0]
- odo=inp.odometry.theta
- b = inp.sonars
- k=[R,L,frontRead,xrightRead,xleftRead,odo,b]
- print 'Dist from robot center to wall on right', R[0]
- print inp.sonars
- if not R[1]:
- print '****** Angle reading not valid ******'
- return (state, k)
- #workingvalues
- #left/right = 0.8, fvel = 0.25, rotate = 90, 0.3, turn = 40, fvel/dleft + 0.1, tt 40, jt = 20, tt = 5, rts = 30, k3 = 3, k4 = 1.7
- desiredRight = 0.74
- desiredLeft = 0.74
- forwardVelocity = 0.375
- rotateTimeStep = 85
- rotateRvel = 0.375
- turnTimeStep = 24
- turnRvel = forwardVelocity/desiredLeft + 0.6
- turnFvel = forwardVelocity*0.5
- turnTolerance = 20
- junctionTolerance = 15
- transitionTolerance = 2
- reverseTimeStep = 30
- obstacleTimeStep =25
- k3=2
- k4=1.72
- k3o=2
- k4o=1.22
- hello=State()
- class WallFollower(sm.SM):
- startState = hello.start()
- def getNextValues(self, state, inp):
- print state
- print botbot.trip
- print hello.n
- print botbot.continueMovement
- print inp[6]
- midpoint = 0.5*(inp[1][0] + inp[0][0])
- if state[0] == 'stop':
- return (state,io.Action(0.0,0.0))
- if state[0] == 'end':
- if inp[2] >= 0.75:
- state = ('end', None, 0)
- return(state,io.Action(forwardVelocity * 0.5))
- else:
- state= ('reverse', reverseTimeStep, 0)
- return(state,io.Action())
- if state[0]=='obstacleLeft':
- if state[1]==0:
- state=('ostraight',0,0)
- return (state,io.Action())
- else:
- print 'hello'
- if inp[0][1]==None:
- return (state,io.Action(0.1,-0.1))
- state=('obstacleLeft',state[1] - 1,0)
- n=1.26*(0 - inp[0][1])
- m=2*(0.3-inp[0][0])
- rvel = m+n
- return(state,io.Action(0.1,rvel))
- if state[0] == 'reverse':
- if state[1] > 0:
- state = ('reverse', state[1] - 1, 0)
- return (state,io.Action(-forwardVelocity,0.0))
- else:
- state = ('rotate', rotateTimeStep, 0)
- return (state,io.Action(0.0,0.0))
- if state[0] == 'rotate':
- if state[1] > 0:
- state = ('rotate', state[1] - 1, 0)
- return (state, io.Action(0.0,rotateRvel))
- elif botbot.continueMovement == False:
- state = ('stop', None, 0)
- return (state, io.Action(0.0,0.0))
- else:
- state = ('straight', 0, 0)
- return (state,io.Action(0.0,0.0))
- if state[0] == 'turnLeft':
- if state[1] > 0:
- state = ('turnLeft', state[1] - 1, 0)
- return (state,io.Action(turnFvel,turnRvel))
- else:
- state = ('straight', turnTolerance, 0)
- return (state,io.Action(forwardVelocity*0.5,0.0))
- if state[0] == 'turnRight':
- if state[1] > 0:
- state = ('turnRight', state[1] - 1, 0)
- return (state,io.Action(turnFvel,-turnRvel))
- else:
- state = ('straight', turnTolerance, 0)
- return(state,io.Action(forwardVelocity*0.5,0.0))
- if state[0] =='junctionStraight':
- if state[1] > 0:
- state = ('junctionStraight', state[1] - 1, 0)
- elif inp[6][0] <= 2 and inp[6][5] <= 2:
- state = ('straight', turnTolerance, 0)
- return (state,io.Action(forwardVelocity,0.0))
- else:
- return(state,io.Action(forwardVelocity,0.0))
- if state[1] == 0 and state[0] != 'junctionStraight':
- if inp[6][0] > 4 or inp[6][5] > 4:
- state = (state[0], state[1], state[2] + 1)
- else:
- state = (state[0], state[1], 0)
- if state[2] > transitionTolerance:
- state = hello.junction()
- if hello.n[0][1] in ('A', 'B', 'C', 'D', 'X') and inp[6][2] <= 1 and inp[6][3] <= 1:
- state = hello.end()
- if inp[6][2]<=0.75 and inp[6][4]>=0.85 and inp[6][4]!=5:
- state=('obstacleLeft', obstacleTimeStep,0)
- return (state, io.Action())
- if state[0] == 'straight':
- if inp[0][1] == None and inp[1][1] == None:
- return(state,io.Action(forwardVelocity,0))
- elif inp[0][1] == None:
- state = ('Lstraight', state[1], state[2])
- else:
- state = ('Rstraight', state[1], state[2])
- return(state,io.Action(forwardVelocity,0))
- if state[0] == 'Lstraight':
- if state[1] > 0:
- state = ('Lstraight', state[1] - 1, state[2])
- if inp[0][1] == None and inp[1][1] == None:
- rvel=0
- elif inp[1][1] == None:
- n=k4*(0-inp[0][1])
- m=k3*(midpoint-inp[0][0])
- rvel= m+n
- state = ('Rstraight', state[1], state[2])
- else:
- n=k4*(0 - inp[1][1])
- m=k3*(midpoint-inp[1][0])
- rvel = -m+n
- state = ('Lstraight', state[1], state[2])
- return(state,io.Action(forwardVelocity,rvel))
- if state[0] == 'Rstraight':
- if state[1] > 0:
- state = ('Rstraight', state[1] - 1, state[2])
- if inp[0][1] == None and inp[1][1] == None:
- rvel=0
- elif inp[0][1] == None:
- n=k4*(0-inp[1][1])
- m=k3*(midpoint-inp[1][0])
- rvel= -m+n
- state = ('Lstraight', state[1], state[2])
- else:
- n=k4*(0 - inp[0][1])
- m=k3*(midpoint-inp[0][0])
- rvel = m+n
- state = ('Rstraight', state[1], state[2])
- return(state,io.Action(forwardVelocity,rvel))
- ###obstaclestraight
- if state[0] == 'ostraight':
- if inp[0][1] == None and inp[1][1] == None:
- return(state,io.Action(forwardVelocity/2,0))
- elif inp[0][1] == None:
- state = ('oLstraight', state[1], state[2])
- else:
- state = ('oRstraight', state[1], state[2])
- return(state,io.Action(forwardVelocity/2,0))
- if state[0] == 'oLstraight':
- if state[1] > 0:
- state = ('oLstraight', state[1] - 1, state[2])
- if inp[0][1] == None and inp[1][1] == None:
- rvel=0
- elif inp[1][1] == None:
- n=k4o*(0-inp[0][1])
- m=k3o*(midpoint-inp[0][0])
- rvel= m+n
- state = ('oRstraight', state[1], state[2])
- else:
- n=k4o*(0 - inp[1][1])
- m=k3o*(midpoint-inp[1][0])
- rvel = -m+n
- state = ('oLstraight', state[1], state[2])
- return(state,io.Action(forwardVelocity/2,rvel))
- if state[0] == 'oRstraight':
- if state[1] > 0:
- state = ('oRstraight', state[1] - 1, state[2])
- if inp[0][1] == None and inp[1][1] == None:
- rvel=0
- elif inp[0][1] == None:
- n=k4o*(0-inp[1][1])
- m=k3o*(midpoint-inp[1][0])
- rvel= -m+n
- state = ('oLstraight', state[1], state[2])
- else:
- n=k4o*(0 - inp[0][1])
- m=k3o*(midpoint-inp[0][0])
- rvel = m+n
- state = ('oRstraight', state[1], state[2])
- return(state,io.Action(forwardVelocity/2,rvel))
- return(state,io.Action(forwardVelocity/2,0.0))
- print botbot.trip
- sensorMachine = Sensor()
- sensorMachine.name = 'sensor'
- mySM = sm.Cascade(sensorMachine, WallFollower())
- ######################################################################
- #
- # Running the robot
- #
- ######################################################################
- def setup():
- robot.gfx = gfx.RobotGraphics(drawSlimeTrail=False)
- robot.behavior = mySM
- robot.behavior.start(traceTasks = robot.gfx.tasks())
- def step():
- robot.behavior.step(io.SensorInput()).execute()
- def brainStop():
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement