I hope that you are having fun with this little zippy thing. It is interesting how simple the application of sensors can be and how much difference it can make. As you have learned the basics, it's now time to move ahead and give the car some more powers.
In the previous code, we just made the robot stop in front of the obstacles, why don't we make it steer around the car? It's going to be super simple yet super fun. All we need to do is to tweak the function stop() and make it able to turn. Obviously, we will also change the name of the function from stop() to turn() just for the sake of clarity. One thing to remember that you won't have to rewrite the code; all we need to do is some minor tweaking. So, let's see the code and then I will tell you what exactly has changed and why:
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 turn():
GPIO.output(Motor1a,0)
GPIO.output(Motor1b,1)
GPIO.output(Motor2a,1)
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
while F < min_dist:
turn()
As you would have noted, everything remains pretty much the same except for the following:
def turn():
GPIO.output(Motor1a,0)
GPIO.output(Motor1b,1)
GPIO.output(Motor2a,1)
GPIO.output(Motor2b,0)
This part of the code is defining the turn() function in which the opposite side wheels of the vehicles would be spinning in the opposite direction; hence, making the car turn on its own axis:
min_dist = 20
while F < min_dist:
turn()
Now this is the main part of the program; in this part, we are defining what the car would do if it encounters any sort of obstacle in front of it. In our previous programs, we were primarily just telling the robot to stop as soon as it encounters any obstacle; however, now we are chaining the stop function with a turn function, which we have defined previously in the program.
We simply put in a condition as follows:
min_dist = 20
If F < min_dist:
turn()
Then, it would turn just for a fraction of seconds, as the microcontroller would parse through the code and execute it and get out of the condition. To do this, our Raspberry Pi would hardly take a couple of microseconds. So, we might not even able to see what has happened. Hence, in our program, we have used a while loop. This essentially keeps the loops running till the time condition is fulfilled. Our condition is while F < min_dist:, so till the time the robot is detecting an object in front of it, it will keep executing the function inside it, which in our case is, the turn() function. So in simple words, till the time it has not turned enough to avoid the obstacle, the vehicle would keep turning and then once the loop is executed, it will again jump back to the main program and keep going straight.
Simple isn't it? That's the beauty about programming!