JavaFX on Raspberry Pi: Some fun with stepper motor

Recently had some fun when controlling my stepper motor with my Raspberry Pi.

The motor: a 28BYJ-48 with ULN2003 driver unit:

DSC_5687

Cool: Pi4J has a ready to use GpioStepperMotorComponent.

Let’s assume this architecture:

StepperMotorControl2

Thus we start with the StepperMotorAdapter using this component.

First some helpfull defines:

private final int oneRevolution = 2038;
private final int quarterRevolution = oneRevolution / 4;
private final int halfRevolution = oneRevolution / 2;
private final int oneDegreeRevolution = oneRevolution / 360;

then we need to get the GpioController and provide 4 GPIO pins set to digital output mode

gpio = GpioFactory.getInstance();

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)};

and we need a byte array to define a step sequence e.g. a single motor step sequence (every coil is turned on once per step):

singleStepSequence = new byte[4];
    singleStepSequence[0] = (byte) 0b0001;
    singleStepSequence[1] = (byte) 0b0010;
    singleStepSequence[2] = (byte) 0b0100;
    singleStepSequence[3] = (byte) 0b1000;

Now we can create the motor:

motor = new GpioStepperMotorComponent(pins);
        motor.setStepSequence(currentStepSequence);
        motor.setStepsPerRevolution(oneRevolution);

and some methods to control the motor:

public void stop() {
    motor.stop();
}

public void forward() {
    motor.reverse();
}

public void backward() {
    motor.forward();
}

public void oneStepBackward() {
    motor.step(oneDegreeRevolution);
}

public void oneStepForward() {
    motor.step(-oneDegreeRevolution);
}

public void halfRevolutionBackward() {
    motor.step(halfRevolution);
}

public void halfRevolutionForward() {
    motor.step(-halfRevolution);
}

public void quarterRevolutionBackward() {
    motor.step(quarterRevolution);
}

public void quarterRevolutionForward() {
    motor.step(-quarterRevolution);
}

This adapter is used by the StepperMotorControl which implements handlers like:

adjustBackwardButton.setOnMousePressed(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent t) {
        stepperMotorAdapter.backward();
    }
});
adjustBackwardButton.setOnMouseReleased(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent t) {
        stepperMotorAdapter.stop();
    }
});
adjustForwardButton.setOnMousePressed(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent t) {
        stepperMotorAdapter.forward();
    }
});
adjustForwardButton.setOnMouseReleased(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent t) {
        stepperMotorAdapter.stop();
    }
});

and provides FXML usable methods like:

@FXML
public void backward() {
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            stepperMotorAdapter.backward();
        }
    });
}

@FXML
public void forward() {
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            stepperMotorAdapter.forward();
        }
    });
}

@FXML
public void stop() {
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            stepperMotorAdapter.stop();
        }
    });
}

Finally this is the JavaFX UI running on the RasPi:
StepperMotorControl

Get the complete code here:

https://bitbucket.org/Jerady/raspberry-cpio-control-fx

Icons on the buttons are provided by:
FontAwesomeFX

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 thought on “JavaFX on Raspberry Pi: Some fun with stepper motor”