forked from bitmakerlabs/mc_hammer_bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.coffee
64 lines (45 loc) · 1.41 KB
/
server.coffee
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
Bot = require './bot'
express = require 'express'
bluetooth = true
moch = ( process.argv.indexOf("--dry-run") != -1 );
if bluetooth
opts =
port: "/dev/tty.FireFly-CB32-RNI-SPP"
baud: 115200
else
opts =
port: "/dev/tty.usbserial-A9007MTf"
baud: 9600
opts.moch = moch
bot = new Bot(opts)
bot.connect()
# callibration = {a: 1, s: 1, d: 1, f: 1}
bindings =
forward: {a: +1, s: +1, d: +1, f: +1}
turn: {a: +1, s: -1, d: +1, f: -1}
strafe: {a: +1, s: -1, d: -1, f: +1}
motionControlSettings = { forward: 0, turn: 0, strafe: 0 }
app = express()
app.use express.bodyParser()
app.enable("jsonp callback")
app.get '/motors', (req, res) ->
res.json bot.motorOutputs
app.get '/motion-control', (req, res) ->
res.json motionControlSettings
app.get '/motion-control/update', (req, res) ->
motorOutputs = {}
data = req.query
console.log data
motorOutputs = {}
# Computing the motor outputs from the json per-axis inputs
for axis, b of bindings
for k, dir of b
motionControlSettings[axis] = ( data[axis] || 0 )
motorOutputs[k] = ( motorOutputs[k] || 0 ) + ( data[axis] || 0 ) * dir
# Binding the motor's output to a range of -1 to +1
motorOutputs[motor] = Math.min( +1, Math.max(-1, v) )*10 for motor, v of motorOutputs
# Updating the motors
bot.set_motor motor, to: v for motor, v of motorOutputs
res.jsonp("ACK");
app.listen(8071)
console.log('Listening on port 8071')