Presentation is loading. Please wait.

Presentation is loading. Please wait.

NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.

Similar presentations


Presentation on theme: "NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines."— Presentation transcript:

1 NQC Brief Introduction – Part 2 David Schilling

2 NQC – Where to put code? Programs Tasks Functions Subroutines

3 Programs

4 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

5 Definition of a ‘program’ A program is a collection of tasks that tells your robot what to do The ‘starting’ point for a program is a task called “main()” A program is compiled into byte code which is downloaded to the RCX

6 Defining a Program task main() { // your program goes here }

7 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

8 Program Limitations Total program size is limited Programs are completely stand-alone A program has only 32 global variables All programs share these same 32 locations for their variables

9 Tasks

10 Besides the “main” task, a program can have up to 9 other tasks (10 in total) Tasks run concurrently with each other Tasks are useful for defining a specific behaviour that your robot will need Start and stop tasks as needed from any other task

11 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 }

12 Task APIs Tasks can be started and stopped by any task start task; // notice: not ‘task()’ stop task; StopAllTasks(); // terminates program

13 Task Limitations Each task has only 16 local variables It is your responsibility to make sure that resources (inputs/outputs) are properly shared between tasks

14 Functions

15 Functions are useful for organizing your code – collecting bits of code that do one specific thing Give each function a useful name Functions can have parameters passed to them

16 Defining a Function To define a function: void FunctionName( void ) { } To call a function: FunctionName();

17 Function “gotchas” Functions are ‘inlined’ – that means they are inserted into the task’s code where ever you call them This can lead to unanticipated code bloat if you have lots of them or use them improperly Also you’ll need to be careful with the use of local variables – you can run out Unlike C, functions can’t return a value

18 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

19 Function Inlining int 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() }

20 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; }

21 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 )

22 Pass By Value If your program doesn’t need the function to modify the parameter, or if you are passing a constant, use Pass By Value void DeliverBottles( int x ) { // use ‘x’ here }

23 Pass By Value, cont’d Call your function with a parameter: DeliverBottles( 5 ); int Num = 3; DeliverBottles( Num ); // note, here ‘Num’ still equals 3

24 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 }

25 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

26 Subroutines

27 Subroutines are similar to functions but with a different set of limitations A single copy of the code will exist (not inlined) which means code won’t grow excessively No parameters or return value Can’t call another subroutine Local variables can be a problem

28 Defining a Subroutine To declare a subroutine use: sub SubroutineName() { } To call a subroutine use: SubroutineName();

29 Subroutine Example sub DeliverMilkBottle() { On( OUT_C ); while( SENSOR_3 == false ) ; Off( OUT_C ); }

30 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 program Changing 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

31 Summary TypeNumberArguments Program5no Task10no Functionunlimitedyes Subroutine8no


Download ppt "NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines."

Similar presentations


Ads by Google