Making it fully autonomous

Now, you must have understood the basics of autonomous driving using a simple proximity sensor. Now is the time when we make it fully autonomous. To make it fully autonomous, we must understand and map our surroundings rather than to just turn the vehicle till the time it encounters an obstacle. We basically need to divide this whole activity in the following two basic parts:

  • Scanning the environment
  • Deciding what to do with the perceived data

Now, let's first write the code and then see what we need to do: 

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
Motor2a = 23
Motor2b = 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 right():
GPIO.output(Motor1a,0)
GPIO.output(Motor1b,1)
GPIO.output(Motor2a,1)
GPIO.output(Motor2b,0)

def left():
GPIO.output(Motor1a,1)
GPIO.output(Motor1b,0)
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:

forward()

F_value = adc0.get_last_result()
F = (1.0 / (F_value / 13.15)) - 0.35

min_dist = 20
if F< min_dist:

stop()

right()
time.sleep(1)

F_value = adc0.get_last_result()
F = (1.0 / (F_value / 13.15)) - 0.35
R = F

left()

time.sleep(2)

F_value = adc0.get_last_result()
F = (1.0 / (F_value / 13.15)) - 0.3

L = F

if L < R:
right()
time.sleep(2)

else:
forward()

Now most of the program is just like all of our previous programs; in this program, we have defined the following functions: 

  • forward()
  • right()
  • left()
  • stop()

There is not much I need to tell you about defining the functions, so let's move ahead and see what else do we have in stock for us.

The main action is going on in our infinite loop while True:. Let's see what exactly is happening:

while True:

forward()

F_value = adc0.get_last_result()
F = (1.0 / (F_value / 13.15)) - 0.35

min_dist = 20
if F< min_dist:

stop()

Let's see what this part of code is doing:

  • The first thing that is executed as soon as our program enters the infinite loop is the forward() function; that is, as soon as the infinite loop is executed, the vehicle will start to go forward
  • Thereafter, F_value = adc.get_last_result() is taking the reading from ADC and storing it in a variable named F_value 
  • F = (1.0/(F-value/13.15))-0.35 is calculating the distance into understandable metric distance value
  • min_dist = 20 , we have simply defined the minimum distance that we will be using later

Once this part of code is done, then the if statement will check whether F < min_dist:. If it is so, then the code that is under the if statement will start to execute. The first line of this will be the stop() function. So whenever the vehicle encounters any obstacle in front of it, the first thing it will do is stop. 

Now, as I mentioned, the first part of our code is to understand the environment, so let's go ahead and see how we do it :

right()
time.sleep(1)

F_value = adc0.get_last_result()
F = (1.0 / (F_value / 13.15)) - 0.35
R = F

left()

time.sleep(2)

F_value = adc0.get_last_result()
F = (1.0 / (F_value / 13.15)) - 0.35

L = F

After the vehicle has stopped, it will immediately turn right. As you can see, the next line of code is time.sleep(1), so for another 1 second, the vehicle will keep turning right. We have randomly picked a time of 1 second, you can tweak it later. 

Once it has turned right, it will again take the reading from the proximity sensor, and in using this code R=F, we are storing that value in a variable named R.

After it has done that, the car will turn to the other side, that is, toward left side using the left() function, and it will keep turning left for 2 seconds as we have time.sleep(2). This will turn the car toward left of the obstacle. Once it has turned left, it will again take in the value of proximity sensor and store the value in a variable L using the code L = F.

So essentially what we have done is that we have scanned the areas around us. In the center, we have an obstacle. It will first turn right and take the distance value of the right side; thereafter, we will turn left and take the distance value of the left side. So we essentially know the environment around the obstacle. 

Now we come to the part where we have to make a decision, in which direction we have to go forward. Let's see how we will do it:

  if L < R:
right()
time.sleep(2)

else:
forward()

Using an if statement, we are comparing the values of the proximity sensor for the right and left of the obstacle by this code if L < R:. If L is smaller than R, then the vehicle will turn right for 2 seconds. If the condition is not true, then the else: statement would come into action, which will in turn make the vehicle go forward. 

Now if we see the code in a larger picture, the following things are happening:

  • The vehicle would go forward until it encounters an obstacle 
  • Upon encountering an obstacle, the robot will stop
  • It will first turn right and measure the distance to objects in front of it
  • Then, it will turn left and measure the distance to objects in front of it
  • After this, it will compare the distance of both left and right and choose which direction it has to go in
  • If it has to go right, it will turn right and then go forward
  • If it has to go left, then it would already be in the left turned orientation, so it simply has to go straight 

Let's upload the code and see whether things happen according to plan or not. Remember this, though every environment is different and every vehicle is different, so you may have to tweak the code to make it work smoothly. 

Now I will leave you with a problem. What if in both case the reading of the sensor is infinity or the maximum possible value that it can give? What will the robot do?

Go ahead, do some brainstorming and see what we can do to solve this problem! 

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

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