Search in sources :

Example 16 with RoboContext

use of com.robo4j.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 = Thread.currentThread().getContextClassLoader().getResourceAsStream("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.util.SystemUtil) IOException(java.io.IOException) RoboContext(com.robo4j.RoboContext) RoboBuilder(com.robo4j.RoboBuilder) RoboBuilderException(com.robo4j.RoboBuilderException) InputStream(java.io.InputStream) RoboReference(com.robo4j.RoboReference) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext)

Example 17 with RoboContext

use of com.robo4j.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(MtkGPSUnit.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.RoboBuilder) RoboContext(com.robo4j.RoboContext) GPSEvent(com.robo4j.hw.rpi.gps.GPSEvent)

Example 18 with RoboContext

use of com.robo4j.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, GyroAction.CONTINUOUS, new Tuple3f(1.0f, 1.0f, 1.0f)));
    System.out.println("Will report angular changes indefinitely.\nPress <Enter> to quit!");
    System.in.read();
}
Also used : Tuple3f(com.robo4j.math.geometry.Tuple3f) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext)

Example 19 with RoboContext

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

the class AdafruitAlphanumericUnitExample method main.

public static void main(String[] args) throws Exception {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    InputStream settings = AdafruitBiColor24BackpackExample.class.getClassLoader().getResourceAsStream("alphanumericexample.xml");
    RoboContext ctx = new RoboBuilder().add(settings).build();
    ctx.start();
    RoboReference<AlphaNumericMessage> alphaUnit = ctx.getReference("alphanumeric");
    AtomicInteger textPosition = new AtomicInteger();
    executor.scheduleAtFixedRate(() -> {
        if (textPosition.getAndIncrement() >= MESSAGE.length - 1) {
            textPosition.set(0);
        }
        alphaUnit.sendMessage(AlphaNumericMessage.MESSAGE_CLEAR);
        byte currentChar = MESSAGE[textPosition.get()];
        adjustBuffer(currentChar);
        alphaUnit.sendMessage(new AlphaNumericMessage(BackpackMessageCommand.PAINT, BUFFER.clone(), new boolean[4], 0));
        alphaUnit.sendMessage(AlphaNumericMessage.MESSAGE_DISPLAY);
    }, 1, 500, TimeUnit.MILLISECONDS);
    System.out.println("Press enter to quit\n");
    System.in.read();
    alphaUnit.sendMessage(AlphaNumericMessage.MESSAGE_CLEAR);
    alphaUnit.sendMessage(AlphaNumericMessage.MESSAGE_DISPLAY);
    executor.shutdown();
    ctx.shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InputStream(java.io.InputStream) RoboContext(com.robo4j.RoboContext) RoboBuilder(com.robo4j.RoboBuilder)

Example 20 with RoboContext

use of com.robo4j.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;
    InputStream settings;
    switch(args.length) {
        case 1:
            settings = Files.newInputStream(Paths.get(args[0]));
            break;
        case 3:
            startAngle = Float.parseFloat(args[0]);
            range = Float.parseFloat(args[1]);
            step = Float.parseFloat(args[2]);
        default:
            settings = ServoUnitExample.class.getClassLoader().getResourceAsStream("lidarexample.xml");
    }
    Configuration controllerConfiguration = new ConfigurationBuilder().addFloat(LaserScannerTestController.CONFIG_KEY_START_ANGLE, startAngle).addFloat(LaserScannerTestController.CONFIG_KEY_RANGE, range).addFloat(LaserScannerTestController.CONFIG_KEY_STEP, step).build();
    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();
    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<Float> tiltServo = ctx.getReference("laserscanner.tilt");
    tiltServo.sendMessage(Float.valueOf(0));
    RoboReference<String> reference = ctx.getReference("controller");
    ctx.start();
    System.out.println("Starting scanning for ever\nPress <Enter> to quit");
    reference.sendMessage("scan");
    System.in.read();
}
Also used : ConfigurationBuilder(com.robo4j.configuration.ConfigurationBuilder) Configuration(com.robo4j.configuration.Configuration) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext)

Aggregations

RoboContext (com.robo4j.RoboContext)50 RoboBuilder (com.robo4j.RoboBuilder)33 Test (org.junit.jupiter.api.Test)20 InputStream (java.io.InputStream)17 Configuration (com.robo4j.configuration.Configuration)10 ConfigurationBuilder (com.robo4j.configuration.ConfigurationBuilder)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 FileInputStream (java.io.FileInputStream)5 RoboReference (com.robo4j.RoboReference)4 RoboBuilderException (com.robo4j.RoboBuilderException)3 ServerPathConfig (com.robo4j.socket.http.units.ServerPathConfig)3 List (java.util.List)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 StringConsumer (com.robo4j.StringConsumer)2 BiColor (com.robo4j.hw.rpi.i2c.adafruitbackpack.BiColor)2 DataEvent3f (com.robo4j.hw.rpi.imu.bno.DataEvent3f)2 HttpMethod (com.robo4j.socket.http.HttpMethod)2 CameraMessage (com.robo4j.socket.http.codec.CameraMessage)2 HttpPathMethodDTO (com.robo4j.socket.http.dto.HttpPathMethodDTO)2