Hardware Reference
In-Depth Information
Engage thrusters
1. As always, we will get started by imporing the required modules,
especially Rpi.GPIO :
import RPi.GPIO as GPIO
from time import sleep
2. We will set the pin coniguraion that we will use in this program:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.IN)
GPIO.setup(25,GPIO.IN)
3.
The control logic explained earlier is implemented as follows:
state = 1
prev_state = 0
while True:
#both sensors are on white surface
if ((GPIO.input(18)==GPIO.HIGH) and (GPIO.input(25)==GPIO.
HIGH)):
state = 0
#left sensor alone is on the black surface
elif ((GPIO.input(18)==GPIO.LOW) and (GPIO.input(25)==GPIO.
HIGH)):
state = 1
#right sensor alone is on the black surface
elif ((GPIO.input(18)==GPIO.HIGH) and (GPIO.input(25)==GPIO.
LOW)):
state = 2
#if sensor state has changed since last time, update motor
control
if state != prev_state:
if state == 0:
#move robot forward
elif state == 1:
#turn robot left
elif state == 2:
#turn robot right
prev_state = state
sleep(0.15)
The sensor states are checked once every 15 milliseconds. If there is a change of
sensor states, the motor control is updated (as per the logic explained in this task).
 
Search WWH ::




Custom Search