RobotC is a programming environment based on the popular functional programming language C. It was developed by the CMU Robotics Academy, an institute of the Carnegie Mellon University in Pittsburgh, Pennsylvania, that is "committed to using robotics to excite children about science and technology and to help create a more technologically literate society," as its web site states. Figure 2-21 shows RobotC.
Figure 2-21. RobotC
While a 30-day evaluation version can be downloaded for free, RobotC is a commercially available product, with a price of around $50. Presently, RobotC is available for Windows XP only but plans for Mac support have been announced already by the Robotics Academy.
An interesting point with RobotC is that it is cross-platform, supporting not only the NXT but also its MINDSTORMS predecessor, the RCX, and the popular VEX, a robotics kit produced by Innovation First, Inc. It is claimed that programs written for one of these platforms are portable to others with only little change. This is made possible by a Virtual Machine (VM), a layer that provides the glue between the code laid down in the RobotC language and the hardware it runs on, and that glue is necessarily different on each target platform. Hence, RobotC programs are run by an interpreter that does not operate on the native instruction set of the robotics controller but on the instructions of the VM (these VM instructions are called bytecode). Other high-level programming languages such as Java and C# are based upon this very concept. Yet, it is different from the one the original C language and NXT-G are following
.While performance often is a crucial point for interpreted languages, RobotC prides itself on providing better performance than NXT-G on the Brick. This is due to the use of a particular RobotCspecific firmware that differs from the official NXT one. Hence, before RobotC programs can be run, the official firmware has to be replaced by RobotC's on the Brick.
There are other features that make RobotC attractive to programmers such as real-time debugging (which is pretty difficult with NXT-G), an optimizing bytecode compiler, and support for almost all concepts of the powerful C language.
The IDE of RobotC is rather clear, consisting of a menu bar and an editor (Figure 2-22).
Figure 2-22. RobotC's IDE
The menu bar is the place where you can start commands such as opening or saving programs, selecting the target platform, connecting to the NXT Brick, downloading RobotC's firmware, and compiling and downloading programs (Figure 2-23).
Figure 2-23. Downloading the firmware to the Brick
It's also from here where you start the debugger (Figure 2-24)
.
Figure 2-24. RobotC's real-time debugger
The editor is separated into two parts: a tree that lists language elements, and the actual editing section. The elements in the tree can be dragged into the editing section, thus helping the programmer easily gain an overview of the language elements and save on typing. The editing section is the place where you actually write your program code. It features syntax highlighting, code completion, and intelligent indenting.
Again, you need an infinite loop for the endless execution of the program. The two motors are run by two separate commands:
// endless loop
while(true) {
// run motors B and C at a power level of 75%
motor[motorB] = 75;
motor[motorC] = 75;
}
You stop the motors in a similar way to running them—just set the power to 0:
// stop motors B and C
motor[motorB] = 0;
motor[motorC] = 0;
Run motor A by reading the variable nMotorEncoder that is the degrees counter:
// rotate motor by 45 degrees
nMotorEncoder[motorA] = 0;
while(nMotorEncoder[motorA] < 45 ) {
motor[motorA] = 75;
}
motor[motorA] = 0;
You need a loop for executing some statements until the ultrasonic sensor detects an object nearer than 50 inches:
const tSensors ultrasonicSensor = (tSensors) S1;
do {
// perform some statements
...
// do so while ultrasonic sensor does not detect
// an object nearer than 50 inches
} while(SensorValue(ultrasonicSensor) > 50);
Play a tone by using the PlayTone convenience method predefined by RobotC:
// play a tone (frequency 500, duration 50 * 10 ms)
PlayTone(500, 50);