Thursday Feb 28, 2013

Java Communications API and Serial Ports Live Forever with Java Embedded

"I love Java technology! But, I have split-personality disorder. No, I don't. Yes, I do..."

Here's one of the Dr. Dobb's bloggers, Al Williams, expressing his love-hate relationship with Java Embedded technology and serial ports.

See:

I love Java! Yes, I do!

Here's a quote:

 The lines, however, are blurring 
 between all the different kinds 
 of systems. You almost can't avoid 
 Java somewhere. The Arduino uses 
 Java internally. So does Eclipse. 
 Many data acquisition systems need 
 to connect over a network and Java 
 works well for that, either on the 
 device or running on a host PC (or 
 even a Raspberry Pi).
Good for you, Al! At least you know Java technology on small embedded devices is there and ready for you whenever you come around and warm up to Java Embedded. :-)

Thursday Feb 21, 2013

New NetBeans 7.3 released for HTML5, JavaScript, and CSS: Great UI for Java Embedded

A great way to use Java Embedded is to slap a lightweight Jetty Web Server on it and write a Java Embedded App to generate HTML5/JavaScript/CSS Web pages as the Embedded UI. Just point your Web browser to your embedded device over the network to interact with it. And, you can now use NetBeans 7.3 as your IDE. Way cool.

See:

NetBeans 7.3 w/HTML5

Here's a quote:

 With the release of version 7.3 of 
 NetBeans, Oracle has updated the IDE 
 (integrated developer environment) so 
 Java developers can more easily build 
 rich HTML5-based user interfaces for 
 their mobile and Web applications.
I've got NetBeans 7.3 running on my MacBook Pro at home Windows 7 laptop at work. It's the best editor and/or IDE for Java, PHP, C/C++, HTML5, and JavaScript that I've seen (including XEmacs which has been my go-to source code editor since dinosaurs roamed the earth).

Wednesday Feb 20, 2013

Connect robot servo to RPi and use Java Embedded to program it (Part 5)

And, now the Java source code for driving the servo connected to your Rapberry Pi. It's pretty straight-forward since we will use the GPIO again to send a signal to the servo.

This time we need to send a Pulse Width Modulation (PWM) signal. This means the signal we send is a wave that we generate out the GPIO port that has a certain frequency (between 30-50 Hz for the servo), with a specific pulse width for everytime the wave is in the high position (pulse).

Here's how PWM looks using Java programming. It's much more readable than writing PWM code in C or in Python.

/*
 * Java Embedded Raspberry Pi Servo app
 */
package jerpiservo;

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

/**
 *
 * @author hinkmond
 */
public class ServoController {
    
    static final String GPIO_OUT = "out";
    static final String GPIO_ON = "1";
    static final String GPIO_OFF = "0";
    
    static String[] GpioChannels =  { "24" };

    /**
     * @param args the command line arguments
     */
    @SuppressWarnings("UseSpecificCatch")
    public static void main(String[] args) {
        FileWriter[] commandChannels;
        
        try {
            
            /*** Init GPIO port 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");

            // Loop through all ports if more than 1
            for (String gpioChannel : GpioChannels) {
                System.out.println(gpioChannel);
    
                // Reset the port, if needed
                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 port ***/
            
            // Set up a GPIO port as a command channel
            FileWriter commandChannel = new 
			FileWriter("/sys/class/gpio/gpio" +
              	GpioChannels[0] + "/value");
            
            // Set initial variables for PWM
            int period = 20;
            int repeatLoop = 25;
            
            int counter;
            
            // Loop forever to create Pulse Width Modulation - PWM
            while (true) {
                
                /*--- Move servo clockwise to 90 degree position ---*/
                
                // Create a pulse for repeatLoop number of cycles
                for (counter=0; counter<repeatLoop; counter++) {
                    
                    // HIGH: Set GPIO port ON
                    commandChannel.write(GPIO_ON);
                    commandChannel.flush();               
                
                    // Pulse Width determined by amount of 
                    //   sleep time while HIGH
                    java.lang.Thread.sleep(0, 800000);
        
                    // LOW: Set GPIO port OFF
                    commandChannel.write(GPIO_OFF);
                    commandChannel.flush();
                
                    // Frequency determined by amount of 
                    //  sleep time while LOW
                    java.lang.Thread.sleep(period);
                }
                
                /*--- Move servo counterclockwise to 0 
				degree position ---*/

                // Create a pulse for repeatLoop number of cycles
                for (counter=0; counter<repeatLoop; counter++) {
                    
                    // HIGH: Set GPIO port ON
                    commandChannel.write(GPIO_ON);
                    commandChannel.flush();               
                
                    // Pulse Width determined by amount of 
                    //   time while HIGH
                    java.lang.Thread.sleep(2, 200000);
        
                    // LOW: Set GPIO port OFF
                    commandChannel.write(GPIO_OFF);
                    commandChannel.flush();
                
                    // Frequency determined by amount of 
                    //  time while LOW
                    java.lang.Thread.sleep(period);
                }
            }
            
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

So, with this signal that we send over the GPIO, we can tell the servo to move to a certain position (just like telling a robot arm to move to a certain angle). We can use the this concept to have Java technology control a Raspberry Pi servo or sets of servos for robotics, industrial automation, healthcare, the Mars rover, you name it!

See the previous posts for the full series on the steps to this cool demo:
Connect robot servo to RPi and use Java Embedded to program it (Part 1)
Connect robot servo to RPi and use Java Embedded to program it (Part 2)
Connect robot servo to RPi and use Java Embedded to program it (Part 3)
Connect robot servo to RPi and use Java Embedded to program it (Part 4)
Connect robot servo to RPi and use Java Embedded to program it (Part 5)

Wednesday Feb 13, 2013

Connect robot servo to RPi and use Java Embedded to program it (Part 4)

"We need more power, Scotty!" When connecting a servo to your Raspberry Pi with Java Embedded, make sure to add a more powerful 2 amp USB power adapter, rather than use the original 0.7 amp adapter that comes with the RPi device. This is because connecting a servo to your RPi GPIO will draw up to 400 milliamps and steal away power from the RPi itself, making it crash, reboot, or worse.

See:

More power for your RPi Servo

Here's what to buy:

  1 SIIG 2 Amp/2 Port USB Adaptor
  1 Micro USB cable
So add that to your list at Fry's. It's all good. An extra trip to Fry's never hurt anyone. ;-)

See the full series of blogs posts for this demo:
Connect robot servo to RPi and use Java Embedded to program it (Part 1)
Connect robot servo to RPi and use Java Embedded to program it (Part 2)
Connect robot servo to RPi and use Java Embedded to program it (Part 3)
Connect robot servo to RPi and use Java Embedded to program it (Part 4)
Connect robot servo to RPi and use Java Embedded to program it (Part 5)

Thursday Feb 07, 2013

Connect robot servo to RPi and use Java Embedded to program it (Part 3)

It's Alive! My Raspberry Pi servo motor is alive with Java Embedded programming!

See:

See RPi Servo demo w/Java Embedded

I'm using Java Embedded to run a Java app that uses the GPIO pins to drive an analog LS-0006 servo motor. Just think of this as a way to control a robotic arm or industrial automation equipment.

Here's a code snippet for now to show what I'm doing (more details and full Java code explanation in a future blog post):

            // Loop forever to create Pulse Width Modulation - PWM
            while (true) {
                
                /*--- Move clockwise to 90 degrees position ---*/
                
                // Create a pulse for repeatLoop number of cycles
                for (counter=0; counter < repeatLoop; counter++) {
                    
                    // HIGH: Set GPIO port ON
                    commandChannel.write(GPIO_ON);
                    commandChannel.flush();               
                
                    // Pulse Width determined by amount of 
                    //   sleep time while HIGH
                    java.lang.Thread.sleep(0, 800000);
        
                    // LOW: Set GPIO port OFF
                    commandChannel.write(GPIO_OFF);
                    commandChannel.flush();
                
                    // Frequency determined by amount of 
                    //  sleep time while LOW
                    java.lang.Thread.sleep(period);
                }
                
                /*--- Move counterclockwise to 0 degrees position ---*/

                // Create a pulse for repeatLoop number of cycles
                for (counter=0; counter < repeatLoop; counter++) {
                    commandChannel.write(GPIO_ON);
                    commandChannel.flush();               
                
                    java.lang.Thread.sleep(2, 200000);
        
                    // Set GPIO port OFF
                    commandChannel.write(GPIO_OFF);
                    commandChannel.flush();
                
                    // Wait for a while
                    java.lang.Thread.sleep(period);
                }
            }

See the full series of blogs posts for this demo:
Connect robot servo to RPi and use Java Embedded to program it (Part 1)
Connect robot servo to RPi and use Java Embedded to program it (Part 2)
Connect robot servo to RPi and use Java Embedded to program it (Part 3)
Connect robot servo to RPi and use Java Embedded to program it (Part 4)
Connect robot servo to RPi and use Java Embedded to program it (Part 5)

Wednesday Feb 06, 2013

Connect robot servo to RPi and use Java Embedded to program it (Part 2)

Now that you have the arm you want connected to the servo, you need to connect the servo to the GPIO pins on the Raspberry Pi.

Here you see 3 jumper wires connected to the servo. Connect a jumper wire to the black (ground), red (power), and yellow (signal) connector jacks on servo. I used a black jumper wire to the black connector, blue jumper wire to the red connector, and yellow jumper wire to the yellow connector (see photo).





Then you need to connect the black, blue, and yellow jumper wires to the GPIO pins on your RPi. Refer to the GPIO pinout photo here to see which pins to use.

For our project, we will connect the black jumper wire to ground Pin #6 GND, blue jumper wire to power Pin #2 +5V, and yellow jumper wire to Pin #18, GPIO24.

That's it. Now we're ready to write the Java Embedded application to drive the servo to make it move.

See the full series of blogs posts for this demo:
Connect robot servo to RPi and use Java Embedded to program it (Part 1)
Connect robot servo to RPi and use Java Embedded to program it (Part 2)
Connect robot servo to RPi and use Java Embedded to program it (Part 3)
Connect robot servo to RPi and use Java Embedded to program it (Part 4)
Connect robot servo to RPi and use Java Embedded to program it (Part 5)

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
« February 2013 »
SunMonTueWedThuFriSat
     
1
2
3
4
5
8
9
10
11
12
14
15
16
17
18
19
22
23
24
25
26
27
  
       
Today