Looking around 

Okay then enough of movie quotes. We can talk about many other functions that we can use over lists, but what we have done is enough for now. We will see the rest of them as the need arise. But for now let's take the things a step further in robotics. You might have seen a rotating object on top of many autonomous cars. The production cars generally don't tend to have primarily due to its high price, but research purpose cars are always loaded with it.

So what is this device? It's named LIDAR; it is an acronym for Light Detection and Ranging. I know bad acronym. There is a reason for LIDAR to be very common. It gives distance reading of the areas around it in a very precise way. However, buying it for our projects would slightly overkill as a good one would cost you close $500 to $10,000. If you still think that it's in your budget, then you would be very lucky! But for those who don't want to buy it. I have a good news for you. Today, we are going to build our own LIDAR scanner. So to make an area scanner, we need a servo over which we will mount our IR proximity sensor. Now to do this, we would need a slight makeshift arrangement. You can take a cardboard and fix it like we have done in the picture here, or otherwise, you can also use a right-angled aluminum and drill it to fix the components if you want it to do the pro way. The one thing to remember that the sensor must be facing exactly parallel to the ground and not up or down. 

Once the mounting is done, then it's time to connect the rest of the hardware. So go ahead and connect the hardware, as shown in the following diagram:

OK, so let's see what this thing can do, so get ready and upload this code: 

import RPi.GPIO as GPIO
import time
import Adafruit_ADS1x15

adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1

adc.start_adc(0, gain=GAIN)
GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.OUT)
GPIO.setwarnings(False)

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

j=12.5
k=2.5
i=0

distLR=[]
distRL=[]

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

i=0
k=0

del distLR[:]

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

print distRL

i=0
k=2.5
j=12.5

del distRL[:]

What did the code do? If it ran fine, then it should return you the scanned readings entire 180 degree broken down into 10 even steps. Go ahead—try it out and then return to see what actually is happening.

Now most of the code is elementary, and you must have also got an idea of what this code is actually doing. However, let's get deeper into it and see the specifics:

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

In this part of the program, we have defined a function named Distance(). As you can see, it is simply getting the reading from the ADC in the step D_value = adc0.get_last_result(); thereafter, this is the value procured that is stored in a variable D is then computed in the line D = (1.0/F-value/13.15)) - 0.35 to get the metric reading from the ADC reading. Finally, using the line Return D, we are returning the value D from the function:

distLR=[] 
distRL=[]

We have declared two lists: distLR, namely for distance for left to right swipe of the servo and distRL for the distance received in right to left swipe of the servo. You might be wondering how is it that there is nothing inside these brackets. It is completely normal to have an empty array declared. There is no need for them to have value initially:


while k<=12.5:
servo.ChangeDutyCycle(k)
time.sleep(.1)
distLR.insert(i,Distance())
k = k + 1
i = i + 1
print distLR

Now this is where the real action is happening. The while loop will be executed only till the time the value of k is less than or equal to 12.5. In the next line servo.ChangeDutyCycle(k), the value of the duty cycle will be whatever the value of k would be. Initially, the value of k would be 2.5 as we have already defined in the beginning of the program. Now we add another line time sleep(.1), which will make the program halt for .1 second. This is necessary; otherwise, the program would parse through this loop within milliseconds and the servo would not be able to cope up with it. Hence, this is a short delay. In the next line, we have distLR.insert(I,Distance()). This line of program is doing a lot of things. First, as we have named a Distance() function inside this line. As we defined, it would calculate the distance using the ADC and the IR proximity sensor. Thereafter, it would insert that distance value inside an the list distLR at the position I. Previously in our program, we have already assigned the value i = 0; hence, the distance value would be put up in the first position in the array. Once this entire process is done, then we move forward and increment the value by one in this line k = k + 1; thereafter, we do the same thing in I = I + 1. Now finally, once this loop's executed, the values of the list is printed using the line print distLR:

        i=0
k=0

In this line, we are simply resetting the values of i = 0 and k = 0 for the next loop: 

        del distLR[:]

This may be slightly new for you.  Whenever we use a colon inside a bracket, that basically means that the entire elements of the array would be deleted: 

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

print distRL

In this code, the same thing is happening that we did for the left to right swipe; the only difference being is that we are saving it a new list named distRL, and the swipe starts from 12.5% duty cycle and ends at 2.5%:

   i=0
k=2.5
j=12.5

del distRL[:]

When we have printed all the values, we again reset the values of i = 1k = 2.5, and j = 12.5 so that our first loop can start seamlessly further to it we are also making sure that there is nothing left inside the list distRL.

So this is how our code was working, straight and simple!