MINDSTORMS NXT with JAVA A guide to getting started Berry Nall Robotics Coach, Roma High School I. Introduction What is leJOS? “leJOS NXJ is a Java programming environment for the LEGO MINDSTORMS NXT ®. It allows you to program LEGO ® robots in Java.” http://lejos.sourceforge.net/nxt/nxj/tutorial/Preliminaries/Intro.htm Why would educators use leJOS? Introduce students to text-based programming Inspire student interest in JAVA, a widelyused programming language Allow students to take advantage of leJOS’ superior performance At the 2011-12 TCEA Region One Advanced Arena competition, the top 3 robots and at least 4 of the top 6 finishers were programmed using text-based programming (leJOS or RobotC). Other Advantages of leJOS (compared to NXT) Faster loading and program execution Programs use less memory Greater accuracy Scalable Free! Who can learn to use leJOS? Anyone here (and your students) can learn enough to program for a TCEA competition. We hope some of our students will go far beyond what we can do. II. Eclipse IDE An Integrated Development Environment is a program that allows you to write (and run) programs. Uploading leJOS to the brick If something goes wrong? The brick will most likely make a ticking noise. 1. Reinstall the NXJ software using the LEGO-supplied program. 2. Upload the leJOS software again. Exploring Eclipse Upload program Locate program files Edit program Upload status Eclipse helps In Eclipse, the view is called a “perspective.” Students may change it accidentally, causing confusion. This view is saved as the “leJOS” perspective. Current perspective To restore, click “Window”, then choose “Reset perspective” Eclipse helps Eclipse tells you if there is a typing error or an illegal command: Eclipse helps Click here to load a program: III. JAVA basics JAVA Rules to remember Capitalization matters! Ex: tcea is not TCEA is not tCeA Every command ends with a semicolon // means you are inserting a comment; everything after the slashes is ignored IV. Writing programs Program Sections Import statements File name Define pilot, sensors import lejos.nxt.*; import lejos.robotics.navigation.*; import lejos.util.Stopwatch; /** *comment to name author (please change!) * */ public class TCEA { //Variables and declarations go here public static void main(String[] args) { // program goes here } Commands for robot to follow } To start: modify TCEA.java for your robot Experimentally determine the wheel diameter. Make the “travel” statement a comment and remove the comment slashes from the “rotate” statement. Experimentally determine the wheel base. Note: the wheel base may need to be modified for different surfaces or designs even if the actual size doesn’t change. Test your robot: TCEA1rectangle.java If the wheel diameter and wheelbase are set correctly, the robot will travel in a rectangle and arrive where it started. To program your robot: save the modified TCEA.java with a new name. Change the class to the name of your file. Add commands to make the robot move.