Simple Pi Robot

Simple Pi Robot aims to put robot control in simple form.

Simple Pi Robot aims to put robot control in simple form.

The part list

(1) Raspberry pi (Any model) but with the recent launch of pizero or pi 2 shall be a good option, my current model employs B+.

(2) 40 pin GPIO cable (if using pi B+ or pi 2).

(3) Breadboard (for quickly assembly various sensors).

(4) A 2wd chassis.

(5) Distance sensor (Ultrasonic HC SR04).

(6) Power bank (for powering up the pi).

(7) Rechargeable batteries AA (preferably 2100 mAH).

(8)Jumper wires both Male & female type, Resistors.

(9) WiFi adapter (EDUP/EDIMAX for communicating wirelessly with pi).

(10) Memory card (4GB and higher for running the OS on pi).

(11) Motor drivers (L298 ).

(12 )servo motor.

(13) Miscellaneous – cable ties (for tying the jumper wires) and foam tape (for holding servo or any other sensor where screw assembly cannot be used).

Step 1: Choosing the Motor drive shield

Currently there are very few motor drive shield available for raspberry pi, to name some:-

(1) RTK Motor controller shield 

(2) Pololu DRV8835 Dual Motor Driver Kit for Raspberry Pi 

(3) Adafruit DC and Stepper Motor HAT for Raspberry Pi

(4) Raspirobot board made by Adafruit 

and recently underdevelopment like ZEROBORG 

One of the most common problem building any robot, is to minimize the wiring requirement and the same can be achieved by using the shields/hat. I tried building my robot, first with one of the shield equivalent to Raspirobot from the manufacture ALSROBOT. The kits ships from china but the problem was i was unable to increase the motor input voltage. The maximum was less than 5 Volts, with slight unbalance between the voltages, still one can check the following link – ALSROBOT – PI Motor driver shield

Anyhow In my current tutorial i have used the cheap and versatile L298 motor driver – L298.

The advantage with the above board apart from being cheap, is that there is regulated 5Volts output is available.

I have used the L298 board to drive two DC motors and 1 servo motors.

Step 2: The L298 Motor Driver

I have Mounted L298 Motor driver at the bottom of my chassis, now to connect the DC motor and servo motor connect

(i) DC Motor -A to output -A (+ & -).

(ii) DC Motor -B to output -B (+ & -).

(iii) Servo motor +ve to + 5V regulated supply of L298 Board & servo motor -ve to GND of L298 Board.

Keep the enable pin jumper as it is, if you don't want speed control, else remove the jumper. The jumper in place ensures +5V supply to enable pin which in turn drives motor at rated speed.

Now connect 4 nos jumper wires to the control inputs, connect other end of jumper wires to GPIO pin as designated in next step.

For servo motor connect one no. jumper wire for control to GPIO pin as in next step

Step 3: The servo Motor

The servo has a 3 wire connection: power, ground, and control. The power source must be constantly applied.

The control signal is pulse width modulated (PWM), but here the duration of the positive-going pulse determines the position of the servo shaft. For instance, a 1.520 millisecond pulse is the center position for a Futaba S148 servo. A longer pulse makes the servo turn to a clockwise-from-center position, and a shorter pulse makes the servo turn to a counter-clockwise-from-center position.

I have used Futuba s3003 servo – the connection is very simple the “+” & “-” goes to the L298 Board as outlined earlier. It's important to look at the operating voltage of servo ( in my case its 4.8 – 6 V see image above), the signal wire is to be connected to GPIO output , normally its white or orange.

Controlling servo motors in raspberry pi might be tricky, but then there is very powerful library hosted @ RPIO.PWM ,to install it on pi use the following code.

sudo apt-get install python-setuptools
sudo easy_install -U RPIO

To know more about RPIO.PWM and the DMA used , please refer to the link https://pythonhosted.org/RPIO/pwm_py.html

Step 4: The Robot chasis

I have used Ellipzo Robot chassis with 2wd, 2wd are simple and easy to control.

The kit comes included with DC Motors, pan kit for servo motors and all necessary hardware's to assemble the kit.

The detailed link is available at – Ellipzo Robot chaisis kit.

Pl. refer to video for assembly of Raspberry Pi, along with L298 motor driver, the Breakout board, camera module and the power bank.

Step 5: The Distance sensor

Integrating the distance sensor is easy and we need just 1k resistor, along with jumper wires. Connect VCC & GND to pi +5Volts & GND respectively.

The other two pins TRIG & ECHO are to be connected to GPIO pins as in preceding steps. Remember to connect the resistor as shown in image.

The Python code to measure distance is included in the last step.

Step 6: Raspberry Pi -camera – Video streaming using VLC player

Here I have used Pi camera module, the set up is quite easy and you can refer the link :Raspberry pi Camera setup

For video streaming, with VLC begin with installing VLC on raspberry pi

sudo apt-get install vlc

To start streaming the Camera Video Using RTSP enter following

raspivid -o - -t 0 -n | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264

or with proper width & height use following code

raspivid -o - -t 0 -n -w 600 -h 400 -fps 12 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264

Now to view the stream over VLC player open VLC on your remote system than open a network stream using

rtsp://###.###.###.###:8554/

where ###.###.###.### is the address of your pi given by the network router.

Now as the pi moves inside your home see the video stream on your remote system.

Step 7: Raspberry pi pin-out & python code

Step 8: Some Assembly Images

Code

from RPIO import PWM
import RPi.GPIO as GPIO
from RPIO import PWM
import RPi.GPIO as GPIO
import time
from time import sleep
from subprocess import call


GPIO.setmode(GPIO.BCM)

GPIO.setup(19,GPIO.OUT)
GPIO.setup(26,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(20,GPIO.OUT)
GPIO.setup(21,GPIO.IN)
GPIO.setup(8,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(9,GPIO.OUT)

TRIG=18
ECHO=17



print"controls"
print"1: move forward"
print"2: move reverse"
print"3: stop robot"
print"4: take picture with user defined name"
print"5: move forward with speed control"
print"6: Rotate the Robot"
print"7: Turn the Robot"
print"8: for servo control please"
print"11 : welcome to autonomous control"
print"press enter to send command"

def takestillpic(inp):
    print" please enter photo character"
    inp = raw_input()
    call ( ["raspistill -vf -hf -o " + str(inp) + ".jpg" ],shell=True )
    



def fwd():
    GPIO.output(19,True)
    GPIO.output(26,False)
    GPIO.output(16,True)
    GPIO.output(20,False)

def rev():
    GPIO.output(19,False)
    GPIO.output(26,True)
    GPIO.output(16,False)
    GPIO.output(20,True)

def stop():
    GPIO.output(19,False)
    GPIO.output(26,False)
    GPIO.output(16,False)
    GPIO.output(20,False)

def distmeas():
    print" Distance measurement in progress"

    GPIO.setup(TRIG,GPIO.OUT)
    GPIO.setup(ECHO,GPIO.IN)

    GPIO.output(TRIG,False)
    print" waiting for sensor to settle please"
    time.sleep(2)

    GPIO.output(TRIG,True)
    time.sleep(0.00001)
    GPIO.output(TRIG,False)

    while GPIO.input(ECHO)==0:
        pulse_start=time.time()

    while GPIO.input(ECHO)==1:
        pulse_end=time.time()


    pulse_duration = pulse_end - pulse_start

    distance = pulse_duration * 17150

    distance = round(distance,2)

    print " Distance ", distance, "cm"

    if distance < 50 :
           GPIO.output(19,False)
           GPIO.output(26,False)
           GPIO.output(16,False)
           GPIO.output(20,False)
       
           time.sleep(1)
           print " robot stopped as distance is less"
           print " Now Robot going Backward"
           GPIO.output(19,False)
           GPIO.output(26,True)
           GPIO.output(16,False)
           GPIO.output(20,True)
       
           time.sleep(1)
           GPIO.output(19,False)
           GPIO.output(26,False)
           GPIO.output(16,False)
           GPIO.output(20,False)

           TLr()
           time.sleep(4)
           fwd()
           distmeas()
           
           
    else:
         distmeas()
         
           
    
    

def TL():
    GPIO.output(19,True)
    GPIO.output(26,False)
    GPIO.output(16,False)
    GPIO.output(20,False)
def TLr():
    GPIO.output(19,True)
    GPIO.output(26,False)
    time.sleep(0.75)
    GPIO.output(19,False)
    GPIO.output(26,False)


while True:
    inp= raw_input()
    if inp =="1":
        fwd()
        
        print"robot moving in fwd direction"
        
        
    
    elif inp =="2":
        rev()
        print"robot moving in rev direction"
    elif inp=="3":
        stop()
        
    
        print"robot stopped"

    elif inp =="4":
          takestillpic(inp)
          print " photo please"  
          
        
          
          
        
    elif inp =="5":
        GPIO.output(7,False)
        GPIO.output(8,False)

    elif inp =="6":
         TL()
    elif inp =="7":
         TLr()
    elif inp =="8":
         servo = PWM.Servo()
         servo.set_servo(27,1000)
         time.sleep(2)
         servo.stop_servo(27)
    elif inp =="9":
         servo = PWM.Servo()
         servo.set_servo(27,1500)
         time.sleep(2)
         servo.stop_servo(27)
    elif inp =="10":
         servo = PWM.Servo()
         servo.set_servo(27,2000)
         time.sleep(2)
         servo.stop_servo(27)
    elif inp == "11":
         
         fwd()
         distmeas()
         

GPIO.cleanup()

Source: Simple Pi Robot


About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

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

Scroll to Top