Skip to content

Commit

Permalink
#3 Enhanced grab detection
Browse files Browse the repository at this point in the history
  • Loading branch information
pappist committed Aug 12, 2016
1 parent de677ea commit 234234d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.incquerylabs.iot.leapmotion.drools.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.kie.api.KieBase;
Expand All @@ -22,6 +24,10 @@ public class DroolsFrameStream extends AbstractFrameStream {

public static final String STREAM_ID = "FrameStream";

List<Frame> accepted_grabs;

long counter = 0;

public DroolsFrameStream(IAddress sourceAddress) {
super(sourceAddress);

Expand All @@ -35,6 +41,10 @@ public DroolsFrameStream(IAddress sourceAddress) {

kieSession = new AtomicReference<KieSession>(kieBase.newKieSession());

accepted_grabs = new ArrayList<Frame>();

kieSession.get().setGlobal("accepted_grabs", accepted_grabs);

stream = new AtomicReference<EntryPoint>(kieSession.get().getEntryPoint( STREAM_ID ));

}
Expand All @@ -43,7 +53,9 @@ public DroolsFrameStream(IAddress sourceAddress) {
public void processFrame(Frame frame) {
if(stream != null)
stream.get().insert(frame);
kieSession.get().fireAllRules();
counter++;
if(counter % 10 == 0)
kieSession.get().fireAllRules();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ package rules
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Finger.Type;

//list any import classes here.
import java.util.Date;

declare Frame
@role(event)
Expand All @@ -16,16 +15,18 @@ declare Finger
end

declare window FrameEvents
Frame() over window:time( 200ms ) from entry-point "FrameStream"
Frame() over window:time( 2s ) from entry-point "FrameStream"
end

//declare any global variables here
global java.util.List accepted_grabs

rule "Grab gesture"
when
$A1 : Frame( fingers.extended().count() == 5 ) from window FrameEvents
$A2 : Frame( fingers.extended().count() == 0, this after [ 50ms ] $A1 ) from window FrameEvents
$A2 : Frame( fingers.extended().count() == 0, this after $A1 ) from window FrameEvents
then
System.out.println("GRAB!");
if(accepted_grabs.isEmpty() || $A2.timestamp() - ((Frame)accepted_grabs.get(0)).timestamp() > 2000000) {
System.out.println("GRAB! " + new Date($A2.timestamp() / 1000).toLocaleString());
accepted_grabs.add(0, $A2);
}
end

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public static void main(String[] args) throws IOException, InterruptedException
// XXX. read from args
String workingDirectory = String.format("%s/leapmotion", System.getProperty("user.dir"));

// String streampath = String.format("%s/stream_%d.lmstream", workingDirectory, System.currentTimeMillis());
String streampath = String.format("%s/stream_%d.lmstream", workingDirectory, System.currentTimeMillis());

Controller controller = new Controller();

controller.frame(30);

PublisherPool.initializePool(new ZMQFactory());

controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public ZmqFramePublisher(IAddress address) {
public void onFrame(Controller controller) {
try {
Frame frame = controller.frame();
if(frame.isValid() && !frame.hands().isEmpty())
if(frame.isValid() && !frame.hands().isEmpty()) {
PublisherPool.getInstance().next(address).publish(frame.serialize(), 0);
// System.out.println(String.format("Frame published: %d / %d", frame.id(), frame.timestamp()));
System.out.println(String.format("Frame published: %d / %d", frame.id(), frame.timestamp()));
}
} catch (Exception e) {
// TODO: logging
e.printStackTrace();
Expand Down

0 comments on commit 234234d

Please sign in to comment.