RoboCatz.com

Variables


Declaration

Only a few computer langugages require variables to be declared before they can be used. Often, this declaration also defines the type of variable (i.e., its "cast") and consequently the amount of storage space needed to accommodate the variable in memory. For example, in RobotC, an integer variable can be declared using the "int" keyword as in:

int someVariableName;

Different types of variables include:

In C and many other c-based languages, the type of "cast" of the variable (known as its "typecast") is strictly monitored by the compiler and incorrect "typecasting" (i.e., trying to store the wrong type of data in a variable) will result in a compiler or run time error. This "strict typecasting" requirement means that programs have to becarefully written and well understood before the compiler will prepare the program for execution by the computer/robot. While these programs may be more robust (i.e.,less prone to failure), it can be harder to write.

In some languages, variable declaration is optional. For example, in JavaScript, a variable can be declared using the "var" keyword as in:

var someVariableName;

In Javascript it is not necessary to define the type (or "cast") of the variable. But this also means that the same variable name can be used to store any form of data and this can introduce logic and programming errors into your systems.

Initialization

The initialization of a variable prepares it for use in the program. Here are a few examples:
  1. var i=0;
  2. int i=0;
  3. for(i=0; i<10; i++) {
In each of these examples, the variable "i" is initialized to the value of "0"; In the first two, the variable "i" is initialized with its declaration. In the third example, the variable "i" is initialized but not declared and therefore may require a previous declaration depending on the language you are using.

Once a variable is initialized, it can then be used in "expressions" as in:

  1. if( i > 10) {
  2. degrees = i * 360;
  3. switch( i ) { case 1: case 2: case 3: }
As you can see, if "i" is defined but not yet initialized, then these expressions become difficult to interpred.

Potential Uses

Configuration

Variables such as wheel diameter, wheel base measurements, orientation of the motors, direction of the robot, gear ratio, light calibration information. These configuration variables can be stored in files located in the robot's internal file storage system. One of the advantages of storing configuration information onto the robot's file storage system is that it enables you to separate these values from the program. You could then have different robots (of different configurations) run the same program without having to modify the program itself.
A type of configuration variable can be used to control the operation of the program. For example, if your team needs to run a light calibration function at the beginning of each tournament match (but not necessary for each practice run during the practice sessions), you could set a boolean variable (true / false) to store whether the robot is being run on a tournament day. If it is, then the calibration routine would be automatically executed at the start of the program.

Loop Counting

Sometimes the robot or program needs to perform a certain action (function) for a specific number of times. Variables can be used to keep track of this information. Loop counting is not as often performed as other types of loops (infinite loops or loops with certain criteria).

Use as Criteria in Expressions

Variables such as distance, degrees of rotation, or percent of light/dark. Telling the robot to do something until a criteria has been reached (e.g., moving forward until the ultrasonic sensor measures the wall as less than 6 centimeters away) requires that the "criteria" is determined prior to the start of the loop. It may be defined as a constant (i.e., part of the loop's initialization). Or, it may be defined as a variable that is passed into the loop. Initializing the criteria as a variable give you the maximum flexibility in implementing the loop as a separate function or MyBlock. In essence you could then pass the criteria to the function or MyBlock enabling you to implement the function or MyBlock using different values as needed.