Problems with Zephyr JS
Kevin Stöckl <k_stoeckl@...>
Hello,
when i try to run following code in the zephyr js ide, then the application did not start. What could be the problem? In the console it just writes "echo o" and than it stops.
var aio = require("aio");
//var ble = require("ble");
var pins = require("arduino101_pins");
// heart rate calculation
var IBI = 600; // value holds the time interval between beats! Must be seeded!
var pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".
var rate = [10]; // array to hold last ten IBI values
var sampleCounter = 0; // used to determine pulse timing
var lastBeatTime = 0; // used to find IBI
var P = 2048; // used to find peak in pulse wave, seeded
var T = 2048; // used to find trough in pulse wave, seeded
var thresh = 2100; // used to find instant moment of heart beat, seeded
var amp = 400; // used to hold amplitude of pulse waveform, seeded
var firstBeat = true; // used to seed rate array so we startup with reasonable BPM
var secondBeat = false; // used to seed rate array so we startup with reasonable BPM
// pins
var pin = aio.open({device:0, pin: pins.A1 });
function measureHeartRate(signal) {
var HR = 0;
sampleCounter += 2; // keep track of the time in mS with this variable
var N = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise
// find the peak and trough of the pulse wave
if (signal < thresh && (N > ((IBI / 5) * 3))) { // avoid dichrotic noise by waiting 3/5 of last IBI
if (signal < T) { // T is the trough
T = signal; // keep track of lowest point in pulse wave
}
}
if (signal > thresh && signal > P) { // thresh condition helps avoid noise
P = signal; // P is the peak
} // keep track of highest point in pulse wave
// NOW IT'S TIME TO LOOK FOR THE HEART BEAT
// signal surges up in value every time there is a pulse
if (N > 250 && N < 2000) { // avoid noise. 30 bpm < possible human heart beat < 240
if ((signal > thresh) && (pulse == false) && (N > ((IBI / 5) * 3))) {
pulse = true; // set the pulse flag when we think there is a pulse
IBI = sampleCounter - lastBeatTime; // measure time between beats in mS
lastBeatTime = sampleCounter; // keep track of time for next pulse
if (secondBeat) { // if this is the second beat, if secondBeat == TRUE
console.log("Measured 2nd beat");
secondBeat = false; // clear secondBeat flag
for(var i = 0; i <= 9; i++) { // seed the running total to get a realisitic BPM at startup
rate[i] = IBI;
}
}
if (firstBeat) { // if it's the first time we found a beat, if firstBeat == TRUE
console.log("Measured 1st beat");
firstBeat = false; // clear firstBeat flag
secondBeat = true; // set the second beat flag
return 0; // IBI value is unreliable so discard it
}
// keep a running total of the last 10 IBI values
var runningTotal = 0; // clear the runningTotal variable
for (var i = 0; i <= 8; i++) { // shift data in the rate array
rate[i] = rate[i + 1]; // and drop the oldest IBI value
runningTotal += rate[i]; // add up the 9 oldest IBI values
}
rate[9] = IBI; // add the latest IBI to the rate array
runningTotal += rate[9]; // add the latest IBI to runningTotal
runningTotal /= 10; // average the last 10 IBI values
HR = 60000 / runningTotal; // how many beats can fit into a minute? that's BPM!
}
}
if (signal < thresh && pulse == true) { // when the values are going down, the beat is over
pulse = false; // reset the pulse flag so we can do it again
amp = P - T; // get amplitude of the pulse wave
thresh = (amp * 2 / 3) + T; // set thresh at 67% of the amplitude
P = thresh; // reset these for next time
T = thresh;
}
if (N > 2500) { // if 2.5 seconds go by without a beat
thresh = 2048; // set thresh default
P = 2048; // set P default
T = 2048; // set T default
lastBeatTime = sampleCounter; // bring the lastBeatTime up to date
firstBeat = true; // set these to avoid noise
secondBeat = false; // when we get the heartbeat back
}
return HR; // return integer instead of float
}
setInterval(function() {
var rawSignal = pin.read();
var signal = measureHeartRate(rawSignal);
// send notification
console.log("HR=" + signal);
}, 0);
//var ble = require("ble");
var pins = require("arduino101_pins");
// heart rate calculation
var IBI = 600; // value holds the time interval between beats! Must be seeded!
var pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".
var rate = [10]; // array to hold last ten IBI values
var sampleCounter = 0; // used to determine pulse timing
var lastBeatTime = 0; // used to find IBI
var P = 2048; // used to find peak in pulse wave, seeded
var T = 2048; // used to find trough in pulse wave, seeded
var thresh = 2100; // used to find instant moment of heart beat, seeded
var amp = 400; // used to hold amplitude of pulse waveform, seeded
var firstBeat = true; // used to seed rate array so we startup with reasonable BPM
var secondBeat = false; // used to seed rate array so we startup with reasonable BPM
// pins
var pin = aio.open({device:0, pin: pins.A1 });
function measureHeartRate(signal) {
var HR = 0;
sampleCounter += 2; // keep track of the time in mS with this variable
var N = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise
// find the peak and trough of the pulse wave
if (signal < thresh && (N > ((IBI / 5) * 3))) { // avoid dichrotic noise by waiting 3/5 of last IBI
if (signal < T) { // T is the trough
T = signal; // keep track of lowest point in pulse wave
}
}
if (signal > thresh && signal > P) { // thresh condition helps avoid noise
P = signal; // P is the peak
} // keep track of highest point in pulse wave
// NOW IT'S TIME TO LOOK FOR THE HEART BEAT
// signal surges up in value every time there is a pulse
if (N > 250 && N < 2000) { // avoid noise. 30 bpm < possible human heart beat < 240
if ((signal > thresh) && (pulse == false) && (N > ((IBI / 5) * 3))) {
pulse = true; // set the pulse flag when we think there is a pulse
IBI = sampleCounter - lastBeatTime; // measure time between beats in mS
lastBeatTime = sampleCounter; // keep track of time for next pulse
if (secondBeat) { // if this is the second beat, if secondBeat == TRUE
console.log("Measured 2nd beat");
secondBeat = false; // clear secondBeat flag
for(var i = 0; i <= 9; i++) { // seed the running total to get a realisitic BPM at startup
rate[i] = IBI;
}
}
if (firstBeat) { // if it's the first time we found a beat, if firstBeat == TRUE
console.log("Measured 1st beat");
firstBeat = false; // clear firstBeat flag
secondBeat = true; // set the second beat flag
return 0; // IBI value is unreliable so discard it
}
// keep a running total of the last 10 IBI values
var runningTotal = 0; // clear the runningTotal variable
for (var i = 0; i <= 8; i++) { // shift data in the rate array
rate[i] = rate[i + 1]; // and drop the oldest IBI value
runningTotal += rate[i]; // add up the 9 oldest IBI values
}
rate[9] = IBI; // add the latest IBI to the rate array
runningTotal += rate[9]; // add the latest IBI to runningTotal
runningTotal /= 10; // average the last 10 IBI values
HR = 60000 / runningTotal; // how many beats can fit into a minute? that's BPM!
}
}
if (signal < thresh && pulse == true) { // when the values are going down, the beat is over
pulse = false; // reset the pulse flag so we can do it again
amp = P - T; // get amplitude of the pulse wave
thresh = (amp * 2 / 3) + T; // set thresh at 67% of the amplitude
P = thresh; // reset these for next time
T = thresh;
}
if (N > 2500) { // if 2.5 seconds go by without a beat
thresh = 2048; // set thresh default
P = 2048; // set P default
T = 2048; // set T default
lastBeatTime = sampleCounter; // bring the lastBeatTime up to date
firstBeat = true; // set these to avoid noise
secondBeat = false; // when we get the heartbeat back
}
return HR; // return integer instead of float
}
setInterval(function() {
var rawSignal = pin.read();
var signal = measureHeartRate(rawSignal);
// send notification
console.log("HR=" + signal);
}, 0);