-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_speed1.py
115 lines (103 loc) · 2.11 KB
/
control_speed1.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
import sys,tty,termios
class _Getch:
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(3)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
#----------------------------------------#
import time
from SunFounder_TB6612 import TB6612
import RPi.GPIO as GPIO
def main():
import time
#Connect MA to BCM20
#Connect MB to BCM16
#Connect PWMA to BCM12
#Connect PWMB to BCM26
GPIO.setmode(GPIO.BCM)
GPIO.setup((12, 26), GPIO.OUT)
a = GPIO.PWM(12, 60)
b = GPIO.PWM(26, 60)
a.start(0)
b.start(0)
def a_speed(value):
a.ChangeDutyCycle(value)
def b_speed(value):
b.ChangeDutyCycle(value)
motorA = TB6612.Motor(20)
motorB = TB6612.Motor(16)
motorA.debug = True
motorB.debug = True
motorA.pwm = a_speed
motorB.pwm = b_speed
delay = 0.1
state = 0
inkey = _Getch()
while 1:
k=inkey()
if k!='':break
# while 1:
if k=='\x1b[A':
state = 1
print(state)
elif k=='\x1b[B':
state = -1
print(state)
elif k=='\x1b[C':
state = 2
elif k=='\x1b[D':
state = 3
else:
state = 0
# break
# i = 0
while state == 1:
print("forward")
motorA.forward()
motorA.speed = 100
motorB.forward()
motorB.speed = 100
time.sleep(delay)
k1=inkey()
if k1!='':break
while state == -1:
print("backward")
motorA.backward()
motorA.speed = 100
motorB.backward()
motorB.speed = 100
time.sleep(delay)
k1=inkey()
if k1!='':break
while state == 2:
print("right")
motorA.forward()
motorA.speed = 30
motorB.backward()
motorB.speed = 30
time.sleep(delay)
k1=inkey()
if k1!='':break
while state == 3:
print("forward")
motorA.backward()
motorA.speed = 30
motorB.forward()
motorB.speed = 30
k1=inkey()
if k1!='':break
def destroy():
motorA.stop()
motorB.stop()
#if __name__ == '__main__':
while True:
try:
main()
except KeyboardInterrupt:
break
destroy()