|
|
@ -1,4 +1,7 @@ |
|
|
|
import sys |
|
|
|
import sys |
|
|
|
|
|
|
|
import serial |
|
|
|
|
|
|
|
import sys |
|
|
|
|
|
|
|
import time |
|
|
|
|
|
|
|
|
|
|
|
from twisted.internet import reactor |
|
|
|
from twisted.internet import reactor |
|
|
|
from twisted.python import log |
|
|
|
from twisted.python import log |
|
|
@ -11,12 +14,34 @@ from autobahn.twisted.websocket import WebSocketServerFactory, \ |
|
|
|
from autobahn.twisted.resource import WebSocketResource |
|
|
|
from autobahn.twisted.resource import WebSocketResource |
|
|
|
|
|
|
|
|
|
|
|
i = 0 |
|
|
|
i = 0 |
|
|
|
|
|
|
|
turn_sleeptime = 0.8 |
|
|
|
|
|
|
|
straight_sleeptime = 0.5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setMotor(command): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if command == 'D': |
|
|
|
|
|
|
|
vacuum_serial.write(str.encode( |
|
|
|
|
|
|
|
"SetMotor LWheelDist 155 RWheelDist 155 Speed 150 \n")) |
|
|
|
|
|
|
|
time.sleep(straight_sleeptime) |
|
|
|
|
|
|
|
if command == 'L': |
|
|
|
|
|
|
|
vacuum_serial.write(str.encode( |
|
|
|
|
|
|
|
"SetMotor LWheelDist -190 RWheelDist 190 Speed 200 \n")) |
|
|
|
|
|
|
|
time.sleep(turn_sleeptime) |
|
|
|
|
|
|
|
if command == 'R': |
|
|
|
|
|
|
|
vacuum_serial.write(str.encode( |
|
|
|
|
|
|
|
"SetMotor LWheelDist 190 RWheelDist -190 Speed 200 \n")) |
|
|
|
|
|
|
|
time.sleep(turn_sleeptime) |
|
|
|
|
|
|
|
if command == 'F': |
|
|
|
|
|
|
|
vacuum_serial.write(str.encode( |
|
|
|
|
|
|
|
"SetMotor LWheelDist -155 RWheelDist -155 Speed 150 \n")) |
|
|
|
|
|
|
|
time.sleep(straight_sleeptime) |
|
|
|
|
|
|
|
|
|
|
|
def setServo(servoChannel, position): |
|
|
|
def setServo(servoChannel, position): |
|
|
|
servoStr = str(servoChannel) + "=" + str(position) + "\n" |
|
|
|
servoStr = str(servoChannel) + "=" + str(position) + "\n" |
|
|
|
with open("/dev/servoblaster", "wb") as f: |
|
|
|
with open("/dev/servoblaster", "wb") as f: |
|
|
|
f.write(bytes(servoStr, 'utf-8')) |
|
|
|
f.write(bytes(servoStr, 'utf-8')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EchoServerProtocol(WebSocketServerProtocol): |
|
|
|
class EchoServerProtocol(WebSocketServerProtocol): |
|
|
|
|
|
|
|
|
|
|
|
def onConnect(self, request): |
|
|
|
def onConnect(self, request): |
|
|
@ -25,22 +50,32 @@ class EchoServerProtocol(WebSocketServerProtocol): |
|
|
|
def onMessage(self, payload, isBinary): |
|
|
|
def onMessage(self, payload, isBinary): |
|
|
|
global i |
|
|
|
global i |
|
|
|
i = i + 1 |
|
|
|
i = i + 1 |
|
|
|
if isBinary: |
|
|
|
# if isBinary: |
|
|
|
print("Binary message received: {0} bytes".format(len(payload))) |
|
|
|
# print("Binary message received: {0} bytes".format(len(payload))) |
|
|
|
else: |
|
|
|
# else: |
|
|
|
print("Text message received: {0}".format(payload.decode('utf8'))) |
|
|
|
# print("Text message received: {0}".format(payload.decode('utf8'))) |
|
|
|
string = " " + str(i) |
|
|
|
string = " " + str(i) |
|
|
|
self.sendMessage(payload + bytes(string, 'utf-8'), isBinary) |
|
|
|
self.sendMessage(payload + bytes(string, 'utf-8'), isBinary) |
|
|
|
string = payload.decode('ASCII') |
|
|
|
string = payload.decode('ASCII') |
|
|
|
setServo(0, int(string.split("/")[0])) |
|
|
|
print("Str ->{0}".format(string)) |
|
|
|
|
|
|
|
if string[0] != 'R' and string[0] != 'L' and string[0] != 'F' and string[0] != 'D': |
|
|
|
|
|
|
|
setServo(2, int(string.split("/")[0])) |
|
|
|
setServo(1, int(string.split("/")[1])) |
|
|
|
setServo(1, int(string.split("/")[1])) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
setMotor(string[0]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vacuum_serial = serial.Serial('/dev/ttyACM0', 115200) |
|
|
|
|
|
|
|
vacuum_serial.write(str.encode("testmode on \n")) |
|
|
|
|
|
|
|
time.sleep(1) |
|
|
|
|
|
|
|
|
|
|
|
log.startLogging(sys.stdout) |
|
|
|
log.startLogging(sys.stdout) |
|
|
|
|
|
|
|
|
|
|
|
factory = WebSocketServerFactory(u"ws://10.174.136.143:45684") |
|
|
|
factory = WebSocketServerFactory(u"ws://10.253.35.143:45684") |
|
|
|
factory.protocol = EchoServerProtocol |
|
|
|
factory.protocol = EchoServerProtocol |
|
|
|
|
|
|
|
|
|
|
|
resource = WebSocketResource(factory) |
|
|
|
resource = WebSocketResource(factory) |
|
|
@ -57,3 +92,11 @@ if __name__ == '__main__': |
|
|
|
reactor.listenTCP(45684, site) |
|
|
|
reactor.listenTCP(45684, site) |
|
|
|
|
|
|
|
|
|
|
|
reactor.run() |
|
|
|
reactor.run() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vacuum_serial.write(str.encode("testmode off \n")) |
|
|
|
|
|
|
|
vacuum_serial.close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except KeyboardInterrupt: |
|
|
|
|
|
|
|
print("Fuuuuuuuuuuuu") |
|
|
|
|
|
|
|
vacuum_serial.write(str.encode("testmode off \n")) |
|
|
|
|
|
|
|
vacuum_serial.close() |
|
|
|