Autonomous PI Robot Tutorial - Part II

In this Tutorial we are continuing where we left in Part I

Now let's peek into Logic Implementation for robot anonymous Motion.

First we will create infinite loop and read the sensor readings:

distincmsleft, distincmsleft1, distincmsstraight, distincmsright1, distincmsright = GetSensorReadings()

Now we will detect if there is any obstacle in front first:

Case 1:

if distincmsstraight < distance_obstacle:

If there is any obstacle in front, we should stop the robot and then look for left and right readings. If the there is obstacle on left – turn right and vice versa. If there is no obstacle on either side, we are prioritising right turn here which is handled in else case. Below code does the trick:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if distincmsstraight &lt; distance_obstacle:
    Brake()
    if distincmsleft &lt; distance_obstacle or distincmsleft1 &lt; distance_obstacle:
        Brake()
        for x in range(1, 3, 1):
            Right()
            time.sleep(0.1)
            Brake()
    elif distincmsright &lt; distance_obstacle or distincmsright1 &lt; distance_obstacle:
        Brake()
        for x in range(1, 3, 1):
            Left()
            time.sleep(0.1)
            Brake()
    else:
        Brake()
        for x in range(1, 3, 1):
            Right()
            time.sleep(0.1)
            Brake()

case 2:
1
elif distincmsleft &lt; distance_obstacle or distincmsleft1 &lt; distance_obstacle:
Check if there is obstacle on left – if there is any obstacle turn right which is handled by below code:
1
2
3
4
5
Brake()
for x in range(1, 3, 1):
    Right()
    time.sleep(0.1)
    Brake()

case 3:

Check if there is obstacle on right – if there is any obstacle turn left which is handled by below code:

1
2
3
4
5
6
elif distincmsright &lt; distance_obstacle or distincmsright1 &lt; distance_obstacle:
    Brake()
    for x in range(1, 3, 1):
        Left()
        time.sleep(0.1)
        Brake()

case 4:

If none of above master cases are observed – simply move forward for 0.5 second and then again perform the same operation again! 

1
2
3
4
else:
    Forward()
    time.sleep(0.5)
    Brake()

Putting it all together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
TRIG = 18
ECHO = 22
servo_pin = 40
duty_cycle = 7.5
distance_obstacle = 40
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.setup(servo_pin, GPIO.OUT)
GPIO.output(TRIG, False)
pwm_servo = GPIO.PWM(servo_pin, 50)
pwm_servo.start(duty_cycle)
print "Waiting For Sensor To Settle"
time.sleep(2)
def ReadSensorReading():
    GPIO.output(TRIG, False# Set TRIG as LOWd
    time.sleep(0.1# Delay
    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)
    while GPIO.input(ECHO) == 0:
        pulse_start = time.time()
    while GPIO.input(ECHO) == 1:
        pulse_end = time.time()
    pulse_duration = pulse_end - pulse_start
    distance = pulse_duration * 17150
    distance = round(distance, 2)
    return distance
def Forward():
    GPIO.output(11, 1)
    GPIO.output(13, 0)
    GPIO.output(15, 1)
    GPIO.output(16, 0)
def Reverse():
    GPIO.output(11, 0)
    GPIO.output(13, 1)
    GPIO.output(15, 0)
    GPIO.output(16, 1)
def Right():
    GPIO.output(11, 1)
    GPIO.output(13, 0)
    GPIO.output(15, 0)
    GPIO.output(16, 1)
def Left():
    GPIO.output(11, 0)
    GPIO.output(13, 1)
    GPIO.output(15, 1)
    GPIO.output(16, 0)
def Brake():
    GPIO.output(11, 1)
    GPIO.output(13, 1)
    GPIO.output(15, 1)
    GPIO.output(16, 1)
def GetSensorReadings():
    pwm_servo.ChangeDutyCycle(13)
    time.sleep(0.1)
    distincmsleft = ReadSensorReading()
    print "distincmsleft " + str(distincmsleft)
    time.sleep(0.1)
    pwm_servo.ChangeDutyCycle(10)
    time.sleep(0.1)
    distincmsleft1 = ReadSensorReading()
    print "distincmsleft1 " + str(distincmsleft1)
    time.sleep(0.1)
    pwm_servo.ChangeDutyCycle(7.5)
    time.sleep(0.1)
    distincmsstraight = ReadSensorReading()
    print "distincmsstraight " + str(distincmsstraight)
    time.sleep(0.1)
    pwm_servo.ChangeDutyCycle(5)
    time.sleep(0.1)
    distincmsright1 = ReadSensorReading()
    print "distincmsright1 " + str(distincmsright1)
    time.sleep(0.1)
    pwm_servo.ChangeDutyCycle(3)
    time.sleep(0.1)
    distincmsright = ReadSensorReading()
    print "distincmsright " + str(distincmsright)
    time.sleep(0.1)
    return distincmsleft, distincmsleft1, distincmsstraight, distincmsright1, distincmsright
try:
    while (True):
        distincmsleft, distincmsleft1, distincmsstraight, distincmsright1, distincmsright = GetSensorReadings()
        if distincmsstraight &lt; distance_obstacle:
            Brake()
            if distincmsleft &lt; distance_obstacle or distincmsleft1 &lt; distance_obstacle:
                Brake()
                for x in range(1, 3, 1):
                    Right()
                    time.sleep(0.1)
                    Brake()
            elif distincmsright &lt; distance_obstacle or distincmsright1 &lt; distance_obstacle:
                Brake()
                for x in range(1, 3, 1):
                    Left()
                    time.sleep(0.1)
                    Brake()
            else:
                Brake()
                for x in range(1, 3, 1):
                    Right()
                    time.sleep(0.1)
                    Brake()
        elif distincmsleft &lt; distance_obstacle or distincmsleft1 &lt; distance_obstacle:
            Brake()
            for x in range(1, 3, 1):
                Right()
                time.sleep(0.1)
                Brake()
        elif distincmsright &lt; distance_obstacle or distincmsright1 &lt; distance_obstacle:
            Brake()
            for x in range(1, 3, 1):
                Left()
                time.sleep(0.1)
                Brake()
        else:
            Forward()
            time.sleep(0.5)
            Brake()
finally:
    pwm_servo.ChangeDutyCycle(7.5)
    Brake()
    print("Cleaning Up!")
    GPIO.cleanup()

Final Outcome




Types of projects that you can build with above setup:

There are endless possibilities as using this for experimentation and learning. But below are top 10 inspiration that will help you to broaden vision:
  1. Wireless Computer controlled robot.
  2. Add sensors and make it autonomous. Need extra sensors.
  3. Line follower with IR sensor.  Need extra sensors.
  4. Mobile controlled robot via bluetooth.
  5. Setup local server on PI and control it via browser.
  6. IOT machine – control via cloud.
  7. Enhance fleet management system via sensors. Need extra sensors.
  8. Add camera and track things. Will need PI camera.
  9. Room cleaner robot. Need extra vacuum and sensors.
  10. Voice controlled robot. Will need microphone attached.
So, get the hardware- PlugIn Internet- Start Making


GOD BLESS.WORK HARD

Comments