1 / 17

LEGO NXT Robot Programming

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

maja
Download Presentation

LEGO NXT Robot Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LEGO NXT Robot Programming Introduction to Programming a Lego NXT robot in Java

  2. 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

  3. 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

  4. NetBeans

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. Auto-generated code Call custom initialisation Custom initialization method

  11. Robot Navigation import 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 motion pilot.setMoveSpeed(3); pilot.setTurnSpeed(20); • Move and rotate pilot.travel(-12); pilot.rotate(90); The Tribot has the motors on backwards so needs negative number for distance

  12. 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();

  13. Creating a Timer • Create an instance variable private 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 timer touchTimer.start(); touchTimer.stop();

  14. The Touch Sensor • Create an instance variable private TouchSensortouchSensor; • Create the object and attach to the port touchSensor = new TouchSensor(SensorPort.S1); • Check if the sensor is pressed if (touchSensor.isPressed()) …………

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

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

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

More Related