LEGO NXT Robot Programming

17
LEGO NXT Robot Programming Introduction to Programming a Lego NXT robot in Java

description

LEGO NXT Robot Programming. Introduction to Programming a Lego NXT robot in Java. Objectives. Software requirements Java Libraries Creating a Java forms application Running Java applications Creating objects and initialisation Robot navigation Using a timer The touch sensor - PowerPoint PPT Presentation

Transcript of LEGO NXT Robot Programming

Page 1: LEGO NXT Robot Programming

LEGO NXT Robot Programming

Introduction to Programming a Lego NXT robot in Java

Page 2: LEGO NXT Robot Programming

Objectives• Software requirements• Java Libraries• Creating a Java forms application• Running Java applications• Creating objects and initialisation• Robot navigation• Using a timer• The touch sensor• Displaying a message box• Pausing the program to enable the robot to respond• The ultrasonic sensor

Page 3: LEGO NXT Robot Programming

Software Required

• NetBeans Java development software– SE version is sufficient

• LeJOS Java robot API– PC installer– Download special firmware to the robot– Programs can run on the PC (controlling the robot)– Programs can be downloaded and run on the

robot

Page 4: LEGO NXT Robot Programming

NetBeans

Page 5: LEGO NXT Robot Programming

Libraries required

• Copy libraries to folder within project:– C:\Program Files\leJOS NXJ\lib\pccomm.jar– C:\Program Files\leJOS NXJ\lib\pctools.jar– C:\Program Files\leJOS NXJ\3rdparty\lib\

bluecove.jar• Link to project within NetBeans

– Right click project– Select Libraries category– Add Jar/Folder button

Page 6: LEGO NXT Robot Programming

Forms Development

• Select controls from palette• Set properties• Rename variable (right click control)• Attach an event handler

– Double click control– Select “Create new action”– Choose a name for your event-handler– Code will be created for the method

Page 7: LEGO NXT Robot Programming

Running a NetBeans App

• To run from within NetBeans– Make sure the required project is the currently highlighted one

• Right click project and select “Set as Main project”– Use the Green arrow on the toolbar– Or right click the project name and select “Run”– Interrupt if necessary using Run/Stop build/run from the menu

• To run from outside NetBeans– Use the hammer icon on the toolbar or right click project and select

“Build”– This will create an executable JAR in the dist folder of the project– Double click the JAR file to run

Page 8: LEGO NXT Robot Programming

Java Coding (General)

• Import external Java classes (similar to Using statements in C#)import lejos.nxt.*;import lejos.robotics.navigation.*;

• Instance variables– Placed at the bottom of form code:

private TachoPilot pilot;

• DO NOT Modify auto-generated code

Page 9: LEGO NXT Robot Programming

Initialisation and creating objects

• This will normally be done in the constructor• The constructor already contains a lot of auto-

generated code• Add your code at the end• Suggestion:

– Write your own initialisation method– Call this from the bottom of the constructor– This means only one line needs to be added to the

constructor and keeps your code separate

Page 10: LEGO NXT Robot Programming

Auto-generated code

Custom initialization method

Call custom initialisation

Page 11: LEGO NXT Robot Programming

Robot Navigationimport lejos.robotics.navigation.*;

• Create pilot object (in initialisation method)private TachoPilot pilot;pilot = new TachoPilot(2.1f, 4.4f, Motor.A, Motor.C, true);

• Set speed of motionpilot.setMoveSpeed(3);pilot.setTurnSpeed(20);

• Move and rotatepilot.travel(-12);pilot.rotate(90);

11

The Tribot has the motors on backwards so needs negative number for distance

Page 12: LEGO NXT Robot Programming

More Navigation Methods

• Methods exist to move the robot continuously:pilot.backward();pilot.forward();pilot.stop();

• Remember if your robot has the motors on backwards then backwards will move forwards etc!

• It is also possible to obtain the maximum available speeds:float fMaxMoveSpeed = pilot.getMoveMaxSpeed();float fMaxTurnSpeed = pilot.getTurnMaxSpeed();

Page 13: LEGO NXT Robot Programming

Creating a Timer

• Create an instance variableprivate Timer touchTimer;

• Create a timer object, which includes the interval and an “action listener” method which is called for each timer tick touchTimer = new Timer(50, new ActionListener() { public void actionPerformed(ActionEvent e) { touchTimerTick(); }});

• Start or stop the timertouchTimer.start();touchTimer.stop();

Page 14: LEGO NXT Robot Programming

The Touch Sensor

• Create an instance variableprivate TouchSensor touchSensor;

• Create the object and attach to the porttouchSensor = new TouchSensor(SensorPort.S1);

• Check if the sensor is pressedif (touchSensor.isPressed()) …………

Page 15: LEGO NXT Robot Programming

Simple Message Box

• Uses a class from the Java “Swing” libraryimport javax.swing.JOptionPane;

• A simple message boxJOptionPane.showMessageDialog(null, "Message");

• A message box with a title and message typeJOptionPane.showMessageDialog(null, "Message", "Title", JOptionPane.ERROR_MESSAGE);

• A variety of other message types are available

Page 16: LEGO NXT Robot Programming

Adding a Pause

• Use the Thread.sleep command but requires exception handling to be added//include a 25ms delay to pick up valuestry{ Thread.sleep(25);}catch(Exception e){ JOptionPane.showMessageDialog(null, "Error in pause"); return;}

Page 17: LEGO NXT Robot Programming

The Ultrasonic Sensor

• Created in the same way as the touch sensor• To obtain the value should first “ping” the

sensorultrasonicSensor.ping();

• Then pause for at least a 25ms delay (see previous slide)

• Distance value can then be obtainediDist = ultrasonicSensor.getDistance();