A Raspberry PI – DC motor, stepper motor, ultra sonic sensor project

I was in Cloudera Hadoop Developer training recently and there was interesting discussion on how low cost computer like raspberry PI can be used to setup Hadoop cluster for developer testing and proof of concepts. I started digging into that but in parallel i’ve decided to use such low configured CPU to do some robotics projects.

Here is the first project i recently finished with Raspberry PI.
A self-trajectory motor vehicle. It has its own brain(not remote controlled) coded in python language to make all decisions.
It runs on two external power sources, 12V and 5V. 12V to rotate main DC motors while 5V for CPU processing, stepper motor movements and for obstacle sensing(ultrasonic sensor). PI is operating those motors through two L298N circuit board.

Scroll for code below.

Raspberry_PI_motor1

Raspberry_PI_motor2

Raspberry_PI_motor3

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

#GPIO.cleanup()

wheel1A=27
wheel1B=10
wheel2A=17
wheel2B=22

eye1A=7
eye1B=8

eyeWA=6
eyeWB=13
eyeWC=19
eyeWD=26

GPIO.setup(wheel1A, GPIO.OUT)
GPIO.setup(wheel1B, GPIO.OUT)
GPIO.setup(wheel2A, GPIO.OUT)
GPIO.setup(wheel2B, GPIO.OUT)
GPIO.setup(eye1A, GPIO.IN)
GPIO.setup(eye1B, GPIO.OUT)

#eyes
GPIO.setup(eyeWA,GPIO.OUT)
GPIO.setup(eyeWB,GPIO.OUT)
GPIO.setup(eyeWC,GPIO.OUT)
GPIO.setup(eyeWD,GPIO.OUT)

def spinRight():
        print('SR')
        GPIO.output(wheel1A,1)
        GPIO.output(wheel1B,1)
        GPIO.output(wheel2A,0)
        GPIO.output(wheel2B,0)
def spinLeft():
        print('SL')
        GPIO.output(wheel1A,0)
        GPIO.output(wheel1B,0)
        GPIO.output(wheel2A,1)
        GPIO.output(wheel2B,1)
def goForward():
        GPIO.output(wheel1A,1)
        GPIO.output(wheel1B,0)
        GPIO.output(wheel2A,0)
        GPIO.output(wheel2B,1)
def goBackward():
        GPIO.output(wheel1A,0)
        GPIO.output(wheel1B,1)
        GPIO.output(wheel2A,1)
        GPIO.output(wheel2B,0)
def stop():
        GPIO.output(wheel1A,0)
        GPIO.output(wheel1B,0)
        GPIO.output(wheel2A,0)
        GPIO.output(wheel2B,0)

##stepper_motor_start
delay = 0.015
cSteps=0
def setStep(w1, w2, w3, w4):
        GPIO.output(eyeWA, w1)
        GPIO.output(eyeWB, w2)
        GPIO.output(eyeWC, w3)
        GPIO.output(eyeWD, w4)
def rotateLeft(steps,check):
        global cSteps
        for i in range(0, steps):
                print('L'+str(cSteps))
                setStep(1,0,1,0)
                time.sleep(delay)
                setStep(0,1,1,0)
                time.sleep(delay)
                setStep(0,1,0,1)
                time.sleep(delay)
                setStep(1,0,0,1)
                time.sleep(delay)
                cSteps=cSteps-1
                if(check==True and checkDistance()!=-99):
                        return 1
        return 0

def rotateRight(steps,check):
        global cSteps
        for i in range(0, steps):
                print('R'+str(cSteps))
                setStep(1,0,0,1)
                time.sleep(delay)
                setStep(0,1,0,1)
                time.sleep(delay)
                setStep(0,1,1,0)
                time.sleep(delay)
                setStep(1,0,1,0)
                time.sleep(delay)
                cSteps=cSteps+1
                if(check==True and checkDistance()!=-99):
                        return 1
        return 0
##stepper_motor_end

##eye_start
def checkDistance():
        GPIO.output(eye1B,1)
        time.sleep(0.00001)
        GPIO.output(eye1B,0)
        while GPIO.input(eye1A)==0:
                pass
        start = time.time()
        while GPIO.input(eye1A) == 1:
                pass

        stop=time.time()
        distanceCM = (stop - start) * 170 * 100
        #print distanceCM
        #if distanceCM < 50 and (distanceCM <5 or distanceCM > 8):
        if distanceCM < 50:
                return -99

arrow = 'F'
findPath ='F'
skip='F'
maxRotate=6 #must be even number
while(1==1):
        #print(arrow)
        if(arrow=='SL'):
                spinLeft()
        if(arrow=='SR'):
                spinRight()
        if(arrow=='B'):
                goBackward()
        if(arrow=='F'):
                goForward()
        if(arrow=='X'):
                stop()
                if(findPath=='T'):
                        skip='T'
                        cSteps=0
                        found=rotateLeft(maxRotate, True)
                        if(found==0):
                                print('No left')
                                found=rotateRight((maxRotate*2) + 1, True)
                                if(found==1):
                                        rotateLeft(cSteps, False) #return eye to the front
                                        spinRight()


                        else:
                                print('Found something in left')
                                if(cSteps<0):
                                        cSteps = cSteps * -1
                                rotateRight(cSteps, False) #return eye to the front
                                spinLeft()
                        print('done........')
                        arrow='F'
                        findPath=''
                        setStep(0,0,0,0)
                        time.sleep(0.1)
                time.sleep(0.5)
        else:
                time.sleep(0.125)
        #arrow=raw_input("")

        if skip=='F' and checkDistance()==-99:
                arrow='X'
                findPath='T'
        else:
                skip='F'
        #if distanceCM > 200:
        #       arrow='F'

Leave a Reply

Your email address will not be published. Required fields are marked *