-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard_control.py
executable file
·157 lines (123 loc) · 3.87 KB
/
keyboard_control.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
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
# Autor: Ingmar Stapel
# Date: 20141229
# Version: 1.0
# Homepage: www.raspberry-pi-car.com
import sys, tty, termios, os
import L298NHBridge as HBridge
import wirus
speedleft = 0
speedright = 0
# Instructions for when the user has an interface
print("w/s: direction")
print("a/d: steering")
print("e: autonomous mode")
print("q: stops the motors")
print("p: print motor speed (L/R)")
print("x: exit")
# The catch method can determine which key has been pressed
# by the user on the keyboard.
def getch():
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
# Infinite loop
# The loop will not end until the user presses the
# exit key 'X' or the program crashes...
def printscreen():
# Print the motor speed just for interest
os.system('clear')
print("w/s: direction")
print("a/d: steering")
print("q: stops the motors")
print("x: exit")
print("========== Speed Control ==========")
print "left motor: ", speedleft
print "right motor: ", speedright
while True:
# Keyboard character retrieval method. This method will save
# the pressed key into the variable char
char = getch()
# The car will drive forward when the "w" key is pressed
if(char == "w"):
# synchronize after a turning the motor speed
# if speedleft > speedright:
# speedleft = speedright
# if speedright > speedleft:
# speedright = speedleft
# accelerate the RaPi car
speedleft = speedleft + 0.1
speedright = speedright + 0.1
if speedleft > 1:
speedleft = 1
if speedright > 1:
speedright = 1
HBridge.setMotorLeft(speedleft)
HBridge.setMotorRight(speedright)
printscreen()
# The car will reverse when the "s" key is pressed
if(char == "s"):
# synchronize after a turning the motor speed
# if speedleft > speedright:
# speedleft = speedright
# if speedright > speedleft:
# speedright = speedleft
# slow down the RaPi car
speedleft = speedleft - 0.1
speedright = speedright - 0.1
if speedleft < -1:
speedleft = -1
if speedright < -1:
speedright = -1
HBridge.setMotorLeft(speedleft)
HBridge.setMotorRight(speedright)
printscreen()
# Stop the motors
if(char == "q"):
speedleft = 0
speedright = 0
HBridge.setMotorLeft(speedleft)
HBridge.setMotorRight(speedright)
printscreen()
# The "d" key will toggle the steering right
if(char == "d"):
#if speedright > speedleft:
speedright = speedright - 0.1
speedleft = speedleft + 0.1
if speedright < -1:
speedright = -1
if speedleft > 1:
speedleft = 1
HBridge.setMotorLeft(speedleft)
HBridge.setMotorRight(speedright)
printscreen()
# The "a" key will toggle the steering left
if(char == "a"):
#if speedleft > speedright:
speedleft = speedleft - 0.1
speedright = speedright + 0.1
if speedleft < -1:
speedleft = -1
if speedright > 1:
speedright = 1
HBridge.setMotorLeft(speedleft)
HBridge.setMotorRight(speedright)
printscreen()
# The "x" key will break the loop and exit the program
if(char == "x"):
HBridge.setMotorLeft(0)
HBridge.setMotorRight(0)
HBridge.exit()
print("Program Ended")
break
if (char == "e"):
HBridge.exit()
wirus.Wirus()
# The keyboard character variable char has to be set blank. We need
# to set it blank to save the next key pressed by the user
char = ""
# End