/
can_choose.py
113 lines (74 loc) · 2.52 KB
/
can_choose.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
#can choose sensor
import RPi.GPIO as GPIO
from time import sleep
from datatime import datetime
sensor_out_recive =
motor1_pwm =
motor1_dir =
motor2_pwm =
motor2_dir =
print("please input how_times")
how_times = int(input())
print("plase input sleep_time that about motor acceleration")
sleep_time = int(input())
interbal = int(input())
#Stick sensor_pin+/- into 2 or 4 and 6
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor_out_recive,GPIO.IN) #recive pin
GPIO.setup(motor1_pwm,GPIO.OUT)
GPIO.setup(motor1_dir,GPIO.OUT)
GPIO.setup(motor2_pwm,GPIO.OUT)
GPIO.setup(motor2_dir,GPIO.OUT)
pdw1 = GPIO.PWM(motor1_pwm,50)
pdw2 = GPIO.PWM(motor2_pwm,50)
try:
count = 1
pdw1.start(0)
pdw2.start(0)
GPIO.output(motor1_dir,GPIO.HIGH) # HIGH is positive direction
GPIO.output(motor2_dir,GPIO.HIGH)
for i in range(100): #first speed up
pdw1.ChangeDutyCycle(i)
pdw2.ChangeDutyCycle(i)
sleep(sleep_time)
while i < how_times
if(GPIO.input(sensor_out_recive) == GPIO.LOW):
print(str(cnt) + "times")
count = count + 1
sleep(sleep_time)
for i in range(100,0,-10): #decelerate speed
pwm1.ChangeDutyCycle(i)
pdw2.ChangeDutyCycle(i)
sleep(sleep_time)
if i == 0: # finish decelerate speed
print("stop,Because sensor sense")
print("please choose 1 or 2")
select = int(input()) # select
if select == 1: #go staright
for i in range(100):
pwm1.ChangeDutyCycle(i) #up speed
pwm2.ChangeDutyCycle(i)
sleep(sleep_time)
if select == 2: # go back
GPIO.output(motor1_dir,GPIO.LOW) #change direction
GPIO.output(motor2_dir,GPIO.LOW)
for i in range(100):# up speed
pwm1.ChangeDutyCycle(i)
pwm2.ChangeDutyCycle(i)
sleep(sleep_time)
else:
print(GPIO.input(sensor_out_recive))
print("go straight")
sleep(interbal)
for i range(100,0,-20);
pdw1.ChangeDutyCycle(i)
pdw2.ChangeDutyCycle(i)
print("decelerate speed")
except KeyboardlInterrput:
print("Interrput CTRL + c")
finally:
pdw1.stop()
pdw2.stop()
GPIO.clenup()
print("finish clenup")