在 java 中错过了来自跳跃动作的点击手势
missed tap gestures from leap motion in java
我写了一个测试代码来查看我的点击手势是否被跳跃动作接受。
在 运行 代码之后,我注意到我必须非常积极地点击,即便如此,我每点击几次也只能收到一次点击。
我怎样才能更改我的代码以防止出现这种情况?
(请注意,我对编码很陌生,所以请详细说明,谢谢)
文件#1
package com.leaptoarduino;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Gesture;
public class Leapd
{
//Main
public static final void main(String args[])
{
//Initialize serial communications
RS232Protocol serial = new RS232Protocol();
serial.connect("COM3");
//Initialize the Leapd listener
LeapdListener leap = new LeapdListener(serial);
Controller controller = new Controller();
controller.addListener(leap);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
//Set up controller for gestures
controller.config().setFloat("Gesture.KeyTap.MinDownVelocity", 5.0f);
controller.config().setFloat("Gesture.KeyTap.HistorySeconds", .8f);
controller.config().setFloat("Gesture.KeyTap.MinDistance", 20.0f);
controller.config().save();
}
}
文件#2
package com.leaptoarduino;
import com.leapmotion.leap.*;
import com.leapmotion.leap.Gesture.Type;
import com.leapmotion.leap.KeyTapGesture;
public class LeapdListener extends Listener
{
//Serial port that will be used to communicate with the Arduino
private RS232Protocol serial;
//Constructor
public LeapdListener(RS232Protocol serial)
{
this.serial = serial;
}
//Member function: onInit
public void onInit(Controller controller)
{
System.out.println("Initialized");
}
//Member function: onConncect
public void onConnect(Controller controller)
{
System.out.println("Connected");
}
//Member Function: onDisconnect
public void onDisconnect(Controller controller)
{
System.out.println("Disconnected");
}
//Member Function: onExit
public void onExit(Controller controller)
{
System.out.println("Exited");
}
//Member Function: onFrame
public void onFrame(Controller controller)
{
//Get the most recent frame
Frame frame = controller.frame();
//Verify a hand is in view
if (frame.hands().count() > 0)
{
GestureList gestures = frame.gestures();
for (Gesture gesture: gestures)
{
if (gesture.type() == Type.TYPE_KEY_TAP)
{
KeyTapGesture keytap = new KeyTapGesture (gesture);
System.out.println("KEYTAPPED");
}
}
//Send the tap data to the Arduino
// TBD
//Give the Arduino some time to process our data
try { Thread.sleep(30); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
}
当您在 OnFrame 处理程序中休眠线程时,您将丢失来自 Leap Motion 服务的帧。您的部分问题可能是在那些丢失的帧中识别了点击手势。 Frame.gestures() 采用 "sinceFrame" 参数来避免此问题:frame.gestures(sinceFrame)
为您提供在 sinceFrame 和当前帧之间发生的所有手势,因此您不会错过任何丢帧的情况或跳过。您所做的是在每次运行 onFrame 处理程序时将当前帧保存到 lastFrameProcessed 变量。您将这个 lastFrameProcessed 变量传递给 gestures() 以获得完整的手势列表。类似于:
Frame lastFrameProcessed = Frame.invalid();
public void onFrame(Controller controller)
{
//Get the most recent frame
Frame frame = controller.frame();
//Verify a hand is in view
if (frame.hands().count() > 0)
{
GestureList gestures = frame.gestures(lastFrameProcessed);
for (Gesture gesture: gestures)
{
if (gesture.type() == Type.TYPE_KEY_TAP)
{
KeyTapGesture keytap = new KeyTapGesture (gesture);
System.out.println("KEYTAPPED");
}
}
//Send the tap data to the Arduino
// TBD
//Give the Arduino some time to process our data
try { Thread.sleep(30); }
catch (InterruptedException e) { e.printStackTrace(); }
}
lastFrameProcessed = frame;
}
我写了一个测试代码来查看我的点击手势是否被跳跃动作接受。 在 运行 代码之后,我注意到我必须非常积极地点击,即便如此,我每点击几次也只能收到一次点击。 我怎样才能更改我的代码以防止出现这种情况?
(请注意,我对编码很陌生,所以请详细说明,谢谢)
文件#1
package com.leaptoarduino;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Gesture;
public class Leapd
{
//Main
public static final void main(String args[])
{
//Initialize serial communications
RS232Protocol serial = new RS232Protocol();
serial.connect("COM3");
//Initialize the Leapd listener
LeapdListener leap = new LeapdListener(serial);
Controller controller = new Controller();
controller.addListener(leap);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
//Set up controller for gestures
controller.config().setFloat("Gesture.KeyTap.MinDownVelocity", 5.0f);
controller.config().setFloat("Gesture.KeyTap.HistorySeconds", .8f);
controller.config().setFloat("Gesture.KeyTap.MinDistance", 20.0f);
controller.config().save();
}
}
文件#2
package com.leaptoarduino;
import com.leapmotion.leap.*;
import com.leapmotion.leap.Gesture.Type;
import com.leapmotion.leap.KeyTapGesture;
public class LeapdListener extends Listener
{
//Serial port that will be used to communicate with the Arduino
private RS232Protocol serial;
//Constructor
public LeapdListener(RS232Protocol serial)
{
this.serial = serial;
}
//Member function: onInit
public void onInit(Controller controller)
{
System.out.println("Initialized");
}
//Member function: onConncect
public void onConnect(Controller controller)
{
System.out.println("Connected");
}
//Member Function: onDisconnect
public void onDisconnect(Controller controller)
{
System.out.println("Disconnected");
}
//Member Function: onExit
public void onExit(Controller controller)
{
System.out.println("Exited");
}
//Member Function: onFrame
public void onFrame(Controller controller)
{
//Get the most recent frame
Frame frame = controller.frame();
//Verify a hand is in view
if (frame.hands().count() > 0)
{
GestureList gestures = frame.gestures();
for (Gesture gesture: gestures)
{
if (gesture.type() == Type.TYPE_KEY_TAP)
{
KeyTapGesture keytap = new KeyTapGesture (gesture);
System.out.println("KEYTAPPED");
}
}
//Send the tap data to the Arduino
// TBD
//Give the Arduino some time to process our data
try { Thread.sleep(30); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
}
当您在 OnFrame 处理程序中休眠线程时,您将丢失来自 Leap Motion 服务的帧。您的部分问题可能是在那些丢失的帧中识别了点击手势。 Frame.gestures() 采用 "sinceFrame" 参数来避免此问题:frame.gestures(sinceFrame)
为您提供在 sinceFrame 和当前帧之间发生的所有手势,因此您不会错过任何丢帧的情况或跳过。您所做的是在每次运行 onFrame 处理程序时将当前帧保存到 lastFrameProcessed 变量。您将这个 lastFrameProcessed 变量传递给 gestures() 以获得完整的手势列表。类似于:
Frame lastFrameProcessed = Frame.invalid();
public void onFrame(Controller controller)
{
//Get the most recent frame
Frame frame = controller.frame();
//Verify a hand is in view
if (frame.hands().count() > 0)
{
GestureList gestures = frame.gestures(lastFrameProcessed);
for (Gesture gesture: gestures)
{
if (gesture.type() == Type.TYPE_KEY_TAP)
{
KeyTapGesture keytap = new KeyTapGesture (gesture);
System.out.println("KEYTAPPED");
}
}
//Send the tap data to the Arduino
// TBD
//Give the Arduino some time to process our data
try { Thread.sleep(30); }
catch (InterruptedException e) { e.printStackTrace(); }
}
lastFrameProcessed = frame;
}