To access the full video please subscribe to FLLCasts.com
- #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
Perfect STEM course. Module 1 - Smart Car with Raspberry PI
Disassemble a remote control car. Change the brain of the car with a smart computer like Raspberry Pi. Build a smart device with artificial intelligence that you could control from your phone and that could freely navigate itself in the real world and on the Internet. Use your hands. Develop programs for your robot and your phone. Be curious and invent.
The perfect course lives up to its name. You move through the content, we check it and return feedback to you.
In the end, you should be able to better understand how to program and design smart devices that would make the world a better place. For everybody age 12+, 16+, 21+, 35+, etc. The hardware costs about 150$. It changes through the years so it might take some time for you to find it as Raspberry PI, motor drivers, power banks and h-bridges.. they change. Note that the course was designed to be led by a teacher with a decent knowledge in embedded hardware and software. This, on the other hand should not stop you, if you are curious.
- 118
- 42:47
- 136
Execute Python program about car distance sensor from the computer
This is going to be one complex connection with four pins and three resistors, so brace yourselves!
- 3
- 0
- 1
- 3d_rotation 0