Basic Motor Programming


Topics
Motors
Sleeping

Handouts
Cheat Sheet on todays commands

Lesson
Begin with this sample program. Ask the class what it does. Have the class go through the whole program and explain what each part does.
import josx.platform.rcx.*;
public class Hello
{
   public static void main (String[] args)
   throws Exception
    {
       Motor.A.forward();
       Motor.C.forward();
       Thread.sleep(2000);
       Motor.A.stop();
       Motor.C.stop();
   }
}

Once the students understand the program ask them how it could be changed so that instead of going forward the robot turned for 2 seconds.
Note: I have found it helpful to use the analogy of a wheel chair to explain the concept of turning. So if I was in a wheel chair and the wheel on my left was motor C and on my right was motor A, how do I turn the chair?


Now have the students walk through putting this program on to their robot. Start by having the students open a text editor and copying down this program into their text editor.
NOTE: I heavily recommend the use of some sort of "smart" editor or at least one that colors the text. I use a program called TextPad which is very simmiler to Notepad except it colors text and if properly setup will compile and download RCX programs. Instructions on downloading and setting up TextPad can be found here However, programs like NotePad will also work.
Walk the students through compiling, linking and downloading the program onto their RCX. (The instructions for d\ oing this can be found here.)

Common Problems:
If you are using Notepad it automatically saves the file as a text file unless the "Save as Type" is set to "All Files". If lejos can't find your file try saving it again and make sure "Save as type" is set to "All Files"
If compiling and downloading in DOS and it can't find the file make sure that you are in the same directory as the file.


Now that all the students have got that program working explain the program to them line by line.

    import josx.platform.rcx.*;

This line ALWAYS needs to be included at the top of ALL programs writen for the RCX. Whenever we use a command in a program line LCD.clear() the RCX has to know what that means. This line tells the program where to find a "library" of all the terms. (If you are using TextPad the lejos command "links" the program it looks at this line to find the "library" and uses it to interpret the code.)

    public class Hello

This line tells the program that it's name is Hello and it is of type public which means any other program can ru\ n it. All programs we will be writing in these lessons will be type public.

    {

This is a begining bracket. Everything between the { } brackets is part of the program called Hello.

    public static void main (String[] args)

This line needs to be in every program. This denotes the "main" method. (we will learn about methods latter) This is what will be run when the program starts.

    throws Exception

This line deals with exceptions. For now just type it in exactly as shown here. We will explain exceptions latt\ er.

    {

This is another begining bracket. This one begins the main method, the main method ends with the } bracket. <\ BR>
    Motor.A.forward();
    Motor.C.forward();

This tells the RCX to turn on Motors A and C both in the forward direction.
    Thread.sleep(2000);

This tells the program to wait or sleep for 2000 milliseconds.

    Motor.A.stop();
    Motor.C.stop();

This tells the RCX to stop both motors A and C. Note: this means that not only are they turned off but "breaks" are applied. If you want them to turn off and free spin use the flt() command.

      }
    }


These two lines end the main method and the Hello class.

Have the students divide into their groups. (Groups of 3 are optimal) Have each group program their robots to go forward, turn at least 360 degrees and go backwards. After they compleat that have each group design a short dance for their robot to be shown off at the end of the day.
The main point of this lesson is to get the students used to programming the robots. It is very easy to rush through motors and cover them very quickly. However, students need to have a firm understanding of the basics before they try the complex programs.