Skip to content

Commit cc839b3

Browse files
authored
Merge pull request #62 from pushtheworldllc/dev-sim-accel
Add accel data to simulator closes #61
2 parents 682074d + e2cd5d1 commit cc839b3

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.3.6
2+
3+
### New Features
4+
5+
* Simulator now has accelerometer data
6+
17
# 0.3.5
28

39
### New Features

openBCISample.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ var sampleModule = {
296296
sinePhaseRad.fill(0);
297297

298298
var auxData = [0,0,0];
299+
var accelCounter = 0;
300+
// With 250Hz, every 10 samples, with 125Hz, every 5...
301+
var samplesPerAccelRate = Math.floor(sampleRateHz / 25); // best to make this an integer
302+
if (samplesPerAccelRate < 1) samplesPerAccelRate = 1;
299303

300304
// Init arrays to hold coefficients for each channel and init to 0
301305
// This gives the 1/f filter memory on each iteration
@@ -352,7 +356,31 @@ var sampleModule = {
352356
} else {
353357
newSample.sampleNumber = previousSampleNumber + 1;
354358
}
355-
newSample.auxData = auxData;
359+
360+
/**
361+
* Sample rate of accelerometer is 25Hz... when the accelCounter hits the relative sample rate of the accel
362+
* we will output a new accel value. The approach will be to consider that Z should be about 1 and X and Y
363+
* should be somewhere around 0.
364+
*/
365+
if (accelCounter == samplesPerAccelRate) {
366+
// Initialize a new array
367+
var accelArray = [0,0,0];
368+
// Calculate X
369+
accelArray[0] = (Math.random() * 0.1 * (Math.random() > 0.5 ? -1 : 1));
370+
// Calculate Y
371+
accelArray[1] = (Math.random() * 0.1 * (Math.random() > 0.5 ? -1 : 1));
372+
// Calculate Z, this is around 1
373+
accelArray[2] = 1 - ((Math.random() * 0.4) * (Math.random() > 0.5 ? -1 : 1));
374+
// Store the newly calculated value
375+
newSample.auxData = accelArray;
376+
// Reset the counter
377+
accelCounter = 0;
378+
} else {
379+
// Increment counter
380+
accelCounter++;
381+
// Store the default value
382+
newSample.auxData = auxData;
383+
}
356384

357385
return newSample;
358386
};

package.json

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

test/OpenBCISample-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,23 @@ describe('openBCISample',function() {
242242
});
243243
});
244244
});
245+
it('should generate a sample with accel data every 25Hz',function() {
246+
var generateSample = openBCISample.randomSample(k.OBCINumberOfChannelsDefault, k.OBCISampleRate250);
247+
var newSample = generateSample(0);
248+
249+
var passed = false;
250+
// Should get one non-zero auxData array (on the 10th sample)
251+
for (var i = 0; i < 10; i++) {
252+
newSample = generateSample(newSample.sampleNumber);
253+
if (newSample.auxData[0] !== 0 || newSample.auxData[1] !== 0 || newSample.auxData[2] !== 0) {
254+
passed = true;
255+
newSample.auxData[0].should.be.approximately(0,0.1);
256+
newSample.auxData[1].should.be.approximately(0,0.1);
257+
newSample.auxData[2].should.be.approximately(1,0.4);
258+
}
259+
}
260+
assert(passed,"a sample with accel data was produced");
261+
});
245262
});
246263
describe('#impedanceCalculationForChannel', function() {
247264

0 commit comments

Comments
 (0)