Advertisement
savageautomate

Raspberry Pi - GPIO Stepper Motor Example

Dec 31st, 2012
41,915
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * **********************************************************************
  3.  * ORGANIZATION  :  Pi4J
  4.  * PROJECT       :  Pi4J :: Java Examples
  5.  * FILENAME      :  StepperMotorGpioExample.java  
  6.  *
  7.  * This file is part of the Pi4J project. More information about
  8.  * this project can be found here:  http://www.pi4j.com/
  9.  * **********************************************************************
  10.  * Copyright (C) 2012 Pi4J
  11.  * Licensed under the Apache License, Version 2.0 (the "License");
  12.  * you may not use this file except in compliance with the License.
  13.  * You may obtain a copy of the License at
  14.  *
  15.  *      http://www.apache.org/licenses/LICENSE-2.0
  16.  *
  17.  * Unless required by applicable law or agreed to in writing, software
  18.  * distributed under the License is distributed on an "AS IS" BASIS,
  19.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20.  * See the License for the specific language governing permissions and
  21.  * limitations under the License.
  22.  */
  23. import com.pi4j.component.motor.impl.GpioStepperMotorComponent;
  24. import com.pi4j.io.gpio.GpioController;
  25. import com.pi4j.io.gpio.GpioFactory;
  26. import com.pi4j.io.gpio.GpioPinDigitalOutput;
  27. import com.pi4j.io.gpio.PinState;
  28. import com.pi4j.io.gpio.RaspiPin;
  29.  
  30. /**
  31.  * This example code demonstrates how to control a stepper motor
  32.  * using the GPIO pins on the Raspberry Pi.  
  33.  *
  34.  * @author Robert Savage
  35.  */
  36. public class StepperMotorGpioExample
  37. {
  38.     public static void main(String[] args) throws InterruptedException
  39.     {
  40.         System.out.println("<--Pi4J--> GPIO Stepper Motor Example ... started.");
  41.        
  42.         // create gpio controller
  43.         final GpioController gpio = GpioFactory.getInstance();
  44.        
  45.         // provision gpio pins #00 to #03 as output pins and ensure in LOW state
  46.         final GpioPinDigitalOutput[] pins = {
  47.                 gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00, PinState.LOW),
  48.                 gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, PinState.LOW),
  49.                 gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02, PinState.LOW),
  50.                 gpio.provisionDigitalOutputPin(RaspiPin.GPIO_03, PinState.LOW)};
  51.  
  52.         // this will ensure that the motor is stopped when the program terminates
  53.         gpio.setShutdownOptions(true, PinState.LOW, pins);
  54.        
  55.         // create motor component
  56.         GpioStepperMotorComponent motor = new GpioStepperMotorComponent(pins);
  57.  
  58.         // @see http://www.lirtex.com/robotics/stepper-motor-controller-circuit/
  59.         //      for additional details on stepping techniques
  60.  
  61.         // create byte array to demonstrate a single-step sequencing
  62.         // (This is the most basic method, turning on a single electromagnet every time.
  63.         //  This sequence requires the least amount of energy and generates the smoothest movement.)
  64.         byte[] single_step_sequence = new byte[4];
  65.         single_step_sequence[0] = (byte) 0b0001;  
  66.         single_step_sequence[1] = (byte) 0b0010;
  67.         single_step_sequence[2] = (byte) 0b0100;
  68.         single_step_sequence[3] = (byte) 0b1000;
  69.  
  70.         // create byte array to demonstrate a double-step sequencing
  71.         // (In this method two coils are turned on simultaneously.  This method does not generate
  72.         //  a smooth movement as the previous method, and it requires double the current, but as
  73.         //  return it generates double the torque.)
  74.         byte[] double_step_sequence = new byte[4];
  75.         double_step_sequence[0] = (byte) 0b0011;  
  76.         double_step_sequence[1] = (byte) 0b0110;
  77.         double_step_sequence[2] = (byte) 0b1100;
  78.         double_step_sequence[3] = (byte) 0b1001;
  79.        
  80.         // create byte array to demonstrate a half-step sequencing
  81.         // (In this method two coils are turned on simultaneously.  This method does not generate
  82.         //  a smooth movement as the previous method, and it requires double the current, but as
  83.         //  return it generates double the torque.)
  84.         byte[] half_step_sequence = new byte[8];
  85.         half_step_sequence[0] = (byte) 0b0001;  
  86.         half_step_sequence[1] = (byte) 0b0011;
  87.         half_step_sequence[2] = (byte) 0b0010;
  88.         half_step_sequence[3] = (byte) 0b0110;
  89.         half_step_sequence[4] = (byte) 0b0100;
  90.         half_step_sequence[5] = (byte) 0b1100;
  91.         half_step_sequence[6] = (byte) 0b1000;
  92.         half_step_sequence[7] = (byte) 0b1001;
  93.  
  94.         // define stepper parameters before attempting to control motor
  95.         // anything lower than 2 ms does not work for my sample motor using single step sequence
  96.         motor.setStepInterval(2);  
  97.         motor.setStepSequence(single_step_sequence);
  98.  
  99.         // There are 32 steps per revolution on my sample motor,
  100.         // and inside is a ~1/64 reduction gear set.
  101.         // Gear reduction is actually: (32/9)/(22/11)x(26/9)x(31/10)=63.683950617
  102.         // This means is that there are really 32*63.683950617 steps per revolution
  103.         // =  2037.88641975 ~ 2038 steps!
  104.         motor.setStepsPerRevolution(2038);
  105.  
  106.         // test motor control : STEPPING FORWARD
  107.         System.out.println("   Motor FORWARD for 2038 steps.");
  108.         motor.step(2038);
  109.         System.out.println("   Motor STOPPED for 2 seconds.");
  110.         Thread.sleep(2000);
  111.        
  112.         // test motor control : STEPPING REVERSE
  113.         System.out.println("   Motor REVERSE for 2038 steps.");
  114.         motor.step(-2038);
  115.         System.out.println("   Motor STOPPED for 2 seconds.");
  116.         Thread.sleep(2000);
  117.  
  118.         // test motor control : ROTATE FORWARD
  119.         System.out.println("   Motor FORWARD for 2 revolutions.");
  120.         motor.rotate(2);
  121.         System.out.println("   Motor STOPPED for 2 seconds.");
  122.         Thread.sleep(2000);
  123.  
  124.         // test motor control : ROTATE REVERSE
  125.         System.out.println("   Motor REVERSE for 2 revolutions.");
  126.         motor.rotate(-2);
  127.         System.out.println("   Motor STOPPED for 2 seconds.");
  128.         Thread.sleep(2000);
  129.        
  130.         // test motor control : TIMED FORWARD
  131.         System.out.println("   Motor FORWARD for 5 seconds.");
  132.         motor.forward(5000);
  133.         System.out.println("   Motor STOPPED for 2 seconds.");
  134.         Thread.sleep(2000);
  135.        
  136.         // test motor control : TIMED REVERSE
  137.         System.out.println("   Motor REVERSE for 5 seconds.");
  138.         motor.reverse(5000);
  139.         System.out.println("   Motor STOPPED for 2 seconds.");
  140.         Thread.sleep(2000);
  141.        
  142.         // test motor control : ROTATE FORWARD with different timing and sequence
  143.         System.out.println("   Motor FORWARD with slower speed and higher torque for 1 revolution.");
  144.         motor.setStepSequence(double_step_sequence);
  145.         motor.setStepInterval(10);
  146.         motor.rotate(1);
  147.         System.out.println("   Motor STOPPED.");
  148.  
  149.         // final stop to ensure no motor activity
  150.         motor.stop();
  151.     }
  152. }
Advertisement
RAW Paste Data Copied
Advertisement