nqc brief introduction – part 2 david schilling. nqc – where to put code? programs tasks...
TRANSCRIPT
NQC
Brief Introduction – Part 2
David Schilling
NQC – Where to put code?
ProgramsTasksFunctionsSubroutines
Programs
RCX Programs
The RCX supports five programs loaded at the same time (with Lego firmware)Use Program (“Prgm”) button on the RCX to select which program to run
Definition of a ‘program’
A program is a collection of tasks that tells your robot what to doThe ‘starting’ point for a program is a task called “main()”A program is compiled into byte code which is downloaded to the RCX
Defining a Program
task main() { // your program goes here }
Program APIs
If you want to know what program slot your program is running in, use:
x = Program();
To start running a different program from the one currently running, use:
SelectProgram(k); // k = 0 .. 4
An integer
Program Limitations
Total program size is limitedPrograms are completely stand-aloneA program has only 32 global variablesAll programs share these same 32 locations for their variables
Tasks
Tasks
Besides the “main” task, a program can have up to 9 other tasks (10 in total)Tasks run concurrently with each otherTasks are useful for defining a specific behaviour that your robot will needStart and stop tasks as needed from any other task
Defining a Task
Pick a name for your task; ‘main’ is the task that starts the program
task TrackLine() { // code to track a line goes here }
Task APIs
Tasks can be started and stopped by any task
start task; // notice: not ‘task()’
stop task; StopAllTasks(); // terminates
program
Task Limitations
Each task has only 16 local variablesIt is your responsibility to make sure that resources (inputs/outputs) are properly shared between tasks
Functions
Functions
Functions are useful for organizing your code – collecting bits of code that do one specific thingGive each function a useful nameFunctions can have parameters passed to them
Defining a Function
To define a function:
void FunctionName( void ) { }
To call a function:
FunctionName();
Function “gotchas”
Functions are ‘inlined’ – that means they are inserted into the task’s code where ever you call themThis can lead to unanticipated code bloat if you have lots of them or use them improperlyAlso you’ll need to be careful with the use of local variables – you can run outUnlike C, functions can’t return a value
A Simple Example Function
void DeliverMilkBottle( void ) { On( OUT_C ); while( SENSOR_3 == false ) ; Off( OUT_C ); } // in a real program, remember to
use “#define”s for sensors and outputs
Function Inliningint x;
void DoSomething()
{
PlaySound( SOUND_UP );
x = x+1;
}
void DoSomethingElse()
{
On( OUT_A );
DoSomething();
Wait( 100 );
Off( OUT_A );
}
task main()
{
x = 0;
DoSomething();
DoSomethingElse(); // first time
DoSomethingElse(); // second time
}
int x;
task main()
{
x = 0;
// DoSomething();
PlaySound( SOUND_UP ); // from DoSomething()
x = x+1; // from DoSomething()
// DoSomethingElse(); // first time
On( OUT_A ); // from DoSomethingElse()
PlaySound( SOUND_UP ); // from DoSomething()
x = x+1; // from DoSomething()
Wait( 100 ); // from DoSomethingElse()
Off( OUT_A ); // from DoSomethingElse()
// DoSomethingElse(); // second time
On( OUT_A ); // from DoSomethingElse()
PlaySound( SOUND_UP ); // from DoSomething()
x = x+1; // from DoSomething()
Wait( 100 ); // from DoSomethingElse()
Off( OUT_A ); // from DoSomethingElse()
}
A Function with a Parameter
You can pass one or more parameters to a function:
void DeliverBottles( int x ) { while( x > 0 ) { DeliverMilkBottle(); // call a function x = x – 1; } }
Parameters to Functions
There are several different ways of passing a parameter to a function, but you’ll mainly use these two: Pass By Value: void Fn( int x ) Pass By Reference: void Fn( int& x )
Pass By Value
If your program doesn’t need the function to modify the parameter, or if you are passing a constant, usePass By Value
void DeliverBottles( int x ) { // use ‘x’ here }
Pass By Value, cont’d
Call your function with a parameter:
DeliverBottles( 5 ); int Num = 3; DeliverBottles( Num ); // note, here ‘Num’ still equals 3
Pass By Reference
If you want your function to modify the parameter for the rest of the program, use Pass By Reference
void PickUpBottles( int& x ) { // use or change the value of ‘x’
here }
Pass By Reference, cont’d
Call your function with a parameter:
PickUpBottles( 5 ); // ERROR!!!
int Num = 300; PickUpBottles( Num ); // ‘Num’ probably has a new value
now
Subroutines
Subroutines
Subroutines are similar to functions but with a different set of limitationsA single copy of the code will exist (not inlined) which means code won’t grow excessivelyNo parameters or return valueCan’t call another subroutineLocal variables can be a problem
Defining a Subroutine
To declare a subroutine use: sub SubroutineName() { }
To call a subroutine use: SubroutineName();
Subroutine Example
sub DeliverMilkBottle() { On( OUT_C ); while( SENSOR_3 == false ) ; Off( OUT_C ); }
Use of Subroutines
Subroutines should probably be avoided unless you have a large program, or have a function that you call in many places in your programChanging a ‘function’ to a ‘subroutine’ will take a bit of work, so be careful if you decide to modify a function after you’ve written it already
Summary
Type Number Arguments
Program 5 no
Task 10 no
Function unlimited yes
Subroutine
8 no