def check_front(): ##Define the variable f_dist as the distance from the front sensor to the nearest object f_dist = sensors.front_distance() ##Instruct action: if an object is closer than 15 cm away, print "Too close, and the distance", and continue if f_dist < 15: driveme.init() driveme.reverse(2)
def autonomy(): ##Set the time to run (for actions other than forward) tf = 1 ##Introduce a variable, x, that will take on a sudorandom value to drive the vechicle in explore mode. x will take on values from 1-7 x = random.randrange(0, 8) ##Set actions for the vechicle based on the value of x ##Drive forward for 5 seconds if x = 0 if x == 0: check_front() ##Initialise GPIO pins (based on instructions defined in driveme.py) driveme.init() ##Drive forward for 5 seconds driveme.forward(5) ##Pivot left for tf seconds if x = 1 elif x == 1: check_front() driveme.init() driveme.pivot_left(tf) ##Pivot right for tf seconds if x = 2 elif x == 2: check_front() driveme.init() driveme.pivot_right(tf) ##Drive forward and to the left for tf seconds if x = 3 elif x == 3: check_front() driveme.init() driveme.turn_left_fwd(tf) ##Drive forward and to the right for tf seconds if x = 4 elif x == 4: check_front() driveme.init() driveme.turn_right_fwd(tf) ##Drive left and reverse for tf seconds if x = 5 elif x == 5: check_front() driveme.init() driveme.turn_left_rev(tf) ##Drive right and reverse for tf seconds if x = 6 elif x == 6: check_front() driveme.init() driveme.turn_right_rev(tf) ##Reverse for tf seconds if x = 7 elif x == 7: check_front() driveme.init() driveme.reverse(tf)
def check_front(): driveme.init() dist = sensors.distance() if dist < 15: print('Too close,', dist) driveme.init() driveme.reverse(2) dist = sensors.distance() if dist < 15: print('Too close,', dist) driveme.init() driveme.pivot_left(3) driveme.init() driveme.reverse(2) dist = sensors.distance() if dist < 15: print('Too close, giving up', dist) sys.exit()
##Using Python 2.7.3 import RPi.GPIO as gpio import time import sys import driveme ##Driveme.py Test Code: ##Define the time to drive for (tf) tf = 3 #drive vechicle forward for tf driveme.forward(tf) #turn vehicle left while moving forward for tf seconds driveme.reverse(tf) #turn vehicle left while moving forward for tf seconds driveme.turn_left_fwd(tf) #turn vehicle right while moving forward for tf seconds driveme.turn_right_fwd(tf) #turn vehicle left while reversing for tf seconds driveme.turn_left_rev(tf) #turn vehicle right while reversing for tf seconds driveme.turn_right_rev(tf) #Pivot vehicle clockwise (right) for tf seconds driveme.pivot_right(tf) #Pivot vehicle counterclockwise (left) while moving forward for tf seconds driveme.pivot_left(tf)