//
you're reading...
How-to's

HTTYR: Gesture Control using LeapMotion

2014-04-26 17.13.08

This post is part of a series on how to train your robot.

If you have a Leap Motion device and have registered as a developer, you can use your Leap Motion to control your robot with gestures in the air.

The following program controls the robot with a few finger gestures. A circle gesture  rotates the robot clockwise or anticlockwise. A screen tab gesture moves the robot forward. A key tap stops the robot, and a swipe gesture moves it back a short distance.

import java.io.IOException;
import java.rmi.RemoteException;

import lejos.hardware.BrickFinder;
import lejos.remote.ev3.RMIRegulatedMotor;
import lejos.remote.ev3.RemoteEV3;

import com.leapmotion.leap.*;
import com.leapmotion.leap.Gesture.State;

class GestureListener extends Listener {
  RemoteEV3 ev3;
  RMIRegulatedMotor left, right;

  public void onInit(Controller controller) {
    ev3 = (RemoteEV3) BrickFinder.getDefault();
    left = ev3.createRegulatedMotor("A", 'L');
    right = ev3.createRegulatedMotor("B", 'L');
  }

  public void onConnect(Controller controller) {
    controller.enableGesture(Gesture.Type.TYPE_SWIPE);
    controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
    controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
    controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
  }

  public void onFrame(Controller controller) {
    // Get the most recent frame and report some basic information
    Frame frame = controller.frame();

    GestureList gestures = frame.gestures();
    for (int i = 0; i < gestures.count(); i++) {
      Gesture gesture = gestures.get(i);
      try {
        switch (gesture.type()) {
        case TYPE_CIRCLE:
          CircleGesture circle = new CircleGesture(gesture);
          boolean clockwise = (circle.pointable().direction()
              .angleTo(circle.normal()) <= Math.PI / 4);
          if (circle.state() == State.STATE_STOP) {
            System.out.println("Circle");
            // Turn robot
            if (clockwise) {
              left.forward();
              right.backward();
            } else {
              left.backward();
              right.forward();
            }
          }
          break;
        case TYPE_SWIPE:
          System.out.println("Swipe");
          // Go back a bit
          left.rotate(-500, true);
          right.rotate(-500, true);
          break;
        case TYPE_KEY_TAP:
          System.out.println("Key Tap");
          // Stop robot
          left.stop(true);
          right.stop(true);
          break;
        case TYPE_SCREEN_TAP:
          System.out.println("Screen Tap");
          // Go forward
          left.forward();
          right.forward();
          break;
        }
      } catch (RemoteException e) {
        e.printStackTrace();
      }
    }
  }

  public void close() {
    try {
      left.close();
      right.close();
    } catch (RemoteException e) {
    }
  }
}

public class LeapControl {
  public static void main(String[] args) {
    GestureListener listener = new GestureListener();
    Controller controller = new Controller();
    controller.addListener(listener);

    System.out.println("Press Enter to quit...");
    try {
      System.in.read();
    } catch (IOException e) {
    }

    listener.close();
    controller.removeListener(listener);
  }
}

Here is a video of the finger gestures.

Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: