Skip to content

Commit fe2abac

Browse files
author
AJ Keller
committed
Add example for time syncing
1 parent 2528d56 commit fe2abac

File tree

8 files changed

+126
-1
lines changed

8 files changed

+126
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ Send the command to tell the board to start the syncing protocol. Must be connec
895895
timeRoundTrip: 0, // Simply timeSyncSetPacket - timeSyncSent.
896896
timeTransmission: 0, // Estimated time it took for time sync set packet to be sent from Board to Driver.
897897
timeOffset: 0, // The map (or translation) from boardTime to module time.
898+
timeOffsetMaster: 0, // The map (or translation) from boardTime to module time averaged over time syncs.
898899
valid: false // If there was an error in the process, valid will be false and no time sync was done. It's important to resolve this so we can perform multiple promise syncs as show in the example below.
899900
}
900901
```

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.3.2
2+
3+
### Enhancements
4+
5+
* Added master time offset `timeOffsetMaster` to `syncObj` which is a running average across sync attempts.
6+
17
# 1.3.1
28

39
### Bug Fixes

examples/timeSync/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "timesync",
3+
"version": "1.0.0",
4+
"description": "Time sync example",
5+
"main": "timeSync.js",
6+
"scripts": {
7+
"start": "node timeSync.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [
11+
"time",
12+
"sync"
13+
],
14+
"author": "AJ Keller",
15+
"license": "MIT",
16+
"dependencies": {
17+
"openbci": "^1.3.1"
18+
}
19+
}

examples/timeSync/timeSync.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* This is an example of time syncing every second for one minute. Used with a
3+
* real board.
4+
* To run:
5+
* change directory to this file `cd examples/timeSync`
6+
* do `npm install`
7+
* then `npm start`
8+
*/
9+
10+
var OpenBCIBoard = require('openbci').OpenBCIBoard;
11+
12+
var ourBoard = new OpenBCIBoard({});
13+
14+
const resyncPeriodMin = 5; // re sync every five minutes
15+
const secondsInMinute = 60;
16+
var sampleRate = 250; // Default to 250, ALWAYS verify with a call to `.sampleRate()` after 'ready' event!
17+
var timeSyncPossible = false;
18+
19+
ourBoard.autoFindOpenBCIBoard().then(portName => {
20+
if(portName) {
21+
/**
22+
* Connect to the board with portName
23+
* i.e. ourBoard.connect(portName).....
24+
*/
25+
// Call to connect
26+
ourBoard.connect(portName).then(() => {
27+
console.log(`connected`);
28+
29+
})
30+
.catch(err => {
31+
console.log(`connect: ${err}`);
32+
});
33+
} else {
34+
/**Unable to auto find OpenBCI board*/
35+
}
36+
});
37+
38+
var readyFunc = () => {
39+
// Get the sample rate after 'ready'
40+
sampleRate = ourBoard.sampleRate();
41+
// Find out if you can even time sync, you must be using v2 and this is only accurate after a `.softReset()` call which is called internally on `.connect()`. We parse the `.softReset()` response for the presence of firmware version 2 properties.
42+
timeSyncPossible = ourBoard.usingVersionTwoFirmware();
43+
44+
if (timeSyncPossible) {
45+
ourBoard.streamStart()
46+
.catch(err => {
47+
console.log(`stream start: ${err}`);
48+
});
49+
} else {
50+
killFunc();
51+
}
52+
53+
}
54+
55+
var killFunc = () => {
56+
ourBoard.disconnect()
57+
.then(() => {
58+
process.kill();
59+
});
60+
}
61+
62+
var sampleFunc = sample => {
63+
// Resynchronize every every second
64+
if (sample._count % (sampleRate * 1) === 0) {
65+
ourBoard.syncClocksFull()
66+
.then(syncObj => {
67+
// Sync was successful
68+
if (syncObj.valid) {
69+
// Log the object to check it out!
70+
console.log(`timeOffset`,syncObj.timeOffsetMaster);
71+
} else {
72+
// Retry it
73+
console.log(`Was not able to sync... retry!`);
74+
}
75+
});
76+
}
77+
78+
if (sample.timeStamp) { // true after the first successful sync
79+
if (sample.timeStamp < 10 * 60 * 60 * 1000) { // Less than 10 hours
80+
console.log(`Bad time sync ${sample.timeStamp}`);
81+
}
82+
}
83+
84+
// Stop after one minute
85+
if (sample._count > sampleRate * 60) {
86+
killFunc();
87+
}
88+
89+
}
90+
91+
// Subscribe to your functions
92+
ourBoard.on('ready',readyFunc);
93+
ourBoard.on('sample',sampleFunc);

openBCIBoard.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,8 @@ function OpenBCIFactory() {
19331933
this.sync.timeOffsetMaster = this.sync.curSyncObj.timeOffset;
19341934
}
19351935

1936+
this.sync.curSyncObj.timeOffsetMaster = this.sync.timeOffsetMaster;
1937+
19361938
if (this.options.verbose) {
19371939
console.log(`Master offset ${this.sync.timeOffsetMaster} ms`);
19381940
}

openBCISample.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ function newSyncObject() {
586586
timeRoundTrip: 0,
587587
timeTransmission: 0,
588588
timeOffset: 0,
589+
timeOffsetMaster: 0,
589590
valid: false
590591
}
591592
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openbci",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "The official Node.js SDK for the OpenBCI Biosensor Board.",
55
"main": "openBCIBoard",
66
"scripts": {

test/OpenBCISample-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,9 @@ describe('openBCISample',function() {
13521352
it("should have property timeOffset",function() {
13531353
expect(syncObj).to.have.property("timeOffset",0);
13541354
});
1355+
it("should have property timeOffsetMaster",function() {
1356+
expect(syncObj).to.have.property("timeOffsetMaster",0);
1357+
});
13551358
it("should have property timeRoundTrip",function() {
13561359
expect(syncObj).to.have.property("timeRoundTrip",0);
13571360
});

0 commit comments

Comments
 (0)