+ All Categories
Home > Software > Reactive Java Robotics IoT - jPrime 2016

Reactive Java Robotics IoT - jPrime 2016

Date post: 22-Jan-2018
Category:
Upload: trayan-iliev
View: 461 times
Download: 0 times
Share this document with a friend
62
Trayan Iliev CEO of IPT – Intellectual Products & Technologies [email protected] http://iproduct.org jPrime Conference May 26-27 ‘16, Sofia Reactive Java Robotics and IoT
Transcript
Page 1: Reactive Java Robotics IoT - jPrime 2016

Trayan IlievCEO of IPT – Intellectual

Products & Technologies

[email protected]://iproduct.org

jPrime ConferenceMay 26-27 ‘16, Sofia

Reactive Java Robotics and IoT

Page 2: Reactive Java Robotics IoT - jPrime 2016

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.

Page 3: Reactive Java Robotics IoT - jPrime 2016

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

Page 4: Reactive Java Robotics IoT - jPrime 2016

Robots Can Be Complex

4

Page 5: Reactive Java Robotics IoT - jPrime 2016

… 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

Page 6: Reactive Java Robotics IoT - jPrime 2016

They Can Be Social :)

6

Left image: CC BY-SA 2.5, https://commons.wikimedia.org/w/index.php?curid=374949Right image: https://commons.wikimedia.org/w/index.php?curid=1485971

Page 7: Reactive Java Robotics IoT - jPrime 2016

Engineering, Science & Art

7

Source: https://commons.wikimedia.org/w/index.php?curid=551256, CC BY-SA 3.0

Page 8: Reactive Java Robotics IoT - jPrime 2016

Lonely Riders Driving By Their Own

8

Source: https://commons.wikimedia.org/w/index.php?curid=1822443, CC BY-SA 3.0

Page 9: Reactive Java Robotics IoT - jPrime 2016

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

Page 10: Reactive Java Robotics IoT - jPrime 2016

Intelligent Agents

10

Characteristics:

Autonomous

Reactive

Proactive

Social - ACLs

Adaptive (learning)

Personality

MobilityLeft image:https://commons.wikimedia.org/w/index.php?curid=915484,Right image: https://commons.wikimedia.org/w/index.php?curid=1924396,

Page 11: Reactive Java Robotics IoT - jPrime 2016

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 )

Page 12: Reactive Java Robotics IoT - jPrime 2016

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)

Page 13: Reactive Java Robotics IoT - jPrime 2016

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

Page 14: Reactive Java Robotics IoT - jPrime 2016

"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

Page 15: Reactive Java Robotics IoT - jPrime 2016

• Proxies: HTTP CONNECT – Tunnelling

• HTTP/S can be used WebSockets over SSL (wss:// scheme)

Bidirectional IoT – WS, HTTP/2

Page 16: Reactive Java Robotics IoT - jPrime 2016

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 / CoAPManagement: 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

Page 17: Reactive Java Robotics IoT - jPrime 2016

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

Page 18: Reactive Java Robotics IoT - jPrime 2016

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]

Page 19: Reactive Java Robotics IoT - jPrime 2016

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

Page 20: Reactive Java Robotics IoT - jPrime 2016

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

Page 21: Reactive Java Robotics IoT - jPrime 2016

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.

Page 22: Reactive Java Robotics IoT - jPrime 2016

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 )

Page 23: Reactive Java Robotics IoT - jPrime 2016

Functional Reactive (FRP)

23

According to Connal Elliot's (ground-breaking paper @ Conference on Functional Programming, 1997), FRP is:

(a) Denotative (b) Temporally continuous

Page 24: Reactive Java Robotics IoT - jPrime 2016

Reactive Manifesto

24

[http://www.reactivemanifesto.org]

Page 25: Reactive Java Robotics IoT - jPrime 2016

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/]

Page 26: Reactive Java Robotics IoT - jPrime 2016

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

Page 27: Reactive Java Robotics IoT - jPrime 2016

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

Page 28: Reactive Java Robotics IoT - jPrime 2016

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 2015Extensions : on Wed Jun 17 21:54:02 GMT+02:00 2015

Page 29: Reactive Java Robotics IoT - jPrime 2016

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.

Page 30: Reactive Java Robotics IoT - jPrime 2016

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

Page 31: Reactive Java Robotics IoT - jPrime 2016

Reactor Projects

31

https://github.com/reactor/reactor, Apache Software License 2.0

Page 32: Reactive Java Robotics IoT - jPrime 2016

Reactor Flux

32

https://github.com/reactor/reactor-core, Apache Software License 2.0

Page 33: Reactive Java Robotics IoT - jPrime 2016

Reactor Mono

33

https://github.com/reactor/reactor-core, Apache Software License 2.0

Page 34: Reactive Java Robotics IoT - jPrime 2016

Example: Flux.combineLatest()

34

https://projectreactor.io/core/docs/api/, Apache Software License 2.0

Page 35: Reactive Java Robotics IoT - jPrime 2016

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); } }

Page 36: Reactive Java Robotics IoT - jPrime 2016

Reactor Bus: IPTPI Java Robot

36

Page 37: Reactive Java Robotics IoT - jPrime 2016

37

Meet IPTPI :)

Page 38: Reactive Java Robotics IoT - jPrime 2016

38

Ups...

Page 39: Reactive Java Robotics IoT - jPrime 2016

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/

Page 40: Reactive Java Robotics IoT - jPrime 2016

IPTPI: RPi2 + Ardunio Robot

40

3D accelerometers, gyros, and compass MinIMU-9 v2

Pololu DRV8835 Dual Motor Driverfor Raspberry Pi

Arduino Leonardo cloneA-Star 32U4 Micro

USB Stereo Speakers - 5V

LiPo Powebank15000 mAh

Page 41: Reactive Java Robotics IoT - jPrime 2016

IPTPI: RPi2 + Ardunio Robot

41

Raspberry Pi 2 (quad-core ARMv7 @ 900MHz)

IR Optical Sensor QRD1114Array (Line Following)

Adafruit 2.8" PiTFT - Capacitive Touch Screen

Page 42: Reactive Java Robotics IoT - jPrime 2016

42

Page 43: Reactive Java Robotics IoT - jPrime 2016

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/tree/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.

Page 44: Reactive Java Robotics IoT - jPrime 2016

Tale of Simplicity: DDD

44

Page 45: Reactive Java Robotics IoT - jPrime 2016

Let's See Some Real Code

45

jPrime Demo code is available @ GitHub: https://github.com/iproduct/jprime-demo

Page 46: Reactive Java Robotics IoT - jPrime 2016

IPTPI Reactive Streams

46

EncoderReadings

ArduinoDataFluxion

ArduinoSerialData

PositionFluxion

RobotPositions

CommandMovementSubscriber

RobotWSService(using Reactor)

Angular 2 /TypeScript

MovementCommands

Page 47: Reactive Java Robotics IoT - jPrime 2016

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);

Page 48: Reactive Java Robotics IoT - jPrime 2016

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();}

Page 49: Reactive Java Robotics IoT - jPrime 2016

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();

Page 50: Reactive Java Robotics IoT - jPrime 2016

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());}

Page 51: Reactive Java Robotics IoT - jPrime 2016

IPTPI: PositionFluxion I

51

ReduxPattern!

Page 52: Reactive Java Robotics IoT - jPrime 2016

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) { ... }}

Page 53: Reactive Java Robotics IoT - jPrime 2016

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()); }}

Page 54: Reactive Java Robotics IoT - jPrime 2016

Reactor IO – NetStreams API

54

http://projectreactor.io/io/docs/reference/, Apache License 2.0

Page 55: Reactive Java Robotics IoT - jPrime 2016

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));}

Page 56: Reactive Java Robotics IoT - jPrime 2016

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))) )); };}

Page 57: Reactive Java Robotics IoT - jPrime 2016

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)

Page 58: Reactive Java Robotics IoT - jPrime 2016

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 :)

Page 59: Reactive Java Robotics IoT - jPrime 2016

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

Page 60: Reactive Java Robotics IoT - jPrime 2016

Tale of Simplicity: DDD

60

http://robolearn.org/

Page 61: Reactive Java Robotics IoT - jPrime 2016

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/]

Page 62: Reactive Java Robotics IoT - jPrime 2016

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


Recommended