-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfollower.py
194 lines (170 loc) · 4.09 KB
/
follower.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import serial
import RPi.GPIO as GPIO
import time
def readSensor(_ser):
_ser.flushInput()
_ser.readline()
raw_data = _ser.readline()
data = raw_data.split('\t')
if len(data)<3:
return 0,0,0 # Error
else:
data = list(map(lambda x: x.strip(),data))
val = list(map(lambda x: int(x), data))
return val[0],val[1],val[2]
def isNumeric(s):
try:
int(s)
return True
except ValueError:
return False
def initSensor():
_ser = serial.Serial('/dev/ttyS0', 9600, timeout=1)
return _ser
def initMotor():
pwm0 = 18
pwm1 = 13
ain1 = 2
ain2 = 3
bin1 = 5
bin2 = 6
GPIO.setmode(GPIO.BCM)
GPIO.setup(pwm0, GPIO.OUT)
GPIO.setup(pwm1, GPIO.OUT)
GPIO.setup(ain1, GPIO.OUT)
GPIO.setup(ain2, GPIO.OUT)
GPIO.setup(bin1, GPIO.OUT)
GPIO.setup(bin2, GPIO.OUT)
GPIO.output(ain1, False)
GPIO.output(ain2, True)
GPIO.output(bin1, True)
GPIO.output(bin2, False)
p0 = GPIO.PWM(pwm0, 50)
p0.start(50)
p0.ChangeDutyCycle(0)
p0.ChangeFrequency(50)
p1 = GPIO.PWM(pwm1, 50)
p1.start(50)
p1.ChangeDutyCycle(0)
p1.ChangeFrequency(50)
return p0,p1
def setMotor(lr, cmd):
FWD1 = False if lr=="r" else True
FWD2 = True if lr=="r" else False
in1 = 2 if lr=="r" else 5
in2 = 3 if lr=="r" else 6
if cmd == 'F':
GPIO.output(in1, FWD1)
GPIO.output(in2, FWD2)
elif cmd == 'R':
GPIO.output(in1, FWD2)
GPIO.output(in2, FWD1)
elif cmd == 'S':
GPIO.output(in1, False)
GPIO.output(in2, False)
def setMotorSpeed(lr, speed):
global ml
global mr
if isNumeric(speed):
speed = speed if speed<100 else 100
speed = speed if speed>0 else 0
if lr=="r":
mr.ChangeDutyCycle(speed)
elif lr=="l":
ml.ChangeDutyCycle(speed)
def pt_right():
global turn_bias
global lspeed_bias
global rspeed_bias
setMotorSpeed("l",50*lspeed_bias);
setMotorSpeed("r",50*rspeed_bias);
setMotor("l",'F')
setMotor("r",'R')
time.sleep(0.05 * turn_bias)
setMotor("l",'F')
setMotor("r",'F')
setMotorSpeed("l",90*lspeed_bias);
setMotorSpeed("r",90*rspeed_bias);
def pt_left():
global turn_bias
global lspeed_bias
global rspeed_bias
setMotorSpeed("l",50*lspeed_bias);
setMotorSpeed("r",50*rspeed_bias);
setMotor("l",'R')
setMotor("r",'F')
time.sleep(0.05 * turn_bias)
setMotor("l",'F')
setMotor("r",'F')
setMotorSpeed("l",90*lspeed_bias);
setMotorSpeed("r",90*rspeed_bias);
def straight_a_bit(dist):
global speed_bias
global lspeed_bias
global rspeed_bias
setMotorSpeed("l",90*lspeed_bias);
setMotorSpeed("r",90*rspeed_bias);
setMotor("l",'F')
setMotor("r",'F')
time.sleep(0.01 * dist / speed_bias)
setMotor("l",'F')
setMotor("r",'F')
mr,ml = initMotor()
ser = initSensor()
heading = 0
target_heading = 0
thl = [450,400,390,340,290]
dist_bias = 0
turn_bias = 35
lspeed_bias = 0.9
rspeed_bias = 0.9
speed_bias = (lspeed_bias + rspeed_bias) / 2
try:
setMotor("l",'F')
setMotor("r",'F')
while True:
th = list(map(lambda x: x + dist_bias,thl))
IRH,IRF,IRB = readSensor(ser)
heading = IRF-IRB
lspeed_bias, rspeed_bias = 0.9, 0.9
print "target heading: {}".format(target_heading)
print "IRH: {} IRF: {} IRB: {}".format(IRH,IRF,IRB)
if IRH > 300 :
pt_right()
target_heading = 0
if IRF < 200 :
straight_a_bit(70)
pt_left()
straight_a_bit(170)
elif IRF < th[4]:
target_heading = 80
elif IRF < th[3]:
target_heading = 80
elif IRF < th[2]:
target_heading = 50
elif IRF < th[1]:
target_heading = 0
elif IRF < th[0]:
target_heading = -50
else:
target_heading = -80
# heading difference
hdiff = abs(heading - target_heading)
if hdiff < 20
lspeed_bias, rspeed_bias = 0.9, 0.9
elif hdiff < 40
lspeed_bias, rspeed_bias = 0.7, 0.7
elif hdiff < 60
lspeed_bias, rspeed_bias = 0.5, 0.5
else
lspeed_bias, rspeed_bias = 0.3, 0.3
if heading - target_heading > 0 :
setMotorSpeed("l",90*lspeed_bias);
setMotorSpeed("r",(90-(heading - target_heading)/4.4)*rspeed_bias);
if heading - target_heading < 0 :
setMotorSpeed("l",(90+(heading - target_heading)/4.4)*lspeed_bias);
setMotorSpeed("r",90*rspeed_bias);
finally:
ml.stop()
mr.stop()
GPIO.cleanup()