Robots

back

It's finally time to start programming the Robots! Step 1 is making sure all the software is installed, and correctly. It is a bit of a pain, especially for those who have 64-bit machines.


Installing Software

Right now, you should have the NXT USB drivers, and Eclipse. Now we need to get the LeJOS software and eclipse plugin. LeJOS is a 32-bit program, which is fine as long as you install the 32-bit version of everything. That includes the JDK.

  1. Install JDK 1.6. Remember to download the 32-bit version (x86).
  2. Install LeJOS NXT v0.9.0. After the install, it will prompt you to install the LeJOS NXT Firmware onto the NXT brick. If you don't have NXT v0.9.0 on your brick (it will say LeJOS 0.9.0 on the startup splash screen if you do), go ahead and flash your brick.
  3. Install the LeJOS plugin in Eclipse. In Eclipse, go to Help > Install New Software.... From here, click "Available Software Sites". You should see a screen similar to below:
    Installing LeJOS plugin

    Click the Add button, and add LeJOS (url: http://lejos.sourceforge.net/tools/eclipse/plugin/nxj/). After you have added the site, you should be able to go back to the Install software screen, and select LeJOS in the Work with drop-down box.
    Installing LeJOS plugin

    Check LeJos NXT Support, and complete the install process.

    After Eclipse restarts, go to Window > Preferences and make sure the NXT_HOME field is filled in accordingly. Installing LeJOS plugin


Your First Java NXT Program

As usual, we will work on a "Hello World" program for the Robot's LCD Screen. First, lets figure out how to print to the LCD screen, by examining the LeJOS API.

Hello World NXT

Hello World NXT

So, we know we can use the LCD.DrawString method in order to write to the LCD screen. Let's create a new Eclipse project, and create a HelloWorld class. In order to use the NXT API, we must convert the project to a LeJOS project: Hello World NXT

Now, we can write the Hello World code:

        public static void main (String[] args)
	{
	      LCD.drawString("Hello World",3,4);
	      try
	      {
	           Thread. sleep(2000);
	      }catch(Exception e){}
        }
      
Once you have coded this in (We will discuss this in class), make sure your robot is connected via USB cable to your computer, and make sure it is on. From there, right click on your file, select Run As, and select LeJOS NXT Program. This should compile, copy the file to your robot, and execute. If you have any problems, let me know. Hello World NXT


Controlling Motors

To get started, let's checkout Motor in the LeJOS API. This is how we are going to access the motors connected to the robot. The Methods necessary to operate the motors are located here.

The Basic Movement Example

      import lejos.nxt.*;

      public class Move {
        public static void main (String[] aArg)
	{
	   Motor.A.setSpeed(720);
	   Motor.B.setSpeed(720);
	   Motor.A.forward();
           Motor.B.forward();
	   try
	   {
	      Thread.sleep(1000);
	   }catch(Exception e){}
			            
           Motor.A.stop();
           Motor.B.stop();
	}
      }
      
This sets the speed of the motors connected to port's A and B on the robot to rotate 720 degrees/second. It then tells them to go forward. The robot will perform forward motion for 1 second (1000 miliseconds), and then stop both motors.
Exercise 28: How would you perform a turn using these methods?


Exercise 29: Lets try to write a program that can navigate the track you followed using sensors yesterday.