0% found this document useful (0 votes)
181 views98 pages

Lego Java

LOLOLOLOLOLOLOLOL

Uploaded by

Polunomynals
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views98 pages

Lego Java

LOLOLOLOLOLOLOLOL

Uploaded by

Polunomynals
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

Programming LEGO Mindstorms NXT robots. Macrofaculty profiled documents.

Programming Lego Mindstorms NXT with Java. LeJOS NXJ Virtual Machine.

Legal notes

2008,2011 Silesian University of Technology, Gliwice, Poland, Ph. D. Piotr Czekalski This document is copyrighted. Copying, lending, distribution and redistribution on author's permission only. This document contains noncommercial, educational content only thus cannot be utilized as a part of of any commercial business. Author of this document assures that has made efforts to achieve correctness of the contents of this document as well as tried to provide latest and most modern review of the related technology on the hardware, software and programming methods. However it is obvious that author cannot guarantee the content not to contain some minor or major mistakes or shortcomings, mostly because of rapid popularity on the subject, changes of legal state, changes in current knowledge and trends, hardware and software updates, etc. This document is provided as is with no warranty of any kind and cannot be a subject of any claims on the content nor author. Author kindly requests all users of this document to provide information on any mistakes or shortcomings in this documents pro publico bono. As this document can exist in many versions it is necessary to check doubtful content versus latest release of this document. Any trademarks used in this document may be copyrighted by appropriate owners, particularly: LEGO, the LEGO logo, DUPLO, BIONICLE, MINDSTORMS, the BELVILLE, CLIKITS, KNIGHTS KINGDOM and EXO-FORCE logos, the Brick and Knob configurations and the Minifigure is a trademark of the LEGO Group. Microsoft, Robotics Studio, .NET logo, Visual Studio, ActiveX and Windows is a trademark of the Microsoft Corporation. Java, JDK and JAVA logo is a trademark of the Oracle Corp. ageia PhysX is a trademark of the nVIDIA Corporation.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

Plan of the presentation


Changing original LEGO firmware to the LeJOS NXJ Java virtual machine. Preparing Eclipse environment to build LeJOS native autonomous programs. LeJOS API brief reference with samples.
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

Part 1

Installing LeJOS firmware. Running sample test program.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

LeJOS NXJ
New firmware replaces genuine LEGO (reversible operation). Dedicated compiler and linker (imports it's own java.lang.* libraries). A set of utilities to work with NXT Brick. API covers both Genuine LEGO and 3rd party components (sensors, servos, comm., etc.). It is Open Source project. Current version 0.9.0 beta (16th May 2011) Formerly API was for RCX (RIS) now NXT. LeJOS powered robot Jitter flew on International Space Station in 2001 ;-).
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

What is needed to work with LeJOS & Java?


LEGO Mindstorms Edu/Retail NXT Software. libusb-win32 filter driver (not for x64). Java SE SDK (JDK). Apache Ant (optional). Eclipse (optional but recommended). USB cable Bluecove library (jar file) for iCommand remote access (warning iCommand is obsolete now).
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

Setup procedure.
LEGO Mindstorms Edu/Retail Software
Dedicated setup.

Java SDK http://java.sun.com/


Dedicated setup.

Apache Ant http://ant.apache.org/bindownload.cgi


Unpack to the designated folder.

LeJOS http://lejos.sourceforge.net/nxj-downloads.php
Dedicated setup (also ZIP available).

LibUSB http://libusb-win32.sourceforge.net/#downloads
Dedicated setup. Extra procedure for Windows 7/Vista exists, not for x64.
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

Setup procedure (cont.).


Environment variables:

NXJ_HOME LeJOS folder, i.e: c:\Program Files\LeJOS NXJ or C:\Program Files (x86)\leJOS NXJ (on x64) JAVA_HOME JDK setup folder (must be JDK instead of JRE) (warning: for x64 Windows OSes JDK have to be x86 anyway) ANT_HOME Ant scripts folder, i.e.: c:\apache-ant-1.8.2ant PATH - %NXJ_HOME%\bin;%JAVA_HOME%\bin;%ANT_HOME %\bin;%PATH%

iCommand extra settings (remote controlled mode, obsolete):

CLASSPATH - .;C:\icommand0.7\dist\icommand.jar;C:\icommand0.7\dist\bluecove-2.1.0.jar;
Ph.D. Eng. Piotr Czekalski

Version 1.53 | 20th November 2011

Setup procedure (cont.).


Installing NXJ firmware into the NXT Intelligent Brick:

Brick have to be connected over USB, detected (drivers installed) and ready. cmd: > nxjflash or nxjflashg Sometime manual entering into firmware update mode is necessary (press hardware reset button for few seconds see image below). NXJ can be replaced with genuine firmware again (the NXJ firmware flashing is reversible procedure) using LEGO Mindstorms Edu/Retail NXT software NXT Intelligent Brick allows up to about 100 updates to the firmware.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

Setup procedure (cont.).


Run sample program:

Cmd:> cd %NXJ_HOME%\samples\View (Lejos v 0.6) Cmd:> cd %USERPROFILE%\leJOSNXJProjects\samples\View (v 0.7 and newer) Cmd:> nxjc View.java Cmd:> nxj -r -u View

Allows to watch NXT(NXJ) robot state: View sensor values. View servomotors state. View system state.
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

10

Part 2
Configuring Eclipse to compile autonomous projects for NXJ. Creating a project for NXJ. Configuring three exteral tools (compile, upload, browse NXT brick contents) to work with NXT directly from Eclipse IDE.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

11

Configuring Eclipse.
Eclipse http://www.eclipse.org/downloads/
Follow instructions available on-line (currently just unpack).

Preparing Eclipse to compile for NXJ: Create project:

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

12

Configuring Eclipse (cont.).


Project details. Watch carefully project location (Project layout section) because external tools configuration depends on it.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

13

Configuring Eclipse (cont.).


Configure classpath for NXJ: Project->Properties: Java Build Path/Libraries. [Add External JARs]: <LeJOS path>

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

14

Configuring Eclipse (cont.).


Configure NXJ compile command: Run->External Tools->External Tools Configurations: Program/[New]. Use IDE variables. Watch *.java files location (project root or subfolders).

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

15

Configuring Eclipse (cont.).


Configure upload to NXT brick command: Run->External Tools->External Tools Configurations: Program/[New]. Use IDE variables.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

16

Configuring Eclipse (cont.).


Configure browse NXT Intelligent Brick contents: Run->External Tools->External Tools Configurations: Program/[New].

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

17

Simple test program.

import lejos.nxt.*; public class HelloWorld {


public static void main (String[] aArg) throws Exception {


LCD.clear(); LCD.drawString("Welcome !", 1, 1); Button.waitForPress();

}
Ph.D. Eng. Piotr Czekalski

Version 1.53 | 20th November 2011

18

Compiling and running.


Mark the source file to compile (required!).

Program starting procedure: 1.Compile. 2.Upload to NXT. 3.Run using nxjbrowse or directly from the firmware menu.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

19

Other method (preffered).

Using LeJOS plugin for Eclipse: Installation procedure. Configuration procedure. Sample project.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

20

Installing LeJOS plugin for Eclipse.


Run Eclipse, click [menu]: Help->Install New Software. Paste the following link and click Add: Mark both checkboxes (leJOS NXJ*) click [Next] and follow a wizard. Installing may take some time as it requires a bunch of dependency packages. Restart Eclipse. Observe an integrated help context for LeJOS available in Eclipse now.
http://lejos.sourceforge.net/tools/eclipse/plugin/nxj/

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

21

Configuring LeJOS plugin for Eclipse.


LeJOS plugin is aware of NXJ location path, thus needs some configuration. Click [menu]: Window->Preferences, choose leJOS NXJ then paste leJOS NXJ root installation directory:

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

22

Using LeJOS plugin for Eclipse.


Using plugin one may flash the firmware as required click [Menu]->leJOS NXJ->Upload Firmware (if still required). To create a new project, simply create a new Java project, then right-click in Package Explorer and select leJOS NXJ-> Convert to leJOS NXJ Project: Write and compile as in any regular Java application. When ready, right-click the project and choose Upload Program to the NXT Brick. The Console shall write something similar to: project <name> now is a leJOS NXJ project
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

23

Add some extra tools.

LeJOS comes with bunch of valuable tools. Those may be configured as Eclipse External Tools, i.e.: nxjbrowse NXJ brick file explorer nxjmonitor a remote program monitor allows to browse sensor input and servomotor output values almost realtime (well, not in fact too slow for most applications), also draws tracing message (useful). Works over Bluetooth connection. nxjdataviewer data logger downloader and presentation tool (both BT & usb) nxjconsoleviewer RConsole output (graphical tool) nxjconsole as above but command line nxjcontrol combines above in one + some extra features nxjdebug debugger no longer exists, replaced by nxjconsole The tools include trace and debugging, also nxjbrowse tool. Procedure same as configuring compiler and linker using external tools.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

24

LeJOS API.
Three modes (two remote, one autonomous):

Remote for LCP: lejos.pc.comm,

lejos.nxt,lejos.robotics, lejos.geom

Remote iCommand API: icommand.navigation,


icommand.nxt, icommand.nxtcomm, icommand.vision(obsolete)

Autonomous:

java.awt, java.io, java.lang, java.util, java.awt Micro edition: javax.bluetooth, javax.microedition.io, javax.microedition.lcdui Other: lejos.devices, lejos.robotics.navigation, lejos.nxt, lejos.nxt.comm, lejos.nxt.debug, lejos.nxt.remote, lejos.rcxcomm, lejos.robotics.subsumption, lejos.util, lejos.robotics
Ph.D. Eng. Piotr Czekalski

Version 1.53 | 20th November 2011

25

LeJOS API reference.


lejos.nxt.* all classes related to the sensors, NXT buttons & sound, actuators (servos), 3rd party included partially. Interfaces, abstract classes and RCX compatibility API (sensors, motors). lejos.nxt.addon all 3rd party sensors and servos (generally those not included in LEGO NXT kit), RCX sensors and motors support lejos.nxt.comm communication with NXT over USB & Bluetooth (Lego Communication Protocol implementation included). lejos.nxt.remote remote managing other NXT over Bluetooth (LCP included). lejos.nxt.debug brings debugging capabilities.
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

26

LeJOS API reference (cont.).


lejos.addon.gps GPS support classes. lejos.util utility classes, mostly for debugging and

tracing, i.e. asserts, data loggers, watches, timers, recycled classes, button click counters, etc.
lejos.addon.keyboard SPP (over BT) keyboard

support classes.

javax.bluetooth Java bluetooth support

compatibility classes.

javax.microedition J2ME compatibility classes.

Current version implements I/O, LCD UI and JSR179 location API.


Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

27

LeJOS API reference (cont).


lejos.robotics.navigation navigators & pilots. lejos.robotics.subsumption Support for subsumption architecture (behavioral programming). lejos.robotics.localization Support for Monte Carlo & Particle Filtering based position tracking of a robot. lejos.robotics.mapping Support for loadable / definable maps of object, works with lejos.robotics.navigation package to provide deterministic range to an object located on the map. lejos.robotics.objectdetection Object (feature, when on map) detection broker. Capable to notify one listener based on multiple sensor readings (i.e. range / touch sensor wall detection). lejos.robotics.pathfinding various path finding algorithms with respect to the map obstacles.
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

28

LeJOS API reference (cont.).


lejos.geom geometric shape support for robotics

using flat coordinates.

lejos.charset character sets encoder / decoder

(Latin1 / UTF8 supported)

lejos.io lejos specific stream reader / writer classes native support for java.io

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

29

lejos.nxt.* package reference.

Basic components, most common use when compiling for NXT Intelligent Brick:
Common solution part. Different and exceptional cases.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

30

LCD text mode.


Static class LCD. Text mode 16 cols (X: 0..15) x 8 rows (Y: 0..7) Methods:
public static drawString(String str, int x, int y); public static drawString(String str, int x, int y, boolean invert); - inverted text public static drawInt(int i, int x, int y); - left aligned, uses as many characters as necessary public static drawInt(int i, int places, int x, int y); - right aligned, uses provided number of characters
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

31

LCD text mode (cont.).


Methods:
public static drawChar(char c, int x, int y, boolean invert); public static clear();

Sample:

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

32

LCD graphics mode.


Graphics mode 100 px (X: 0..99) x 64 px (Y: 0..63) Class:
javax.microedition.lcdui.Graphics;

Methods (general description): Draw lines, arcs, figures, images, Strings, Ints, Chars Drawing style and fill can be provided.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

33

LCD streamed mode.


Console style drawing bottom line, scrolls up history entries Class:
lejos.nxt.LCDOutputStream;

Method (general description):


public void write(int c); - writes a low-order 8 bits (byte) to output stream (console) the remaining 24 high-order bits are abandoned. Other java.io.OutputStream methods like: close, flush and so one.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

34

NXT Intelligent Brick buttons.


4 static fields to identify buttons Common methods:
public final boolean isPressed(); public final void waitForPressAndRelease(); static int waitForPress(); public void addButtonListener(ButtonListener aListener); - up to 4 delegates each static int readButtons(); - low level function reads using bit mask: ENTER=0x01, LEFT=0x02, RIGHT=0x04, ESC=0x08

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

35

Sample:

NXT Intelligent Brick buttons (cont.).

import lejos.nxt.*; public class ButtonPresses { void public static void main (String[] args) throws Exception { while (true) { LCD.clear(); If (Button.ENTER.isPressed()) LCD.drawString("ENTER",0,0); if (Button.ESCAPE.isPressed()) LCD.drawString("ESCAPE",0,0); if (Button.LEFT.isPressed()) LCD.drawString("LEFT",0,0); if (Button.RIGHT.isPressed()) LCD.drawString("RIGHT",0,0); } }
Ph.D. Eng. Piotr Czekalski

Version 1.53 | 20th November 2011

36

Sample:

NXT Intelligent Brick buttons (cont.).

import lejos.nxt.*; public class ButtonTest { void public static void main (String[] args) throws Exception { Button.ENTER.waitForPressAndRelease(); LCD.drawString("Finished", 3, 4); Thread.sleep(2000); } }

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

37

Power source monitoring.


Class:
lejos.nxt.Battery;

Methods:
static float getVoltage(); static int getVoltageMilliVolt();

Sample:
import lejos.nxt.*; public class BatteryTest { void main (String[] args) throws Exception { LCD.drawString("Battery: " + Battery.getVoltage(),0,0); Thread.sleep(2000); } }
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

38

Sound (by brick speaker).


Class:
lejos.nxt.Sound;

Methods:
public static void systemSound(boolean aQueued, int aCode); Acode=(0: short beep, 1: double beep, 2: descending, 3: ascending) public static void beep(); public static void twoBeeps(); public static void beepSequence(); public static void beepSequenceUp(); public static void buzz();
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

39

Sound (by brick speaker).


Sample:

import lejos.nxt.*; public class Tune { private static final short [] note = { 2349,115, 0,5, 1760,165, 0,35, 1760,28, 0,13, 1976,23, 0,18, 1760,18, 0,23, 1568,15, 0,25, 1480,103, 0,18, 1175,180, 0,20, 1760,18, 0,23, 1976,20, 0,20, 1760,15, 0,25, 1568,15, 0,25, 2217,98, 0,23, 1760,88, 0,33, 1760,75, 0,5, 1760,20, 0,20, 1760,20, 0,20, 1976,18, 0,23, 1760,18, 0,23, 2217,225, 0,15, 2217,218}; void public static void main(String [] args) {

for(int i=0;i<note.length; i+=2) {

final short w = note[i+1]; final int n = note[i]; if (n != 0) Sound.playTone(n, w*10); try { Thread.sleep(w*10); } catch (InterruptedException e) {}
Ph.D. Eng. Piotr Czekalski

}}} 40

Version 1.53 | 20th November 2011

Touch sensor.
Class:
TouchSensor class instance with port;

Constructor:
public TouchSensor(ADSensorPort port);

Common methods:
public boolean isPressed();

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

41

Touch sensor (cont.).


Sample:

import lejos.nxt.*; public class TouchTest {


void public static main(String [] args) { throws Exception {

TouchSensor touch = new TouchSensor(SensorPort.S1); while (!touch.isPressed()); LCD.drawString(Finished, 3, 4); Thread.sleep(2000);

} }

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

42

Light sensor.
Class:

LightSensor class instance with port;

Constructor:
public LightSensor(ADSensorPort port);

Methods:
void setFloodlight(boolean bOn); public int readValue(); public int readNormalizedValue(); void calibrateHigh(); void calibrateLow(); void setHigh(int high); void setLow(int low); int getHigh(); int getLow();
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

43

Light sensor (cont.).


Sample:

import lejos.nxt.*; public class LightTest { void main (String[] args)


throws Exception {

LightSensor light = new LightSensor(SensorPort.S1); while (true) {


LCD.drawInt(light.readValue(), 4, 0, 0); LCD.drawInt(light.readNormalizedValue(), 4, 0, 1); LCD.drawInt(SensorPort.S1.readRawValue(), 4, 0, 2); LCD.drawInt(SensorPort.S1.readValue(), 4, 0, 3); Thread.sleep(2000);

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

44

Sound sensor.
Class:

SoundSensor class instance with port;

Constructor:
public SoundSensor(ADSensorPort port); public SoundSensor(ADSensorPort port, boolean bDBA);

Methods:
int readValue(); void setDBA(boolean dba);

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

45

Sound sensor (cont.).


Samples:

import lejos.nxt.*; public class SoundScope { void main (String[] args) throws Exception { SoundSensor light = new SoundSensor(SensorPort.S1); while (!Button.ESCAPE.isPressed()) { LCD.clear(); for(int i=0;i<100;i++) LCD.setPixel(1,i,60 - (sound.readValue()/2)); Thread.sleep(20); } } } }

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

46

Ultrasound distance sensor.


Class:

UltrasonicSensor class instance with I2C port;

Constructor:
public UltrasonicSensor(I2CPort port);

Methods:
int getDistance(); void ping(); int readDistances(int [] distances); int continuous();

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

47

Ultrasound distance sensor (cont.).


Sample:

import lejos.nxt.*; public class SonicTest { void main(String[] args) throws Exception { UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S1); while(!Button.ESCAPE.isPressed()) { LCD.clear(); LCD.drawInt(sonic.getDistance(), 0, 3); } } }

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

48

NXT Interactive Servomotor.


Class:
Motor class; (Motor.A, Motor.B, Motor.C)

Methods:
void flt(); void forward(); void backward(); int getActualSpeed(); float getBasePower(); int getLimitAngle(); int getMode(); int getPower(); int getSpeed(); int getStopAngle();
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

49

NXT Interactive Servomotor (cont.).


Methods:
int getTachoCount(); void resetTachoCount(); boolean isMoving(); boolean isRotating(); boolean isRegulating(); void lock(); void regulateSpeed(boolean yes); void reverseDirection(); void rotate(int angle); //blokujce void rotate(int angle, boolean immediate); void rotateTo(int angle); void rotateTo(int angle, boolean immediate);
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

50

NXT Interactive Servomotor (cont.).


Methods:
void setBrakePower(int pwr); void setPower(int power); void setSpeed(int speed); // stopni/min. void stop(); void smoothAcceleration(boolean yes);

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

51

NXT Interactive Servomotor (cont.).


Sample:

import lejos.nxt.*; public class HelloWorld { public static void main(String [] args) {

Motor.B.resetTachoCount(); Motor.C.resetTachoCount();

while (!Button.ENTER.isPressed()) {

LCD.drawInt(Motor.B.getTachoCount(), 1, 1); LCD.drawInt(Motor.C.getTachoCount(), 1, 2); try { Thread.sleep(100); LCD.clear(); } catch(InterruptedException e){}

}}}

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

52

NXT Interactive Servomotor (cont.).


Sample (cont.):

(...) Motor.B.setSpeed(720);// 2 RPM Motor.C.setSpeed(720); Motor.B.forward(); Motor.C.forward(); Thread.sleep (1000); //to trzeba oboy musi by Try-catch eby skompilowa Motor.A.stop(); Motor.C.stop(); Motor.A.regulateSpeed(true); Motor.A.rotateTo( 360); Motor.A.rotate(-720,true); while(Motor.A.isRotating()){}; int angle = Motor.A.getTachoCount(); // should be -360 (...)
Ph.D. Eng. Piotr Czekalski

Version 1.53 | 20th November 2011

53

3 party sensors (base) lejos.nxt.addon

rd

Sensors other than I2C use A/D port (ADSensorPort) and have dedicated classes, not inheriting common base class (but object, as all classes do). All I2C sensors inherit from I2CSensor.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

54

3 party sensors (base).


I2C:

rd

I2CSensor class instance with I2C port;

Constructor:
public UltrasonicSensor(I2CPort port);

Methods:
int getData(int register, byte[] buf, int len); int sendData(int register, byte value); int sendData(int register, byte[] buf, int len); void setAddress(int addr); // 7-bit I2CPort getPort(); String getProductID(); String getSensorType(); String getVersion();
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

55

Compass sensor.
Both HiTechnic & Mindsensors:

CompassSensor class instance with I2C port;

Constructor:
public CompassSensor(I2CPort port);

Methods:
float getDegrees(); //0.1 grade accuracy clockwise float getDegreesCartesian; // counterwise void resetCartesianZero(); void startCalibration(); // min. 20s/per turn, Mindsensors min. 2 turns, HiTechinc: 1.5 to 2 void stopCalibration();
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

56

Compass sensor.
Sample:
import lejos.nxt.*; import lejos.nxt.addon.*; public class Compass { public static void main(String [] args) { CompassSensor cs = new CompassSensor(SensorPort.S4); Motor.B.setSpeed(60); Motor.B.forward(); float stopnie=cs.getDegrees(); while (stopnie>1) { LCD.clear(); LCD.drawInt((int)stopnie, 1, 1); stopnie=cs.getDegrees(); } Motor.B.stop(); LCD.clear(); LCD.drawString("Znalazlem polnoc!", 1, 2); try { Button.ENTER.waitForPressAndRelease(); } catch(InterruptedException e){}} }

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

57

Tilt / acceleration.
HiTechnic:

AccelHTSensor;

Constructor:
public AccelHTSensor(I2CPort port); public AccelHTSensor(I2CPort port, int address);

Methods:
int getXAccel(); int getXTilt(); int getYAccel(); int getYTilt(); int getZAccel(); int getZTilt();

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

58

Tilt / acceleration.
Mindsensors:

AccelMindSensor;

Constructor:
public AccelMindSensor(I2CPort port); public AccelMindSensor(I2CPort port, int address);

Methods:
int getXAccel(); int getXTilt(); int getYAccel(); int getYTilt(); int getZAccel(); int getZTilt();

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

59

Gyro sensor.
Class:

GyroSensor class instance with port;

Constructor:
public GyroSensor(AdSensorPort port);

Methods:
int readValue(); void setOffset(int offset);

Requires setting the offset to provide accurate results (offset can be measured during runtime). DirectionFinder based on gyro (keeps heading):
public GyroDirectionFinder
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

60

HiTechnic color sensor.


Class:

ColorSensorHT class instance with I2C port;

Constructor: public ColorSensorHT(I2CPort port); Methods: Color getColor(); - returns RGB at once in Color class int getBlue/Green/Red(); int getColorIndexNumber();int getColorID(); int getMode(); int getRGBNormalized(int color); int getRGBRaw(int color); int initBlackLevel(); int initWhiteBalance();
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

61

HiTechnic angle sensor.


Class:

AngleSensor class instance with I2C port;

Constructor: public ColorSensor(I2CPort port); public ColorSensor(I2CPort port, int mode); public ColorSensor(I2CPort port, int address, int mode, int type); Methods: calibrateAngle() getAccAngle() - read accumulated angle getAngle() - read current angle getRPM() - read current rotation speed resetAccAngle() - reset the accumulated angle count
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

62

Other sensors (briefly).


IR Seeker (HiTechnic)IR source directional scanner:

IRSeeker class instance with I2C port; IRSeekerV2 class instance with I2C port; OpticalDistanceSensor class instance with I2C port; NXTCam class instance with I2C port;

Optical distance meter (Mindsensors):

NXTCam (Mindsensors):

Sony PSP2 controller interface PSP-NX (Mindsensors):


PSPNXController class with regular SensorPort;
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

63

Other sensors (briefly).


PFLink power functions over Mindsensors NRLinkNx:

PFMate I2C control LEGO Power Functions IR receiver PFMateMotor Motor class for PFMate PFMotorPort Motorport class for PFMate

Mindsensors line leader sensor for black / white lines:

NXTLineLeader I2C NXTMMX / MMXRegulatedMotor / NXTMMXMotor


Ph.D. Eng. Piotr Czekalski

Mindsensors NXTMMX motor multiplexer:

Version 1.53 | 20th November 2011

64

Other sensors (briefly).


Codatex RFID sensor:

RFIDSensor I2C

HiTechnic EOPD (Electro Optical Proximity Detector) capable to detect very small distance changes, spotlight independent:

EOPD ADSensorPort GPSSensor I2C

Dexter Industries dGPS sensor:

Firgelli linear actuators (motor+encoder based) various models:

LinearActuator MotorPort
Ph.D. Eng. Piotr Czekalski

Version 1.53 | 20th November 2011

65

Other sensors (briefly).


Mindsensors RTC:

RealTimeClock I2C TouchMUX ADSensorPort

Mindsensors touch sensors multiplexer:

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

66

Other sensors (briefly) (cont.).


Multiplexer interface for RCX motors (Mindsensors):

RCXMotorMultiplexer class instance with I2C port; RCXPlexedMotorPort class instance;

Single motor of RCX motor multiplexer:

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

67

RCX sensors connected to the NXT over accessory cable.


RCX Light Sensor:
RCXLightSensor class instance, using LegacySensorPort class;

RCX Motor:
RCXMotor class instance, using BasicMotorPort class;

RCX Rotation Sensor:

RCXRotationSensor class instance, using LegacySensorPort class; RCXTemperatureSensor class instance, using LegacySensorPort class;
Ph.D. Eng. Piotr Czekalski

RCX Temperature Sensor:

Version 1.53 | 20th November 2011

68

NXT to NXT remote connections.


Allows to manage other NXTs through NXT. Communication over BT. NXTs have to be paired. Package: lejos.nxt.remote
Slave LCP compatible firmware Master

LCP compatible firmware

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

69

NXT to NXT remote connections.


Communicates with other NXT using LCP protocol. Supports sensors (class RemoteSensorPort): ADSensorPort, BasicSensorPort, I2CPort, NXTProtocol and SensorConstants. Support motors (class RemoteMotorPort): BasicMotorPort, NXTProtocol, TachoMotorPort,Encoder. A similar to autonomous mode API class RemoteNXT supports almost all features of remote NXT over Bluetooth and LCP protocol.
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

70

Creating user menu on NXT brick.


lejos.util package Class: TextMenu class instance Constructors: public TextMenu(String[] items); public TextMenu(String[] items, int topRow); public TextMenu(String[] items, int topRow, String title); Methods: int select(); int select(int startIdx); int select(int startInd, int timeout);
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

71

Creating user menu on NXT brick (cont.).


Methods:
String[] getItems(); void setItems(String[] items); void setTitle(String title); void quit(); // przydatne dla innego wtku void resetTimeout();

When pressing Esc select returns -1, when canceled by quit() returns -2.
Select is a blocking call, quit() can be called by

other thread asynchronously.


Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

72

PID controller - lejos.util.


Class:

PIDController;

Constructor: public PIDController(int setpoint); Methods: doPID(int processVariable) PID calculation for single iteration freezeIntegral(boolean status) getPIDParam(int paramID) setPIDParam(int paramID, float value)

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

73

Reading NXT brick settings.


Class:
Settings class.

Method:
static Properties getProperties(); static String getProperty(String key, String defaultValue); static void getProperty(String key, String Value);

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

74

Cooperation over Bluetooth.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

75

Cooperation over Bluetooth (cont.).


The other device must provide Bluetooth SPP service (Serial Port Profile over Bluetooth). All classes and interfaces are available in lejos.nxt.comm package. Stream definitions available in java.io Both stream and packed communication possible. Base classes: Bluetooth / BTConnection LCP / LCPBTResponder / LCPResponder USB / USBConnection RS485 / RS485Connection BitBus Network, port 4 Rconsole for tracing
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

76

Tracing and monitoring.


Program execution can be traced on the PC over Bluetooth connection almost realtime. Run LCPBTResponder thread on the NXT brick and NXT Monitor tool at the PC. Sample (NXJ part):
LCPBTResponder lcpThread = new LCPBTResponder(); lcpThread.setDaemon(true); lcpThread.start();

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

77

Debugging.
LeJOS NXJ provides basic support for handling critical situations and finding errors: Runtime exception handling, Debugging over USB. NXJ supports most of standard Java exceptions. Remote debugging requires to communicate with a PC computer by: Calling one following methods in NXJ program:
public static void open(int timeout); public static void open();

Running debugger on PC (nxjdebug) (nxjconsole).


Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

78

Debugging (cont.).
Send text to the debugger:
public static void out(String s);

Close connection with PC debugger:


public static void close();

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

79

Advanced programming techniques.

Programming using pilot and navigator classes. Subsumption programming with behaviours implementation, hierarchy and cooperation.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

80

Navigator classes
lejos.robotics.navigation pack. Generally for Tribots, however servomotor ports can be defined freely. Supports abstract/high level creation of the robot moves. Programming of movements by high level navigator classes (implement Navigator); SimpleNavigator (acutally useless, obsolete) TachoNavigator; CompassNavigator; Uses Pilot or CompassPilot classes to execute desired robot movment and to identify XY localisation. Warning for the inertia of movements, according to the physical phenomenas (see theory vs practice)
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

81

Subsumption programming.
lejos.robotics.subsumption pack. High level definition of the robot behaviour. Pro's: clear and well organized code, clear relations among behaviours, separates behaviours from the low level action implementation, very easy extension by new behaviours.. Flaws: longer project startup time (at the beginning), hard to identify all possibilities, template based project organisation (may be pro),
Version 1.53 | 20th November 2011

difficult debugging.

Ph.D. Eng. Piotr Czekalski

82

Subsumption programming.
Behaviour class must implement lejos.robotics.subsumption.Behavior interface. Interface methods to implement (obligatory): void action(); // main behaviour actions void supress(); // stop/brake executed to stop action(); Won't finish until action(); stops, thus developer must ensure to make action(); safely interruptable by it's own. boolean takeControl(); // tests, if the action shall be executed.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

83

Subsumption programming.
Arbitrator class ensures checking if behaviour shall be executed and calling appropriate actions: lejos.robotics.subsumption.Arbitrator Constructor: Arbitrator(Behavior[] behaviors); // start(); need to be called afterwards void start(); Arbitrator circulates over behaviours with respect to their priority and checks if execution condition is true for takeControl(); behaviors table index is a priority of the behaviour.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

84

Subsumption programming.
If two behaviours are ready to execute, only the higher priority one is executed.
Arbitrator is singlethreaded ! Can loose some environment / sensor events because interaction can disappear before behaviour receives control to check execution condition. Execution buffering is necessary then (foolproof takeControl() method). When higher level behaviour occurs, the lower level behaviour's supress() method is called to break it. One behaviour is never executed one by one. If during next check the same behaviour is still active, next lower priority behaviour is being executed.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

85

Threads, events, delegates.


A dedicated for listeners exists so called listener thread (starts if at least a one delegate has been declared): Button Listeners, Sensor Port Listeners. Provides non-blocking, asynchronous port, button and sensor processing using events and delegates. A dedicated thread (regulator thread) exists for every Interactive Servomotor that ensures command execution in an asychronous way. Bluetooth class runs separated thread for communication. Timer class runs separate thread also. One can create own threads and daemons (inherited from Thread base class). Beware locks and parallel jobs!
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

86

Threads, events, delegates (cont.).


Sample:

import lejos.nxt.*; public class ListenForButtons {


public static void main (String[] args) {

Button.ENTER.addButtonListener(new ButtonListener() {

public void buttonPressed(Button b) {

LCD.drawString("ENTER pressed",0,0);

} public void buttonReleased(Button b) {

LCD.clear(); ();

}); while (true);

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

87

Threads, events, delegates (cont.).

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

88

iCommand is currently obsolete, as replaced by LEJOS NXJ however still provide remote controlled environment even if LEJOS is efficient but autonomous. LCP compatible firmware needed (NXJ or Genuine v. 1.05 or later). Firmware acts as communication broker between PC and NXT brick. Main algorithm is executed on the PC using PC Java virtual machine (JRE) (Windows/Linux/Mac OS). Packages icommand.*(since Lejos 0.8 it is dead)
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

Remote PC-NXJ programming. iCommand

89

Remote PC-NXJ programming. iCommand (cont.).


Latest iCommand library 0.7 Install procedure Unpack to the desired folder (c:\ best) Copy Bluecove<...>.jar to the
<...>\icommand\dist\
(8th May 2009)
th

Latest BlueCove library 2.1.0 (8

May 2009)

Modify system environment classpath variable to contain at least:


.;C:\icommand\dist\icommand.jar; C:\icommand\dist\bluecove.jar;

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

90

Remote PC-NXJ programming. iCommand (cont.).


NXT and PC have to be paired and trusted. COM port (outgoing to the NXT) have to be installed (usually identified by Dev B). COM port above have to be configured with Bluecove library icommand.properties file, containing i.e.: NXTCOMM=COM4 line and provide nxt.btaddress when working with multiple robots BT-visible. Above works with Microsoft Windows XP, WIDCOMM or genuine Microsoft Bluetooth stack. Other stacks and OS may need RXTX library utility.
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

91

Remote PC-NXJ programming. iCommand (cont.).


icommand.nxt package like lejos.nxj but limited. icommand.navigation package like lejos.robotics.navigation but limited icommand.nxt.comm package like lejos.nxj.comm but really small part implemented icommand.vision pack for environment image recognition using PC webcams (not related to the Mindsensor's NXTCam).

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

92

Remote PC-NXJ programming. lejos.nxt.remote.


Provides a low level access to the remote NXT using LCP. Sensor reading class InputValues Actuators / output: OutputValues class, RemoteMotor class, three instances of motors: (i.e. Motor.B.Forward) RemoteMotorSensor class. Helper classes: NXTCommand low level communication, RemoteBattery, DeviceInfo, FileInfo, FirmwareInfo.
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

93

LeJOS PC API is designed for Oracle / Sun Java classes.jar libraries. Subset of autonomous packages, similar programing and behaviour. Firmware acts as communication broker between PC and NXT brick (using LCP). Main algorithm is executed on the PC using PC Java virtual machine (JRE) (Windows/Linux/Mac OS).

Remote PC-NXJ programming. LeJOS PC API.

Works with almost any firmware that supports LCP (in theory).
Version 1.53 | 20th November 2011 Ph.D. Eng. Piotr Czekalski

94

Packages:

Remote PC-NXJ programming. LeJOS PC API (cont.).


lejos.nxt genuine NXT sensors, servos. lejos.nxt.addon - 3rd party sensors, motors, multiplexers and so on. lejos.nxt.remote NXT2NXT communication over Bluetooth protocol (managed from PC through other brick). lejos.pc.comm Bluetooth and USB communication interface lejos.util (partially different from autonomous implementation) lejos.geom

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

95

Packages:

Remote PC-NXJ programming. LeJOS PC API (cont.).


lejos.robotics hal for other robotics packages lejos.robotics.localization lejos.robotics.mapping lejos.robotics.navigation lejos.robotics.objectdetection lejos.robotics.pathfinding

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

96

Literature
Maximum LEGO NXT: Building Robots with Java Brains, Brian Bagnall, 2007. On-line: LeJOS Tutorial, http://lejos.sourceforge.net/nxt/nxj/tutorial/index.htm, August 2011. On-line: NXJ API, http://lejos.sourceforge.net/nxt/nxj/api/index.html, August 2011. On-line: PC NXJ API: http://lejos.sourceforge.net/nxt/pc/api/index.html, August 2011. On-line: iCommand API: http://lejos.sourceforge.net/nxt/icommand/api/index.html, December 2010.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

97

This is the end. Thank you for your attention.

Version 1.53 | 20th November 2011

Ph.D. Eng. Piotr Czekalski

98

You might also like