Make mcopy-arduino work with multiple devices

TODO: partition the queue and locks so commands can overlap when
passing to different devices
This commit is contained in:
Matt 2016-04-18 21:26:45 -04:00
parent 3fa5b06e82
commit f4b75d7398
4 changed files with 234 additions and 91 deletions

View File

@ -2565,7 +2565,7 @@ log.info = function (action, service, status, time) {
'use strict'; 'use strict';
var obj = log.display(action, service, status, time); var obj = log.display(action, service, status, time);
log.report(obj); log.report(obj);
console.log(obj); //console.log(obj);
}; };
/****** /******
@ -2588,13 +2588,16 @@ seq.run = function () {
} }
if (seq.stop()) { if (seq.stop()) {
$('.row input').removeClass('h'); $('.row input').removeClass('h');
console.log('Sequence stepped'); $('#numbers div').removeClass('h');
//console.log('Sequence stepped');
log.info('Sequence stopped', 'SERIAL', true);
return false; return false;
} }
if (seq.i <= mcopy.state.sequence.arr.length && c !== undefined) { if (seq.i <= mcopy.state.sequence.arr.length && c !== undefined) {
log.info('Step ' + seq.i + ' command ' + c, 'SEQUENCE', true); log.info('Step ' + seq.i + ' command ' + c, 'SEQUENCE', true);
//gui action //gui action
$('.row input').removeClass('h'); $('.row input').removeClass('h');
$('#numbers div').removeClass('h');
$('.row input[x=' + seq.i + ']').addClass('h'); $('.row input[x=' + seq.i + ']').addClass('h');
$('#numbers div[x=' + seq.i + ']').addClass('h'); $('#numbers div[x=' + seq.i + ']').addClass('h');
if (c === 'CF'){ if (c === 'CF'){
@ -2617,6 +2620,7 @@ seq.run = function () {
if (mcopy.loopCount < mcopy.loop) { if (mcopy.loopCount < mcopy.loop) {
log.info('Loop ' + mcopy.loopCount + ' completed', 'SEQUENCE', true); log.info('Loop ' + mcopy.loopCount + ' completed', 'SEQUENCE', true);
$('.row input').removeClass('h'); $('.row input').removeClass('h');
$('#numbers div').removeClass('h');
seq.i = 0; seq.i = 0;
seq.run(); seq.run();
} else { } else {
@ -2766,24 +2770,28 @@ cmd.cam_forward = function (rgb, callback) {
'use strict'; 'use strict';
var res = function (ms) { var res = function (ms) {
gui.updateState(); gui.updateState();
light.set([0, 0, 0], function () { setTimeout(function () {
if (callback) { callback(ms); } light.set([0, 0, 0], function () {
}); if (callback) { callback(ms); }
});
}, mcopy.cfg.arduino.serialDelay);
}; };
if (!mcopy.state.camera.direction) { if (!mcopy.state.camera.direction) {
cam.set(true, function () { cam.set(true, function () {
setTimeout(function () { setTimeout( function () {
light.set(rgb, function () { light.set(rgb, function () {
cam.move(res); setTimeout( function () {
cam.move(res);
}, mcopy.cfg.arduino.serialDelay);
}); });
}, mcopy.cfg.arduino.serialDelay); }, mcopy.cfg.arduino.serialDelay);
}); });
} else { } else {
setTimeout(function () { light.set(rgb, function () {
light.set(rgb, function () { setTimeout(function () {
cam.move(res); cam.move(res);
}); }, mcopy.cfg.arduino.serialDelay);
}, mcopy.cfg.arduino.serialDelay); });
} }
}; };
cmd.black_forward = function (callback) { cmd.black_forward = function (callback) {

View File

@ -7,7 +7,7 @@ var serialport = require('serialport'),
Arduino handlers Arduino handlers
*******/ *******/
mcopy.arduino = { mcopy.arduino = {
path : '', path : {},
known: [ known: [
'/dev/tty.usbmodem1a161', '/dev/tty.usbmodem1a161',
'/dev/tty.usbserial-A800f8dk', '/dev/tty.usbserial-A800f8dk',
@ -16,19 +16,24 @@ mcopy.arduino = {
'/dev/tty.usbserial-a900f6de', '/dev/tty.usbserial-a900f6de',
'/dev/tty.usbmodem1a141' '/dev/tty.usbmodem1a141'
], ],
serial : {}, serial : {
connect : {},
projector : {},
camera : {}
},
baud : 57600, baud : 57600,
queue : {}, queue : {},
timer : 0, timer : 0,
lock : false lock : false
}; };
mcopy.arduino.init = function (callback) { mcopy.arduino.enumerate = function (callback) {
'use strict'; 'use strict';
console.log('Searching for devices...'); console.log('Searching for devices...');
var cmd = 'ls /dev/tty.*'; var cmd = 'ls /dev/tty.*';
exec(cmd, function (e, std) { exec(cmd, function (e, std) {
var devices = std.split('\n'), var devices = std.split('\n'),
matches = []; matches = [];
if (e) { return callback(e); }
devices.pop(); devices.pop();
for (var i = 0; i < devices.length; i++) { for (var i = 0; i < devices.length; i++) {
if (devices[i].indexOf('usbserial') !== -1 if (devices[i].indexOf('usbserial') !== -1
@ -37,25 +42,20 @@ mcopy.arduino.init = function (callback) {
} }
} }
if (matches.length === 0) { if (matches.length === 0) {
//console.log('No USB devices found.');
if (callback) { callback('No USB devices found'); } if (callback) { callback('No USB devices found'); }
} else if (matches.length > 0) { } else if (matches.length > 0) {
//console.log('Found ' + matches[0]); if (callback) { callback(null, matches); }
mcopy.arduino.path = matches[0];
//once connected to the arduino
//start user interface
if (callback) { callback(null, mcopy.arduino.path); }
} }
}); });
}; };
//commands which respond to a sent char //commands which respond to a sent char
mcopy.arduino.send = function (cmd, res) { mcopy.arduino.send = function (serial, cmd, res) {
'use strict'; 'use strict';
if (!mcopy.arduino.lock) { if (!mcopy.arduino.lock) {
mcopy.arduino.lock = true; mcopy.arduino.lock = true;
mcopy.arduino.queue[cmd] = res; mcopy.arduino.queue[cmd] = res;
setTimeout(function () { setTimeout(function () {
mcopy.arduino.serial.write(cmd, function (err, results) { mcopy.arduino.serial[serial].write(cmd, function (err, results) {
if (err) { console.log(err); } if (err) { console.log(err); }
mcopy.arduino.lock = false; mcopy.arduino.lock = false;
mcopy.arduino.timer = new Date().getTime(); mcopy.arduino.timer = new Date().getTime();
@ -64,16 +64,21 @@ mcopy.arduino.send = function (cmd, res) {
} }
}; };
//send strings, after char triggers firmware to accept //send strings, after char triggers firmware to accept
mcopy.arduino.string = function (str) { mcopy.arduino.string = function (serial, str) {
'use strict'; 'use strict';
setTimeout(function () { setTimeout(function () {
mcopy.arduino.serial.write(str, function (err, results) { if (typeof mcopy.arduino.serial[serial].fake !== 'undefined'
if (err) { console.log(err); } && mcopy.arduino.serial[serial].fake) {
//console.log('sent: ' + str); mcopy.arduino.serial[serial].string(str);
}); } else {
mcopy.arduino.serial[serial].write(str, function (err, results) {
if (err) { console.log(err); }
//console.log('sent: ' + str);
});
}
}, mcopy.cfg.arduino.serialDelay); }, mcopy.cfg.arduino.serialDelay);
}; };
//with same over serial when done //respond with same char over serial when done
mcopy.arduino.end = function (data) { mcopy.arduino.end = function (data) {
'use strict'; 'use strict';
var end = new Date().getTime(), var end = new Date().getTime(),
@ -81,59 +86,115 @@ mcopy.arduino.end = function (data) {
if (mcopy.arduino.queue[data] !== undefined) { if (mcopy.arduino.queue[data] !== undefined) {
mcopy.arduino.lock = false; mcopy.arduino.lock = false;
console.log('Command ' + data + ' took ' + ms + 'ms'); console.log('Command ' + data + ' took ' + ms + 'ms');
mcopy.arduino.queue[data](ms); mcopy.arduino.queue[data](ms); //execute callback
delete mcopy.arduino.queue[data]; //add timestamp? delete mcopy.arduino.queue[data];
} else { } else {
console.log('Received stray "' + data + '" from ' + mcopy.arduino.path); //silent to user console.log('Received stray "' + data + '"'); //silent to user
} }
}; };
mcopy.arduino.connect = function (callback) { mcopy.arduino.connect = function (serial, device, confirm, callback) {
'use strict'; 'use strict';
console.log('Connecting to ' + mcopy.arduino.path + '...'); mcopy.arduino.path[serial] = device;
mcopy.arduino.serial = new SerialPort(mcopy.arduino.path, { mcopy.arduino.serial[serial] = new SerialPort(mcopy.arduino.path[serial], {
baudrate: mcopy.cfg.arduino.baud, baudrate: mcopy.cfg.arduino.baud,
parser: serialport.parsers.readline("\n") parser: serialport.parsers.readline("\n")
}, false); }, false);
mcopy.arduino.serial.open(function (error) { mcopy.arduino.serial[serial].open(function (error) {
if (error) { if (error) {
if (callback) { callback(error); }
return console.log('failed to open: '+ error); return console.log('failed to open: '+ error);
} else { } else {
console.log('Opened connection with ' + mcopy.arduino.path); console.log('Opened connection with ' + mcopy.arduino.path[serial]);
mcopy.arduino.serial.on('data', function (data) { if (!confirm) {
data = data.replace('\r', ''); mcopy.arduino.serial[serial].on('data', function (data) {
mcopy.arduino.end(data); data = data.replace('\r', '');
}); mcopy.arduino.end(data);
setTimeout(function () {
console.log('Verifying firmware...');
mcopy.arduino.send(mcopy.cfg.arduino.cmd.connect, function () {
console.log('Firmware verified');
console.log('Optical printer ready!');
if (callback) { callback(); }
}); });
}, 2000); } else {
mcopy.arduino.serial[serial].on('data', function (data) {
data = data.replace('\r', '');
mcopy.arduino.confirmEnd(data);
});
}
if (callback) {
callback(null, mcopy.arduino.path[serial]);
}
} }
}); });
}; };
mcopy.arduino.fakeConnect = function (callback) { mcopy.arduino.confirmExec = {};
mcopy.arduino.confirmEnd = function (data) {
'use strict';
if (data === mcopy.cfg.arduino.cmd.connect
|| data === mcopy.cfg.arduino.cmd.proj_identifier
|| data === mcopy.cfg.arduino.cmd.cam_identifier) {
mcopy.arduino.confirmExec(null, data);
mcopy.arduino.confirmExec = {};
}
};
mcopy.arduino.verify = function (callback) {
'use strict';
mcopy.arduino.confirmExec = function (err, data) {
if (data === mcopy.cfg.arduino.cmd.connect) {
callback(null, true);
}
};
setTimeout(function () {
mcopy.arduino.serial['connect'].write(mcopy.cfg.arduino.cmd.connect, function (err, results) {
if (err) {
return console.log(err);
}
});
}, mcopy.cfg.arduino.serialDelay);
};
mcopy.arduino.distinguish = function (callback) {
'use strict';
mcopy.arduino.confirmExec = function (err, data) {
if (data === mcopy.cfg.arduino.cmd.proj_identifier) {
callback(null, 'projector');
} else if (data === mcopy.cfg.arduino.cmd.cam_identifier) {
callback(null, 'camera');
}
};
setTimeout(function () {
mcopy.arduino.serial['connect'].write(mcopy.cfg.arduino.cmd.mcopy_identifier, function (err, results) {
if (err) {
return console.log(err);
}
});
}, mcopy.cfg.arduino.serialDelay);
};
mcopy.arduino.close = function (callback) {
'use strict';
mcopy.arduino.serial['connect'].close(function (err) {
if (callback) {
callback(err);
}
});
};
mcopy.arduino.fakeConnect = function (serial, callback) {
console.log('Connecting to fake arduino...'); console.log('Connecting to fake arduino...');
mcopy.arduino.serial = { mcopy.arduino.serial[serial] = {
write : function (cmd, res) { write : function (cmd, res) {
var t = { var t = {
c : mcopy.cfg.arduino.cam.time + mcopy.cfg.arduino.cam.delay, c : mcopy.cfg.arduino.cam.time + mcopy.cfg.arduino.cam.delay,
p : mcopy.cfg.arduino.proj.time + mcopy.cfg.arduino.proj.delay p : mcopy.cfg.arduino.proj.time + mcopy.cfg.arduino.proj.delay
}, },
timeout = t[cmd]; timeout = t[cmd];
if (typeof timeout === 'undefined') timeout = 10; if (typeof timeout === 'undefined') timeout = 10;
mcopy.arduino.timer = +new Date(); mcopy.arduino.timer = +new Date();
setTimeout(function () { setTimeout(function () {
mcopy.arduino.end(cmd); mcopy.arduino.end(cmd);
}, timeout); }, timeout);
}, },
string : function (str) { string : function (str) {
//do nothing //do nothing
return true; return true;
} },
fake : true
}; };
console.log('Connected to fake arduino! Not real! Doesn\'t exist!'); console.log('Connected to fake arduino! Not real! Doesn\'t exist!');
if (callback) callback(); if (callback) callback();

View File

@ -13,7 +13,9 @@ var electron = require('electron'),
mcopy = {}, mcopy = {},
mainWindow, mainWindow,
mscript, mscript,
arduino; arduino,
projector,
camera;
mcopy.cfg = {}; mcopy.cfg = {};
mcopy.cfgFile = './data/cfg.json'; mcopy.cfgFile = './data/cfg.json';
@ -43,21 +45,85 @@ var init = function () {
arduino = require('./lib/mcopy-arduino.js')(mcopy.cfg); arduino = require('./lib/mcopy-arduino.js')(mcopy.cfg);
mscript = require('./lib/mscript.js'); mscript = require('./lib/mscript.js');
setTimeout(function () { setTimeout(function () {
arduino.init(function (err, device) { arduino.enumerate(enumerateDevices);
if (err) { }, 1000);
log.info(err, 'SERIAL', false, true); };
arduino.fakeConnect(function () {
log.info('Connected to fake USB device', 'SERIAL', true, true); var enumerateDevices = function (err, devices) {
'use strict';
if (err) {
log.info(err, 'SERIAL', false, true);
arduino.fakeConnect('projector', function () {
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true);
});
arduino.fakeConnect('camera', function () {
log.info('Connected to fake CAMERA device', 'SERIAL', true, true);
});
} else {
log.info('Found ' + devices.length + ' USB devices', 'SERIAL', true, true);
distinguishDevices(devices);
}
};
var distinguishDevice = function (device, callback) {
'use strict';
var connectCb = function (err, device) {
if (err) {
return console.error(err);
}
setTimeout(function () {
arduino.verify(verifyCb);
}, 2000);
},
verifyCb = function (err, success) {
if (err) {
return console.error(err);
}
setTimeout(function () {
arduino.distinguish(distinguishCb);
}, 1000);
},
distinguishCb = function (err, type) {
if (err) {
return console.error(err);
}
if (callback) { callback(err, type); }
}
arduino.connect('connect', device, true, connectCb);
};
//Cases for 1 or 2 arduinos connected
var distinguishDevices = function (devices) {
'use strict';
var distinguishOne = function (err, type) {
arduino.close(function () {
if (type === 'projector') {
arduino.connect('projector', devices[0], false, function () {
log.info('Connected to ' + devices[0] + ' as PROJECTOR', 'SERIAL', true, true);
}); });
} else { if (devices.length === 1) {
log.info('Found device ' + device, 'SERIAL', true, true); arduino.fakeConnect('camera', function () {
arduino.connect(function () { log.info('Connected to fake CAMERA device', 'SERIAL', true, true);
log.info('Connected to device ' + device, 'SERIAL', true, true); });
}
} else if (type === 'camera') {
arduino.connect('camera', devices[0], false, function () {
log.info('Connected to ' + devices[0] + ' as CAMERA', 'SERIAL', true, true);
}); });
if (devices.length === 1) {
arduino.fakeConnect('projector', function () {
log.info('Connected to fake PROJECTOR device', 'SERIAL', true, true);
});
}
} }
}); });
}, 1000); },
distinguishTwo = function (err, type) {
};
distinguishDevice(devices[0], distinguishOne);
}; };
var createMenu = function () { var createMenu = function () {
@ -233,10 +299,10 @@ light.listen = function () {
light.set = function (rgb, id) { light.set = function (rgb, id) {
'use strict'; 'use strict';
var str = rgb.join(','); var str = rgb.join(',');
arduino.send(mcopy.cfg.arduino.cmd.light, function (ms) { arduino.send('projector', mcopy.cfg.arduino.cmd.light, function (ms) {
light.end(rgb, id, ms); light.end(rgb, id, ms);
}); });
arduino.string(str); arduino.string('projector', str);
}; };
light.end = function (rgb, id, ms) { light.end = function (rgb, id, ms) {
'use strict'; 'use strict';
@ -261,13 +327,13 @@ proj.set = function (dir, id) {
cmd = mcopy.cfg.arduino.cmd.proj_backward; cmd = mcopy.cfg.arduino.cmd.proj_backward;
} }
proj.state.dir = dir; proj.state.dir = dir;
arduino.send(cmd, function (ms) { arduino.send('projector', cmd, function (ms) {
proj.end(cmd, id, ms); proj.end(cmd, id, ms);
}); });
}; };
proj.move = function (frame, id) { proj.move = function (frame, id) {
'use strict'; 'use strict';
arduino.send(mcopy.cfg.arduino.cmd.projector, function (ms) { arduino.send('projector', mcopy.cfg.arduino.cmd.projector, function (ms) {
proj.end(mcopy.cfg.arduino.cmd.projector, id, ms); proj.end(mcopy.cfg.arduino.cmd.projector, id, ms);
}); });
}; };
@ -319,13 +385,13 @@ cam.set = function (dir, id) {
cmd = mcopy.cfg.arduino.cmd.cam_backward; cmd = mcopy.cfg.arduino.cmd.cam_backward;
} }
cam.state.dir = dir; cam.state.dir = dir;
arduino.send(cmd, function (ms) { arduino.send('camera', cmd, function (ms) {
cam.end(cmd, id, ms); cam.end(cmd, id, ms);
}); });
}; };
cam.move = function (frame, id) { cam.move = function (frame, id) {
'use strict'; 'use strict';
arduino.send(mcopy.cfg.arduino.cmd.camera, function (ms) { arduino.send('camera', mcopy.cfg.arduino.cmd.camera, function (ms) {
cam.end(mcopy.cfg.arduino.cmd.camera, id, ms); cam.end(mcopy.cfg.arduino.cmd.camera, id, ms);
}); });
}; };

View File

@ -109,7 +109,7 @@ log.info = function (action, service, status, time) {
'use strict'; 'use strict';
var obj = log.display(action, service, status, time); var obj = log.display(action, service, status, time);
log.report(obj); log.report(obj);
console.log(obj); //console.log(obj);
}; };
/****** /******
@ -132,13 +132,16 @@ seq.run = function () {
} }
if (seq.stop()) { if (seq.stop()) {
$('.row input').removeClass('h'); $('.row input').removeClass('h');
console.log('Sequence stepped'); $('#numbers div').removeClass('h');
//console.log('Sequence stepped');
log.info('Sequence stopped', 'SERIAL', true);
return false; return false;
} }
if (seq.i <= mcopy.state.sequence.arr.length && c !== undefined) { if (seq.i <= mcopy.state.sequence.arr.length && c !== undefined) {
log.info('Step ' + seq.i + ' command ' + c, 'SEQUENCE', true); log.info('Step ' + seq.i + ' command ' + c, 'SEQUENCE', true);
//gui action //gui action
$('.row input').removeClass('h'); $('.row input').removeClass('h');
$('#numbers div').removeClass('h');
$('.row input[x=' + seq.i + ']').addClass('h'); $('.row input[x=' + seq.i + ']').addClass('h');
$('#numbers div[x=' + seq.i + ']').addClass('h'); $('#numbers div[x=' + seq.i + ']').addClass('h');
if (c === 'CF'){ if (c === 'CF'){
@ -161,6 +164,7 @@ seq.run = function () {
if (mcopy.loopCount < mcopy.loop) { if (mcopy.loopCount < mcopy.loop) {
log.info('Loop ' + mcopy.loopCount + ' completed', 'SEQUENCE', true); log.info('Loop ' + mcopy.loopCount + ' completed', 'SEQUENCE', true);
$('.row input').removeClass('h'); $('.row input').removeClass('h');
$('#numbers div').removeClass('h');
seq.i = 0; seq.i = 0;
seq.run(); seq.run();
} else { } else {
@ -310,24 +314,28 @@ cmd.cam_forward = function (rgb, callback) {
'use strict'; 'use strict';
var res = function (ms) { var res = function (ms) {
gui.updateState(); gui.updateState();
light.set([0, 0, 0], function () { setTimeout(function () {
if (callback) { callback(ms); } light.set([0, 0, 0], function () {
}); if (callback) { callback(ms); }
});
}, mcopy.cfg.arduino.serialDelay);
}; };
if (!mcopy.state.camera.direction) { if (!mcopy.state.camera.direction) {
cam.set(true, function () { cam.set(true, function () {
setTimeout(function () { setTimeout( function () {
light.set(rgb, function () { light.set(rgb, function () {
cam.move(res); setTimeout( function () {
cam.move(res);
}, mcopy.cfg.arduino.serialDelay);
}); });
}, mcopy.cfg.arduino.serialDelay); }, mcopy.cfg.arduino.serialDelay);
}); });
} else { } else {
setTimeout(function () { light.set(rgb, function () {
light.set(rgb, function () { setTimeout(function () {
cam.move(res); cam.move(res);
}); }, mcopy.cfg.arduino.serialDelay);
}, mcopy.cfg.arduino.serialDelay); });
} }
}; };
cmd.black_forward = function (callback) { cmd.black_forward = function (callback) {