from Myro import * init() def main(): # Braitenberg vehicle#1: Alive while True: L = getLight("center") forward(normalize(L)) main() def normalize(v): # Normalize v (in the range 0..5000) to 0..1.0, inversely return 1.0 - v/5000.0 1.0 - 35.0/5000.0 1.0 - 3000.0/5000.0 1.0 - 200.0/5000.0 def normalize(v): if v > Ambient: v = Ambient return 1.0 - v/Ambient # Braitenberg Vehicle#1: Alive from Myro import * initialize(ask("What port?")) Ambient = getLight("center") def normalize(v): if v > Ambient: v = Ambient return 1.0 - v/Ambient def main(): # Braitenberg vehicle#1: Alive while True: L = getLight("center") forward(normalize(L)) # Vraitenberg Vehicle#2a from Myro import * initialize(ask("What port?")) Ambient = sum(getLight())/3.0 def normalize(v): if v > Ambient: v = Ambient return 1.0 - v/Ambient def main(): # Braitenberg vehicle#2a: Coward while True: L = getLight("left") R = getLight("right") motors(normalize(L), normalize(R)) def normalize(v): if v > Ambient: v = Ambient return v/Ambient def normalize(v): mean = Ambient/2.0 stddev = Ambient/6.0 if v >= Ambient: v = Ambient return exp(-(v - mean)**2 / (2*(stddev**2))) thresh = 50 for t in timer(30): # Get sensor values for left and right light sensors L = getLight('left') R = getLight('right') # decide how to act based on sensors values if (L - R) > thresh: # left is seeing less light than right so turn right turnRight(1.0) elif (R - L) > thresh: # right is seeing less light than left, so turn left turnLeft(1.0) else: # the difference is less than the threshold, stay put stop() # Light follower from Myro import * initialize(ask("What port?")) # program settings... thresh = 50 fwdSpeed = 0.8 cruiseSpeed = 0.5 turnSpeed = 0.7 # left turn, -0.7 will be right turn def main(): while True: # get light sensor values for left, center, and right L, C, R = getLight() # decide how to act based on sensor values if C < thresh: # bright light from straight ahead, go forward move(fwdSpeed, 0) elif L < thresh: # bright light at left, turn left move(cruiseSpeed, turnSpeed) elif R < thresh: # bright light on right side, turn right move(cruiseSpeed, -turnSpeed) else: # no bright light, move forward slowly (or stop?) move(cruiseSpeed/2, 0) main() # Avoiding Obstacles from Myro import * initialize(ask("What port?")) # program settings... cruiseSpeed = 0.6 turnSpeed = 0.5 # this is a left turn, -0.5 will be right turn def main(): while True: # get sensor values for left and right IR sensors L, R = getIR() L = 1 - L R = 1 - R # decide how to act based on sensors values if L and R: # obstacle straight ahead, turn (randomly) move(0, turnSpeed) elif L: # obstacle on left, turn right move(cruiseSpeed, -turnSpeed) elif R: # obstacle on right, turn left move(cruiseSpeed, turnSpeed) else: # no obstacles move(cruiseSpeed, 0) main() # Avoiding Obstacles by bumping from Myro import * initialize(ask("What port?")) # program settings... cruiseSpeed = 1.0 turnSpeed = 0.5 # this is a left turn, -0.5 will be right turn def main(): while True: if getStall(): # I am stalled, turn (randomly?) move(0, turnSpeed) else: # I am not stalled, cruise on move(cruiseSpeed, 0) main()