Repository navigation
Expand file tree
/
Copy pathserver.py.orig
More file actions
81 lines (71 loc) · 2.08 KB
/
Copy pathserver.py.orig
File metadata and controls
81 lines (71 loc) · 2.08 KB
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
import threading
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template
import time
global running
c=0
#Initialize TOrnado to use 'GET' and load index.html
class MainHandler(tornado.web.RequestHandler):
def get(self):
loader = tornado.template.Loader(".")
self.write(loader.load("index.html").generate())
#Code for handling the data sent from the webpage
class WSHandler(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
print 'connection opened...'
def on_message(self, message): # receives the data from the webpage and is stored in the variable message
global c
print 'received:', message # prints the revived from the webpage
if message == "u": # checks for the received data and assigns different values to c which controls the movement of robot.
c = "8";
if message == "d":
c = "2"
if message == "l":
c = "6"
if message == "r":
c = "4"
if message == "b":
c = "5"
print c
if c == '8' :
print "Running Forward"
elif c == '2' :
print "Running Reverse"
elif c == '4' :
print "Turning Right"
elif c == '6' :
print "Turning Left"
elif c == '5' :
print "Stopped"
print "Values Updated"
def on_close(self):
print 'connection closed...'
application = tornado.web.Application([
(r'/ws', WSHandler),
(r'/', MainHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": "./resources"}),
])
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print "Ready"
print "running"
print running
print "running"
while running:
time.sleep(.2) # sleep for 200 ms
if __name__ == "__main__":
running = True
thread1 = myThread(1, "Thread-1", 1)
thread1.setDaemon(True)
thread1.start()
application.listen(9093) #9093 #starts the websockets connection
tornado.ioloop.IOLoop.instance().start()