TheGMODSPAZ
Posts: 5
Joined: Wed Jan 01, 2014 10:14 pm

Using Sensors with Motors

Tue Jan 21, 2014 8:15 pm

Here I have been working on two pieces of python code (essentially to build a small robot) one piece controls the sensors and calculates the distance between an object and the ultrasonic sensor. Keep in mind the sensors are at the front and back of the robot.

The other piece of code I have written controls the motors DEPENDING on the distance between objects and the sensors. I will be using the L298N motor control board for this robot.

I have one question:
Is my code correct? Will there be any faults?

I am asking this because:
1) I have yet to build the robot itself
2) I am curious as to whether of not the code will work, even though I've checked my code through the IDLE, with no errors.

Anyway, here's the code:

Sensors

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(12,GPIO.OUT)  
GPIO.setup(16,GPIO.IN)  
GPIO.setup(18,GPIO.OUT)  
GPIO.setup(22,GPIO.IN)

while True:
    GPIO.output(12, False)
    time.sleep(0.5)
    GPIO.output(12, True)   
    time.sleep(0.00001)
    GPIO.output(12, False)
    start_front = time.time()
    while GPIO.input(16)==0:
      start_front = time.time()
    while GPIO.input(16)==1:
      stop_front = time.time()

    #Calculation to get distance of front sensor.
def Distance_Front():
    elapsed = stop_front-start_front
    distance = elapsed * 34000
    distance = distance / 2

    GPIO.output(18, False)
    time.sleep(0.5)
    GPIO.output(18, True)   
    time.sleep(0.00001)
    GPIO.output(18, False)
    start_back = time.time()
    while GPIO.input(22)==0:
      start_back = time.time()
    while GPIO.input(22)==1:
      stop_back = time.time()

    #Calculation to get distance of rear sensor.
def Distance_Back():
    elapsed = stop_back-start_back
    distance = elapsed * 34000
    distance = distance / 2
GPIO.cleanup()
Motors

Code: Select all

import RPi.GPIO as GPIO
import Range_Finder.py as Rangefinder
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7,GPIO.OUT)  
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)  
GPIO.setup(15,GPIO.OUT)

while:
    GPIO.output(7,True)
    GPIO.output(13,True)
    GPIO.output(11,False)
    GPIO.output(15,False)#Moving forwards.
    if Rangefinder.Distance_Front <= 10#If distance is less than or equal to.
        GPIO.output(7,True)
        GPIO.output(13,False)
        GPIO.output(11,True)
        GPIO.output(15,False)#Make a turn.
        if Rangefinder.Distance_Front > 10#If distance is greater than.
            GPIO.output(7,True)
            GPIO.output(13,True)
            GPIO.output(11,False)
            GPIO.output(15,False)#Moving forwards.
    if Rangefinder.Distance_Back <= 10#If distance is less than or equal to.
        GPIO.output(7,True)
        GPIO.output(13,True)
        GPIO.output(11,False)
        GPIO.output(15,False)#Drive forwards to increase distance from rear.
        if Rangefinder.Distance_Back > 10#If distance is greater than.
            GPIO.output(7,True)
            GPIO.output(13,True)
            GPIO.output(11,False)
            GPIO.output(15,False)#Moving forwards.
GPIO.cleanup()
Thanks!

Return to “Advanced users”