-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
287 lines (239 loc) · 5.84 KB
/
test.py
File metadata and controls
287 lines (239 loc) · 5.84 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from machine import Pin, time_pulse_us, PWM
import time
import utime
# --- Constants & Configurations ---
# Motor Pin Definitions
RIGHT_MOTOR_1_PIN = 10
RIGHT_MOTOR_2_PIN = 11
LEFT_MOTOR_1_PIN = 12
LEFT_MOTOR_2_PIN = 15
ENA_PIN = 7
ENB_PIN = 6
# Servos
#SERVO_PIN = 16
#ultrasonic pins
TRIG_PIN = 14
ECHO_PIN = 17
# IR sensor Pins
IR1_PIN = 1
IR2_PIN = 2
IR3_PIN = 4
IR4_PIN = 5
IR5_PIN = 6
# PID Tuning
KP = 0.2
KD = 0.1
MAX_SPEED = 4500
#global variables
pos1 = 115 #servo
#set_position = 200
d = 0
last_p =0
sum_w = 0
sum_i = 0
error = 0
i=0
p = 0
sensor = [0, 0, 0, 0, 0]
current_position=0
# --- Pin Initializations ---
# Motor pins
right_motor_1 = Pin(RIGHT_MOTOR_1_PIN, Pin.OUT)
right_motor_2 = Pin(RIGHT_MOTOR_2_PIN, Pin.OUT)
left_motor_1 = Pin(LEFT_MOTOR_1_PIN, Pin.OUT)
left_motor_2 = Pin(LEFT_MOTOR_2_PIN, Pin.OUT)
#PWM
enable1 = PWM(Pin(ENA_PIN))
enable2 = PWM(Pin(ENB_PIN))
enable1.freq(500)
enable2.freq(500)
#ultrasonic
trig_pin = Pin(TRIG_PIN, Pin.OUT)
echo_pin = Pin(ECHO_PIN, Pin.IN)
# IR sensors
ir_pins = [Pin(IR1_PIN, Pin.IN), Pin(IR2_PIN, Pin.IN), Pin(IR3_PIN, Pin.IN), Pin(IR4_PIN, Pin.IN), Pin(IR5_PIN, Pin.IN)]
'''
# Servo
servo = PWM(Pin(SERVO_PIN))
servo.freq(50) # Set servo PWM frequency to 50Hz (standard)
initial servo position
def set_servo_angle(angle):
pulse_width = int(2000 / 180 * angle + 500)
servo.duty_u16(pulse_width * 3.268292682926829) #convert pulse width into correct duty cycle
'''
def setspeed(left_speed, right_speed):
enable1.duty_u16(left_speed)
enable2.duty_u16(right_speed)
# --- Function Definitions ---
def measure_distance():
trig_pin.low()
time.sleep_us(2)
trig_pin.high()
time.sleep_us(10)
trig_pin.low()
while not echo_pin.value():
pass
time1 = time.ticks_us()
while echo_pin.value():
pass
time2 = time.ticks_us()
distance = time.ticks_diff(time2, time1)
return distance * 340 / 2 / 10000
def sensor_readings():
return [pin.value() for pin in ir_pins]
def forward_cross(c):
cross = 0
while cross < c:
forward()
if all(reading == 1 for reading in sensor_readings()):
cross+=1
while all(reading == 1 for reading in sensor_readings()):
forward()
def left_u_turn():
while ir_pins[2].value() == 1:
left_motor_1.off()
left_motor_2.off()
right_motor_1.off()
right_motor_2.on()
setspeed(150, 150)
while ir_pins[2].value() == 0:
left_motor_1.off()
left_motor_2.off()
right_motor_1.off()
right_motor_2.on()
setspeed(150, 150)
stop()
time.sleep_ms(500)
def right_u_turn():
while ir_pins[2].value() == 1:
left_motor_1.off()
left_motor_2.on()
right_motor_1.off()
right_motor_2.off()
setspeed(150, 150)
while ir_pins[2].value() == 0:
left_motor_1.off()
left_motor_2.on()
right_motor_1.off()
right_motor_2.off()
setspeed(150, 150)
stop()
time.sleep_ms(500)
def u_turn():
while ir_pins[2].value() == 1:
left_motor_1.off()
left_motor_2.on()
right_motor_1.on()
right_motor_2.off()
setspeed(150, 150)
while ir_pins[2].value() == 0:
left_motor_1.off()
left_motor_2.on()
right_motor_1.on()
right_motor_2.off()
setspeed(150, 150)
stop()
time.sleep_ms(500)
def turn_left():
stop()
time.sleep_ms(500)
while ir_pins[2].value() == 0:
left_motor_1.off()
left_motor_2.off()
right_motor_1.off()
right_motor_2.on()
setspeed(150, 150)
stop()
time.sleep_ms(500)
def forward():
global sum_w, sum_i, p, d, last_p,current_position
sum_w = 0
sum_i = 0
sensor=sensor_readings()
for i in range(5):
sum_w += (sensor[i] * i * 100)
sum_i += sensor[i]
if(sum_i != 0):
current_position = sum_w / sum_i
else :
current_position =0
p= 100 - current_position
d = p - last_p
last_p=p
error = (p*KP + d*KD)
if error < 0:
leftWheel = MAX_SPEED
rightWheel = int(MAX_SPEED - error)
else:
rightWheel = MAX_SPEED
leftWheel = int(MAX_SPEED+error)
left_motor_1.off()
left_motor_2.on()
right_motor_1.off()
right_motor_2.on()
setspeed(leftWheel, rightWheel)
def stop():
right_motor_1.off()
right_motor_2.off()
left_motor_1.off()
left_motor_2.off()
setspeed(0, 0)
# --- Main Loop ---
while True:
distance = measure_distance()
# set_servo_angle(pos1)
T=True
while not T:
forward()
T=all(reading == 1 for reading in sensor_readings())
right_u_turn()
forward_cross(3)
right_u_turn()
while distance > 13:
forward()
distance = measure_distance()
stop()
'''
for pos1 in range(115, 19, -1):
set_servo_angle(pos1)
time.sleep_ms(25)
time.sleep_ms(1000)
for pos1 in range(20,116):
set_servo_angle(pos1)
time.sleep_ms(25)
'''
time.sleep_ms(1000)
u_turn()
forward_cross(2)
left_u_turn()
forward_cross(1)
while distance > 13:
forward()
distance = measure_distance()
stop()
'''
for pos1 in range(115, 19, -1):
set_servo_angle(pos1)
time.sleep_ms(25)
time.sleep_ms(1000)
for pos1 in range(20,116):
set_servo_angle(pos1)
time.sleep_ms(25)
'''
time.sleep_ms(1000)
forward_cross(2)
while distance > 13:
forward()
distance = measure_distance()
stop()
'''
for pos1 in range(115, 19, -1):
set_servo_angle(pos1)
time.sleep_ms(25)
time.sleep_ms(1000)
for pos1 in range(20,116):
set_servo_angle(pos1)
time.sleep_ms(25)
time.sleep_ms(1000)
'''
time.sleep_ms(10000)