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

Slides:



Advertisements
Similar presentations
LEGO Robotics Lecture 1: Getting acquainted with your robotic environment.
Advertisements

1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Not Quite C: A CS 0 Option LMICSE Workshop June , 2005 Alma College.
LEGO Mindstorms RIS 2.0 Programming: NQC Code B.A. Juliano and R.S. Renner September 2004 California State University, Chico Intelligent Systems Laboratory.
Topic 8 – Introduction to Pointers and Function Output Parameters.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Introduction to the Arduino
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
NXC (and NBC) NXC (Not eXactly C) is a language similar to NQC, which was the most popular way to program the RCX Built on NBC, the Next Byte Code ‘assembler’
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
NQC Brief Introduction – Part 3 David Schilling. NQC – Where is my data? Programs have 32 global variables Tasks have 16 local variables These are always.
Learners Support Publications Functions in C++
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
NQC / BricxCC Brief Introduction David Schilling.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Controlling Program Flow with Decision Structures.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Fundamental Programming Fundamental Programming Introduction to Functions.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Chapter 9: Value-Returning Functions
Computer Organization and Design Pointers, Arrays and Strings in C
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User-Written Functions
Spreadsheet-Based Decision Support Systems
Pre-processor Directives
PL/SQL Scripting in Oracle:
Functions Inputs Output
Lesson 2: Building Blocks of Programming
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
6 Chapter Functions.
Reference Parameters.
CS1201: Programming Language 2
NQC Program Structure 3 types of code blocks with their own features and limitations 1. Tasks 2. Subroutines 3. Inline Functions.
Functions, Part 2 of 42 Topics Functions That Return a Value
CPS125.
Presentation transcript:

NQC Brief Introduction – Part 2 David Schilling

NQC – Where to put code? Programs Tasks Functions Subroutines

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

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 = An integer

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

Tasks

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

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 variables It is your responsibility to make sure that resources (inputs/outputs) are properly shared between tasks

Functions

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

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

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 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() }

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, use Pass 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 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

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

Summary TypeNumberArguments Program5no Task10no Functionunlimitedyes Subroutine8no