Teacher notes: Solution to the challenges Pro Preview

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #871
  • 04 May 2018

Adjust the maximum sensor measurement to be your height

from gpiozero import DistanceSensor

sensor = DistanceSensor(echo = 6, trigger = 5, max_distance = 1.88)

print("That is all!")

Avoid obstacles - steer left when DS is less than 50cm, no steer when DS is more than 60cm

from gpiozero import DistanceSensor				# a library to simplify the code
from gpiozero import Motor					# a library to simplify the code
from gpiozero import PWMLED

from time import sleep

sensor = DistanceSensor(echo = 6, trigger = 5, max_distance = 2)

forwardPin = 27
backwardPin = 22
leftPin = 23
rightPin = 24
Motor.left = Motor.forward					# define left() function alias for easier reading later
Motor.right = Motor.backward					# define right() function alias for easier reading later

move = Motor(forwardPin, backwardPin)
steer = Motor(leftPin, rightPin)
move.stop()
steer.stop()

try:									# ignore that keyword, it is very very advanced stuff
	while True:
		# wait obstacle
		move.forward()
		while sensor.distance > 0.50:
			sleep(0.1)					# wait obstacle in front
		#eof while no obstacle
		move.stop()						# avoid collision
		print("Obstacle N%d detected" % (i+1))
		
		steer.left()
		move.forward()						# steer to avoid the obstacle
		while sensor.distance < 0.60:
			sleep(0.1)					# wait obstacle in front
		#eof while there is still an obstacle in front of us
		
		move.stop()
		steer.stop()						# path is clear, continue straight
		print("Clear path N%d detected" % (i+1))
		      
	#eof forever loop
	
except KeyboardInterrupt:						# more advanced stuff happening
	pass								# all that helps the program to continue
move.stop()
steer.stop()

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Perfect STEM course. Module 1 - Smart Car with Raspberry PI
  • 118
  • 42:47
  • 136