Design Lab 2: This code could be expanded to have the robot turn the opposite direction depending on the side of it that the sonar is being triggered upon. However, for simplicity when near walls, the robot halts and turns until it is free.
class MySMClass(sm.SM): def __init__(self): self.startState = "Nothing" def getNextValues(self, state, inp): print "State :", state if state == "Nothing": if inp.sonars[3] < 0.5 or inp.sonars[4] < 0.5 or \ inp.sonars[2] < 0.5 or inp.sonars[5] < 0.5: action = io.Action(fvel = 0.0, rvel = 0.50) return ("Obsticle",action) else: action = io.Action(fvel = 0.3, rvel = 0.00) return ("Nothing",action) elif state == "Obsticle": if inp.sonars[3] > 0.75 and inp.sonars[4] > 0.75 and \ inp.sonars[2] > 0.5 and inp.sonars[5] > 0.5: action = io.Action(fvel = 0.05, rvel = 0.25) return ("Follow",action) else: action = io.Action(fvel = 0.0, rvel = 0.50) return ("Obsticle", action) else: if inp.sonars[2] > 0.35 and inp.sonars[5] > 0.35 and \ inp.sonars[1] > 0.30 and inp.sonars[6] > 0.30 and \ inp.sonars[0] > 0.25 and inp.sonars[7] > 0.25: action = io.Action(fvel = 0.3, rvel = 0.0) return ("Nothing", action) else: action = io.Action(fvel = 0.05, rvel = 0.25) return ("Follow", action)
Join our real-time social learning platform and learn together with your friends!