CS91 Building Robots
Lab Assignment--Week 2

This week we will focus on learning about the miniboard. First we will use the HEXMON program to control motors and sensors via direct commands to the miniboard. Then we will write programs in Micro C and download them to the miniboard for testing.

The Robotics Lab

Notice that there are 3 computers in the robotics lab. Two are PCs and one is a terminal. You may use either PC to write Micro C programs and to download them, although it will probably be easier if each team picks one of the PCs to store their programs on. To use HEXMON you need to use the terminal.

Using HEXMON and Downloading

HEXMON is useful for getting a good understanding of your sensors before you ever write a program. To use HEXMON you must first download the hexmon40.s19 program to a miniboard.

1. Go to the PC nearest the terminal. It should be turned on, but you may need to turn the monitor on.

2. Type cd \ to get to the top level directory (cd stands for change directory ). Then type dir to see the contents of this directory. You should see a couple of team directories, such as teamA and teamB.

3. Move to one of these directories by typing cd teamA. Type dir again to see the contents here. You should see the hexmon40.s19 program.

4. Now you should get a miniboard and prepare it for downloading as described on pages 11-13 of the technical reference manual. First you must provide power to the miniboard by inserting the power connector and turning the board on. Once the board is in download mode you can type

     dlm hexmon40 -port com2
to place HEXMON in the miniboard's memory. Notice that you do not include the file extension (the characters after the period). Since it is easy to forget to type the port designation we have created a shorthand version of this command; instead you can type
     load hexmon40

5. If you see a message that the program downloaded successfully you are ready to move on.

Testing sensors and motors with HEXMON

1. Move from the PC to the terminal and detach the serial cable. Turn on the terminal and attach its serial cable. Now put the miniboard into run mode.

2. Review the notes on HEXMON on pages 18-26 in the technical reference manual.

3. First we want to be sure that everything is connected properly. Type a series of esses (e.g. sssssssss) until the miniboard returns a prompt sign >. Whenever you mistype a command while using HEXMON, you can reset the miniboard with a series of esses.

4. There should be two light sensors, a digital sensor, a motor, and the necessary connectors near the terminal. Connect all of these components to the miniboard in the appropriate locations on the miniboard. Refer to Figure 2 on page 3 of the technical reference manual for guidance.

5. Now turn to Figure 11 on page 23. First I would like you to explore the values returned by the two light sensors. If you put one of the light sensors in position 0 in the analog ports on the board, then to read its value you type r00000054. The terminal will echo back the r followed by the value read in hexadecimal. Convert this hex value into decimal; remember that this converted value should be in the range of 0-255.

6. Test out both light sensors under a variety of light conditions (i.e. ambient light in room with overhead light on, ambient light with overhead light off, flashlight pointed directly at sensor, flashlight pointed directly but at increasingly far distances, light sensor covered by hand, etc). In your lab notebook, record the values obtained for both light sensors and convert them to decimal. What do high values correspond to? How closely to the two sensors correspond?

7. Now let's test the digital sensor. Without depressing the sensor, type r00001003 and record the value. Now depress the sensor and hold it down while you retype the same command. The output value should be different. Try moving the sensor to different digital ports and recording the values returned.

8. Controlling the motors is a bit tricky. First you must decide which motors you want to enable and in which direction. Second you must determine their speed (the default is full speed).

	w0010005d	enable motor 1 in reverse direction 
	w0011005d	enable motor 1 in forward direction 
	w0020005d	enable motor 2 in reverse direction 
	w0022005d	enable motor 2 in forward direction 
	w0040005d	enable motor 3 in reverse direction 
	w0080005d	enable motor 4 in reverse direction 
	w0070005d	enable motors 1, 2, and 3 in reverse direction 
	w0076005d	enables motors 1,2, and 3 with 1 in reverse
                        and 2 and 3 in forward direction 

Notice that to enable a combination of motors you add together the values for the individual motors. You don't need to physically connect motors to test these commands since the miniboard contains lights which show how the motors are being controlled. Read in the manual how to change the speed of the motors and test this on an actual motor.

Writing and Compiling Micro C Programs

Turn to pages 9-11 of the reference manual and review the miniboard library functions. We are going to write our first Micro C program using printdec to display the current sensor readings on the terminal for us. This is a slightly easier way to examine sensor values.

1. Go back to the PC and type

     edit test1.c
to open a new file with that name. Enter the following program. Notice that it is good programming practice to define the ports that will be used as constants at the top of the program.

/* print out the current sensor readings */ 

#define LIGHT1 0  /* <== enter the appropriate analog port # */ 
#define LIGHT2 1  /* <== enter the appropriate analog port # */ 
#define TOUCH1 0  /* <== enter the appropriate digital port # */

main() 
{ 
   while (1) { 
     printdec(analog(LIGHT1)); 
     printdec(analog(LGIHT2)); 
     printdec(digital(TOUCH1));
     msleep(5000); 
   } 
}

2. Press Alt to get to the editor's menu and save this file and then exit the editor.

3. To complie this program type

     cc11 test1 
(do not include the file extension). Correct any errors.

4. Once the program is error free, download it to your miniboard. Then move back to the terminal, connect the miniboard to the terminal's serial port. Put the miniboard into run mode and watch the sensor values that appear on the screen. Use the flashlight to be sure that the values change appropriately.

5. Go back to the PC and write a new program called test2.c that will do the following: if the touch sensor is depressed it will reverse the motor for 2 seconds, otherwise the motor should be on at full forward. Download and test this program (you will not need the terminal to test this program or the next one).

6. Finally write a program called test3.c that will do the following: if the light is very bright on either light sensor (you determine how bright) then the motor stops, otherwise the motor should be on at full forward. Record both the programs you wrote in your lab book.