NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks...
-
Upload
suzan-fleming -
Category
Documents
-
view
213 -
download
0
Transcript of NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks...
![Page 1: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/1.jpg)
NQC
Brief Introduction – Part 2
David Schilling
![Page 2: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/2.jpg)
NQC – Where to put code?
ProgramsTasksFunctionsSubroutines
![Page 3: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/3.jpg)
Programs
![Page 4: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/4.jpg)
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
![Page 5: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/5.jpg)
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
![Page 6: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/6.jpg)
Defining a Program
task main() { // your program goes here }
![Page 7: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/7.jpg)
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
![Page 8: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/8.jpg)
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
![Page 9: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/9.jpg)
Tasks
![Page 10: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/10.jpg)
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
![Page 11: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/11.jpg)
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 }
![Page 12: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/12.jpg)
Task APIs
Tasks can be started and stopped by any task
start task; // notice: not ‘task()’
stop task; StopAllTasks(); // terminates
program
![Page 13: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/13.jpg)
Task Limitations
Each task has only 16 local variablesIt is your responsibility to make sure that resources (inputs/outputs) are properly shared between tasks
![Page 14: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/14.jpg)
Functions
![Page 15: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/15.jpg)
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
![Page 16: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/16.jpg)
Defining a Function
To define a function:
void FunctionName( void ) { }
To call a function:
FunctionName();
![Page 17: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/17.jpg)
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
![Page 18: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/18.jpg)
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
![Page 19: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/19.jpg)
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()
}
![Page 20: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/20.jpg)
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; } }
![Page 21: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/21.jpg)
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 )
![Page 22: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/22.jpg)
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 }
![Page 23: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/23.jpg)
Pass By Value, cont’d
Call your function with a parameter:
DeliverBottles( 5 ); int Num = 3; DeliverBottles( Num ); // note, here ‘Num’ still equals 3
![Page 24: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/24.jpg)
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 }
![Page 25: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/25.jpg)
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
![Page 26: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/26.jpg)
Subroutines
![Page 27: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/27.jpg)
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
![Page 28: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/28.jpg)
Defining a Subroutine
To declare a subroutine use: sub SubroutineName() { }
To call a subroutine use: SubroutineName();
![Page 29: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/29.jpg)
Subroutine Example
sub DeliverMilkBottle() { On( OUT_C ); while( SENSOR_3 == false ) ; Off( OUT_C ); }
![Page 30: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/30.jpg)
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
![Page 31: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.](https://reader035.fdocuments.us/reader035/viewer/2022070415/5697c02a1a28abf838cd82f8/html5/thumbnails/31.jpg)
Summary
Type Number Arguments
Program 5 no
Task 10 no
Function unlimited yes
Subroutine
8 no