Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # autorunning
- from gpiozero import InputDevice, OutputDevice, Robot
- from time import sleep, time
- import random
- sleep(2)
- fwdsp = 0.75
- bcksp = 0.25
- trig = OutputDevice(4)
- echo = InputDevice(17)
- boris = Robot(right =(20,21),left =(19,26))
- duration = 10
- end_time = time() + duration
- running = True
- sleep(2)
- def randtime():
- randtim = random.uniform(0.25, 0.75)
- return randtim
- def get_pulse_time():
- pulse_start, pulse_end = 0,0
- trig.on() # sends a burst
- sleep(0.00001)
- trig.off() # stops burst
- while echo.is_active == False:
- pulse_start = time()
- while echo.is_active == True:
- pulse_end = time()
- return pulse_end - pulse_start
- def calc_dist(duration):
- speed = 343 # speed of sound in air m / second
- distance = speed * duration /2 # distance is there and back hence divide by 2
- #calcs distance in metres
- return distance
- while running:
- duration = get_pulse_time()
- distance = calc_dist(duration)
- if distance < 0.2:
- boris.backward(bcksp)
- sleep(0.2)
- trn = random.choice(["L","R"])
- if trn == "L":
- boris.left(randtime())
- else:
- boris.right(randtime())
- sleep(0.5)
- else:
- boris.forward(fwdsp)
- if time() >= end_time:
- running = False
- boris.stop()
- sleep (0.06)
- print("%.4f" % distance )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement