Tuesday, 23 October 2012

Raspberry Pi Stepper Motor Control using Java.

I came across the Pi4J Project today. The project is working on a library to allow Pi programming from Java. It is not at version 1 yet so still quiet new but the library lets you access the GPIO port on your Pi and join in some of the fun I thought you needed to use python for.

My Pi is currently connected to a stepper motor so I thought I would have ago at getting it to run from java,the below code is the result. Once I had the pi setup and proved the java setup was working correctly I used Eclipse on my Windows PC to write my code/compile and WinSCP to transfer it to the Pi. 

The Pi4J project have a number of examples on their site and their control example (http://pi4j.com/example/control.html) was the starting point for my code. 

I am using a 28BYJ-48  stepper motor and ULN 2003 driver board which I picked up from ebay awhile ago.

Update: The latest version of Pi4J now includes a dedicated stepper motor class and example code.

The eclipse project can be download from: http://github.com/qubecad/hmm-pi.git


package com.qubecad.pi.stepper;

import java.util.HashMap;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;

public class PiStepper {

 /**
  * 
  * Driving a Stepper Motor From Java 
  * 
  * @param args
  * @throws InterruptedException 
  */
 public static void main(String[] args) throws InterruptedException {


  // create gpio controller
  GpioController gpio = GpioFactory.getInstance();

  // Set up the pins and set low to start
  System.out.print("Setting up GPIO Pins for ouput");
  GpioPinDigitalOutput pina = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_05, "Pin A", PinState.LOW);
  GpioPinDigitalOutput pinb = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_06, "Pin B", PinState.LOW);
  GpioPinDigitalOutput pinc = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_10, "Pin C", PinState.LOW);
  GpioPinDigitalOutput pind = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_11, "Pin D", PinState.LOW);


  HashMap driveLogic=new HashMap();

  driveLogic.put(0, "1001");
  driveLogic.put(1, "0001");
  driveLogic.put(2, "0011");
  driveLogic.put(3, "0010");
  driveLogic.put(4, "0110");
  driveLogic.put(5, "0100");
  driveLogic.put(6, "1100");
  driveLogic.put(7, "1000");

  System.out.print("Driving motor");
  while (true) {
   for (int i = 1; i < driveLogic.size(); i++) {

    String grayCode = (String) driveLogic.get(i);
    setPin(pina,grayCode.charAt(0));
    setPin(pinb,grayCode.charAt(1));
    setPin(pinc,grayCode.charAt(2));
    setPin(pind,grayCode.charAt(3));

    Thread.sleep(10);
   }
  }

 }

 /**
  * 
  * Sets the passed pin Low or High depending on the passed value.
  * 
  * @param pin
  * @param value
  */
 private static void setPin(GpioPinDigitalOutput pin,char value){
  if (value == '0') {

   pin.low();
  } else {

   pin.high();
  }
 }

}

3 comments:

Fruitbat said...

Pretty cool you can do it in java now. Think a xmas money splurge on components might be in order :-)

Robert Savage said...

Very cool demonstration! The Pi4J project is looking at also adding some device abstractions .. maybe stepper motor would be a good target to add.

city said...

thanks for sharing.