Skip to content

Instantly share code, notes, and snippets.

@jabrena
Created September 3, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabrena/65f75c7adc52f5f66edb6d114a9e703a to your computer and use it in GitHub Desktop.
Save jabrena/65f75c7adc52f5f66edb6d114a9e703a to your computer and use it in GitHub Desktop.
Modern syntax for Robots with Java 8+
package example;
import ev3dev.actuators.lego.motors.EV3LargeRegulatedMotor;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import lejos.utility.Delay;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MotorEncoderTest {
Consumer<RegulatedMotor> readMotor = motor -> {
IntStream.rangeClosed(1,50).boxed()
.forEach(i -> {
LOGGER.info("{} {}", i , motor.getTachoCount());
Delay.msDelay(200);
});
};
Consumer<RegulatedMotor> writeMotor = motor -> {
IntStream.rangeClosed(1, 2).boxed()
.forEach(i -> {
LOGGER.info("FORWARD");
motor.forward();
Delay.msDelay(2000);
LOGGER.info("BACKWARD");
motor.backward();
Delay.msDelay(2000);
});
};
Consumer<RegulatedMotor> processTasks = motor -> {
final ExecutorService executor = Executors.newFixedThreadPool(2);
List<CompletableFuture<Void>> motorTasks = Arrays.asList(
CompletableFuture.runAsync(() -> this.readMotor.accept(motor), executor),
CompletableFuture.runAsync(() -> this.writeMotor.accept(motor), executor)).stream()
.collect(Collectors.toList());
motorTasks.stream().forEach(CompletableFuture::join);
executor.shutdown();
motor.stop();
LOGGER.info("End");
};
public static void main(String[] args) throws Exception{
LOGGER.info("MotorEncoderTest");
MotorEncoderTest example = new MotorEncoderTest();
example.processTasks.accept(new EV3LargeRegulatedMotor(MotorPort.A));
System.exit(0);
}
}
@jabrena
Copy link
Author

jabrena commented Sep 3, 2019

2019-09-03 20:17:58 [main] INFO  example.MotorEncoderTest - MotorEncoderTest
2019-09-03 20:18:02 [main] INFO  e.a.lego.motors.BaseRegulatedMotor - Configuring motor connected on Port: A
2019-09-03 20:18:06 [pool-1-thread-2] INFO  example.MotorEncoderTest - FORWARD
2019-09-03 20:18:06 [pool-1-thread-1] INFO  example.MotorEncoderTest - 1 0
2019-09-03 20:18:07 [pool-1-thread-1] INFO  example.MotorEncoderTest - 2 77
2019-09-03 20:18:07 [pool-1-thread-1] INFO  example.MotorEncoderTest - 3 165
2019-09-03 20:18:07 [pool-1-thread-1] INFO  example.MotorEncoderTest - 4 253
2019-09-03 20:18:07 [pool-1-thread-1] INFO  example.MotorEncoderTest - 5 344
2019-09-03 20:18:08 [pool-1-thread-1] INFO  example.MotorEncoderTest - 6 432
2019-09-03 20:18:08 [pool-1-thread-1] INFO  example.MotorEncoderTest - 7 520
2019-09-03 20:18:08 [pool-1-thread-1] INFO  example.MotorEncoderTest - 8 608
2019-09-03 20:18:08 [pool-1-thread-1] INFO  example.MotorEncoderTest - 9 696
2019-09-03 20:18:09 [pool-1-thread-2] INFO  example.MotorEncoderTest - BACKWARD
2019-09-03 20:18:09 [pool-1-thread-1] INFO  example.MotorEncoderTest - 10 714
2019-09-03 20:18:09 [pool-1-thread-1] INFO  example.MotorEncoderTest - 11 621
2019-09-03 20:18:09 [pool-1-thread-1] INFO  example.MotorEncoderTest - 12 532
2019-09-03 20:18:09 [pool-1-thread-1] INFO  example.MotorEncoderTest - 13 446
2019-09-03 20:18:10 [pool-1-thread-1] INFO  example.MotorEncoderTest - 14 352
2019-09-03 20:18:10 [pool-1-thread-1] INFO  example.MotorEncoderTest - 15 264
2019-09-03 20:18:10 [pool-1-thread-1] INFO  example.MotorEncoderTest - 16 176
2019-09-03 20:18:10 [pool-1-thread-1] INFO  example.MotorEncoderTest - 17 78
2019-09-03 20:18:11 [pool-1-thread-2] INFO  example.MotorEncoderTest - FORWARD
2019-09-03 20:18:11 [pool-1-thread-1] INFO  example.MotorEncoderTest - 18 37
2019-09-03 20:18:11 [pool-1-thread-1] INFO  example.MotorEncoderTest - 19 134
2019-09-03 20:18:11 [pool-1-thread-1] INFO  example.MotorEncoderTest - 20 237
2019-09-03 20:18:12 [pool-1-thread-1] INFO  example.MotorEncoderTest - 21 340
2019-09-03 20:18:12 [pool-1-thread-1] INFO  example.MotorEncoderTest - 22 444
2019-09-03 20:18:12 [pool-1-thread-1] INFO  example.MotorEncoderTest - 23 547
2019-09-03 20:18:12 [pool-1-thread-1] INFO  example.MotorEncoderTest - 24 637
2019-09-03 20:18:13 [pool-1-thread-2] INFO  example.MotorEncoderTest - BACKWARD
2019-09-03 20:18:13 [pool-1-thread-1] INFO  example.MotorEncoderTest - 25 733
2019-09-03 20:18:13 [pool-1-thread-1] INFO  example.MotorEncoderTest - 26 691
2019-09-03 20:18:13 [pool-1-thread-1] INFO  example.MotorEncoderTest - 27 603
2019-09-03 20:18:13 [pool-1-thread-1] INFO  example.MotorEncoderTest - 28 517
2019-09-03 20:18:14 [pool-1-thread-1] INFO  example.MotorEncoderTest - 29 428
2019-09-03 20:18:14 [pool-1-thread-1] INFO  example.MotorEncoderTest - 30 342
2019-09-03 20:18:14 [pool-1-thread-1] INFO  example.MotorEncoderTest - 31 256
2019-09-03 20:18:14 [pool-1-thread-1] INFO  example.MotorEncoderTest - 32 165
2019-09-03 20:18:15 [pool-1-thread-1] INFO  example.MotorEncoderTest - 33 75
2019-09-03 20:18:15 [pool-1-thread-1] INFO  example.MotorEncoderTest - 34 -13
2019-09-03 20:18:15 [pool-1-thread-1] INFO  example.MotorEncoderTest - 35 -100
2019-09-03 20:18:15 [pool-1-thread-1] INFO  example.MotorEncoderTest - 36 -189
2019-09-03 20:18:16 [pool-1-thread-1] INFO  example.MotorEncoderTest - 37 -284
2019-09-03 20:18:16 [pool-1-thread-1] INFO  example.MotorEncoderTest - 38 -370
2019-09-03 20:18:16 [pool-1-thread-1] INFO  example.MotorEncoderTest - 39 -458
2019-09-03 20:18:16 [pool-1-thread-1] INFO  example.MotorEncoderTest - 40 -545
2019-09-03 20:18:17 [pool-1-thread-1] INFO  example.MotorEncoderTest - 41 -631
2019-09-03 20:18:17 [pool-1-thread-1] INFO  example.MotorEncoderTest - 42 -720
2019-09-03 20:18:17 [pool-1-thread-1] INFO  example.MotorEncoderTest - 43 -808
2019-09-03 20:18:17 [pool-1-thread-1] INFO  example.MotorEncoderTest - 44 -894
2019-09-03 20:18:18 [pool-1-thread-1] INFO  example.MotorEncoderTest - 45 -982
2019-09-03 20:18:18 [pool-1-thread-1] INFO  example.MotorEncoderTest - 46 -1070
2019-09-03 20:18:18 [pool-1-thread-1] INFO  example.MotorEncoderTest - 47 -1158
2019-09-03 20:18:18 [pool-1-thread-1] INFO  example.MotorEncoderTest - 48 -1248
2019-09-03 20:18:19 [pool-1-thread-1] INFO  example.MotorEncoderTest - 49 -1341
2019-09-03 20:18:19 [pool-1-thread-1] INFO  example.MotorEncoderTest - 50 -1428
2019-09-03 20:18:19 [main] INFO  example.MotorEncoderTest - End

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment