use of com.robo4j.RoboContext 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();
}
use of com.robo4j.RoboContext 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();
}
use of com.robo4j.RoboContext 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());
}
use of com.robo4j.RoboContext 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);
}
use of com.robo4j.RoboContext 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);
}
Aggregations