-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
228 lines (197 loc) · 7.73 KB
/
server.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
var io = require('socket.io')(http);
var five = require("johnny-five");
var plotbot = require("./lib/plotbot.js");
// TODO: rearrange directory structure to make "public" folder
app.use(express.static('static'));
// Nope, this gets you in trouble! Use the minified compiled version
// app.use(express.static('node_modules/bezier-js/lib'));
// app.get('/', function(req, res){
// res.sendFile(path.join(__dirname, './drawCanvas/drawcanvas.html'));
// });
// app.get('/bezier.js', function(req, res){
// // This might be a bad way to serve it, I don't know, but w/e it works
// res.sendFile(path.join(__dirname, './node_modules/bezier-js/bezier.js'));
// });
// Handle debug mode
var DEBUG = false;
if (process.argv.indexOf('debug') != -1) {
DEBUG = true;
}
if (DEBUG) {
// use MockFirmata
console.log("DEBUG mode. Using mock-firmata");
var mocks = require('mock-firmata');
var MockFirmata = mocks.Firmata;
var board = new five.Board({
io: new MockFirmata({
// pins: [
// {
// supportedModes: [2,3,8,10,11]
// }
// ]
}),
debug: true,
repl: true
});
// monkey patch Stepper.step so that callbacks work in mock mode
var STEPPER_MOCK_DELAY = 15;
five.Stepper.prototype.step = function(steps, callback) {
setTimeout(callback, STEPPER_MOCK_DELAY);
};
} else {
// Use a real, non-mock johnny-five board
var board = new five.Board();
}
function updateClient(socket) {
var curr_coords = plotbot.getCartesian(plotbot.stepDelta, 'rel');
socket.emit('update',
{
'coords': [curr_coords.x, curr_coords.y],
'canvasWidth': plotbot.DRAWING_AREA_WIDTH,
'canvasHeight': plotbot.DRAWING_AREA_HEIGHT,
});
}
board.on("ready", function() {
console.log('board ready.');
// Initialize stepper motors
// WIRING:
// Both steppers AABB is (from left to right)
// brown, black, orange, yellow.
//
// Pot dialed min (cw) to lowest setting.
//
// M1 & M2 all grounded for full-step mode.
//
// The direction depends on your setup, you may have to swap extend_dir & retract_dir
var stepperLeft = new five.Stepper({
type: five.Stepper.TYPE.DRIVER,
stepsPerRev: 48,
pins: {
step: 2,
dir: 3
},
rpm: plotbot.MAX_RPM,
});
stepperLeft.extend_dir = five.Stepper.DIRECTION.CCW;
stepperLeft.retract_dir = five.Stepper.DIRECTION.CW;
stepperLeft.name = 'left';
var stepperRight = new five.Stepper({
type: five.Stepper.TYPE.DRIVER,
stepsPerRev: 48,
pins: {
step: 10,
dir: 11
},
rpm: plotbot.MAX_RPM,
});
stepperRight.extend_dir = five.Stepper.DIRECTION.CW;
stepperRight.retract_dir = five.Stepper.DIRECTION.CCW;
stepperRight.name = 'right';
function activateMotors(stepsLeft, stepsRight, callback) {
// positive steps are extensions, negative steps are retractions.
function eachStepper(stepperObj, stepsVal, rpms, doneSteppingCallback) {
if (typeof rpms != 'number') {
throw new Error("rpms must be a number");
}
var dir = 0;
if (stepsVal === 0) {
// skip ahead. 0 steps isn't an error, necessarily
console.log('Zero stepsVal, for stepper "' + stepperObj.name + '", skipping.');
doneSteppingCallback(); //XXX: this is new, is it trouble???
return false;
}
// determine the direction from the sign of the stepsVal
if (stepsVal > 0) {
// extend if positive
dir = stepperObj.extend_dir;
} else {
// retract if negative
dir = stepperObj.retract_dir;
}
stepperObj.direction(dir).rpm(rpms).step(Math.abs(stepsVal), function(){
console.log(stepperObj.name + " " + stepsVal + ' completed. RPM: ' + rpms);
doneSteppingCallback();
});
}
var slow_rpms;
if (Math.abs(stepsLeft) > Math.abs(stepsRight)) {
// left has more steps, it's the "longer" movement
eachStepper(stepperLeft, stepsLeft, plotbot.MAX_RPM, callback);
slow_rpms = plotbot.MAX_RPM * Math.abs(stepsRight/stepsLeft);
eachStepper(stepperRight, stepsRight, slow_rpms, function(){
console.log('right is done stepping');
});
} else {
// right is the "longer" movement (or they finish at the same time)
slow_rpms = plotbot.MAX_RPM * Math.abs(stepsLeft/stepsRight);
eachStepper(stepperLeft, stepsLeft, slow_rpms, function(){
console.log('left is done stepping');
});
eachStepper(stepperRight, stepsRight, plotbot.MAX_RPM, callback);
}
// finally, update the stepDelta
plotbot.stepDelta[0] += stepsLeft;
plotbot.stepDelta[1] += stepsRight;
}
io.on('connection', function (socket) {
console.log('io connection');
// initial update to give cursor position + canvas width & height
updateClient(socket);
// Single steps
socket.on('step', function (data, socket_callback) {
console.log('got step event from browser', data);
console.log("WARNING: 'step' event does not validate the destination.");
// TODO: check that stepsLeft & stepsRight attributes exist in data
if((data.stepsLeft !== 0 && Math.abs(data.stepsLeft) !== 1 ) ||
(data.stepsRight !== 0 && Math.abs(data.stepsRight) !== 1)) {
console.log("WARNING: Bad single-step values. Skipping.");
socket_callback({'status':'bad step values'});
} else {
activateMotors(data.stepsLeft, data.stepsRight, function() {
// It's good, let the client/canvas make the virtual steps
dest_cartesian = plotbot.getCartesian(plotbot.stepDelta, 'rel');
socket_callback({'status': 'ok', 'dest': dest_cartesian});
});
}
});
// straight line
socket.on('line', function (data, socket_callback) {
console.log('drawing line with data: ');
console.log(data);
// Calculate & validate step deltas
var destDelta = plotbot.getBipolarFromCart(data.x, data.y, 'rel');
var leftDelta = destDelta[0] - plotbot.stepDelta[0];
var rightDelta = destDelta[1] - plotbot.stepDelta[1];
if (plotbot.validateStepDelta(destDelta)) {
activateMotors(leftDelta, rightDelta, function() {
// It's good, let the client/canvas make the virtual steps
dest_cartesian = plotbot.getCartesian(plotbot.stepDelta, 'rel');
console.log(dest_cartesian);
console.log('stepDelta is:');
console.log(plotbot.stepDelta);
socket_callback({'status': 'ok', 'dest': dest_cartesian});
});
} else {
socket_callback({'status': 'invalid destination'});
}
});
});
// Inject into repl for debugging
// TODO: (I can't get into the repl in mock-firmata...?)
this.repl.inject({
stepperLeft: stepperLeft,
stepperRight: stepperRight,
activateMotors: activateMotors,
plotbot: plotbot,
});
});
if (DEBUG) {
board.emit('ready');
}
http.listen(3000, function(){
console.log('listening on *:3000');
});