Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import math
  2. import lib601.util as util
  3. import lib601.sm as sm
  4. import lib601.gfx as gfx
  5. from soar.io import io
  6.  
  7. class MySMClass(sm.SM):
  8.     startState = 'nothing'
  9.     def getNextValues(self, state, inp):
  10.         #return (state, io.Action(fvel = 0, rvel = 0))
  11.         # 3 is if it sees something in front
  12.         # 4 is if it sees something at right
  13.        
  14.         if (self.state == 'nothing'):
  15.             if (inp.sonars[4] <= .53):
  16.                 return ('something', io.Action(fvel = 0.05, rvel = 0))
  17.             return ('nothing', io.Action(fvel = .2, rvel = 0))
  18.         else:
  19.             if(inp.sonars[7]<.3 or inp.sonars[3] <.3):
  20.                return (state, io.Action(fvel = -0.05, rvel = .1))
  21.                 #something too close to me, turn right (cw)
  22.             elif (inp.sonars[3] <.5 ):
  23.                 return (state, io.Action(fvel = 0.1, rvel = .3))
  24.                 #something in front of me, turn right (ccw)
  25.             elif(inp.sonars[7] >.5):
  26.                 return (state, io.Action(fvel = 0.1, rvel = -.2))                
  27.                 #nothing to the right of me, turn left (cw)
  28.             else:
  29.                 #if something at right, go forward
  30.                 return (state, io.Action(fvel = .2, rvel = 0))
  31.  
  32.        
  33.  
  34. mySM = MySMClass()
  35. mySM.name = 'brainSM'
  36.  
  37. ######################################################################
  38. ###
  39. ###          Brain methods
  40. ###
  41. ######################################################################
  42.  
  43. def plotSonar(sonarNum):
  44.     robot.gfx.addDynamicPlotFunction(y=('sonar'+str(sonarNum),
  45.                                         lambda:
  46.                                         io.SensorInput().sonars[sonarNum]))
  47.  
  48. # this function is called when the brain is (re)loaded
  49. def setup():
  50.     robot.gfx = gfx.RobotGraphics(drawSlimeTrail=False, # slime trails
  51.                                   sonarMonitor=True) # sonar monitor widget
  52.    
  53.     # set robot's behavior
  54.     robot.behavior = mySM
  55.  
  56. # this function is called when the start button is pushed
  57. def brainStart():
  58.     robot.behavior.start(traceTasks = robot.gfx.tasks())
  59.  
  60. # this function is called 10 times per second
  61. def step():
  62.     inp = io.SensorInput()
  63.     print inp.sonars[3]
  64.     robot.behavior.step(inp).execute()
  65.     io.done(robot.behavior.isDone())
  66.  
  67. # called when the stop button is pushed
  68. def brainStop():
  69.     pass
  70.  
  71. # called when brain or world is reloaded (before setup)
  72. def shutdown():
  73.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement