Best
Published © GPL3+

Pi Car

Build a Semi-autonomous, 4WD Raspberry Pi Car with a camera stream!

BeginnerFull instructions provided48,046
Pi Car

Things used in this project

Hardware components

Raspberry Pi 1 Model B
Raspberry Pi 1 Model B
×1
4-wheel Robot Smart Car Chassis Kits car
×1
L298N Stepper Motor Driver Controller Board Module
×1
HC-SR04 Sonar Module Distance Sensor
×1
40-pin Male to Female /Male to Male /Female to Female Breadboard Jumper Wires Ribbon Cables
×1
Camera Module
Raspberry Pi Camera Module
×1
External Battery Power Bank
ANY WILL DO! but size is key to ensure it fits on the chassis.
×1
Wireless USB Adapter Dongle
×1
Breadboard (generic)
Breadboard (generic)
×1
Wireless Keyboard
ANY WILL DO!
×1
Scorpi / Flexible Mount Raspberry Pi Model B Camera Board
OPTIONAL! (But very useful!)
×1

Software apps and online services

VLC

Hand tools and fabrication machines

Insulation Tape
Unless your going to solder the wires to the motors that is.
Screwdriver
ANY WILL DO REALLY.

Story

Read more

Schematics

Pi-Car Circuit (Draft1)

First Draft - Will Improve.

Notes: Red Motor wire is Yellow in the Diagram & the Black Motor wire is Green.

Car Schematics

How to wire the PiCar

Code

main.py

Python
This is the main python file that controls the motors. (Including Input from sensor)
import RPi.GPIO as gpio
import time
import sys
import Tkinter as tk
from sensor import distance

def init():
    gpio.setmode(gpio.BOARD)
    gpio.setup(7, gpio.OUT)
    gpio.setup(11, gpio.OUT)
    gpio.setup(13, gpio.OUT)
    gpio.setup(15, gpio.OUT)

def reverse(tf):
    gpio.output(7, False)
    gpio.output(11, True)
    gpio.output(13, False)
    gpio.output(15, True)
    time.sleep(tf)
    
def forward(tf):
    gpio.output(7, True)
    gpio.output(11, False)
    gpio.output(13, True)
    gpio.output(15, False)
    time.sleep(tf)
    
def turn_right(tf):
    gpio.output(7, True)
    gpio.output(11, False)
    gpio.output(13, False)
    gpio.output(15, True)
    time.sleep(tf)
    
def turn_left(tf):
    gpio.output(7, False)
    gpio.output(11, True)
    gpio.output(13, True)
    gpio.output(15, False)
    time.sleep(tf)

def stop(tf):
    gpio.output(7, False)
    gpio.output(11, False)
    gpio.output(13, False)
    gpio.output(15, False)
    time.sleep(tf)
    gpio.cleanup()

def key_input(event):
    init()
    print "Key:", event.char
    key_press = event.char
    sleep_time = 0.060
    
    if key_press.lower() == "w":
        forward(sleep_time)
    elif key_press.lower() == "s":
        reverse(sleep_time)
    elif key_press.lower() == "a":
        turn_left(sleep_time)
    elif key_press.lower() == "d":
        turn_right(sleep_time)
    elif key_press.lower() == "p":
        stop(sleep_time) 
    else:
        pass

    curDis = distance("cm")
    print("Distance:", curDis)
    
    if curDis <15:
        init()
        reverse(0.5)

command = tk.TK()
command.bind('<keypress>', key_input)
command.mainloop()

    

sensor.py

Python
Python file to initialise the Ultrasonic Sensor.
Create this file within the "PiCar" directory.
The video below will walk you through how each line of code works:
https://www.youtube.com/watch?v=HutxiWnX26w
import RPi.GPIO as gpio
import time

def distance(measure='cm'):
    gpio.setmode(gpio.BOARD)
    gpio.setup(12, gpio.OUT)
    gpio.setup(16, gpio.IN)
  
    time.sleep(0.3)
    gpio.ouput(12, True)
    time.sleep(0.00001)
    
    gpio.output(12, False)
    while gpio.input(16) == 0:
        nosig = time.time()
    
    while gpio.input(16) == 1:
        sig = time.time()
    
    tl = sig - nosig
    
    if measure == 'cm':
        distance = tl / 0.000058
    elif measure == 'in:
        distance = tl / 0.000148
    else:
        print('Improper choice of measurement: in or cm')
        distance = None
    
    gpio.cleanup()
    return distance
  
print(distance('cm'))
    

camera.sh

ActionScript
Code explained:
raspivid is used to capture the video
"-o -" causes the output to be written to stdout
"-t 0" sets the timeout to disabled
"-n" stops the video being previewed (remove if you want to see the video on the HDMI output)
cvlc is the console vlc player
"-vvv" and its argument specifies where to get the stream from
"-sout" and its argument specifies where to output it to
#!/bin/bash

raspivid -o - -t 0 -hf -w 600 -h 400 -fps 30 |cvlc -v stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8554}' :demux=h264

Credits

Best

Best

1 project • 13 followers
Thanks to Sentdex .

Comments