Making it more advanced

In the previous chapter we have seen that we are making the car turn in any of the directions just based on our hand movements, however there is a problem with the previous code. Firstly the car is moving in one direction at a time, that is it's going either forward or backward or turning left or right. 

It was not able to make a banking turn based on the hand gestures itself. To make it capable of doing so, we need to make the code even smarter. The overall connections to the robot would be exactly the same. But the code would be slightly different, so let's see what it is: 

import smbus
from time import sleep
import RPi.GPIO as GPIO
int1 = 12
int2 = 16
int3 = 18
int4 = 15
GPIO.setup(int1, GPIO.OUT)
GPIO.setup(int2, GPIO.OUT)
GPIO.setup(int3, GPIO.OUT)
GPIO.setup(int4, GPIO.OUT)
PWM1 = GPIO.PWM(12, 100)
PWM2 = GPIO.PWM(16, 100)
PWM3 = GPIO.PWM(18, 100)
PWM4 = GPIO.PWM(15, 100)
PWM1.start(0)
PWM2.start(0)
PWM3.start(0)
PWM4.start(0)
PWR_MGMT_1 = 0x6B
SMPLRT_DIV = 0x19
CONFIG = 0x1A
GYRO_CONFIG = 0x1B
INT_ENABLE = 0x38
ACCEL_XOUT_H = 0x3B
ACCEL_YOUT_H = 0x3D
ACCEL_ZOUT_H = 0x3F
GYRO_XOUT_H = 0x43
GYRO_YOUT_H = 0x45
GYRO_ZOUT_H = 0x47

def MPU_Init():
bus.write_byte_data(Device_Address, SMPLRT_DIV, 7)
bus.write_byte_data(Device_Address, PWR_MGMT_1, 1)
bus.write_byte_data(Device_Address, CONFIG, 0)
bus.write_byte_data(Device_Address, GYRO_CONFIG, 24)
bus.write_byte_data(Device_Address, INT_ENABLE, 1)
def read_raw_data(addr):
high = bus.read_byte_data(Device_Address, addr)
low = bus.read_byte_data(Device_Address, addr+1)
value = ((high << 8) | low)
if(value > 32768):
value = value - 65536
return value
bus = smbus.SMBus(1)
Device_Address = 0x68
MPU_Init()
while True:
acc_x = read_raw_data(ACCEL_XOUT_H)
acc_y = read_raw_data(ACCEL_YOUT_H)
acc_z = read_raw_data(ACCEL_ZOUT_H)
gyro_x = read_raw_data(GYRO_XOUT_H)
gyro_y = read_raw_data(GYRO_YOUT_H)
gyro_z = read_raw_data(GYRO_ZOUT_H)
Ax = (gyro_x/160)- 16384
Ay = (gyro_y/160)-16384
if Ax > 20:
if Ay > 20:
dc1 = Ax - Ay
PWM1.changeDutyCycle(dc1)
dc3 = Ax + Ay
PWM3.changeDutyCycle(dc3)
elif Ay <- 20:
dc2 = Ax + Ay
PWM1.changeDutyCycle(dc)
dc4 = Ax - Ay
PWM3.changeDutyCycle(dc4)
else:
dc1=Ax
PWM1.changeDutyCycle(dc)
dc3=Ax
PWM3.changeDutyCycle(dc)
if Ax < -20:
if Ay > 20:
dc1 = Ax - Ay
dc3 = Ax + Ay
PWM1.changeDutyCycle(dc1)
PWM3.changeDutyCycle(dc3)
if Ay <- 20:
dc2 = Ax + Ay
dc4 = Ax - Ay
PWM2.changeDutyCycle(dc2)
PWM4.changeDutyCycle(dc4)
else:
dc2=Ax
dc4=Ax
PWM2.changeDutyCycle(dc2)
PWM4.changeDutyCycle(dc4)

Now let's see all the changes we have done to the code. The entire sensing mechanism is the same, however, data processing has a major overhaul. So let's see what it is:

 If Ax > 20:
if Ay > 20:
dc1 = Ax - Ay
PWM1.changeDutyCycle(dc1)
dc3 = Ax + Ay
PWM3.changeDutyCycle(dc3)

Now here we are comparing the value of Ax. If the value of Ax > 20 then the code below it would run. We have done this because, the accelerometer is extremely sensitive and can sense slightest vibration. Due to this there can be erroneous output. So to filter it we have a threshold value of 20. That is till the time the accelerometer is 20% tilted, this code will not come into effect. Similarly we are doing this for the y axis as well. Once that is done that percentage value is given to the line dc1 = Ax - Ay. What this is doing is taking the tilt in x axis which is the forward axis and subtracting it with movement of Y. In the second line we are doing the same thing with the other side of the motors however rather than subtracting the value of Y we are adding it. Hence what it would do is to create a difference of speed in between the speed of motors on the opposite ends. This speed difference would be directly proposal to the angular tilt of the Y axis. Hence more the tilt, the more would be the change in the speed and more would be the angle of the turn. 

elif Ay <- 20:

dc2 = Ax + Ay
PWM1.changeDutyCycle(dc)
dc4 = Ax - Ay
PWM3.changeDutyCycle(dc4)

In the next line what we have done is that we have made a condition for the tilt on the other side of the accelerometer by sensing Ay < -20. If the value is smaller than -20 then the following algorithm would come into play. 

Here the lines are exactly the same. However the mathematical operators have been inverted. Hence  for the first motor, instead of subtracting the value of Y now we are adding it. On the other hand for the second motor rather than adding the value we are subtracting it. 

 Else:

dc1=Ax
PWM1.changeDutyCycle(dc)
dc3=Ax
PWM3.changeDutyCycle(dc)

Finally, if the value of Ax is greater than 20 but the value of Ay is between -20 and +20 then we will assume that the car has to go straight. Hence the value of the Ax is directly getting passed on to both the motors, making it go entirely straight. 

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

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