-
Notifications
You must be signed in to change notification settings - Fork 0
/
car_command_server.py
219 lines (199 loc) · 8.51 KB
/
car_command_server.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import sys
import socket
import socketserver
import abc
import threading
import _thread
from time import sleep
#from socketserver import ThreadingMixIn
from socketserver import StreamRequestHandler
from socketserver import BaseRequestHandler
from socketserver import TCPServer
from datetime import datetime
from motors_cars2 import carsWith2Motor2
from motors_cars2 import getStandartCar2
from motors_cars2 import getStandart4WheelCar2
from laser import laser
from laser import getStandartLaser
from buzzer import buzzer
from buzzer import getStandartBuzzer
from rgblighter import rgblighter
from rgblighter import getStandartrgblighter
from distanceCheck import distanceCheck
from distanceCheck import getStandartDistanceChecker
from arduino_cannon_driver import CannonDriver
from arduino_cannon_driver import getStandartCannonDriver
from general_command_server import GeneralCommandServer
from commander_request_handler import CommandReturns
import RPi.GPIO as GPIO
class CarCommanderFunctionsClass:
def processCommand(self, commandString):
global myCar
global myLaser
global myBuzzer
global myRGBLighter
global myDistanceChecker
global myCannon
commandString = commandString.upper()
if commandString == "FORWARD":
myCar.forward()
return CommandReturns(True,False, "GOING FORWARD")
elif commandString == "BACKWARD":
myCar.backward()
return CommandReturns(True,False, "GOING BACKWARD")
elif commandString == "STOP":
myCar.stop()
return CommandReturns(True,False, "STOPPED")
elif commandString == "KILL":
myCar.stop()
return CommandReturns(True,True, "Shutting down server")
elif commandString == "QUIT":
return CommandReturns(False, False, "Shutting down server")
# Disconnect için bir durum eklenmesi lazım.
else:
commandData = commandString.split()
if len(commandData) < 2:
return CommandReturns(False, False, "INVALID COMMAND OR COMMAND FORMAT - 1")
else:
mainCommand = commandData[0]
commandParam = commandData[1]
if mainCommand == "SPEED":
try:
speed = int(commandParam)
except ValueError:
return CommandReturns(False, False, "INVALID PARAMETER FOR SPEED")
if (speed < 0) or (100 < speed):
return CommandReturns(False, False, "INVALID PARAMETER FOR SPEED")
else:
myCar.setSpeed(speed)
return CommandReturns(True,False, "Changing Speed - " + str(speed))
elif mainCommand == "LEFT":
try:
angle = int(commandParam)
except ValueError:
return CommandReturns(False, False, "INVALID PARAMETER FOR LEFT TURN")
if (angle < 0) or (angle > 100):
return CommandReturns(False, False, "INVALID NUMBER FOR LEFT TURN")
else:
myCar.setAngle(-angle)
return CommandReturns(True,False, "Changing Direction LEFT- " + str(angle))
elif mainCommand == "RIGHT":
try:
angle = int(commandParam)
except ValueError:
return CommandReturns(False, False, "INVALID PARAMETER FOR RIGHT TURN")
if (angle < 0) or (angle > 100):
return CommandReturns(False, False, "INVALID NUMBER FOR RIGHT TURN")
else:
myCar.setAngle(angle)
return CommandReturns(True,False, "Changing Direction RIGHT- " + str(angle))
elif mainCommand == "CANNON":
if commandParam in {"UP", "DOWN", "LEFT", "RIGHT", "SELECT", "LASER"} :
if len(commandData) < 3:
myCannon.sendCommand(commandParam + "\n")
return CommandReturns(True, False, "CANNON " + commandParam)
else:
myCannon.sendCommand(commandParam +" " + commandData[2]+ "\n")
return CommandReturns(True, False, "CANNON " + commandParam + " " + commandData[2])
elif mainCommand == "TEST":
if commandParam in {"MOTOR"} :
if len(commandData) < 3:
return CommandReturns(False, False, "MOTOR TO TEST MISSING IN COMMAND")
else:
try:
testMotor = int(commandData[2])
except ValueError:
return CommandReturns(False, False, "MOTOR INDEX MISFORMULATED TRY 1 2 3 4")
if testMotor == 1:
myCar.stop()
myCar.motorRight.forward()
myCar.motorRight.setSpeed(50)
return CommandReturns(True, False, "TEST RUNNING - " + commandParam + " " + commandData[2])
elif testMotor == 2:
myCar.stop()
myCar.motorLeft.forward()
myCar.motorLeft.setSpeed(50)
return CommandReturns(True, False, "TEST RUNNING - " + commandParam + " " + commandData[2])
elif testMotor == 3:
myCar.stop()
myCar.motorRight2.forward()
myCar.motorRight2.setSpeed(50)
return CommandReturns(True, False, "TEST RUNNING - " + commandParam + " " + commandData[2])
elif testMotor == 4:
myCar.stop()
myCar.motorLeft2.forward()
myCar.motorLeft2.setSpeed(50)
return CommandReturns(True, False, "TEST RUNNING - " + commandParam + " " + commandData[2])
else:
return CommandReturns(False, False, "INVALID MOTOR INDEX TRY 1 2 3 4")
else:
return CommandReturns(False, False, "INVALID COMMAND")
def DefineTheCar():
global myCar
myCar = getStandartCar2()
def Define4WheelTheCar():
global myCar
myCar = getStandart4WheelCar2()
def DefineTheLaser():
global myLaser
myLaser = getStandartLaser()
def DefineTheBuzzer():
global myBuzzer
myBuzzer = getStandartBuzzer()
def DefineTheBuzzer():
global myBuzzer
myBuzzer = getStandartBuzzer()
def DefineTheRGBLighter():
global myRGBLighter
myRGBLighter = getStandartrgblighter()
def DefineTheDistanceChecker():
global myDistanceChecker
global myCar
myDistanceChecker = getStandartDistanceChecker(myCar)
def DefineTheCannonDriver():
global myCannon
myCannon = getStandartCannonDriver()
def runCar():
GPIO.setmode(GPIO.BCM)
DefineTheCar()
DefineTheLaser()
DefineTheBuzzer()
DefineTheRGBLighter()
DefineTheDistanceChecker()
commanderFunctions = CarCommanderFunctionsClass()
myServer = GeneralCommandServer(commanderFunctions, "192.168.0.108", 1700)
myServer.runTheServer()
GPIO.cleanup()
sys.exit(0)
def runCarServer():
GPIO.setmode(GPIO.BCM)
DefineTheCar()
DefineTheLaser()
DefineTheBuzzer()
DefineTheRGBLighter()
DefineTheDistanceChecker()
DefineTheCannonDriver()
commanderFunctions = CarCommanderFunctionsClass()
host = "0.0.0.0"
port = 1700
myServer = GeneralCommandServer(commanderFunctions, host, port)
myServer.runTheServer()
GPIO.cleanup()
sys.exit(0)
def runCarServerWith4Motors():
GPIO.setmode(GPIO.BCM)
Define4WheelTheCar()
DefineTheLaser()
DefineTheBuzzer()
DefineTheRGBLighter()
DefineTheDistanceChecker()
DefineTheCannonDriver()
commanderFunctions = CarCommanderFunctionsClass()
host = "0.0.0.0"
port = 1700
myServer = GeneralCommandServer(commanderFunctions, host, port)
myServer.runTheServer()
GPIO.cleanup()
sys.exit(0)
if __name__ == "__main__":
runCarServerWith4Motors()