-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
149 lines (138 loc) · 4.5 KB
/
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
def start():
try:
import asyncio
import websockets
import car
import time
keys = {
"w": False,
"a": False,
"s": False,
"d": False,
"f": False,
"e": False,
"r": False,
"ArrowUp": False,
"ArrowDown": False,
"ArrowLeft": False,
"ArrowRight": False
}
async def keyHandler(websocket, path):
while True:
#Steuerung
key = await websocket.recv()
eventKey(key)
if carstatus:
try:
carHandler()
except:
carError()
else:
print(key)
def eventKey(key):
try:
if key[2] == "t":
keys[key[0]] = True
elif key[2] == "f":
keys[key[0]] = False
else:
specialKey(key)
except:
print("falsche Taste")
pass
def specialKey(key):
if key == "ArrowUp,true":
keys["ArrowUp"] = True
elif key == "ArrowUp,false":
keys["ArrowUp"] = False
elif key == "ArrowDown,true":
keys["ArrowDown"] = True
elif key == "ArrowDown,false":
keys["ArrowDown"] = False
elif key == "ArrowLeft,true":
keys["ArrowLeft"] = True
elif key == "ArrowLeft,false":
keys["ArrowLeft"] = False
elif key == "ArrowRight,true":
keys["ArrowRight"] = True
elif key == "ArrowRight,false":
keys["ArrowRight"] = False
elif key == "red,true":
car.changeLed(["red", True])
elif key == "red,false":
car.changeLed(["red", False])
elif key == "green,true":
car.changeLed(["green", True])
elif key == "green,false":
car.changeLed(["green", False])
elif key == "blue,true":
car.changeLed(["blue", True])
elif key == "blue,false":
car.changeLed(["blue", False])
def carHandler():
if keys["w"]:
if keys["a"]:
car.driveForwardLeft()
elif keys["d"]:
car.driveForwardRight()
else:
car.driveForward()
elif keys["s"]:
if keys["a"]:
car.driveBackwardLeft()
elif keys["d"]:
car.driveBackwardRight()
else:
car.driveBackward()
else:
car.driveStop()
if keys["e"]:
car.buzzerOn()
else:
car.buzzerOff()
if keys["f"]:
car.changeLed()
keys["f"] = False
elif keys["ArrowUp"]:
car.servoMove("up")
keys["ArrowUp"] = False
elif keys["ArrowDown"]:
car.servoMove("down")
keys["ArrowDown"] = False
elif keys["ArrowLeft"]:
car.servoMove("left")
keys["ArrowLeft"] = False
elif keys["ArrowRight"]:
car.servoMove("right")
keys["ArrowRight"] = False
elif keys["r"]:
car.servoReset()
keys["r"] = False
def carError():
car.driveStop()
car.servoReset()
car.changeLed(["red", True])
car.changeLed(["green", False])
car.changeLed(["blue", False])
car.buzzerOn()
time.sleep(2)
car.changeLed(["red", False])
car.changeLed(["green", False])
car.changeLed(["blue", False])
car.buzzerOff()
car.shutdown
print("-- Starting 4WD --")
car = car.Car()
carstatus = True
"""try:
car = car.Car()
carstatus = True
except:
print("Auto nicht verfügbar.")
carstatus = False
pass"""
start_server = websockets.serve(keyHandler, "0.0.0.0", 5678)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
except KeyboardInterrupt:
carError()