Quantcast
Channel: Oracle Bloggers
Viewing all articles
Browse latest Browse all 19780

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

$
0
0
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

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)


Viewing all articles
Browse latest Browse all 19780

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>