Marcus Johnson
Published © GPL3+

LeapRover

Using LeapMotion, Java and the web services of the Particle Photon, I created a simple Rover Bot that uses leap motion controls.

IntermediateShowcase (no instructions)3 hours1,136
LeapRover

Things used in this project

Story

Read more

Schematics

Rover Schematic

This illustrates the Photon being connected to the PhoBot shield which is connected to the 2 DC motors.

Code

Rover Code (Photon)

C/C++
This is the code that uses webservices from the following libraries:
HC_SR04
PHOBOT
SOFTPWM
SPARKINTERVALTIMER
Make sure you include these before flashing the code to the photon.
// This #include statement was automatically added by the Particle IDE.
#include "SoftPWM/SoftPWM.h"

// This #include statement was automatically added by the Particle IDE.
#include "HC_SR04/HC_SR04.h"

// This #include statement was automatically added by the Particle IDE.
#include "PhoBot/PhoBot.h"


//Declare instance of the PhoBot library
PhoBot p = PhoBot();

void setup() {

   Spark.function("control", control);
}

int control(String command) {
    return p.control(command);
}

Photon Rover Main Class

Java
This is the executable class for the Photon Rover.
package rover;

import java.io.IOException;

import com.leapmotion.leap.Controller;

public class photonRover {

    public static void main(String[] args) 
    {
        //Instance of Motion Listener
        MotionListener listener = new MotionListener();
        
        //Leap motion controller instance
        Controller controller = new Controller();
        
        //Add motion listener to controller
        controller.addListener(listener);

        // Keep this process running until Enter is pressed
        System.out.println("Press Enter to quit...");
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // Remove the listener when done
        controller.removeListener(listener);
    }

}

MotionListener class

Java
This class has the logic for how the LeapMotion reacts when it detects hands/fingers and makes web service calls to the Photon Rover.
package rover;

import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Listener;

public class MotionListener extends Listener {

    private static final String accessToken = "<your_photon_access_token>";
    private static final String deviceID = "<your_photon_device_id>";

    //Constants for hands and fingers
    private static final int totalHands = 1;
    private static final int forward = 5;
    private static final int left = 1;
    private static final int right = 2;
    private static final int back = 4;
   
    private static String controlURL;

    @Override
    public void onConnect(Controller controller) {
        System.out.println("Connected");
    }

    @Override
    public void onFrame(Controller controller) {
        Frame frame = controller.frame();

        //Determine number of hands and fingers from the leap motion
        int hands = frame.hands().count();
        int fingers = frame.fingers().count();

        determineMovement(hands, fingers);
    }

    private static void determineMovement(int hands, int fingers) {
        
        //Build control URL
        StringBuilder url = new StringBuilder();
        url.append("https://api.spark.io/v1/devices/");
        url.append(deviceID);
        url.append("/control");

        controlURL = url.toString();

        if (hands == totalHands) {
            switch (fingers) {
            case forward:
                executeCommand("F-100");
                break;
            case right:
                executeCommand("R-50");
                break;
            case left:
                executeCommand("L-50");
                break;
            case back:
                executeCommand("B-75");
                break;
            default:
                executeCommand("S");
                break;

            }
        }
        else {
          
            //Stop the Rover if wrong number of hands are detected
            executeCommand("S");
        }
    }

    private static void executeCommand(String command) {
        URL url;
        
        //Send command to Rover via POST
        try {
            url = new URL(controlURL);

            HttpURLConnection hConnection = (HttpURLConnection) url
                    .openConnection();
            HttpURLConnection.setFollowRedirects(true);

            hConnection.setDoOutput(true);
            hConnection.setRequestMethod("POST");

            PrintStream ps = new PrintStream(hConnection.getOutputStream());
            ps.print("params=" + command + "&access_token=" + accessToken);
            ps.close();

            hConnection.connect();
            
            hConnection.getResponseCode();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Credits

Marcus Johnson

Marcus Johnson

2 projects • 27 followers
Software engineer with professional experience creating, maintaining, integrating, and testing software in Windows and Unix environments.

Comments

Add projectSign up / Login