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)

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