LEGO NXT Robot Programming Introduction to Programming a

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 Displaying a message box Pausing the program to enable the robot to respond The ultrasonic sensor

Software Required • Net. Beans Java development software – SE version is sufficient • Le. JOS 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

Net. Beans

Libraries required • Copy libraries to folder within project: – C: Program Filesle. JOS NXJlibpccomm. jar – C: Program Filesle. JOS NXJlibpctools. jar – C: Program Filesle. JOS NXJ3 rdpartylibbluecove. jar • Link to project within Net. Beans – Right click project – Select Libraries category – Add Jar/Folder button

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

Running a Net. Beans App • To run from within Net. Beans – 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 Net. Beans – 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

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 Tacho. Pilot pilot; • DO NOT Modify auto-generated code

Initialisation and creating objects • This will normally be done in the constructor • The constructor already contains a lot of autogenerated 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

Auto-generated code Call custom initialisation Custom initialization method

Robot Navigation import lejos. robotics. navigation. *; • Create pilot object (in initialisation method) private Tacho. Pilot pilot; pilot = new Tacho. Pilot(2. 1 f, 4. 4 f, Motor. A, Motor. C, true); • Set speed of motion pilot. set. Move. Speed(3); pilot. set. Turn. Speed(20); • Move and rotate pilot. travel(-12); pilot. rotate(90); The Tribot has the motors on backwards so needs negative number for distance 11

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 f. Max. Move. Speed = pilot. get. Move. Max. Speed(); float f. Max. Turn. Speed = pilot. get. Turn. Max. Speed();

Creating a Timer • Create an instance variable private Timer touch. Timer; • Create a timer object, which includes the interval and an “action listener” method which is called for each timer tick touch. Timer = new Timer(50, new Action. Listener() { public void action. Performed(Action. Event e) { touch. Timer. Tick(); } }); • Start or stop the timer touch. Timer. start(); touch. Timer. stop();

The Touch Sensor • Create an instance variable private Touch. Sensor touch. Sensor; • Create the object and attach to the port touch. Sensor = new Touch. Sensor(Sensor. Port. S 1); • Check if the sensor is pressed if (touch. Sensor. is. Pressed()) …………

Simple Message Box • Uses a class from the Java “Swing” library import javax. swing. JOption. Pane; • A simple message box JOption. Pane. show. Message. Dialog(null, "Message"); • A message box with a title and message type JOption. Pane. show. Message. Dialog(null, "Message", "Title", JOption. Pane. ERROR_MESSAGE); • A variety of other message types are available

Adding a Pause • Use the Thread. sleep command but requires exception handling to be added //include a 25 ms delay to pick up values try { Thread. sleep(25); } catch(Exception e) { JOption. Pane. show. Message. Dialog(null, "Error in pause"); return; }

The Ultrasonic Sensor • Created in the same way as the touch sensor • To obtain the value should first “ping” the sensor ultrasonic. Sensor. ping(); • Then pause for at least a 25 ms delay (see previous slide) • Distance value can then be obtained i. Dist = ultrasonic. Sensor. get. Distance();
- Slides: 17