0

我是 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”。但它在控制台上给了我以下错误:

控制台错误

任何帮助表示赞赏!

谢谢

4

1 回答 1

0

这是无效的 JS:

var readings[numReadings];

看起来你想readings成为一个数组。您不需要使用大小来初始化 JS 数组。要创建一个数组:

var readings = [];

用零填充它:

for (var thisReading = 0; thisReading < numReadings; thisReading++) {
  readings.push[0];
}
于 2016-04-27T21:19:05.573 回答