Search in sources :

Example 36 with RoboBuilder

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

the class AdafruitBiColorMatrix8x8BackpackExample method main.

public static void main(String[] args) throws Exception {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    InputStream settings = AdafruitBiColorMatrix8x8BackpackExample.class.getClassLoader().getResourceAsStream("matrix8x8example.xml");
    RoboContext ctx = new RoboBuilder().add(settings).build();
    ctx.start();
    System.out.println("State after start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    RoboReference<DrawMessage> matrixUnit = ctx.getReference("matrix");
    AtomicInteger position = new AtomicInteger();
    executor.scheduleAtFixedRate(() -> {
        if (position.get() > 7) {
            position.set(0);
        }
        matrixUnit.sendMessage(DrawMessage.MESSAGE_CLEAR);
        matrixUnit.sendMessage(new DrawMessage(BackpackMessageCommand.PAINT, new short[] { (short) position.get() }, new short[] { (short) position.getAndIncrement() }, new BiColor[] { BiColor.getByValue(position.get() % 3 + 1) }));
    }, 2, 1, TimeUnit.SECONDS);
    System.out.println("Press enter to quit\n");
    System.in.read();
    executor.shutdown();
    ctx.shutdown();
}
Also used : BiColor(com.robo4j.hw.rpi.i2c.adafruitbackpack.BiColor) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InputStream(java.io.InputStream) RoboContext(com.robo4j.RoboContext) RoboBuilder(com.robo4j.RoboBuilder)

Example 37 with RoboBuilder

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

the class RoboClawUnitExample method main.

public static void main(String[] args) throws RoboBuilderException, FileNotFoundException {
    InputStream settings = ServoUnitExample.class.getClassLoader().getResourceAsStream("roboclawexample.xml");
    if (args.length != 1) {
        System.out.println("No file specified, using default roboclawexample.xml");
    } else {
        settings = new FileInputStream(args[0]);
    }
    RoboBuilder builder = new RoboBuilder();
    if (settings == null) {
        System.out.println("Could not find the settings for  test!");
        System.exit(2);
    }
    builder.add(settings);
    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));
    String lastCommand = "";
    Scanner scanner = new Scanner(System.in);
    System.out.println("Type the roboclaw unit to control and the speed [-1, 1] and angular direction[-180, 180]. For example:\ntank 1 0\nType q and enter to quit!\n");
    while (!"q".equals(lastCommand = scanner.nextLine())) {
        lastCommand = lastCommand.trim();
        String[] split = lastCommand.split(" ");
        if (split.length != 3) {
            System.out.println("Could not parse " + lastCommand + ". Please try again!");
            continue;
        }
        RoboReference<MotionEvent> servoRef = ctx.getReference(split[0]);
        if (servoRef == null) {
            System.out.println("Could not find any robo unit named " + split[0] + ". Please try again!");
            continue;
        }
        try {
            float speed = Float.parseFloat(split[1]);
            float direction = (float) Math.toRadians(Float.parseFloat(split[2]));
            servoRef.sendMessage(new MotionEvent(speed, direction));
        } catch (Exception e) {
            System.out.println("Could not parse " + split[1] + " as a float number. Error message was: " + e.getMessage() + ". Please try again!");
            continue;
        }
    }
    ctx.shutdown();
    scanner.close();
}
Also used : Scanner(java.util.Scanner) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) ServoUnitExample(com.robo4j.units.rpi.pwm.ServoUnitExample) FileInputStream(java.io.FileInputStream) RoboBuilderException(com.robo4j.RoboBuilderException) FileNotFoundException(java.io.FileNotFoundException)

Example 38 with RoboBuilder

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

the class RemoteSystemBuilderTest method builderRemoteSystemTest.

/**
 * Building {@link com.robo4j.RoboSystem} by xml declarative and programmatic approach
 *
 * @throws RoboBuilderException configuration exception
 */
@Test
void builderRemoteSystemTest() throws RoboBuilderException {
    final RoboBuilder xmlBuilder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteMessageReceiverAckSystem.xml"));
    final RoboContext xmlContext = xmlBuilder.build();
    // @formatter:off
    final Configuration systemConf = new ConfigurationBuilder().addInteger(RoboBuilder.KEY_SCHEDULER_POOL_SIZE, 2).addInteger(RoboBuilder.KEY_WORKER_POOL_SIZE, 2).addInteger(RoboBuilder.KEY_BLOCKING_POOL_SIZE, 6).addBuilder(RoboBuilder.KEY_CONFIGURATION_SERVER, new ConfigurationBuilder().addString("hostname", "localhost").addInteger("port", 0)).addBuilder(RoboBuilder.KEY_CONFIGURATION_EMITTER, new ConfigurationBuilder().addString("multicastAddress", "238.12.15.254").addInteger("port", 0x0FFE).addInteger("heartBeatInterval", 250).addBoolean("enabled", true).addBuilder(RoboBuilder.KEY_CONFIGURATION_EMITTER_METADATA, new ConfigurationBuilder().addString("name", "StringMessageConsumer").addString("class", "MessageConsumer"))).build();
    final RoboContext programmaticContext = new RoboBuilder("9", systemConf).build();
    // @formatter:on
    assertEquals(xmlContext.getConfiguration(), systemConf);
    assertEquals(xmlContext.getId(), programmaticContext.getId());
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 39 with RoboBuilder

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

the class GripperSonicTankPlatformExample method main.

public static void main(String[] args) throws Exception {
    final String robo4jConfig = "robo4jGripperSonicTankPlatform.xml";
    InputStream settings = GripperSonicTankPlatformExample.class.getClassLoader().getResourceAsStream(robo4jConfig);
    if (args.length != 1) {
        System.out.println(String.format("No file specified, using default %s", robo4jConfig));
    } else {
        settings = new FileInputStream(args[0]);
    }
    final RoboBuilder builder = new RoboBuilder();
    if (settings == null) {
        System.out.println("Could not find the settings for test!");
        System.exit(2);
    }
    builder.add(settings);
    RoboContext system = builder.build();
    System.out.println("State before start:");
    System.out.println(SystemUtil.printStateReport(system));
    system.start();
    RoboReference<String> lcd = system.getReference("lcd");
    lcd.sendMessage("Robo4J.io");
    RoboReference<String> sonicSensor = system.getReference("sonicSensor");
    sonicSensor.sendMessage("start");
    shutdown(system);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) FileInputStream(java.io.FileInputStream)

Example 40 with RoboBuilder

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

the class InfraSensorExample method main.

public static void main(String[] args) throws Exception {
    final String robo4jConfig = "robo4jInfraExample.xml";
    InputStream settings = InfraSensorExample.class.getClassLoader().getResourceAsStream(robo4jConfig);
    if (args.length != 1) {
        System.out.println(String.format("No file specified, using default %s", robo4jConfig));
    } else {
        settings = new FileInputStream(args[0]);
    }
    final RoboBuilder builder = new RoboBuilder();
    if (settings == null) {
        System.out.println("Could not find the settings for test!");
        System.exit(2);
    }
    builder.add(settings);
    RoboContext system = builder.build();
    System.out.println("State before start:");
    System.out.println(SystemUtil.printStateReport(system));
    system.start();
    RoboReference<String> lcd = system.getReference("lcd");
    lcd.sendMessage("Robo4J.io");
    RoboReference<String> infraSensor = system.getReference("infraSensor");
    infraSensor.sendMessage("start");
    shutdown(system);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) FileInputStream(java.io.FileInputStream)

Aggregations

RoboBuilder (com.robo4j.RoboBuilder)45 RoboContext (com.robo4j.RoboContext)33 InputStream (java.io.InputStream)20 Configuration (com.robo4j.configuration.Configuration)18 ConfigurationBuilder (com.robo4j.configuration.ConfigurationBuilder)17 Test (org.junit.jupiter.api.Test)13 FileInputStream (java.io.FileInputStream)5 HttpPathConfigJsonBuilder (com.robo4j.socket.http.util.HttpPathConfigJsonBuilder)4 RoboBuilderException (com.robo4j.RoboBuilderException)3 IOException (java.io.IOException)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 CameraMessage (com.robo4j.socket.http.codec.CameraMessage)2 BnoRequest (com.robo4j.units.rpi.imu.BnoRequest)2 FileNotFoundException (java.io.FileNotFoundException)2 Path (java.nio.file.Path)2 Scanner (java.util.Scanner)2