LIDAR on an autonomous vehicle 

Remember the last time we made autonomous car. It was cool, and surely it might be something you can show off to your friends. However, now what we are about to make is surely cooler than anything we have ever done till now.

We are going to put this area scanner over our robotic vehicle. But wait, didn't we scan the area earlier using the same sensor and turning the car to other sides. We did it and it worked fine, almost fine. I bet sometimes it wasn't as accurate as you thought it would be. But that's not the real problem. The main problem is that it was not seamless. It has to stop in between check for spaces and then move in either direction. What we are going to do now is something that is a step ahead. So before doing any more explanation, let's go ahead and make this new robotic vehicle and then you be the judge to decide whether it is cooler. 

So, to make it, you need to mount the area scanner on the vehicle. It's advisable that you set it up at the frontend of the vehicle and make sure that the arm of the servo is able to rotate 180 degrees. You can use the similar method that we did to fix the IR sensor on top of the servo. While you are doing all of this, try using cable ties to make sure the cables are not messy and also make sure to leave some slack for the movement of the shaft and the sensor on top of it. These cable ties can make your life really simple. Once we are all set up, you should connect the IR proximity using an ADS1115 to the Raspberry Pi and then connect the motor driver, as shown in the following diagram: 

Once we are done go ahead and upload the following code:

import RPi.GPIO as GPIO
import time
import Adafruit_ADS1x15

adc0 = Adafruit_ADS1x15.ADS1115()
GAIN = 1
adc0.start_adc(0, gain=GAIN)

GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.OUT)

servo = GPIO.PWM(14, 50)
servo.start(0)

def Distance():
D_value = adc0.get_last_result()
D = (1.0 / (F_value / 13.15)) - 0.35
Return D

GPIO.setup(20,GPIO.OUT)
GPIO.setup(21,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)

LForward = GPIO.PWM(20, 50)
LReverse = GPIO.PWM(21, 50)
RForward = GPIO.PWM(23,50)
RReverse = GPIO.PWM(24,50)

def stop():
LForward.changeDutyCycle(0)
LReverse.changeDutyCycle(0)
RForward.changeDutyCycle(0)
RReverse.changeDutyCycle(0)


def direction(index):

if index == 0 :
LForward.changeDutyCycle(0)
LReverse.changeDutyCycle(30)
RForward.changeDutyCycle(30)
RReverse.changeDutyCycle(0)

elif index == 1

LForward.changeDutyCycle(20)
LReverse.changeDutyCycle(0)
RForward.changeDutyCycle(50)
RReverse.changeDutyCycle(0)

elif index == 2 :

LForward.changeDutyCycle(50)
LReverse.changeDutyCycle(0)
RForward.changeDutyCycle(50)
RReverse.changeDutyCycle(0)

elif index == 3 :

LForward.changeDutyCycle(50)
LReverse.changeDutyCycle(0)
RForward.changeDutyCycle(20)
RReverse.changeDutyCycle(0)


elif index == 4 :

LForward.changeDutyCycle(20)
LReverse.changeDutyCycle(0)
RForward.changeDutyCycle(0)
RReverse.changeDutyCycle(20)

else:
stop()

j=12.5
k=2.5
i=0

dist1=[]
dist2=[]

while True:

while k<=12.5:
servo.ChangeDutyCycle(k)
time.sleep(.2)
dist1.insert(i,Distance())
k = k + 2.5
i = i + 1

print dist1

i=0
k=2

max_dist1 = max(dist1)
max_dist1_index = dist1.index(max_dist1)

direction(max_dist1_index)

del dist1[:]

print max_dist1
print max_dist1_index

while j>=2.5:
servo.ChangeDutyCycle(j)
time.sleep(.2)
j = j - 2.5
dist2.insert(i,Distance())
i = i + 1

print dist2

i=0
j=12

max_dist2 = max(dist2)
max_dist2_index = dist2.index(max_dist2)

direction(max_dist2_index)

del dist2[:]

print max_dist2
print max_dist2_index

Phew! That was long wasn't it? But trust me it might be long, but not tough. So let's see what this code is doing: 

LForward = GPIO.PWM(20, 50)
LReverse = GPIO.PWM(21, 50)
RForward = GPIO.PWM(23,50)
RReverse = GPIO.PWM(24,50)

This stuff might look pretty new to you. Though it isn't. What we are doing is that we are defining which pin number will be operating at what PWM frequency. Also, we have named every GPIO pins that is being used for motor control. OK then, it is fine that we are doing all this, but why have we suddenly started to give PWM to motor drivers. Were we not happy giving a simple high pulse?

The answer is very straightforward. With the use of PWM, we were able to change the brightness of an LED in previous chapters. Similarly, by changing the PWM output to the control pins of the motor driver, you cannot only define which direction to spin in. But also the speed at which it can spin. This is all done with PWM. So let's say pin number 20 is getting a PWM at 50% duty cycle. So it basically means that the motor which is attached to it will get half the input voltage that the motor driver is receiving. So now we can not only control which direction we want the motor to spin but also at what speed we can do so:

def direction(index):

if index == 0 :
LForward.changeDutyCycle(0)
LReverse.changeDutyCycle(30)
RForward.changeDutyCycle(30)
RReverse.changeDutyCycle(0)

elif index == 1
LForward.changeDutyCycle(20)
LReverse.changeDutyCycle(0)
RForward.changeDutyCycle(50)
RReverse.changeDutyCycle(0)

In this statement, we have defined a function direction(index). What this does is that it compares the value of index and based on it. The power will be given to the motors. So lets say that the index is 0. In this case the wheel on the left side would move in reverse direction whereas the right wheel would move in the reverse direction this will turn the robot on its axis. 

In the next statement, we have written an elif statement, so if the else statement is not true, then it will check for the rest else if statement in the body. There are four elif statements in the entire definition of direction(index), which basically means that it will check for each one of it and do either of the activities based on the value of the argument. In this case, it is the index. Further, there is a final else statement, which would be done if none of the cases are true. So according to the statement, it will call a function of stop. That would stop the vehicle:

max_dist1 = max(dist1)

This line is pretty interesting as we are using another fun part of the lists that we have used. So, with the max() method, we can find the largest value inside a list. So, in this line, we are simply finding the max value and putting it in a variable named max_dist1:

max_dist1_index = dist1.index(max_dist1)

The beauty of lists just doesn't seem to end. In this line, we are using another method named index(); this method gives us the index of the value inside the list. So, we can know where the value exists in the list. Hence, in this line, we are proving the value of max_dist1. The method index() searches the index number and stores that value down into a variable named max_dist1_index:

 direction(max_dist1_index)

As we have already defined the function Direction(), now all we are doing is calling the function to decide which direction to go in. Prefect then, power up your vehicles and see how well they are driving and do not forget to shoot a video and post it online.

Have fun!

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

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