nqc brief introduction – part 2 david schilling. nqc – where to put code? programs tasks...

31
NQC Brief Introduction – Part 2 David Schilling

Upload: suzan-fleming

Post on 19-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

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

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

NQC – Where to put code?

ProgramsTasksFunctionsSubroutines

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

Programs

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

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

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

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

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

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

Tasks

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

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

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

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

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

Functions

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

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

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

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

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

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

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

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

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

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

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

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

Subroutines

Page 27: NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions 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

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

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

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

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

Summary

Type Number Arguments

Program 5 no

Task 10 no

Function unlimited yes

Subroutine

8 no