Contact:
B. Dean Pershall
(505) 346-7737
ST247



ATE Home | Engineering Program | Electronics Technology | Natural Resources | Geospatial Information Technologies


Lesson 2.0: Your first programs

Introduction:

In the last lesson you learned how to open programs and run them. Now you will learn how to write them. This manual will assume that you have never programmed before and will start at the very basics of programming. We will then gradually step of the complexity, and when done you will have mastered many programming skill.
First of all we should discuss exactly what is programming. Programming is a way in which humans can give computers instructions which they can understand and then carry out. There are different levels of programming, from high level languages, to low level languages. A computer language acts as a translator between humans and computers. Computers think in purely digital terms. At their most basic level computers think entirely in ones and zeros in a language called binary code. Computers only know the difference between yes, and no or high and low, or true and false. This is the essence of how computers work. However people for the most part do not think or communicate in binary. This is where the computer languages come into play. Computer languages take human words and translate them into the binary code that computers can understand. Low level languages such as assembly lie closer to the computer way of thinking rather than the human way of thinking. While this allows for some of the fastest, and most efficient programs to be made, it is significantly more difficult to program in lower level languages than higher level languages. High level languages such as BASIC, C, C++, and JAVA add a certain layer of separation between humans, and the most basic levels of computers. This allows for sophisticated code to be generated with greater ease. This is not to say that programming in high level languages is easy, for it is not. Even high level languages require you to learn a new way of communicating. As you begin to program you will begin to think logically through your problems in order to generate solutions. This requires skills unique to the world of computing, which require nothing less than a great deal of practice. In this manual you will learn the skills necessary to program a high level language JAVA, and will acquire the unique way of thinking that programmers, and engineers have.

Review the sample programs included in the projects folder to better understand the fuzzy logic that makes your robot tick.

Hello World:

A tradition in computer programming is to make the “Hello World” program the first program you make whenever you are learning or trying a new programming language. The goal of the Hello World program is simple, to display the words "Hello World onto the monitor of the computer. Doing so achieves several things. First of all it shows that you understand the basics of what is needed to make a program. It also shows that you understand the basic syntax (grammar and vocabulary) of a computer language. In addition it shows that your program can interact, in some way with the outside world, thus Hello World.

1. Turn OFF the RoadRunner, and remove power.

2. Open up the Javelin Stamp IDE program.

3. Now open up a new file. To do so select new from the file menu, or press the "ctrl" key and the "n" key at the same time. The editor should look similar to the one below

4. Now it is time to begin writing your program. First of all you need to tell the compiler what libraries it needs. The compiler is the part of the JAVELIN STAMP IDE program that will read your program and translate it into commands that the computer can understand. Libraries are sets of programs that have already been written that you can use in your programs. Basically speaking the computer does not inherently know how to do anything. Thus in order to carry out even the simplest of tasks such as displaying a character to the monitor, a program must be written in order to tell the computer how to carry out the task. However, if you had to re-write every step, in every program that you create it would take an enormous amount of time to write even small programs like the hello world program. Thus, languages such as JAVA let you reuse or call other programs in your program. However, before you can do this you have to tell the computer which program libraries you are going to use. Therefore start off by writing the following line of code (each command you give the computer will be on a different line, and is called a line of code)

import stamp.core.*;

This line of code lets your program use many of the more common STAMP abilities. Take care to note that there is a semicolon at the end of the line. Every line of code must end in a semicolon, this is how the computer knows that you are done with your line of code.

Now it is time for you to create your class. JAVA is an object oriented programming language. Thus, every program must have a class. This is how other programs will be able to call (use) your program, and functions found inside of your program. Basically you will create a class in each program that you write. Then inside of the class you will create functions. Functions are essentially a block of your program that carry out a specific task. For example say you are writing a program that will act as an electronic calendar. You would then create the class called calendar. Then inside of the the class you would have functions, such as a function that displays the current date. You may call this function display_date. Now say you are writing a different program, and this program also needs to display the date. Instead of having to re-write the display_date program you could just add the line import libs.calandar (this changes based on where you saved your program) to the top of the new program. Then by simply adding the line calendar.display_date(); your program will display the date. In essence as you write more and more programs, you can intermix and re-use the programs that you or someone else have already written. In other words there is no need re-write code, you can simply recycle the work that you have already done. If you are confused about this, don't be too concerned, it will become clear as you gain experience with JAVA. For now add the line that will create your class.

    public class hello_world
{

Note the word public. This means that any program may be able to access and use your class. Also note that there is no semicolon on this line. In addition note the open bracket. This signifies the start of the class hello_world This is how the computer knows that the class has begun. Later on you will have a close bracket to let the computer know that the class is done.


Now it is time to create a function within the class. In this case we will create a function called main. Main is a very important function. Main is first function that will be used by the Stamp. It is the master function, and is what will call other functions. Your program starts with main, and ends when main is done. While you can create a class without a main, it is not capable of standing alone by itself. In order for you to download a program into the RoadRunner it must have a main. However, it can only have one main. Thus when you create your class you need to decide whether it will be the primary class that will call upon other classes, or if it is a class that will not have a main, and thus will run when called upon. In later programs you will better be able to see and understand this relationship. For now, we will just focus on the Hello World program, and the main function. So now create your main function.


    public static void main()
{

At this point you now need to give the main function something to do. Well in this case we want it to display the message Hello World. How do you do that? Remember the import line that you put on the top of your program. It just so happens that there is a class that is included inside of the stamp.core library that does exactly what you need. This class is called System, and within System there is a class called out, and within out there is a function called print. So now we can use the function print to display a message to screen whit the following line.

System.out.print()

But now you must tell Sytem.out.print what to print to the screen, by passing it some information. In this case you want to display text. Thus inside of the parenthesis add the following.

    "Hello World"


Thus the line of code to display the message will look as follows

    System.out.print("Hello World");

Don't forget the semicolon. Now you are done with main, so you must let the computer know that you are done. To do this, add a close bracket.

	}


Now you could add another function to the hello_world class, but for now you are done. Thus end the class with another close bracket.

	}


Take care to keep track of the open and close brackets as it can get confusing as you add more and more functions to your class. Your entire program should look as follows.

 import stamp.core.*;
 public class hello_world
 {
  public static void main()
    {
  		System.out.print( "Hello World" );
	}
  }
  

5. Turn on the RoadRunner To do so, place the switch into the NC position, after applying power to the robot. The light should go on.

6. Now run the program. To do so, select project and then program on the task bar.

7. The output of your program should look as follows. If not go over your code (program) and look for a mistake.

8. There is however, a problem with this program. While it will run, it is formatted poorly. In order to make your code (program) more readable by others you need to add comments to your program. First lets add a description of the program at the top of the program. To do this write your description between the following symbols /* and */ The computer will ignore anything between these symbols.

 

9. Now add comments to the lines inside of your program. To do this add the symbol // to a line of code, and then write your comment. The computer will ignore anything after the // until the next line of code.

10. To make the code even more readable, use tabs to indent your code

This completes your introduction to the RoadRunner Review the sample programs included in the projects folder (some of these programs require different sensors not included with your RoadRunner). Have fun and let us know of your developments with the RoadRunner!