-
-
Save Lord-McSweeney/3d806719cf03933a38df6905d852d775 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CamJam EduKit 3 - Robotics | |
# Worksheet 9 - Obstacle Avoidance | |
import RPi.GPIO as GPIO | |
import time | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
# Set variables for the GPIO motor pins | |
pinMotorAForwards = 10 | |
pinMotorABackwards = 9 | |
pinMotorBForwards = 8 | |
pinMotorBBackwards = 7 | |
# Define GPIO pins to use on the Pi | |
pinTrigger = 17 | |
pinEcho = 18 | |
# How many times to turn the pin on and off each second | |
Frequency = 20 | |
# How long the pin stays on each cycle, as a percent (here, it's 30%) | |
DutyCycleA = 30 | |
DutyCycleB = 30 | |
# Setting the duty cycle to 0 means the motors will not turn | |
Stop = 0 | |
GPIO.setup(pinMotorAForwards, GPIO.OUT) | |
GPIO.setup(pinMotorABackwards, GPIO.OUT) | |
GPIO.setup(pinMotorBForwards, GPIO.OUT) | |
GPIO.setup(pinMotorBBackwards, GPIO.OUT) | |
# set pins as output and input | |
GPIO.setup(pinTrigger, GPIO.OUT) # Trigger | |
GPIO.setup(pinEcho, GPIO.IN) # Echo | |
# Distance Variables | |
HowNear = 15.0 | |
ReverseTime = 0.5 | |
TurnTime = 0.75 | |
pwmMotorAForwards = GPIO.PWM(pinMotorAForwards, Frequency) | |
pwmMotorABackwards = GPIO.PWM(pinMotorABackwards, Frequency) | |
pwmMotorBForwards = GPIO.PWM(pinMotorBForwards, Frequency) | |
pwmMotorBBackwards = GPIO.PWM(pinMotorBBackwards, Frequency) | |
# Start the software PWM with a duty cycle of 0 | |
pwmMotorAForwards.start(Stop) | |
pwmMotorABackwards.start(Stop) | |
pwmMotorBForwards.start(Stop) | |
pwmMotorBBackwards.start(Stop) | |
def StopMotors(): | |
pwmMotorAForwards.ChangeDutyCycle(Stop) | |
pwmMotorABackwards.ChangeDutyCycle(Stop) | |
pwmMotorBForwards.ChangeDutyCycle(Stop) | |
pwmMotorBBackwards.ChangeDutyCycle(Stop) | |
def Forwards(): | |
pwmMotorAForwards.ChangeDutyCycle(DutyCycleA) | |
pwmMotorABackwards.ChangeDutyCycle(Stop) | |
pwmMotorBForwards.ChangeDutyCycle(DutyCycleB) | |
pwmMotorBBackwards.ChangeDutyCycle(Stop) | |
def Backwards(): | |
pwmMotorAForwards.ChangeDutyCycle(Stop) | |
pwmMotorABackwards.ChangeDutyCycle(DutyCycleA) | |
pwmMotorBForwards.ChangeDutyCycle(Stop) | |
pwmMotorBBackwards.ChangeDutyCycle(DutyCycleB) | |
def Right(): | |
pwmMotorAForwards.ChangeDutyCycle(Stop) | |
pwmMotorABackwards.ChangeDutyCycle(DutyCycleA) | |
pwmMotorBForwards.ChangeDutyCycle(DutyCycleB) | |
pwmMotorBBackwards.ChangeDutyCycle(Stop) | |
def Left(): | |
pwmMotorAForwards.ChangeDutyCycle(DutyCycleA) | |
pwmMotorABackwards.ChangeDutyCycle(Stop) | |
pwmMotorBForwards.ChangeDutyCycle(Stop) | |
pwmMotorBBackwards.ChangeDutyCycle(DutyCycleB) | |
def Measure(): | |
GPIO.output(pinTrigger, True) | |
time.sleep(0.00001) | |
GPIO.output(pinTrigger, False) | |
StartTime = time.time() | |
StopTime = StartTime | |
while GPIO.input(pinEcho)==0: | |
StartTime = time.time() | |
StopTIme = StartTime | |
while GPIO.input(pinEcho)==1: | |
StopTime = time.time() | |
# If the sensor is too close to an object, the Pi cannot | |
# see the echo quickly enough, so it has to detect that | |
# problem and say what has happened | |
if StopTime-StartTime >= 0.04: | |
print("Hold on there! You're too close for me to see.") | |
StopTime = StartTime | |
break | |
ElapsedTime = StopTime - StartTime | |
Distance = (ElapsedTime * 34326)/2 | |
return Distance | |
def IsNearObstacle(localHowNear): | |
Distance = Measure() | |
print("IsNearObstacle: "+str(Distance)) | |
if Distance < localHowNear: | |
return True | |
else: | |
return False | |
# Move back a little, then turn right | |
def AvoidObstacle(): | |
# Back off | |
print("Backwards") | |
Backwards() | |
time.sleep(ReverseTime) | |
StopMotors() | |
# Turn right | |
print("Right") | |
Right() | |
time.sleep(TurnTime) | |
StopMotors() | |
# Your code to control the robot goes below this line | |
try: | |
# Set trigger to False (Low) | |
GPIO.output(pinTrigger, False) | |
# Allow module to settle | |
time.sleep(0.1) | |
# repeat the next indented block forever | |
while True: | |
Forwards() | |
time.sleep(0.1) | |
if IsNearObstacle(HowNear): | |
StopMotors() | |
AvoidObstacle() | |
# if you press CTRL+C, cleanup and stop | |
except KeyboardInterrupt: | |
GPIO.cleanup() | |
'''while True: | |
answer = input("Give me a command").lower() | |
if answer == "f": | |
Forwards() | |
time.sleep(1) | |
StopMotors() | |
elif answer == "b": | |
Backwards() | |
time.sleep(1) | |
StopMotors() | |
elif answer == "l": | |
Left() | |
time.sleep(0.5) | |
StopMotors() | |
elif answer == "r": | |
Right() | |
time.sleep(0.5) | |
StopMotors() | |
Forwards() | |
time.sleep(1) | |
Left() | |
time.sleep(1) | |
Backwards() | |
time.sleep(1) | |
Right() | |
time.sleep(1) | |
StopMotors() | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment