-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (62 loc) · 1.83 KB
/
main.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
# Import the PCA9685 module.
import Adafruit_PCA9685
import json
# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()
# Configure min and max servo pulse lengths
jaguarMin = 130 # Min pulse length out of 4096
jagaurMax = 650 # Max pulse length out of 4096
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
'''
Remaping function
'''
def remap( x, oMin, oMax, nMin, nMax ): # thanks stackoverflow.com/a/15537393
#range check
if oMin == oMax:
print("Warning: Zero input range")
return None
if nMin == nMax:
print("Warning: Zero output range")
return None
#check reversed input range
reverseInput = False
oldMin = min( oMin, oMax )
oldMax = max( oMin, oMax )
if not oldMin == oMin:
reverseInput = True
#check reversed output range
reverseOutput = False
newMin = min( nMin, nMax )
newMax = max( nMin, nMax )
if not newMin == nMin :
reverseOutput = True
portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
if reverseInput:
portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin)
result = portion + newMin
if reverseOutput:
result = newMax - portion
return result
'''
Webapp-based driving.
'''
def drive(wheels):
i = 0
for wheel in wheels:
pwm.set_pwm(0, i, int(remap(wheel, -1, 1, jaguarMin, jagaurMax)))
i = i + 1
drive([0,0,0,0])
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
class Log(WebSocket):
def handleMessage(self):
# drive
drive(json.loads(self.data))
def handleConencted(self):
print(self.address, 'connected')
def handleClose(self):
print(self.address, 'closed')
# stop bot
drive([0,0,0,0])
server = SimpleWebSocketServer('', 8080, Log)
server.serveforever()