Search in sources :

Example 1 with RoboContext

use of com.robo4j.core.RoboContext in project robo4j by Robo4J.

the class ServoUnitExample method main.

public static void main(String[] args) throws RoboBuilderException {
    RoboBuilder builder = new RoboBuilder();
    InputStream settings = ServoUnitExample.class.getClassLoader().getResourceAsStream("servoexample.xml");
    if (settings == null) {
        System.out.println("Could not find the settings for the ServoUnitExample!");
        System.exit(2);
    }
    builder.add(settings);
    RoboContext ctx = builder.build();
    RoboReference<Float> panRef = ctx.getReference("pan");
    RoboReference<Float> tiltRef = ctx.getReference("tilt");
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            float panDirection = 1.0f;
            while (!stop) {
                for (int tiltStep = 0; tiltStep < TILT_STEPS; tiltStep++) {
                    // Just move the tilt a quarter of max positive.
                    float tilt = tiltStep / (TILT_STEPS * 4.0f);
                    tiltRef.sendMessage(tilt);
                    for (int panStep = 0; panStep < PAN_STEPS; panStep++) {
                        if (stop) {
                            break;
                        }
                        float pan = (panStep * 2.0f / PAN_STEPS - 1.0f) * panDirection;
                        panRef.sendMessage(pan);
                        sleep(50);
                    }
                    panDirection *= -1;
                }
            }
        }
    });
    thread.setDaemon(true);
    thread.start();
    System.out.println("Press enter to quit!");
    try {
        System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
    stop = true;
    ctx.shutdown();
}
Also used : InputStream(java.io.InputStream) RoboBuilder(com.robo4j.core.RoboBuilder) RoboContext(com.robo4j.core.RoboContext) IOException(java.io.IOException)

Example 2 with RoboContext

use of com.robo4j.core.RoboContext in project robo4j by Robo4J.

the class AccelerometerExample method main.

public static void main(String[] args) throws RoboBuilderException, IOException {
    RoboBuilder builder = new RoboBuilder();
    InputStream settings = RoboClassLoader.getInstance().getResource("accelerometerexample.xml");
    if (settings == null) {
        System.out.println("Could not find the settings for the GyroExample!");
        System.exit(2);
    }
    builder.add(settings);
    builder.add(AccelerometerProcessor.class, ID_PROCESSOR);
    RoboContext ctx = builder.build();
    System.out.println("State before start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    ctx.start();
    System.out.println("State after start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    RoboReference<AccelerometerRequest> accelerometer = ctx.getReference("accelerometer");
    RoboReference<AccelerometerEvent> processor = ctx.getReference(ID_PROCESSOR);
    System.out.println("Press enter to start!");
    System.in.read();
    accelerometer.sendMessage(new AccelerometerRequest(processor, true, (Float3D) -> true));
    System.out.println("Will report angular changes indefinitely.\nPress enter to quit!");
    System.in.read();
}
Also used : SystemUtil(com.robo4j.core.util.SystemUtil) RoboClassLoader(com.robo4j.core.client.util.RoboClassLoader) RoboReference(com.robo4j.core.RoboReference) RoboBuilder(com.robo4j.core.RoboBuilder) IOException(java.io.IOException) RoboContext(com.robo4j.core.RoboContext) RoboBuilderException(com.robo4j.core.RoboBuilderException) InputStream(java.io.InputStream) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.core.RoboBuilder) RoboContext(com.robo4j.core.RoboContext)

Example 3 with RoboContext

use of com.robo4j.core.RoboContext in project robo4j by Robo4J.

the class GPSExample method main.

public static void main(String[] args) throws RoboBuilderException, IOException {
    RoboBuilder builder = new RoboBuilder();
    builder.add(GPSUnit.class, ID_GPS);
    builder.add(GPSProcessor.class, ID_PROCESSOR);
    RoboContext ctx = builder.build();
    System.out.println("State before start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    ctx.start();
    System.out.println("State after start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    RoboReference<GPSRequest> gps = ctx.getReference(ID_GPS);
    RoboReference<GPSEvent> processor = ctx.getReference(ID_PROCESSOR);
    System.out.println("Press enter to start requesting events, then press enter again to stop requesting events!");
    System.in.read();
    System.out.println("Requesting GPS events! Press enter to stop!");
    gps.sendMessage(new GPSRequest(processor, Operation.REGISTER));
    System.in.read();
    System.out.println("Ending requesting GPS events...");
    gps.sendMessage(new GPSRequest(processor, Operation.UNREGISTER));
    // Note that we can still get a few more events after this, and that is
    // quite fine. ;)
    System.out.println("All done! Press enter to quit!");
    System.in.read();
    System.out.println("Exiting! Bye!");
    ctx.shutdown();
    // Seems Pi4J keeps an executor with non-daemon threads around after
    // we've used the serial port, even after closing it. :/
    System.exit(0);
}
Also used : RoboBuilder(com.robo4j.core.RoboBuilder) RoboContext(com.robo4j.core.RoboContext) GPSEvent(com.robo4j.hw.rpi.serial.gps.GPSEvent)

Example 4 with RoboContext

use of com.robo4j.core.RoboContext in project robo4j by Robo4J.

the class GyroExample method main.

public static void main(String[] args) throws RoboBuilderException, IOException {
    RoboBuilder builder = new RoboBuilder();
    InputStream settings = GyroExample.class.getClassLoader().getResourceAsStream("gyroexample.xml");
    if (settings == null) {
        System.out.println("Could not find the settings for the GyroExample!");
        System.exit(2);
    }
    builder.add(settings);
    builder.add(GyroProcessor.class, ID_PROCESSOR);
    RoboContext ctx = builder.build();
    System.out.println("State before start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    ctx.start();
    System.out.println("State after start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    RoboReference<GyroRequest> gyro = ctx.getReference("gyro");
    RoboReference<GyroEvent> processor = ctx.getReference(ID_PROCESSOR);
    System.out.println("Let the gyro unit be absolutely still, then press enter to calibrate and start!");
    System.in.read();
    gyro.sendMessage(new GyroRequest(processor, true, true, new Float3D(1.0f, 1.0f, 1.0f)));
    System.out.println("Will report angular changes indefinitely.\nPress enter to quit!");
    System.in.read();
}
Also used : Float3D(com.robo4j.math.geometry.Float3D) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.core.RoboBuilder) RoboContext(com.robo4j.core.RoboContext)

Example 5 with RoboContext

use of com.robo4j.core.RoboContext in project robo4j by Robo4J.

the class LaserScannerExample method main.

public static void main(String[] args) throws RoboBuilderException, IOException {
    float startAngle = -45.0f;
    float range = 90.0f;
    float step = 1.0f;
    if (args.length > 0) {
        startAngle = Float.parseFloat(args[0]);
        if (args.length > 1) {
            range = Float.parseFloat(args[1]);
            if (args.length > 2) {
                step = Float.parseFloat(args[2]);
            }
        }
    }
    Configuration controllerConfiguration = ConfigurationFactory.createEmptyConfiguration();
    controllerConfiguration.setFloat(LaserScannerTestController.CONFIG_KEY_START_ANGLE, startAngle);
    controllerConfiguration.setFloat(LaserScannerTestController.CONFIG_KEY_RANGE, range);
    controllerConfiguration.setFloat(LaserScannerTestController.CONFIG_KEY_STEP, step);
    System.out.println(String.format("Running scans with startAngle=%2.1f, range=%2.1f and step=%2.1f", startAngle, range, step));
    RoboBuilder builder = new RoboBuilder();
    InputStream settings = ServoUnitExample.class.getClassLoader().getResourceAsStream("lidarexample.xml");
    if (settings == null) {
        System.out.println("Could not find the settings for the LaserScannerExample!");
        System.exit(2);
    }
    builder.add(settings).add(LaserScannerTestController.class, controllerConfiguration, "controller").add(LaserScanProcessor.class, "processor");
    RoboContext ctx = builder.build();
    RoboReference<String> reference = ctx.getReference("controller");
    System.out.println("Starting scanning for ever\nPress enter to quit");
    reference.sendMessage("scan");
    System.in.read();
}
Also used : Configuration(com.robo4j.core.configuration.Configuration) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.core.RoboBuilder) RoboContext(com.robo4j.core.RoboContext) ServoUnitExample(com.robo4j.units.rpi.pwm.ServoUnitExample)

Aggregations

RoboBuilder (com.robo4j.core.RoboBuilder)9 RoboContext (com.robo4j.core.RoboContext)9 InputStream (java.io.InputStream)6 RoboBuilderException (com.robo4j.core.RoboBuilderException)2 IOException (java.io.IOException)2 RoboReference (com.robo4j.core.RoboReference)1 RoboClassLoader (com.robo4j.core.client.util.RoboClassLoader)1 Configuration (com.robo4j.core.configuration.Configuration)1 SystemUtil (com.robo4j.core.util.SystemUtil)1 GPSEvent (com.robo4j.hw.rpi.serial.gps.GPSEvent)1 Float3D (com.robo4j.math.geometry.Float3D)1 ServoUnitExample (com.robo4j.units.rpi.pwm.ServoUnitExample)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Scanner (java.util.Scanner)1