-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexperiment.js
83 lines (70 loc) · 2.03 KB
/
experiment.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
var path = require('path');
var fs = require('fs');
var argv = require('yargs').argv;
var jsonfile = require('jsonfile');
var OpenBCIBoard = require('openbci-sdk');
// Experiment Model
var experiment = {
name: argv._[3],
subject: argv._[1],
duration: argv._[5],
filePath: path.join(__dirname, 'data', argv._[3] + '-' + argv._[1] + '.json'),
samplesTotal: 0,
features: [],
labels: []
};
console.log(experiment);
// OpenBCI
const board = new OpenBCIBoard.OpenBCIBoard();
board.autoFindOpenBCIBoard()
.then(onBoardFind);
// Board find handler
function onBoardFind (portName) {
if (portName) {
console.log('board found', portName);
board.connect(portName)
.then(onBoardConnect);
}
}
// Board connect handler
function onBoardConnect () {
board.on('ready', onBoardReady);
}
// Board ready handler
function onBoardReady () {
console.log(experiment.name + ' experiment started');
board.streamStart();
board.on('sample', addSample);
setTimeout(disconnectBoard, experiment.duration);
}
// Add sample
function addSample (sample) {
experiment.samplesTotal++;
experiment.features.push(sample.channelData);
experiment.labels.push(experiment.name);
console.log('pattern', sample.channelData, experiment.name);
}
// Save experiment
function saveExperiment () {
jsonfile.writeFile(experiment.filePath, experiment, { spaces: 2 }, function (error) {
if (!error) {
console.log(experiment.name + ' experiment finished with ' + experiment.samplesTotal + ' samples');
console.log('Experiment path: ' + experiment.filePath);
} else {
console.log(experiment.name + ' experiment failed. sucks to be you.');
}
});
}
/**
* Disconnect board
*/
function disconnectBoard () {
board.streamStop()
.then(function () {
setTimeout(function () {
board.disconnect();
saveExperiment();
console.log('board disconnected');
}, 50);
});
}