Autonomous emergency braking

There is a new technology that newer cars are equipped with. It's called autonomous emergency braking; no matter how serious we are while driving, we do get distractions, such as Facebook or WhatsApp notifications, which tempt us to look away from the road onto the screen of our phones. This can be a leading cause of road accidents; hence, car manufacturers are using autonomous braking technology. This generally relies on long range and short range radars and it detects the proximity of other objects around the car, and in the case of an eminent collision, it applies the brakes to the car autonomously preventing them from colliding from other cars or pedestrians. This is a really cool technology, but what's interesting is that we would be making it today with our own bare hands.

To make this, we will be using the IR proximity sensor to sense the proximity of objects around it. Now go ahead, grab a double-sided tape, and attach the IR distance sensor at the front of the car. Once this is done, connect the circuit as shown here:

All right then, we are all set up to code it up. The following is the code, and just copy it into your Pi:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

import Adafruit_ADS1x15
adc0 = Adafruit_ADS1x15.ADS1115()

GAIN = 1

adc0.start_adc(0, gain=GAIN)


Motor1a = 20
Motor1b = 21
Motor2b = 23
Motor2a = 24

GPIO.setup(Motor1a,GPIO.OUT)
GPIO.setup(Motor1b,GPIO.OUT)
GPIO.setup(Motor2a,GPIO.OUT)
GPIO.setup(Motor2b,GPIO.OUT)

def forward():
GPIO.output(Motor1a,0)
GPIO.output(Motor1b,1)
GPIO.output(Motor2a,0)
GPIO.output(Motor2b,1)

def stop():
GPIO.output(Motor1a,0)
GPIO.output(Motor1b,0)
GPIO.output(Motor2a,0)
GPIO.output(Motor2b,0)

while True:
F_value = adc0.get_last_result()
F = (1.0 / (F_value / 13.15)) - 0.35
forward()

min_dist = 20
if F < min_dist:
stop()

Now, let's see what's actually happening in this code. Everything is very much elementary; the IR proximity sensor is sensing the proximity of objects in front of it and gives the corresponding distance value in the form of analog signals. These signals are then taken by the ADC, and they are converted into digital values. These digital values are finally transferred to Raspberry Pi via the I2C protocol.

So far, so good. But you must be wondering what this line is doing?

   F =    (1.0 / (F_value / 13.15)) - 0.35

There is not much we are doing here, we are simply taking the digital values given by ADC, and using this formula, we are covering that digital value to understandable distance values in the unit of centimeters. This calculation is provided by the manufacturer, and we really don't have to get our head into this. Most of the sensors have these calculations provided. However, if you want to go and understand how and why we are using this formula, then I would recommend you go through the data sheet of the sensor. The data sheet is available easily online on the following link: https://engineering.purdue.edu/ME588/SpecSheets/sharp_gp2d12.pdf.

Moving on, the main part of the code is as follows:

min_dist = 20
If F < min_dist:
stop()

It is again very simple. We have entered a distance value, which in this program, we have set to 20. So, whenever the value of F  (the distance accrued by IR proximity sensor) is smaller than 20, then a stop() function is called. The stop function simply stalls the car and stops it from colliding with anything. 

Let's upload the code and see if it actually works! Make sure that you run this car indoors; otherwise, you would have a tough time trying to stop this car if it does not get any obstacles. Have fun! 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.172.146