Rodrigo Carrilho
Rodrigo Carrilho

Reputation: 69

Attempting smoothing code on node.js and cylon.js but syntax failing

I'm new to JavaScript and have been trying to put a smoothing filter on the output data from my Leap Motion. I get the data using Cylon.js and it basically outputs 3 values (x, y and z). I can't, however, get the smoothing code to work, I think it's because I'm used to the C/C++ syntax and am probably doing something wrong.

The code is this:

"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();

So the data I am trying to filter is the "hand.palmPosition". But it gives me the following error on the console:

Console Error

Any help is appreciated!

Thank you

Upvotes: 0

Views: 87

Answers (1)

Gabriel Florit
Gabriel Florit

Reputation: 2918

This is invalid JS:

var readings[numReadings];

Looks like you want readings to be an array. You don't need to initialize a JS array with a size. To create an array:

var readings = [];

To fill it with zeros:

for (var thisReading = 0; thisReading < numReadings; thisReading++) {
  readings.push[0];
}

Upvotes: 0

Related Questions