halApi.ArmInterface Guide - PSemFlugSim/Base GitHub Wiki

Home

This is an example on how to use the de.gymolching.fsb.halApi.ArmInterface class.


import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import de.gymolching.fsb.hal.ArmFactory;
import de.gymolching.fsb.halApi.ArmInterface;
import java.io.IOException;

/**
 * @author sschaeffner
 */
public class ArmExample {

    private int pos = 0;
    private boolean running = true;

    public void start() {

        //get pi4j GpioController instance from GpioFactory
        final GpioController gpio = GpioFactory.getInstance();

        //get ArmFactory instance
        final ArmFactory armFactory = ArmFactory.getInstance(gpio);

        //initialize arm
        ArmInterface arm1 = null;
        try {
            arm1 = armFactory.provideArm(0);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        }

        //run thread to check the arm's position every 100ms
        final ArmInterface finalArm = arm1;
        new Thread(() -> {
            //last position the thread has received from the arm
            int lastPos = 0;
            while (running) {
                //get arm's position
                this.pos = finalArm.getPosition();

                //only output on position change
                if (pos != lastPos) {
                    lastPos = pos;
                    System.out.print(System.lineSeparator() + "pos=" + pos);
                }

                sleep(100);
            }
        }, "reader").start();

        while (running) {
            //move arm to starting position
            arm1.moveToStartingPosition();
            System.out.println("STARTING POSITION");
            sleep(1000);

            //move arm to position +25
            arm1.setSpeed(100);
            arm1.startForward();
            while (pos < 25 && Launcher.getInstance().isRunning()) {
                sleep(0);
            }
            arm1.stop(false);
            sleep(2000);
        }

    }

    private void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}