building(robots(with(nxt(and(lejos( introduc
TRANSCRIPT
21/10/2011
1
Building Robots with NXT and LEJOS
Introduc<on
Michael Wooldridge (mjw @ liv.ac.uk)
What is the NXT Robot Pla@orm?
• A simple, cheap, well supported pla6orm for experimental robo;cs, manufactured by LEGO
• Comes with a collec;on of LEGO-‐manufactured sensors and actuators, and has a number of third party sensors & actuators available
• The “brain” is the NXT brick
21/10/2011
2
The NXT Brick
The NXT Brick
21/10/2011
3
The NXT Brick • 3 ports for actuators (named A to C)
• Mainly used for motors
• 4 ports for sensors (named 1 to 4) connected via RJ12 (phone-‐like) connector
• Touch sensors, colour sensors, … • LCD display panel (monochrome, 100x64 resolu;on)
• Programs can write to this panel at pixel level
• 4 Input buWons (leX, right, enter, escape) • USB port • Mainly used for downloading programs to the brick
• Bluetooth connec;on • Allows robots to communicate with each other
The NXT Brick
• The brick is a baWery-‐powered computer, specifically intended for robot control
• 32-‐bit ARM7 processor, running at 48MHz
• 256 Kbytes non-‐vola;le flash storage • mainly used for storing programs
• 64 Kbytes RAM
• run;me memory
21/10/2011
4
Ultrasonic Sensors
• Essen;ally sonars • Used to find the distance to obstacles • Work by transmidng ultrasonic signal, and ;ming how long it takes for the signal to get back
• Works up to about 255cm, precision of ±3 cen;meters
• !!CAUTION!! Ultrasonic sensors interfere with each other, if others are using ultrasonics nearby, your robot might get confused
Colour Sensors
• Can be used to iden;fy the colour of objects immediately in front of them
• Simplest use is to detect colour patches (eg tracks) immediately underneath the robot
• Mainly good for red, green, blue, white, black
21/10/2011
5
Touch Sensors
• Detect sensor being pressed • Usually used to make bumpers for forward colision detec;on, and on other sides to detect other vehicles driving into us
• Return a boolean value, • True = depressed • False = not depressed • If we want many touch sensors, we can use a mul-plexer
Servomotors • The basic actuators we will use • Not simply “on/off” motors, but servo motors
• Allow precise posi;oning of motors, fine degree of control of rota;on
• Surprisingly powerful, with sophis;cated gear mechanism
• Simplest use is to connect to wheels and use for locomo;on
• More sophis;cated uses include:
• Building robot arms
• Controlling ultrasonic sensor to give 360° coverage
21/10/2011
6
Standard Configura<on Robot
• For most of the course, we will use robots in a standard configura;on
• Sensors • Port 1 – ultrasonic (facing forward) • Port 2 – colour sensor (at rear) • Port 3 -‐ empty
• Port 4 – touch sensor (front bumper)
Standard Robot Configura<on
• Robot with caterpillar tracks • Output port A – right motor (i.e., right from robot’s perspec;ve)
• Output port B – leX motor
• Output ports C,D -‐-‐ empty
21/10/2011
7
Standard Configua<on Robot
Standard Configura<on Robot
Front bumper Touch sensor
Front-facing Ultrasonic
sensor
motors on left and right
21/10/2011
8
Standard Configura<on Robot
ground facing colour sensor
at rear
Programming NXT Robots: The Default Programming Environments
• Our of the box, NXT bricks come with a LEGO-‐provided opera;ng system
• Programs are developed on a PC with a graphical development environment, and downloaded to the robot via USB
• This development approach is good for small children, wimps, and wusses
21/10/2011
9
LEJOS • LEJOS provides an alterna;ve opera;ng system for NXT robots
• An implementa;on of the JAVA virtual machine for NXT
• Not all Java language features supported! • Not all Java classes supported! • The LEJOS environment provides a much richer, object-‐oriented
development environment
• Special classes provide interface for sensors and actuators • Main drawbacks: limited onboard RAM, limited onboard flash
memory for storing programs (JAVA not renowned for being compact)
LEJOS Development Cycle
1. Develop code on your PC (using eg Eclipse) 2. Compile & link
3. Download linked binary (.nxj) to robot via USB 4. Run Main problem: debugging problem code on robots is hard – very limited UI, oXen no feedback on what problem is.
21/10/2011
10
HelloWorld.java
import lejos.nxt.*; public class HelloWorld { public static void main (String[] args) { System.out.println("Hello World"); Button.waitForPress(); } }
import lejos.nxt.*;
• Makes all the LEJOS classes available by default
21/10/2011
11
public class HelloWorld
• Just as in regular Java, LEJOS development is based on OO principles
• So, the program is built from a set of classes
public static void main (String[] args)
• Just like regular Java, LEJOS needs a class that contains a method called main• This method must be declared • public• static • void• With arguments as indicated
21/10/2011
12
System.out.println("Hello World"); Button.waitForPress();
• If our main(…) method just contained the println(…) statement, then the text would print, but then the program would terminate and the LCD panel would revert to the default – text would disappear
• Button.waitForPress() causes the program to suspend un;l a buWon (any buWon) is pressed.
Using Motors: The NXTMotor Class import lejos.nxt.*;import java.lang.*; // for Threadpublic class ProgOne { public static void main (String[] args) throws Exception { NXTMotor MA = new NXTMotor(MotorPort.A); NXTMotor MB = new NXTMotor(MotorPort.B); MA.setPower(100); //power to 100% MB.setPower(100); Thread.sleep(2000); // suspend for 2 seconds MA.stop(); // stop MB.stop(); }}
21/10/2011
13
Variables
• No;ce that we have completely standard Java variable declara;ons:
NXTMotor MA = new NXTMotor(MotorPort.A);NXTMotor MB = new NXTMotor(MotorPort.B);
• Here, MA and MB are just standard (object) variables
• Can use int, float, etc
The NXTMotor Class • Class constructor takes a single argument, which must be one of the constants A, B, or C defined in the MotorPort class
• Other methods (actually from BaseMotor interface, which NXTMotor implements): forward() motor forward
backward() motor backwards
flt() cuts power to motor, which “floats” to a halt
stop() stops the motor (not the same as flt!)
isMoving() returns true if motor is in mo;on
setPower(int p) set power of motor (0 <= p <= 100)
int getPower() get power of motor (returns value in range 0..100)
21/10/2011
14
Using Sensors: The TouchSensor Class import lejos.nxt.*;Import java.lang.*;public class ProgTwo { public static void main (String[] args) throws Exception { NXTMotor MA = new NXTMotor(MotorPort.A); NXTMotor MB = new NXTMotor(MotorPort.B); TouchSensor T = new TouchSensor(SensorPort.S4); MA.setPower(100); // go! MB.setPower(100); while(! T.isPressed()) { Thread.sleep(200); // motors will keep running! } MA.stop(); MB.stop(); }}
Using Sensors: The TouchSensor Class
• The TouchSensor class has a very simple interface!
• Provides one method: boolean isPressed()
21/10/2011
15
Using Sensors: The UltrasonicSensor Class import lejos.nxt.*;Import java.lang.*; public class ProgThree { public static void main (String[] args) throws Exception { NXTMotor MA = new NXTMotor(MotorPort.A); NXTMotor MB = new NXTMotor(MotorPort.B); UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1); MA.setPower(100); MB.setPower(100); while(!(us.getRange() < 20) ) { // 20cm threshold Thread.sleep(200); } MA.stop(); MB.stop(); }}
Using Sensors: The ColorSensorHT Class
import lejos.nxt.*;import lejos.nxt.addon.ColorSensorHT;public class ProgFour { public static void main(String[] args) throws Exception{
ColorSensorHT cs = new ColorSensorHT(SensorPort.S2);
while( !Button.ESCAPE.isPressed() ) {
LCD.clear(); LCD.drawString("ID = "+cs.getColorID(),0,2); Thread.sleep(500);
}
}}
21/10/2011
16
getColorId()
• 0 = black • 1 = violet • 2 = purple • 3 = blue • 4 = green • 5 = lime
• 6 = yellow • 7 = orange • 8 = red • 9 = crimson
• 10 = magenta
• 11 to 16 = pastels • 17 = white
WA
RN
ING
: you’ll have to do your own calibration! W
AR
NIN
G: y
ou’ll
hav
e to
do
your
ow
n ca
libra
tion!
LEJOS at the Command Line
• Three main tools:
• nxjc – the LEJOS Java compiler
• nxjlink – the LEJOS linker • nxjupload – uploads files to NXT brick
21/10/2011
17
nxjc – The LEJOS Java Compiler
• The LEJOS replacement for javac
• Takes as input a “.java” file • Produces as output a “.class” file
[mjw@mjw-imac]$ nxjc ProgFour.java [mjw@mjw-imac]$
nxjlink – The LEJOS Linker
• Normally in JAVA, linking is done dynamically, when you run the JAVA interpreter/virtual machine over your “.class” file
• (Standard JAVA is dynamically linked: referenced classes are loaded, as needed at run -me. This doesn’t work on NXT because of lack of memory on the brick. Classes are sta-cally linked, i.e., linked at compile ;me.)
[mjw@mjw-imac]$ nxjlink ProgFour -o ProgFour.nxj[mjw@mjw-imac]$
21/10/2011
18
nxjupload – Uploading Files to the NXT Brick
• Will try USB and Bluetooth
• Make sure brick is switched on and showing main menu
[mjw@mjw-imac]$ nxjupload ProgFour.nxjFound NXT: NXT 001653119113leJOS NXJ> Connected to NXTleJOS NXJ> Upload successful in 997 milliseconds[mjw@mjw-imac]$