Search in sources :

Example 1 with RoboBuilderException

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

the class CalibrationUtility method main.

public static void main(String[] args) throws RoboBuilderException, FileNotFoundException {
    InputStream settings = ServoUnitExample.class.getClassLoader().getResourceAsStream("calibration.xml");
    if (args.length != 1) {
        System.out.println("No file specified, using default calibration.xml");
    } else {
        settings = new FileInputStream(args[0]);
    }
    RoboBuilder builder = new RoboBuilder();
    if (settings == null) {
        System.out.println("Could not find the settings for servo calibration 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 servo to control and how much to move the servo, between -1 and 1. For example:\npan -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 != 2) {
            System.out.println("Could not parse " + lastCommand + ". Please try again!");
            continue;
        }
        RoboReference<Float> 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 value = Float.parseFloat(split[1]);
            servoRef.sendMessage(value);
        } 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) FileInputStream(java.io.FileInputStream) RoboBuilderException(com.robo4j.RoboBuilderException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with RoboBuilderException

use of com.robo4j.RoboBuilderException 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 3 with RoboBuilderException

use of com.robo4j.RoboBuilderException 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)

Aggregations

RoboBuilder (com.robo4j.RoboBuilder)3 RoboBuilderException (com.robo4j.RoboBuilderException)3 RoboContext (com.robo4j.RoboContext)3 InputStream (java.io.InputStream)3 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 Scanner (java.util.Scanner)2 RoboReference (com.robo4j.RoboReference)1 ServoUnitExample (com.robo4j.units.rpi.pwm.ServoUnitExample)1 SystemUtil (com.robo4j.util.SystemUtil)1 IOException (java.io.IOException)1