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)

Comments:

Nice series!
But you should check out Pi4J :)

Posted by user404 on September 05, 2013 at 01:26 PM PDT #

You're right, user404! Pi4J rocks! But, this series was more about how to do it all with GPIO using only pure Java API's (no native libraries like libwiringPi.so which Pi4J is dependent on). It's all good though, whether you like using a native C lib w/Pi4J or an all Java approach like in my series.

Both have good performance and both let you program GPIO using Java.

Hinkmond

Posted by Hinkmond Wong on September 05, 2013 at 04:06 PM PDT #

Post a Comment:
Comments are closed for this entry.
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
« April 2014
SunMonTueWedThuFriSat
  
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today