14. ★ step45.java
import com.pi4j.component.motor.impl.GpioStepperMotorComponent;
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 step45 {
public static void main(String[] args) throws InterruptedException {
System.out.println("<--Pi4J--> GPIO Stepper Motor Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// provision gpio pins #00 to #03 as output pins and ensure in LOW state
final GpioPinDigitalOutput[] pins = {
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00, PinState.LOW),
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, PinState.LOW),
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02, PinState.LOW),
gpio.provisionDigitalOutputPin(RaspiPin.GPIO_03, PinState.LOW)};
實作過程 - Coding
(step45.java)
14
15. ★ step45.java (cont’d)
// this will ensure that the motor is stopped when the program terminates
gpio.setShutdownOptions(true, PinState.LOW, pins);
// create motor component
GpioStepperMotorComponent motor = new GpioStepperMotorComponent(pins);
// @see http://www.lirtex.com/robotics/stepper-motor-controller-circuit/
// for additional details on stepping techniques
// create byte array to demonstrate a double-step sequencing
// (In this method two coils are turned on simultaneously. This method does not generate
// a smooth movement as the previous method, and it requires double the current, but as
// return it generates double the torque.)
byte[] double_step_sequence = new byte[4];
double_step_sequence[0] = (byte) 0b0011;
double_step_sequence[1] = (byte) 0b0110;
double_step_sequence[2] = (byte) 0b1100;
double_step_sequence[3] = (byte) 0b1001;
// define stepper parameters before attempting to control motor
// anything lower than 2 ms does not work for my sample motor using single step sequence
motor.setStepInterval(1);
motor.setStepSequence(double_step_sequence);
實作過程 - Coding
(step45.java <cont’d>)
15
16. ★ step45.java (cont’d)
// There are 32 steps per revolution on my sample motor, and inside is a ~1/64 reduction gear set.
// Gear reduction is actually: (32/9)/(22/11)x(26/9)x(31/10)=63.683950617
// This means is that there are really 32*63.683950617 steps per revolution = 2037.88641975 ~ 2038 steps!
motor.setStepsPerRevolution(2038);
motor.rotate(0.125);
System.out.println(" Motor STOPPED.");
// final stop to ensure no motor activity
motor.stop();
// stop all GPIO activity/threads by shutting down the GPIO controller
// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
gpio.shutdown();
}
}
實作過程 - Coding
(step45.java <cont’d>)
16
17. change some code...
★ stepm45.java
public class stepm45 {
……………….
motor.rotate(-0.125);
★ step90.java
public class step90 {
……………….
motor.rotate(0.25);
★ stepm90.java
public class stepm90 {
……………….
motor.rotate(-0.25);
實作過程 - Coding
(stepm45/90/m90/180/m180.java)
17
★ step180.java
public class step180 {
……………….
motor.rotate(0.5);
★ stepm180.java
public class step180 {
……………….
motor.rotate(0.5);
19. ★ index.php (cont’d)
function servo180(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","servo180.php",true);
xmlhttp.send();
}
function step45(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","step45.php",true);
xmlhttp.send();
}
function stepm45(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","step-45.php",true);
xmlhttp.send();
}
function step90(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","step90.php",true);
xmlhttp.send();
}
實作過程 - Coding
(index.php <cont’d>)
19
function stepm90(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","step-90.php",true);
xmlhttp.send();
}
function step180(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","step180.php",true);
xmlhttp.send();
}
function stepm180(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","step-180.php",true);
xmlhttp.send();
}
</script>
</html>