Introduction To Robot C
Introduction To Robot C
by the RCGFoPL
1
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.
2
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.
3
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.
Hardware Setup
Before we can tell our robot to do anything,
we need to initialize the hardware. But dont worry;
as scary as this sounds, RobotC makes it rather
trivial. Just open up the
Motors and Sensors
Setup
dialog, accessible from the
Robot
menu.
From here, you can define the motors and
sensors that will be connected to your robot. You
can also give names to these motors and sensors so
you can refer to them in the code. Once finished,
click OK and RobotC will
magically
insert a few lines
of code at the top of your file. You may always change the setup using the same dialog
later on, and RobotC will update these lines of code to reflect those changes.
Main Task
The main task is what your robot will do when you run the program.
Think of it as the main list of instructions in a how-to guide; after everythings set up
and you have all the required materials, its the step-by-step guide that tells the
robot exactly what to do.
Heres how to define it in your code:
task main {
...put the code in here...
}
Motors
To activate a motor, use the following format:
motor[
motorA
]=
100
;
Substitute
motorA
with whatever name you gave to the motor you wish to
run.
The value
100
can be substituted with any value from -100 to 100. It is the
percentage of power to run the motor at, with negative values causing the
motor to go in reverse direction.
4
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.
Keep in mind, when you set the value of a motor, you are turning it on. The motor
will continue to run until you specifically tell it to turn off.
To stop applying power to a motor, use the following line, once again replacing
motorA
with the name you gave the motor:
motor[
motorA
] = 0;
5
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.
Comments
Comments are used to annotate your code. They can be incredibly useful in
providing information (explanations, todos, bugs, goals, etc.) about your code to yourself or
other programmers. This is especially applicable when multiple people are working on the
same project -- notes can be left for the other programmer to read. Comments can also be
useful in reminding
yourself
about information -- it can be tricky to remember all of the
idiosyncrasies of the program when there are hundreds of lines of code.
Comments are denoted in two different fashions:
motor[motorA] = 100;
//this is a comment
wait1Msec(1024);
/
* this is a
magical
multiline comment*/
motor[motorA] = 0;
Variables
Variables store data.
There are different types of variables used for storing different types of data. The
most common ones are:
int
-- integer.
Examples:
5; -3; 0; 40028;
float
-- supports decimal values. E
xamples:
5.023; -99.2; 4; 3.141592;
string
-- text.
Examples:
Hello, world!; whats your name?; qwerty is
Examples:
true; false;
Getting
a variable returns its current value. This action is not its own line, but
is an expression which you can put into another line. To get the value of a
variable, simply type its name in place of a normal value:
motor[motorA] = x;
You can even set one variable to the current value of another:
y = x;
In this case, the program will read the current value of x
Control Structures
Control structures are blocks you can use when you need to introduce logic or
repetition into your code. Theyre pretty straightforward:
The
if else
statement is the most basic way if introducing logic-based execution in
your code. Use them like this:
if (
y < 5
){
motors[motorA] = 100;
} else {
motors[motorB] = 100;
}
The code inside the parentheses after i
f
must evaluate to a
bool
value. If it is
true
,
the first block of code will execute; if it is
false
, the block after
else
will execute.
The
else
block may be omitted if you dont need to do anything when the value is
false
.
The
while
statement is used for repeating a block of code until the given b
ool
expression evaluates to
false
. Observe:
while (
5 == 4 + 1
){
motors[motorA] = 100;
wait1Msec(1000);
motors[motorA] = 0;
wait1Msec(1000);
}
The program, upon encountering the
while
block, while proceed as follows:
1. Check if the given expression evaluates to t
rue
or f
alse
.
2. If
true
, run the code inside the block, then go back to Step 1.
3. If
false
, exit this
while
block and continue with the rest of the program.
The while block can also be used to wait until a given expression is no longer true by
omitting the block part of it. For example:
while (
numberOfMagicalCookies == 0
);
Here, the program will constantly re-evaluate the expression until it returns false
before continuing. You will see later why this can be rather useful.
The
do while
statement is very similar to the w
hile
statement. The only difference is
7
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.
, our code can figure out which of those five times it is running.
Example:
//this will increment the power of motorA in 10% increments over 10 seconds.
for (int x = 1; x <= 10; x++) {
motors[motorA] = x * 10;
wait1Msec(1000);
}
Functions
Functions are handy,
magical, crumbly, unbelievably tasty bits
of code that you
can create once and reuse as many times as you want. Often, you will hear these
being referred to as
functions
and
methods
interchangeably. This is because some
programming languages call them functions, and others, methods. In reality, i
ts all
8
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.
Pragmas
A pragma, or pre-processor directive, gives the compiler instructions on how and
what extra stuff to compile. Pragmas have two main uses in RobotC:
Include statements
are used for importing libraries (and even other files you wrote
9
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.
yourself) into your program. A common example is importing the drivers for using
the joysticks or for using third-party sensors.
#include
JoystickDriver.c
Inside the quotes, put the name of the file you wish to import. Just remember that
the file must be in the same folder as your source file for the compiler to properly
find it.
Hardware setup
pragmas tell the program to initialize hardware connected to the
robot. The code that
magically
appeared at the top of the file after the hardware set
up contained pragmas. Specifically, it told the compiler what names to accept for
motors and sensors, as well as other things. While these lines of code are v
ery
important
, you will not need to edit them. Any changes you wish to make pertaining
to hardware can be done through the procedures outlined earlier in this guide.
Sensors
Sensors, such as the touch sensor, provide information to the robot. While there are
many different types of sensors, here is an example of a practical way to use the touch
sensor:
Say, for instance, that you need your robot to travel forward until it hits a wall, stop,
and turn. The touch sensor is a very useful tool for the job. Using the sensor and a
while
statement, you can command the robot to move forward until it hits a wall (depressing the
touch sensor), stop, and turn.
To access a sensor, use the following syntax:
SensorValue[
sTouch
]
where
sTouch
is the name of the sensor.
The code would look something like this:
motor[motorA] = 100;
//turn on both motors to go forward
motor[motorB] = 100;
//wait until the touch sensor is pressed
while (SensorValue[touchSensor] != 1);
motor[motorA] = 0; /
/stop motorA but allow motorB to continue, so that
//we turn
Seeking Rescue
from Evil Unicorns
In case youve hungrily consumed
this entire guide and want to learn more
about RobotC, youre in luck. The people
10
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.
hearts content. Or, if youre feeling stylish, you can just press the
F1
key (thats how all the
cool kids do it.)
Documentation is also available on the RobotC website:
http://www.robotc.net/support/nxt/MindstormsWebHelp/index.htm
11
This
nyanical
guidewaswrittenbytheRCGFoPL.
Contactusat
https://sites.google.com/site/rcgfopl/
or
[email protected]
.