-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement.py
116 lines (85 loc) · 2.21 KB
/
movement.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
import RPi.GPIO as GPIO
import time, sys
import tty
import termios
stepPin1 = 31
dirPin1 = 33
stepPin2 = 35
dirPin2 = 37
enPin1 = 12
enPin2 = 16
# keyboard part
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
# read keyboard function
def readkey(getchar_fn=None):
getchar = getchar_fn or readchar
c1 = getchar()
if ord(c1) != 0x1b:
return c1
c2 = getchar()
if ord(c2) != 0x5b:
return c1
c3 = getchar()
return chr(0x10 + ord(c3) - 65)
# initialize PINS
def setup():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(enPin1, GPIO.OUT)
GPIO.setup(enPin2, GPIO.OUT)
GPIO.setup(stepPin1, GPIO.OUT)
GPIO.setup(dirPin1, GPIO.OUT)
GPIO.setup(stepPin2, GPIO.OUT)
GPIO.setup(dirPin2, GPIO.OUT)
GPIO.output(enPin1, 1)
GPIO.output(enPin2, 1)
# movement function for motor1(Y axis):To set movement direction and steps
def move(direc, steps):
GPIO.output(dirPin1, direc)
GPIO.output(enPin1, 0)
for i in range(1, steps):
GPIO.output(stepPin1, 1)
time.sleep(0.005)
GPIO.output(stepPin1, 0)
time.sleep(0.005)
GPIO.output(enPin1, 1)
# movement function for motor2(base):To set movement direction and steps
def move2(direc, steps):
GPIO.output(dirPin2, direc)
GPIO.output(enPin2, 0)
for i in range(1, steps):
GPIO.output(stepPin2, 1)
time.sleep(0.005)
GPIO.output(stepPin2, 0)
time.sleep(0.005)
GPIO.output(enPin2, 1)
def destroy():
GPIO.cleanup()
# initialize Pins
setup()
# while loop keep listening the keyboard input
while True:
key = readkey()
if key == 'w':
move(0, 20) # go up 20 steps
print('forward')
if key == 's':
move(1, 20) # go down 20 steps
print('backward')
if key == 'a': # go left 20 steps
move2(1, 20)
print('turnleft')
if key == 'd': # go right 20 steps
move2(0, 20)
print('turnright')
if key == 'q': # stop
break
# destroy()