8/12/2019 5nqc_found2 (1)
1/9
Preprocessor Directives(last modified 9/19/05 2:47pm)
Statements beginning with # are directives tothe preprocessor.
They DO NOT end with ;
Before the program is compiled, the preprocssor
copies these definitions into the code. These definitions are not required, but make the
code easier to write and more readable
if you want to change the port on which a sensor
is connected, and that appears multiple times inthe program, then a single change in the #define
is all you need to make the change.
8/12/2019 5nqc_found2 (1)
2/9
// tankbot2.nqc - drive and turn
// motors
#define LEFT OUT_A
#define RIGHT OUT_C
// how much time to spend turning or forward
#define TURN_TIME 200#define STRAIGHT_TIME 100
// speed to run a turned motor
#define TURN_POWER 3
task main()
{
// start with both motors onOn(LEFT+RIGHT);
// repeat the following steps forever
while(true)
{
}}
8/12/2019 5nqc_found2 (1)
3/9
Programming Style
Use indentation to visually groupprogram blocks.
Use comments to document the
program
8/12/2019 5nqc_found2 (1)
4/9
// tankbot2.nqc - drive and turn
// motors
#define LEFT OUT_A
task main()
{
// start with both motors on
On(LEFT+RIGHT);
// repeat the following steps forever
while(true)
{
// turn right by slowing down the right tread
SetPower(RIGHT, TURN_POWER);
Wait(TURN_TIME);
// resume going straight
SetPower(RIGHT, OUT_FULL);
Wait(STRAIGHT_TIME);
// turn left
SetPower(LEFT, TURN_POWER);Wait(TURN_TIME);
// resume going straight
SetPower(LEFT, OUT_FULL);
Wait(STRAIGHT_TIME);
}
}
8/12/2019 5nqc_found2 (1)
5/9
Time
NQC is a real-time programming
language.
Time is measured in 1/100 of a second
#define TURN_TIME 200
Will define the turning time to be 2 seconds
8/12/2019 5nqc_found2 (1)
6/9
Using Time to Control Motion
Wait(300);
The program will wait at that statement for 3seconds.
The following program will drive straight for 4seconds, then if OUT_A is attached to the left
tread, the left tread will stop and the righttread will continue to drive for 1secondcausing the tankbot to stear toward the left.
On(OUT_A + OUT_B);
Wait(400);
Off(OUT_A);
Wait(100);
8/12/2019 5nqc_found2 (1)
7/9
Generating a Random Number
NQC has a random number generatingfunction
Random(100);
Generates a random integer between 0and 100. All numbers in the range are
equally likely (uniform distribution)
0 100
8/12/2019 5nqc_found2 (1)
8/9
NQC FunctionsSo far we have seen several NQC functions.
They have been associated with:Motors:
On(arguments);
Off(arguments);SetPower(arguments);
OnFwd(arguments);
Sensors
SetSensor(arguments);
Mathematical
Random(arguments);
8/12/2019 5nqc_found2 (1)
9/9
All NQC functions begin with a capitalletter. As in SetSensor, two capitals are
used to make them more readable. The number of arguments depend on
the function.
Appendix D (Braun) provides a QuickReference to the language. Thefunctions are grouped into categories.NOTE the restrictions. NOTE THERE
ARE A FEW DIFFERENCESBETWEEN RCX AND RCX2.0. Whichsystem do you have?
Top Related