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)

Tuesday Aug 27, 2013

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

Here's my Hexbug Spider Robot going through his first diagnostics. Awwww.... his first steps. Woogie-woogie.

See:

Skynet Model T-1 First Steps

Soon, little Hexbug Spider Robot will use Skynet Big Data to hunt for Sarah Connor.

Java SE Embedded programming enables this robot to run through his new basic level one diagnostics. I'll post the Java source code in the next blog post. It's all pretty straight-forward if you've been following my posts so far. The hard part will be mounting the laser turret and radar targeting system. :-)

Friday Aug 23, 2013

Qualcomm's Uplinq Mobile CodeFest & Hackathon: 03-05Sep2013 San Diego, Calif

Qualcomm is hosting a a Mobile CodeFest and Hackathon in sunny Sandy Eggo, California on Sept. 3-5, 2013. If you're into mobile app development or like winning prizes, make sure to register to attend.

See:

Uplinq Mobile CodeFest & Hackathon

Here's a quote:

 If you're a mobile app developer, the 
 Uplinq Mobile CodeFest & Hackathon are 
 for you. Seeking education? Spend the 
 day attending CodeFest sessions to 
 get tips & tricks from the experts...
Fun! The best overall app wins $10,000 plus 5 tickets to the San Diego Padres vs. San Francisco Giants game on Wed., Sept. 4. That should actually be the "World Champion" San Francisco Giants. Go Giants! (And, sign up today for the Mobile CodeFest & Hackathon)

Friday Aug 02, 2013

RPi with an Oscilloscope and Java Embedded: Ready for Benchmarking (Part 8)

Well, as I mentioned to Robert Savage recently, I know how to get to the same speed in Java SE Embedded as the Pi4J Java to JNI library from Savage Home Automation and more importantly to also achieve the same 5MHz max frequency on a GPIO pin as using C native. But knowing how is not exactly the same as implementing, is it? :-) So, I'll show how instead, since showing is the same as implementing, right? ;-) Well, it should be the same. B-)

The approach in theory could use NIO memory mapped buffer that I was originally thinking should be as fast as C native. But in practice anyone that has tried this has gotten a bug where it says "No device found" error when trying to map the /dev/mem of the Raspberry Pi. That's what @Geomancer86 also noticed also, as he mentioned on twitter.

So, what's the deal? It seems Java SE Embedded NIO does one type of file open and mmap down in the native layer underneath java.nio.ByteBuffer that does not allow for opening native memory devices (only real files on the Linux system). But, we need to access /dev/mem from Java on the Raspberry Pi to access the GPIO pins in memory to flip the bits as fast as possible to reach the maximum possible frequency in doing so.

There something we can learn by looking at Gordon Henderson's WiringPi C native code. That is there's a different way of doing the native file open and mmap that's needed, and we know he's doing it the right way since WiringPi can get that 5MHz on the GPIO pins that we want.

See: https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPi.c
Scroll down to line 1319

// Open the master /dev/memory device

  if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
  {
    if (wiringPiDebug)
    {
      int serr = errno ;
fprintf (stderr, "wiringPiSetup: Unable to open /dev/mem: %s\n", 
  strerror (errno)) ;
      errno = serr ;
    }
    return -1 ;
  }

// GPIO:

  gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, 
    MAP_SHARED, fd, GPIO_BASE) ;
  if ((int32_t)gpio == -1)
  {
    if (wiringPiDebug)
    {
      int serr = errno ;
fprintf (stderr, "wiringPiSetup: mmap failed: %s\n", 
  strerror (errno)) ;
      errno = serr ;
    }
    return -1 ;
  }

Instead of what NIO does currently, we need to instead do the same as above and then hand back a Java ByteBuffer that is pointing to that GPIO Base mapped in memory. Then we can use java.nio.ByteBuffer to twiddle bits representing the GPIO pins and turn them on/off as fast as possible, which in theory gives us that max 5MHz.

If anyone wants to try this, try extracting just that WiringPi native C code from Gordon Henderson's library (above) and make your own JNI call to it from Java SE Embedded to mimic what NIO does. Then make another JNI call to do a write to the memory just like Gordon does to turn the pin on/off as fast as possible. That in theory should give the max 5MHz that C native can do and have it be accessible from the Java SE Embedded side.

So, that's me showing how it could be done, it's up to someone out there to see if that theory holds up. Any takers? Showing is just like implementing, right? :-)

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