-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobot.py
109 lines (86 loc) · 3.1 KB
/
robot.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
import explorerhat, signal, time
import cherrypy
import string
#pip install cherrypy
cherrypy.config.update("server.conf")
class RobotServer(object):
def __init__(self, initialcommand="stop"):
self.status = "stop"
#Start listening for the IR sensors to change
explorerhat.analog.one.changed(self.handle_analog)
explorerhat.analog.two.changed(self.handle_analog)
self.do_command(initialcommand)
@cherrypy.expose
def forward(self, speed=95):
explorerhat.motor.one.forward(int(speed))
explorerhat.motor.two.forward(int(speed))
self.status = "forward"
return self.status
@cherrypy.expose
def backward(self, speed=95):
explorerhat.motor.one.backward(int(speed))
explorerhat.motor.two.backward(int(speed))
self.status = "backward"
return self.status
@cherrypy.expose
def right(self, speed=95):
explorerhat.motor.one.forward(int(speed))
explorerhat.motor.two.backward(int(speed))
self.status = "right"
return self.status
@cherrypy.expose
def left(self, speed=95):
explorerhat.motor.one.backward(int(speed))
explorerhat.motor.two.forward(int(speed))
self.status = "left"
return self.status
@cherrypy.expose
def stop(self):
explorerhat.motor.one.stop()
explorerhat.motor.two.stop()
self.status = "stop"
return self.status
@cherrypy.expose
def index(self):
raise cherrypy.HTTPRedirect("/static/index.html")
@cherrypy.expose
def getstatus(self):
return self.status
def do_command(self, cmd=""):
if (cmd == "forward"):
self.forward()
elif cmd == "backward":
self.backward()
elif cmd == "left":
self.left()
elif cmd == "right":
self.right()
elif cmd == "stop":
self.stop()
return self.status
#Replicate original k9 interface
@cherrypy.expose
def k9(self, motor=""):
return self.do_command(motor)
def handle_analog(self, pin, value):
print (pin.name, value, self.status)
if (value > 2 and self.status != "stop"):
if (self.status != "bump"):
self.oldstatus = self.status
self.status = "bump"
explorerhat.motor.one.stop()
explorerhat.motor.two.stop()
time.sleep(0.2)
explorerhat.motor.one.backward()
explorerhat.motor.two.backward()
time.sleep(0.7)
if (explorerhat.analog.one.read() < explorerhat.analog.two.read()):
explorerhat.motor.one.forward()
explorerhat.motor.two.backward()
else:
explorerhat.motor.one.backward()
explorerhat.motor.two.forward()
time.sleep(0.5)
self.do_command(self.oldstatus)
if __name__ == '__main__':
cherrypy.quickstart(RobotServer(), config="app.conf")