Friday Aug 30, 2013

Skynet Big Data Demo Using Hexbug Spider Robot, Raspberry Pi, and Java SE Embedded (Part 7)

And, as promised, here's the Java SE Embedded source code used on the Raspberry Pi to drive the Skynet Big Data demo using the Hexbug Spider Robot.

This part of the demo is actually just a first level diagnostic test to run the robot through the four controls: left, right, forward, and back, then loop forever to ensure we have all the connections fully functional.

/*
 * Java Embedded Raspberry Pi Robot app
 */
package jerpirobot;

import java.io.FileWriter;
import java.io.File;
import java.io.IOException;

/**
 *
 * @author hinkmond
 */
public class JerpiRobot {
    
    static final String GPIO_OUT = "out";
    static final String GPIO_ON = "1";
    static final String GPIO_OFF = "0";
    
    static String[] GpioChannels = 
    { "0", "1", "4", "17", "21", "22", "10", "9" };
       
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        FileWriter[] commandChannels;
        
        try {
            
            /*** Initialize GPIO ports for output ***/
            
            // Open file handles to GPIO port unexport and export controls
            FileWriter unexportFile = 
                    new FileWriter("/sys/class/gpio/unexport");
            FileWriter exportFile = 
                    new FileWriter("/sys/class/gpio/export");

            for (String gpioChannel : GpioChannels) {
                System.out.println(gpioChannel);
    
                // Reset the port
                File exportFileCheck = new File("/sys/class/gpio/gpio" +
                        gpioChannel);
                if (exportFileCheck.exists()) {
                    unexportFile.write(gpioChannel);
                    unexportFile.flush();
                }
            
                // Set the port for use
                exportFile.write(gpioChannel);   
                exportFile.flush();

                // Open file handle to port input/output control
                FileWriter directionFile =
                    new FileWriter("/sys/class/gpio/gpio" + gpioChannel + 
                        "/direction");
            
                // Set port for output
                directionFile.write(GPIO_OUT);
                directionFile.flush();
            }
            
            /*** Send commands to GPIO ports ***/
            
            commandChannels = new FileWriter[GpioChannels.length];    
            for (int channum=0; channum < GpioChannels.length; channum++) {
                // Open file handle to issue commands to GPIO port
                commandChannels[channum] = 
                        new FileWriter("/sys/class/gpio/gpio" +
                        GpioChannels[channum] + "/value");
            }
            
            int channum;
            
            // Loop forever
            while (true) {
                
                // First Level Diagnostics: Send signal to each direction:
                //   Left, Right, Forward, and Back
                for (channum=4; channum < 8; channum++) {
                    commandChannels[channum].write(GPIO_ON);
                    commandChannels[channum].flush();
                
                    // Wait for a while
                    java.lang.Thread.sleep(1000);
                
                    // Set GPIO port OFF
                    commandChannels[channum].write(GPIO_OFF);
                    commandChannels[channum].flush();
                    
                    // Wait for a while
                    java.lang.Thread.sleep(1000);
                }
            }
        } catch (IOException | InterruptedException exception) {
            exception.printStackTrace();
        }
    }
}

And if you want to see the whole series of steps that put the whole demo together, just refer to these links:

See the previous posts for the full series on the steps to this cool demo:
Skynet Big Data Demo Using Hexbug Spider Robot, Raspberry Pi, and Java SE Embedded (Part 1)
Skynet Big Data Demo Using Hexbug Spider Robot, Raspberry Pi, and Java SE Embedded (Part 2)
Skynet Big Data Demo Using Hexbug Spider Robot, Raspberry Pi, and Java SE Embedded (Part 3)
Skynet Big Data Demo Using Hexbug Spider Robot, Raspberry Pi, and Java SE Embedded (Part 4)
Skynet Big Data Demo Using Hexbug Spider Robot, Raspberry Pi, and Java SE Embedded (Part 5)
Skynet Big Data Demo Using Hexbug Spider Robot, Raspberry Pi, and Java SE Embedded (Part 6)
Skynet Big Data Demo Using Hexbug Spider Robot, Raspberry Pi, and Java SE Embedded (Part 7)

About

Hinkmond Wong's blog on making Machine to Machine (M2M) and the Incredible Internet of Things (IoT) smarter with Java Embedded Technologies

Search

Archives
« August 2013 »
SunMonTueWedThuFriSat
    
1
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
24
25
26
28
29
31
       
Today