|
ROBO-ROMP (while, for,
if-else) |
|
The C++ programming language had its beginnings as the C programming
language. The "++" part of the
language is rather recent. For CSC126
students, most of what we have learned so far can be implemented in both C and
C++. The only C++ statements we have
actually used are the cout, cin statements, and the //comment
style.
Interactive C (IC)
is a version of the C language that runs on the MIT handy board. We use the handy board to control our Lego
robots. IC does not support the C++
instruction set although in more current versions, the // comment style is
accepted. The IC language is interpreted
rather than compiled. Why might an
interpreter be more useful in controlling a robot? What disadvantages are there in interpreting
source code? Why are these disadvantages
not that significant when controlling a robot?
Start Interactive C.
Notice that as you load IC, all IC libraries are loaded
onto the board. Since all libraries
are already loaded, there is no need to #include
these libraries. Hence, IC does not
support the #include
statement.
One of the nice things about IC is the ability to issue
commands to the robot from the IC command prompt.
Robo-Rap!
If we can't use cout,
then how do we print in IC? IC uses the
standard C print function. To print a
character string in C,
the statement syntax is as follows:
printf("Place your statement here\n");
Give your robot a name.
From the command area on the interaction tab, use the printf
statement to say, "Hi, my
name is Roboname". Notice that the statement is printed on the
LCD display of the handy board.
The handy board has rudimentary sound capabilities. It can beep!!
The beep command can also be entered at the command prompt.
In the command space on the interactive tab type in the
command: beep();
Your robot can now talk!!
The beep command produces a 500 Hertz tone for 0.3 seconds.
To hear a continuous beep type: beeper_on( );
To turn the beeper off type: beeper_off( );
IC has a built in sleep
function. The sleep function accepts a
float number as a parameter. This tells
the robot how long to wait, in seconds, before executing the next statement. For example, the statement sleep(1.0);
causes the robot to wait one second.
Note all numbers to the sleep command have to have a decimal point.
All IC programs have a main function, just as in
C++. You can use any editor to create an
IC program. IC version 4.0 has it's own editor. To
enter a new program into IC, click on the New
button on the tools menu. The
editor tab appears.
There are no #include statements in IC. Therefore an IC program has the general form:
void
main( )
{
//comments
statements;
}
Write an IC program that will beep, wait 1 second, beep
again, wait one second, and then beep again.
Save this program. To save a program the first time, click on File
and then Save As
from the menu bar. After the first time you can click on the Save button. IC saves the program with a
.ic extension. DO NOT save the file as a .cpp file.
To run your program:
Modify your program, using a for loop, so that the robot beeps 20 times. Demonstrate to your instructor so that he/she
can initialize your robot check off the Robo-Evaluation
Sheet.
Tickle Me Robot!!
Above is a picture of the robot's "touch
bumper". When the bumper is pushed
in, the yellow pulleys in the center will depress the touch sensor on the
robot's body. The touch sensors are
connected into the handy board via the digital ports. Looking at the picture of the handy board
below, the digital ports are located on the bottom of the board and are
numbered 7 - 15. In this picture, the
touch sensors are plugged into port 15, all the way to the left, and port 7 in
the center. The input ports 0 - 6, are
analog ports and we will not be using them for this lab.
IC has a built-in function that will read if the digital
input port is receiving a signal. The
function is boolean and returns a 1 if there is a
signal, and 0 if not. The parameter for
the function is the number of the digital port to be read. Make sure your touch sensors are plugged
into digital ports 15 and 7 as in the above picture.
Click on the interactive tab. In the command area type: digital(15);
Now push the bumper in, keep it depressed, and type: digital(15);
What were the values returned by the digital function in
each case?
Let's combine all that we have learned into our first
"real" Robo-program!! First
unload the beep program by closing it.
Click on the beep program tab.
Click on File, then Close from the menu bar.
Write an IC program that will:
Hee! Hee! You tickled my right side!!!
The robot waits 1 second before
testing again.
Hee! Hee! You tickled my left side!!!
The robot waits 1 second before
testing again.
You may have to experiment to
find out which digital input is connected to the sensor on the left and which
digital input is connected to the touch sensor on the left.
Robo-Rumba!!!
How can we make your robot a "mobile" robot ?
There are two motors on your robot. One controls the left wheel, the other the
right wheel. Each motor is plugged into a
motor port on the handy board (see the labeled picture above). The motor ports are numbered from 0 to
3. The motors in the picture are plugged
into motor ports 0 and 3. Make sure your
motors are also plugged into motor ports 0 and 3.
The command to move the wheel plugged into port 0 forward
is:
fd(0);
The command to move the wheel plugged into port 3 forward
is:
fd(3);
To turn motor 0 off the command is:
off(0);
To turn all the motors off the command is:
ao( );
Write an IC program that will move your robot forward for
3 seconds and then stop. The forward
commands are persistent. The robot will
move until the ao() or off()
command is executed. The sleep command
is needed after the forward commands so that there is some time between the
execution of the forward command and the off command. The sleep command can be thought of as a
"busy wait" command.
To make the robot turn right, turn the motor on that
controls the left wheel while keeping the right motor off. Do the opposite to turn left.
Write an IC program that will have the handy bug
continuously (while loop):
To rotate the motor in the opposite direction causing the
robot to move backwards, the command is:
bk(0);
bk(3);
Where 0 and 3 refer the motor ports.
Write an IC program that will have the handy bug
continuously (while loop);
Robo-Ruckus!!!!
Now we will write an IC program that will use all that we
have learned. We are now going to
program the robot to avoid obstacles.
Write an IC program that will have the handy bug
continuously:
HAVE FUN!!!!!!
Written by Dr. Susan Imberman
Copyright ã November 2004, All rights reserved.