Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Reactive Java Robotics IoT - jPrime 2016

540 Aufrufe

Veröffentlicht am

My jPrime 2016 presentation shows example of Domain-Driven Design (DDD), Event Sourcing (ES) and Functional Reactive Programming (FRP) using Reactor and Redux in a showcase of Java robotics - two small robots IPTPI (Raspberry Pi 2 + Ardiuno) and LeJaRo (LeJOS).

Veröffentlicht in: Software
  • Als Erste(r) kommentieren

Reactive Java Robotics IoT - jPrime 2016

  1. 1. Trayan Iliev CEO of IPT – Intellectual Products & Technologies tiliev@iproduct.org http://iproduct.org jPrime Conference May 26-27 ‘16, Sofia Reactive Java Robotics and IoT
  2. 2. IPT – Intellectual Products & Technologies: IT Education Evolved 2 Since 2003 we SHARE practical tech knowledge and skills in Java SE/ EE/ Web/ JS/ Angular / React / TS: Java EE/Web, JSF 2, Portlets, Portals: Liferay, GateIn Reactive IoT with Reactor / RxJava / RxJS Angular 2 + Ionic 2+ TypeScript web/mobile client MV* frameworks REST HATEOAS – Distributed Hypermedia APIs Domain Driven Design, Reactive Microservices and Event Sourcing Oracle® & Java™ are (registered) trademarks of Oracle and/or its affiliates. Liferay® is registered trademark of Liferay, Inc. Other names may be trademarks of their respective owners.
  3. 3. Tales of JAVA Robotics 3 There are so many tales to share: Tale of Robotics, IoT and Complexity Tale of Common Sense: DDD Tale of Segregation between Queries and Commands, and ultimate Event Sourcing  Tale of two cities - Imperative and Reactive  Tale of two brave robots: LeJaRo and IPTPI  And a lot of real reactive Java + TypeScript / Angular 2 / WebSocket code 
  4. 4. Robots Can Be Complex 4
  5. 5. … Even More Complex 5 Cross-section of many disciplines:  mechanical engineering  electrical engineering  computer science  artificial intelligence (AI)  human-computer interaction  sociology & psychology Picture by Hugo Elias of the Shadow Robot Company - http://www.shadowrobot.com/media/pictures.shtml, CC BY-SA 3.0
  6. 6. They Can Be Social :) 6 Left image: CC BY-SA 2.5, https://commons.wikimedia.org/w/index.php?curid=374949 Right image: https://commons.wikimedia.org/w/index.php?curid=1485971
  7. 7. Engineering, Science & Art 7 Source: https://commons.wikimedia.org/w/index.php?curid=551256, CC BY-SA 3.0
  8. 8. Lonely Riders Driving By Their Own 8 Source: https://commons.wikimedia.org/w/index.php?curid=1822443, CC BY-SA 3.0
  9. 9. and How Can We Forget 9 Source: https://commons.wikimedia.org/ w/index.php?curid=234900, CC BY-SA 3.0 Source: Korea Institute of Industrial Technology, http://news.naver.com/main/read.nhn? mode=LSD&mid=sec&sid1=102&oid=020&aid=0000371339
  10. 10. Intelligent Agents 10 Characteristics:  Autonomous  Reactive  Proactive  Social - ACLs  Adaptive (learning)  Personality  Mobility Left image:https://commons.wikimedia.org/w/index.php?curid=915484, Right image: https://commons.wikimedia.org/w/index.php?curid=1924396,
  11. 11. Robots: The Most Intelligent Things 11 CC BY 2.0, Source: https://www.flickr.com/photos/wilgengebroed/8249565455/ Radar, GPS, lidar for navigation and obstacle avoidance ( 2007 DARPA Urban Challenge )
  12. 12. The Internet of Things has the potential to change the world, just as the Internet did. Maybe even more so.  Nearly 50 petabytes of data are captured and created by human beings  People have limited time, attention and accuracy  Capturing data about things in the real world in real time  Track and count everything, reduce waste, loss & cost.  Know when things need replacing, repairing or recalling — Kevin Ashton, 'That 'Internet of Things' Thing', RFID Journal, 2009 Internet of Things (IoT)
  13. 13.  There will be nearly 26 billion devices on the Internet of Things by 2020. [Gartner]  More than 30 billion devices will be wirelessly connected to the Internet of Things by 2020 [ABI Research]  It's expected to be a 19 Trillion USD market [John Chambers, Cisco CEO] IoT Perspectives
  14. 14. "Basket of remotes" problem – we'll have hundreds of applications to interface with hundreds of devices that don't share protocols for speaking with one another [Jean-Louis Gassée, Apple initial team, and BeOS co-founder] Only IPv6 addresses are not enough – IoT devices should be also easily and directly accessible for users and [their] agents In read/write mode Preferably using a standard web browser Even behind firewalls IoT - Need for Standards
  15. 15. • Proxies: HTTP CONNECT – Tunnelling • HTTP/S can be used WebSockets over SSL (wss:// scheme) Bidirectional IoT – WS, HTTP/2
  16. 16. IoT Services Architecture 16 Devices: Hardware + Embedded Software + Firmware UART/ I2C/ 2G/ 3G/ LTE/ ZigBee/ 6LowPan/ BLE Aggregation/ Bus: ESB, Message Broker Device Gateway: Local Coordination and Event Aggregation M2M: HTTP(/2) / WS / MQTT / CoAP Management: TR-069 / OMA-DM / OMA LWM2M HTTP, AMQP Cloud (Micro)Service Mng. Docker, Kubernetes/ Apache Brooklyn Web/ Mobile Portal PaaSDashboard PaaS API: Event Processing Services, Analytics
  17. 17. Tracking Complexity 17 We need tools to cope with all that complexity inherent in robotics and IoT domains. Simple solutions are needed – cope with problems through divide and concur on different levels of abstraction: Domain Driven Design (DDD) – back to basics: domain objects, data and logic. Described by Eric Evans in his book: Domain Driven Design: Tackling Complexity in the Heart of Software, 2004
  18. 18. Common Sense: DDD 18 Actually DDD require additional efforts (as most other divide and concur modeling approaches :)  Ubiquitous language and Bounded Contexts  DDD Application Layers: Infrastructure, Domain, Application, Presentation  Hexagonal architecture : OUTSIDE <-> transformer <-> ( application <-> domain ) [A. Cockburn]
  19. 19. Common Sense: DDD 19 Main concepts:  Entities, value objects and modules  Aggregates and Aggregate Roots [Haywood]: value < entity < aggregate < module < BC  Repositories, Factories and Services: application services <-> domain services  Separating interface from implementation
  20. 20. CQRS and Event Sourcing 20 Queries and Commands have different requirements:  Queries: no need for transactions (idempotent) eventual consistency de-normalized reporting DB caching is essential often report aggregate data Naked Objects (Material Views)  Commands: transactional eventual consistency may not be ok normalized DB usually manage single entities
  21. 21. Imperative and Reactive 21 We live in a Connected Universe ... there is hypothesis that all the things in the Universe are intimately connected, and you can not change a bit without changing all. Action – Reaction principle is the essence of how Universe behaves.
  22. 22. Imperative and Reactive  Reactive Programming: using static or dynamic data flows and propagation of change Example: a := b + c  Functional Programming: evaluation of mathematical functions, ➢ Avoids changing-state and mutable data, declarative programming ➢ Side effects free => much easier to understand and predict the program behavior. Example: books.stream().filter(book -> book.getYear() > 2010) .forEach( System.out::println )
  23. 23. Functional Reactive (FRP) 23 According to Connal Elliot's (ground-breaking paper @ Conference on Functional Programming, 1997), FRP is: (a) Denotative (b) Temporally continuous
  24. 24. Reactive Manifesto 24 [http://www.reactivemanifesto.org]
  25. 25. Reactive Programming 25  Microsoft® opens source polyglot project ReactiveX (Reactive Extensions) [http://reactivex.io]: Rx = Observables + LINQ + Schedulers :) Java: RxJava, JavaScript: RxJS, C#: Rx.NET, Scala: RxScala, Clojure: RxClojure, C++: RxCpp, Ruby: Rx.rb, Python: RxPY, Groovy: RxGroovy, JRuby: RxJRuby, Kotlin: RxKotlin ...  Reactive Streams Specification [http://www.reactive-streams.org/] used by  (Spring) Project Reactor [http://projectreactor.io/]
  26. 26. Reactive Streams Spec. 26  Reactive Streams – provides standard for asynchronous stream processing with non-blocking back pressure.  Minimal set of interfaces, methods and protocols for asynchronous data streams  April 30, 2015: has been released version 1.0.0 of Reactive Streams for the JVM (Java API, Specification, TCK and implementation examples)  Java 9: java.util.concurrent.Flow
  27. 27. Reactive Streams Spec. 27  Publisher – provider of potentially unbounded number of sequenced elements, according to Subscriber(s) demand. Publisher.subscribe(Subscriber) => onSubscribe onNext* (onError | onComplete)?  Subscriber – calls Subscription.request(long) to receive notifications  Subscription – one-to-one Subscriber ↔ Publisher, request data and cancel demand (allow cleanup).  Processor = Subscriber + Publisher
  28. 28. FRP = Async Data Streams 28  FRP is asynchronous data-flow programming using the building blocks of functional programming (e.g. map, reduce, filter) and explicitly modeling time  Used for GUIs, robotics, and music. Example (RxJava): Observable.from(new String[]{"Reactive", "Extensions", "Java"}) .take(2).map(s -> s + " : on " + new Date()) .subscribe(s -> System.out.println(s)); Result: Reactive : on Wed Jun 17 21:54:02 GMT+02:00 2015 Extensions : on Wed Jun 17 21:54:02 GMT+02:00 2015
  29. 29. Project Reactor 29  Reactor project allows building high-performance (low latency high throughput) non-blocking asynchronous applications on JVM.  Reactor is designed to be extraordinarily fast and can sustain throughput rates on order of 10's of millions of operations per second.  Reactor has powerful API for declaring data transformations and functional composition.  Makes use of the concept of Mechanical Sympathy built on top of Disruptor / RingBuffer.
  30. 30. Project Reactor 30  Pre-allocation at startup-time  Message-passing structures are bounded  Using Reactive and Event-Driven Architecture patterns => non-blocking end-to-end flows, replies  Implement Reactive Streams Specification – efficient bounded structures requesting no more than capacity  Applies above features to IPC and provides non- blocking IO drivers that are flow-control aware  Expose a Functional API – organize their code in a side-effect free way, which helps you determine you are thread-safe and fault-tolerant
  31. 31. Reactor Projects 31 https://github.com/reactor/reactor, Apache Software License 2.0
  32. 32. Reactor Flux 32 https://github.com/reactor/reactor-core, Apache Software License 2.0
  33. 33. Reactor Mono 33 https://github.com/reactor/reactor-core, Apache Software License 2.0
  34. 34. Example: Flux.combineLatest() 34 https://projectreactor.io/core/docs/api/, Apache Software License 2.0
  35. 35. Reactor: Hello World 35 public class ReactorHelloWorld { public static void main(String... args) throws InterruptedException { Broadcaster<String> sink = Broadcaster.create(); SchedulerGroup sched = SchedulerGroup.async(); sink.dispatchOn(sched) .map(String::toUpperCase) .filter(s -> s.startsWith("HELLO")) .consume(s -> System.out.printf("s=%s%n", s)); sink.onNext("Hello World!"); sink.onNext("Goodbye World!"); Thread.sleep(500); } }
  36. 36. Reactor Bus: IPTPI Java Robot 36
  37. 37. 37 Meet IPTPI :)
  38. 38. 38 Ups...
  39. 39. IPTPI: RPi2 + Ardunio Robot 39  Raspberry Pi 2 (quad-core ARMv7 @ 900MHz) + Arduino Leonardo cloneA-Star 32U4 Micro  Optical encoders (custom), IR optical array, 3D accelerometers, gyros, and compass MinIMU-9 v2  IPTPI is programmed in Java using Pi4J, Reactor, RxJava, Akka  More information about IPTPI: http://robolearn.org/iptpi-robot/
  40. 40. IPTPI: RPi2 + Ardunio Robot 40 3D accelerometers, gyros, and compass MinIMU-9 v2 Pololu DRV8835 Dual Motor Driver for Raspberry Pi Arduino Leonardo clone A-Star 32U4 Micro USB Stereo Speakers - 5V LiPo Powebank 15000 mAh
  41. 41. IPTPI: RPi2 + Ardunio Robot 41 Raspberry Pi 2 (quad-core ARMv7 @ 900MHz) IR Optical Sensor QRD1114 Array (Line Following) Adafruit 2.8" PiTFT - Capacitive Touch Screen
  42. 42. 42
  43. 43. LeJaRo: Lego® Java Robot 43  Modular – 3 motors (with encoders) – one driving each track, and third for robot clamp.  Three sensors: touch sensor (obstacle avoidance), light color sensor (follow line), IR sensor (remote).  LeJaRo is programmed in Java using LeJOS library.  More information about LeJaRo: http://robolearn.org/lejaro/  Programming examples available @GitHub: https://github.com/iproduct/course-social-robotics/tre e/master/motors_demo LEGO® is a registered trademark of LEGO® Group. Programs of IPT are not affiliated, sponsored or endorsed by LEGO® Education or LEGO® Group.
  44. 44. Tale of Simplicity: DDD 44
  45. 45. Let's See Some Real Code  45 jPrime Demo code is available @ GitHub: https://github.com/iproduct/jprime-demo
  46. 46. IPTPI Reactive Streams 46 Encoder Readings ArduinoData Fluxion Arduino SerialData Position Fluxion Robot Positions Command Movement Subscriber RobotWSService (using Reactor) Angular 2 / TypeScript MovementCommands
  47. 47. IPTPI: IPTPIDemo I 47 public class IPTPIVDemo { ... public IPTPIDemo() { //receive Arduino data readings arduino = ArduinoDataFactory.createArduinoDataFluxion(); //calculate robot positions positionsPub =PositionFactory.createPositionFluxion(arduino); presentationViews.add( PositionFactory.createPositionPanel(positionsPub)); //wire robot main controller with services moveSub = MovementFactory .createCommandMovementSubscriber(positionsPub); controller = new RobotController( Subscribers.consumer(this::tearDown),moveSub);
  48. 48. IPTPI: IPTPIDemo II 48 //create view with controller and delegate material views //from query services view = new RobotView("IPTPI Reactive Robotics Demo", controller, presentationViews); //expose as WS service movementSub2 = MovementFactory .createCommandMovementSubscriber(positionsPub); positionsService = new RobotWSService(positionsPub, movementSub2); } public static void main(String[] args) { IPTPIVoxxedDemo demo = new IPTPIVoxxedDemo(); }
  49. 49. IPTPI: ArduinoDataFluxion I 49 fluxion = Broadcaster.create(); emitter = fluxion.startEmitter(); final Serial serial = SerialFactory.createInstance(); serial.addListener(new SerialDataEventListener() { private ByteBuffer buffer = ByteBuffer.allocate(1024); @Override public void dataReceived(SerialDataEvent event) { try { ByteBuffer newBuffer = event.getByteBuffer(); buffer.put(newBuffer); buffer.flip(); ... buffer.get(); long timestamp = buffer.getInt(); //get timestamp int encoderL = -buffer.getInt(); //motors mirrored int encoderR = buffer.getInt();
  50. 50. IPTPI: ArduinoDataFluxion II 50 EncoderReadings readings = new EncoderReadings(encoderR, encoderL, timestamp); emitter.submit(readings); ... buffer.compact(); } catch (Exception e) { e.printStackTrace(); } } }); try { serial.open(PORT, 38400); } catch(SerialPortException | IOException ex) { System.out.println(“SERIAL SETUP FAILED:"+ex.getMessage()); }
  51. 51. IPTPI: PositionFluxion I 51 Redux Pattern!
  52. 52. CommandMovementSubscriber I 52 public class CommandMovementSubscriber extends ConsumerSubscriber<Command<Movement>> { private PositionFluxion positions; public CommandMovementSubscriber(PositionFluxion positions){ this.positions = positions; Gpio.wiringPiSetupGpio(); // initialize wiringPi library Gpio.pinMode(5, Gpio.OUTPUT); // Motor direction pins Gpio.pinMode(6, Gpio.OUTPUT); Gpio.pinMode(12, Gpio.PWM_OUTPUT); // Motor speed pins Gpio.pinMode(13, Gpio.PWM_OUTPUT); Gpio.pwmSetMode(Gpio.PWM_MODE_MS); Gpio.pwmSetRange(MAX_SPEED); Gpio.pwmSetClock(CLOCK_DIVISOR); } @Override public void doNext(Command<Movement> command) { ... } }
  53. 53. CommandMovementSubscriber II 53 private void runMotors(MotorsCommand mc) { //setting motor directions Gpio.digitalWrite(5, mc.getDirR() > 0 ? 1 : 0); Gpio.digitalWrite(6, mc.getDirL() > 0 ? 1 : 0); //setting speed if(mc.getVelocityR()>=0 && mc.getVelocityR() <=MAX_SPEED) Gpio.pwmWrite(12, mc.getVelocityR()); // set speed if(mc.getVelocityL()>=0 && mc.getVelocityL() <=MAX_SPEED) Gpio.pwmWrite(13, mc.getVelocityL()); } }
  54. 54. Reactor IO – NetStreams API 54 http://projectreactor.io/io/docs/reference/, Apache License 2.0
  55. 55. IPTPI: RobotWSService I 55 private void setupServer() throws InterruptedException { httpServer = NetStreams.<Buffer, Buffer>httpServer( HttpServerSpec<Buffer,Buffer> serverSpec -> serverSpec.listen("172.22.0.68", 80) ); httpServer.get("/", getStaticResourceHandler()); httpServer.get("/index.html", getStaticResourceHandler()); httpServer.get("/app/**", getStaticResourceHandler()); ... httpServer.ws("/ws", getWsHandler()); httpServer.start().subscribe( Subscribers.consumer(System.out::println)); }
  56. 56. IPTPI: RobotWSService II 56 private ReactorHttpHandler<Buffer, Buffer> getWsHandler() { return channel -> { System.out.println("Connected a websocket client: " + channel.remoteAddress()); channel.map(Buffer::asString).consume( json -> { System.out.printf(“WS Message: %s%n“, json); Movement movement = gson.fromJson(json, Movement.class); movementCommands.onNext(new Command<>("move", movement)); }); return positions.flatMap(position -> channel.writeWith( Flux.just(Buffer.wrap(gson.toJson(position))) )); }; }
  57. 57. Takeaways: Why Go Reactive? 57 Benefits using Reactive Programming + DDD:  DDD helps to manage complexity in IoT and Robotics - many subsystems = sub-domains  Reactive Streams (Fluxes, Monos) = uni-directional data flows, CQRS, event sourcing, microservices  Reactive Streams can be non-blocking and highly efficient, or can utilize blocking if needed  Naturally implement state management patterns like Redux, allow time travel, replay and data analytics  Clear, declarative data transforms that scale (Map- Reduce, BigData, PaaS)
  58. 58. Takeaways: Why Maybe Not? 58 Cons using Reactive Programming + DDD:  DDD requires additional efforts to clearly separate different (sub) domains – DSL translators, factories...  Reactive Streams utilize functional composition and require entirely different mindset then imperative – feels like learning foreign language  Pure functions and Redux provide much benefits, but there's always temptation to “do it the old way” :)  Tool support for functional programming in Java is still not perfect (in Eclipse at least :)
  59. 59. Additional Resources 59 IPT Reactive Java/JS/Typescript and Angular 2 courses: http://iproduct.org More information about robots @RoboLearn: http://robolearn.org/ Lots of Java robotics and IoT resources @Social Robotics Course GitHub Wiki: https://github.com/iproduct/course-social- robotics/wiki/Lectures
  60. 60. Tale of Simplicity: DDD 60 http://robolearn.org/
  61. 61. Interested in Reactive Java? 61 Meet Us @ Java & FOSS Conferences:  BG Oracle® User Group Conference – 3-5 June in Hotel Rila, Borovetz [http://www.bgoug.org/bg/events/details/96.html]  TuxCon – 9-10 July, Plovdiv [http://tuxcon.mobi/]
  62. 62. Thank’s for Your Attention! 62 Trayan Iliev CEO of IPT – Intellectual Products & Technologies http://iproduct.org/ http://robolearn.org/ https://github.com/iproduct https://twitter.com/trayaniliev https://www.facebook.com/IPT.EACAD https://plus.google.com/+IproductOrg

×