JCrete Embedded Java Workshop

74
JCrete Embedded Java Hacking Stephen Chin Java Technology Ambassador JavaOne Content Chair @steveonjava

description

Slides from the embedded Java workshop on the last day of the JCrete unconference. Includes instructions for Lego Mindstorms EV3 and Raspberry Pi.

Transcript of JCrete Embedded Java Workshop

Page 1: JCrete Embedded Java Workshop

JCrete Embedded Java HackingStephen ChinJava Technology AmbassadorJavaOne Content Chair

@steveonjava

Page 2: JCrete Embedded Java Workshop

What Runs Java?

Page 3: JCrete Embedded Java Workshop

What Runs Java?

Page 4: JCrete Embedded Java Workshop

Java and 3G in a Tiny Package

> Cinterion EHS5

Page 5: JCrete Embedded Java Workshop

Really Tiny…

27.6mm

18.8

mm

Page 6: JCrete Embedded Java Workshop

http://upload.wikimedia.org/wikipedia/commons/3/3d/Cloud_forest_Ecuador.jpg

Page 7: JCrete Embedded Java Workshop

=

Have Java With Your DessertRaspberry Pi

Page 8: JCrete Embedded Java Workshop

Pis are Affordable

$35

Page 9: JCrete Embedded Java Workshop

Chalkboard Electronics Touchscreen

10" or 7" Form Factor

Connects via HDMI/USB

Tested with JavaFX 8

10% Exclusive Discount:

G1F0U796Z083

Page 10: JCrete Embedded Java Workshop

How to Setup Your Pi

> Step 1: Install Linux

> Step 2: Download/Copy Java 8 for ARM

> Step 3: Deploy and Run JVM Language Apps

http://steveonjava.com/javafx-on-raspberry-pi-3-easy-steps/

Page 11: JCrete Embedded Java Workshop

Electronic Safety!

> Unplug from wall before wiring

> Get rid of static by touching a metal surface

> Don't touch exposed wires/metal

> Never remove/insert SD Card while power is on

11

Page 12: JCrete Embedded Java Workshop

What Comes in Your Lab Kit

1. Touch Screen2. SD Card3. Keyboard4. Yellow Box:

Power Adapter LVDS Cable/Board Raspberry Pi Model B Mini-USB Cable (power)

Please Save All the Packaging for Later

Page 13: JCrete Embedded Java Workshop

Hooking Up the Pi (Part A)

1. Insert the SD Card in to the Pi Will appear upside down when looking at the top

of your Pi

2. Insert the HDMI board into the Pi's HDMI jack

3. Connect the Pi power to the HDMI board Use the Micro USB Cable (short one)

13

Important: Connect everything before plugging into the wall

Page 14: JCrete Embedded Java Workshop

Hooking Up the Pi (Part B)

4. Slide the LCD cable into the back of the display Side with gold connectors goes up Be careful, the connector is fragile!

5. Hook up the USB keyboard6. Connect the USB end to one of the Pi's USB host ports

This provides touch input

14

Verify connections and plug into power now

Page 15: JCrete Embedded Java Workshop

Is it Working?

> Should get a bunch of flashing LEDs to indicate booting Boot takes approx 30 seconds

> The LCD screen should light up Might be dim if the light sensor is obstructed

> And you will should see a Linux boot screen with lots of text

Page 16: JCrete Embedded Java Workshop

Logging In

At the login prompt type your username:> piAnd enter the password:> raspberry

Page 17: JCrete Embedded Java Workshop

Running the JavaFX Sample Application

Change directory to the project folder> cd MaryHadALittleLambdaRun the build script> ant

Page 18: JCrete Embedded Java Workshop

18

Page 19: JCrete Embedded Java Workshop

Hacking the Code

Run the nano text editor:> nano src/sample/MapObject.javaSave your changes:> Control-O EnterExit Nano:> Control-XCompile/Run:> ant

Page 20: JCrete Embedded Java Workshop

Mary Had a Little Lambda Project

> Open-source project to demonstrate lambda features> Visual representation of streams, filters, and maps

20

https://github.com/steveonjava/MaryHadALittleLambda

Page 21: JCrete Embedded Java Workshop

Stuff to do…

> Changes to MaryHadALittleLambda: Change the number of sheep Make the rainbow have different colors Change the fox size to be fatter/skinnier Add new graphics (additional image files under images/extra)

21

camel.png lion.png greendragon.png cow.png Brownbear.png

Page 22: JCrete Embedded Java Workshop

GPIO access

Page 23: JCrete Embedded Java Workshop

Wiring LEDs

> Wire the elements in series: Connect the long end

of the LED to GPIO Connect the short end

of the LED to the resistor

Connect the resistor to Ground

23

To GPIO To Ground

Page 24: JCrete Embedded Java Workshop

Using a Breadboard

> Pins are connected horizontally in the center

> The edges are connect vertically

> No current passes the center line

24

Page 25: JCrete Embedded Java Workshop

Pi Cobbler

> Connects your Pi to the Breadboard

> The white line is Pin 1

> Connect it in the center top of your Breadboard

25

Page 26: JCrete Embedded Java Workshop

Pi4J

> The samples are located in/opt/pi4j/examples

> Compile the samples by typing "./build"> Instructions for running the samples are

printed out at the end of the build> Pi4j needs root access to use GPIO (use

"sudo")

26

Page 27: JCrete Embedded Java Workshop

ControlGpioExample

27

> Demonstrates Controlling Pins: pin.low() pin.toggle() pin.pulse(duration, blocking)

> Try creating your own pattern!

sudo java -classpath .:classes:/opt/pi4j/lib/'*' BlinkGpioExample

Page 28: JCrete Embedded Java Workshop

Wiring Buttons

> Connect a circuit across two legs

> While the button is pressed… The legs numbered 1

and 2 are connected The legs numbered 3

and 4 are connected

28

Page 29: JCrete Embedded Java Workshop

ListenGpioExample

29

> Demonstrates Listening to a pin: addListener(pinListener)

> Change the message/action whena button is pressed!

sudo java -classpath .:classes:/opt/pi4j/lib/'*' ListenGpioExample

Page 30: JCrete Embedded Java Workshop

BlinkGpioExample

30

> Use LEDs and Buttons together!

sudo java -classpath .:classes:/opt/pi4j/lib/'*' BlinkGpioExample

Page 31: JCrete Embedded Java Workshop

LeJOSHow it works on the EV3

Page 32: JCrete Embedded Java Workshop

The Heart of the EV3

> TI Sitara AM1808 ARM9, 300Mhz

> 64MB RAM / 16MB Flash> Analog to Digital Converter> 4 Motor Ports> 4 Sensor Ports> Bluetooth / USB> MicroSD

Page 33: JCrete Embedded Java Workshop

EV3 Motors

Page 34: JCrete Embedded Java Workshop

EV3 Sensors

Page 35: JCrete Embedded Java Workshop

Color and Light Sensor

Page 36: JCrete Embedded Java Workshop

High frequency sound waves

Measuring mode Vs Presence Mode

Ultrasonic Sensor

Page 37: JCrete Embedded Java Workshop

Infrared Sensor

Page 38: JCrete Embedded Java Workshop

Remote Control

Page 39: JCrete Embedded Java Workshop

Getting Started with LeJOS

> Micro SD Card (> 2GB)> Compatible WIFI adapter

NetGear WNA1100 EDIMAX EW-7811Un

> Linux (or a Linux VM)

> Details here:

Creating Your SD Card

http://sourceforge.net/p/lejos/wiki/Home/

Page 40: JCrete Embedded Java Workshop

Bluetooth Pairing

> Make sure your Lego is turned on> Open "Devices and Printers" from the

Start menu> Click "Add a device"> Select the Lego brick> After pairing, right click on the new

device and choose "Connect using" > "Access point"

Page 41: JCrete Embedded Java Workshop

Eclipse Setup

> Open Eclipse> Go to "Preferences"> Click on leJOS EV3> Change the brick name to

"10.0.1.1"

41

Page 42: JCrete Embedded Java Workshop

Creating a New LeJOS Project

> Go to "File" > "New" > "Project…"> Choose a LeJOS EV3 Project

42

Page 43: JCrete Embedded Java Workshop

Create a Class File

> Create a new class ("File" > "New" > "Class")

> Give it a package (e.g. sample)> Give it a name (e.g. LCDTest)

43

Page 44: JCrete Embedded Java Workshop

Simple LeJOS Application

import lejos.nxt.Button;import lejos.nxt.LCD;public class EV3FirstProgram { public static void main(String[] args) { LCD.clear(); LCD.drawString("First EV3 Program", 0, 5); Button.waitForAnyPress(); LCD.clear(); LCD.refresh(); }}

Page 45: JCrete Embedded Java Workshop

Parts you will need Step 1

Assemble Brace

Page 46: JCrete Embedded Java Workshop

Step 2 Completed Brace

Assemble Brace

Page 47: JCrete Embedded Java Workshop

Parts you will need Step 1

Build Base

Page 48: JCrete Embedded Java Workshop

Step 2 Completed Base

Build Base

Page 49: JCrete Embedded Java Workshop

Motor parts Snap them on partially

Assemble Motor

Page 50: JCrete Embedded Java Workshop

Take motor and base And connect them like this

Assemble Motor

Page 51: JCrete Embedded Java Workshop

Foot parts Step 1

Add a Foot

Page 52: JCrete Embedded Java Workshop

Step 2 Add the Foot to the Base

Add a Foot

Page 53: JCrete Embedded Java Workshop

Assembled Foot and Base

Add a Foot

Page 54: JCrete Embedded Java Workshop

Lock parts Partially insert the red attachers

Add a Lock

Page 55: JCrete Embedded Java Workshop

Attach the lock Push down the red attachers to secure

Add a Lock

Page 56: JCrete Embedded Java Workshop

Tower parts Step 1 – push the rod all the way through

Construct the Tower

Page 57: JCrete Embedded Java Workshop

Step 2 Attach the Tower to the Base

Construct the Tower

Page 58: JCrete Embedded Java Workshop

Completed Tower

Construct the Tower

Page 59: JCrete Embedded Java Workshop

Fan Motor parts Step 1

Build the Fan Motor

Page 60: JCrete Embedded Java Workshop

Completed Fan Motor

Build the Fan Motor

Page 61: JCrete Embedded Java Workshop

Light Sensor parts Step 1

Construct the Light Sensor

Page 62: JCrete Embedded Java Workshop

Completed Light Sensor

Construct the Light Sensor

Page 63: JCrete Embedded Java Workshop

Fan Blade parts Step 1

Assemble the Fan Blades

Page 64: JCrete Embedded Java Workshop

Fan Blade, Light Sensor, and Fan Motor Completed Fan

Assemble the Fan

Page 65: JCrete Embedded Java Workshop

Assembled Wind Turbine

65

Page 66: JCrete Embedded Java Workshop

Wind Turbine Wiring

> Port A – Medium Moto Power Fan

> Port B – Large Motor Rotate Wind Turbine

> Port 1 – Light Sensor Track Ambient Light

66

Page 67: JCrete Embedded Java Workshop

Gear Box parts Step 1

EC: Gear Box Construction

Page 68: JCrete Embedded Java Workshop

Completed Fan with Gear Box

EC: Gear Box Construction

Page 69: JCrete Embedded Java Workshop

Making the Wind turbine turn

public class WindTurbine { public static void main(String[] args) { EV3MediumRegulatedMotor fan = new EV3MediumRegulatedMotor(MotorPort.A); fan.setSpeed(1500); fan.setAcceleration(150); fan.backward(); Delay.msDelay(10000); }}

Page 70: JCrete Embedded Java Workshop

Making the Tower spin

public class WindTurbine { public static void main(String[] args) { EV3LargeRegulatedMotor base = new EV3LargeRegulatedMotor(MotorPort.B); base.setSpeed(80); base.rotateTo(-90); base.rotateTo(90); }}

Page 71: JCrete Embedded Java Workshop

Checking the Ambient Light

public class WindTurbine { public static void main(String[] args) { EV3ColorSensor light = new EV3ColorSensor(SensorPort.S1); float[] sample = new float[1]; SensorMode mode = light.getAmbientMode(); mode.fetchSample(sample, 0); LCD.drawString("Light = " + sample[0], 0, 4); Delay.msDelay(5000); }}

Page 72: JCrete Embedded Java Workshop

Putting it all together…

> Can you write a program that will:1. Rotate the fan in a circle2. Check the ambient light while rotating3. Move back to the brightest angle4. Spin the fan

http://commons.wikimedia.org/wiki/Wind_generator#mediaviewer/File:Eolienne_et_centrale_thermique_Nuon_Sloterdijk.jpg

Page 73: JCrete Embedded Java Workshop

Stephen Chintweet: @steveonjavablog: http://steveonjava.com

nighthacking.com

Real GeeksLive Hacking

NightHacking Tour

Page 74: JCrete Embedded Java Workshop

Safe Harbor Statement

The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.