Refreshing the frame coming from the Leap Motion when in a while loop

179 Views Asked by At

I have just begun programming in JS for the Leap Motion. I am working on a project where I can control motors based on hand gestures that are being read from the Leap Motion. The frames are being parsed and then commands are being sent to the motors through an Arduino Uno using the johnny-five library. Here is the way I am grabbing the frames from the Leap Motion:

`var five = require('johnny-five'),
Leap = require('leapjs'),
board = new five.Board(),
motor, frame, hand;

board.on('ready', function() 
{   
    var controller = Leap.loop({enableGestures: true}, function(frame) 
    {   
        checkData(frame);
    });
});`

I take the frames from the Leap Motion and send the frame to a function that goes through the frame, reads the gestures, and then calls a new function that controls motors:

    var motorA = new five.Motor([3, 0, 2]);

    motorA.start(255);  

    while(!finished)
    {
        board.wait(5000, function()
        {
            motorA.stop();
            runMotorB(number);
            finsihed = true;
        });
    }

My problem is that I need a while loop to make the program wait for the first motor to end before running the next motor. The board.wait() doesn't seem to stop the program from continuing on to the next motor without waiting for the first motor to stop. I also need to be able to grab frames from the Leap Motion while running the motor in the while loop and waiting for it to end. This is because I want to be able to see if new gestures are occurring while the motor is running. However, whenever I try to grab a new frame using a controller.frame() call, I simply get the previous frame from the Leap Motion that started the while loop, not the frame that the Leap Motion is seeing at that second. Is there a way to see what the Leap Motion is seeing while stuck within the while loop? Or is there a better way for me to be stopping the program from moving onto the next motor without actually waiting?

1

There are 1 best solutions below

0
On BEST ANSWER

Hm. controller.frame() should always get the most recent frame, if the controller is streaming. This should be synonymous with controller.lastConnectionFrame.

I would hold state variables for each motor, and then run everything from your on frame callback.

It might even make sense to have a wrapper class around motor, if it doesn't hold state itself, which could have methods such as startUnlessStarted tryStart, or something.