-
Notifications
You must be signed in to change notification settings - Fork 0
/
Infrared_Remote_Control0.py
123 lines (110 loc) · 2.35 KB
/
Infrared_Remote_Control0.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
import RPi.GPIO as GPIO
import time
from AlphaBot import AlphaBot
Ab = AlphaBot()
DR = 16
DL = 19
IR = 18
PWM = 50
n=0
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(IR,GPIO.IN,GPIO.PUD_UP)
GPIO.setup(DR,GPIO.IN,GPIO.PUD_UP)
GPIO.setup(DL,GPIO.IN,GPIO.PUD_UP)
def getkey():
if GPIO.input(IR) == 0:
count = 0
while GPIO.input(IR) == 0 and count < 200: #9ms
count += 1
time.sleep(0.00006)
count = 0
while GPIO.input(IR) == 1 and count < 80: #4.5ms
count += 1
time.sleep(0.00006)
idx = 0
cnt = 0
data = [0,0,0,0]
for i in range(0,32):
count = 0
while GPIO.input(IR) == 0 and count < 15: #0.56ms
count += 1
time.sleep(0.00006)
count = 0
while GPIO.input(IR) == 1 and count < 40: #0: 0.56mx
count += 1 #1: 1.69ms
time.sleep(0.00006)
if count > 8:
data[idx] |= 1<<cnt
if cnt == 7:
cnt = 0
idx += 1
else:
cnt += 1
print data
if data[0]+data[1] == 0xFF and data[2]+data[3] == 0xFF: #check
return data[2]
if data[0] == 255 and data[1] == 255 and data[2] == 15 and data[3] == 255:
return "repeat"
print('IRremote Test Start ...')
Ab.stop()
try:
while True:
key = getkey()
if(key != None):
if key == "repeat":
n = 0
if key == 0x18:
# Ab.forward()
# print("forward")
DR_status = GPIO.input(DR)
DL_status = GPIO.input(DL)
print("DR_status=%d"%DR_status)
print("DL_status=%d"%DL_status)
if((DL_status == 1) and (DR_status == 1)):
Ab.forward()
print("forward")
elif((DL_status == 1) and (DR_status == 0)):
Ab.left()
print("left")
elif((DL_status == 0) and (DR_status == 1)):
Ab.right()
print("right")
else:
Ab.backward()
time.sleep(0.2)
Ab.left()
time.sleep(0.2)
Ab.stop()
print("backward")
if key == 0x08:
Ab.left()
# print("left")
if key == 0x1c:
Ab.stop()
# print("stop")
if key == 0x5a:
Ab.right()
# print("right")
if key == 0x52:
Ab.backward()
# print("backward")
if key == 0x15:
if(PWM + 10 < 101):
PWM = PWM + 10
Ab.setPWMA(PWM)
Ab.setPWMB(PWM)
print(PWM)
if key == 0x07:
if(PWM - 10 > -1):
PWM = PWM - 10
Ab.setPWMA(PWM)
Ab.setPWMB(PWM)
print(PWM)
else:
n += 1
if n > 20000:
n = 0
Ab.stop()
except KeyboardInterrupt:
GPIO.cleanup();