我是 JavaScript 新手,一直在尝试对 Leap Motion 的输出数据进行平滑过滤。我使用 Cylon.js 获取数据,它基本上输出 3 个值(x、y 和 z)。但是,我无法让平滑代码工作,我认为这是因为我习惯了 C/C++ 语法并且可能做错了什么。
代码是这样的:
"use strict";
var Cylon = require("cylon");
var numReadings = 20;
var readings[numReadings];
var readIndex = 0;
var total = 0;
var average = 0;
for (var thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
Cylon.robot({
connections: {
leapmotion: {
adaptor: "leapmotion"
}
},
devices: {
leapmotion: {
driver: "leapmotion"
}
},
work: function(my) {
my.leapmotion.on("hand", function(hand) {
console.log(hand.palmPosition.join(","));
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = hand.palmPosition;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
console.log(average);
});
}
}).start();
所以我要过滤的数据是“hand.palmPosition”。但它在控制台上给了我以下错误:
任何帮助表示赞赏!
谢谢