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.

Slides:



Advertisements
Similar presentations
Mindstorms State Machines A structured method for handling tasks and events using RoboLab and NQC Brian Smith
Advertisements

1chung 5 th Annual Robofest 2004 Informational Meeting Mar. 20, 2004 Chan Jin Chung Ali Khazaal Jason Lo Computer Science Lawrence Technological University.
Robofest 2005 Introduction to Programming RIS 2.0 RCX Code.
Programming Your Robot (in C)
Variables and Functions ROBOTC Software. Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal.
Part IV: Memory Management
Overview of Data Structures and Algorithms
NQC: Not Quite C (last updated 9/14/05 2:24pm) Each NQC program has a task main () The program is contained within curly braces { ….. } task main() { SetSensor(SENSOR_1,
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Not Quite C: A CS 0 Option LMICSE Workshop June , 2005 Alma College.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Debugging (updated 9/20/06 12:48pm) It doesn’t work…. What do I do????
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Using the LEGO NXT March 2007 David Schilling Gus Jansson.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Introduction to Programming Writing Java Beginning Java Programs.
Nachos Phase 1 Code -Hints and Comments
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Designing with Procedures 1. Designing a Program with Procedures If the code for your program is going to be less than one page, normally don’t bother;
Robotics Finish presentations Bluetooth Communications Brain storm project ideas Programming considerations Homework: Prepare to present proposal for building.
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’
CS107 References and Arrays By Chris Pable Spring 2009.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Wall Encounter By Made easy by Dwayne Abuel.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
1 CS161 Introduction to Computer Science Topic #9.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
NQC / BricxCC Brief Introduction David Schilling.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Variables and Functions ROBOTC Software © 2012 Project Lead The Way, Inc.Principles of Engineering.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
NQC Brief Introduction – Part 2 David Schilling. NQC – Where to put code? Programs Tasks Functions Subroutines.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Oregon Robotics Tournament and Outreach Program RCX Basics.
1. 2 INDEX MAIN DISPLAYS3-4 SYSTEM LAYOUT 5-6 ROOM P-109 DISPLAY7 ROOM P-108 DISPLAY8 ROOM P-107 DISPLAY 9 ROOM P-106 DISPLAY 10 ROOM P-105 DISPLAY 11.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
ROBOTC Software EV3 Robot Workshop
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Introduction to Arduino A very basic intro to Arduino, the IDE and the Servos class.
Atmega328p Introduction for Digital and PWM Output Brion L Fuller II Robotics Club.
Introduction to Programming in RobotC
Chapter 14 Functions.
Week 2 - Wednesday CS 121.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
© Craig Zilles (adapted from slides by Howard Huang)
Chapter 12 Variables and Operators
Introduction to Gobbit Programming
Chapter 5 Conclusion CIS 61.
New Structure Recall “average.cpp” program
Functions Pass By Value Pass by Reference
Programming - Buttons Intro to Robotics.
Programming - Buttons Intro to Robotics.
Oregon Robotics Tournament and Outreach Program
© Craig Zilles (adapted from slides by Howard Huang)
NQC Program Structure 3 types of code blocks with their own features and limitations 1. Tasks 2. Subroutines 3. Inline Functions.
Presentation transcript:

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 in the same location – that is, the locations (and hence the data stored in those locations) are shared between programs

RCX Memory Map Offsets to program, task & subroutine starting locations 32 global variables 16 ‘local’ variables for task #1 16 ‘local’ variables for task #2 … 16 ‘local’ variables for task #10 Program and datalog storage This is schematic; not an actual layout

Uninitialized Variables In normal programming, (on a PC for instance), using a variable before you set its value can lead to very bad behaviour (often crashing your code) It is a very bad idea! In the confines of an RCX, with careful coding, however...

Data-location sharing implication You can use the data stored from one program in another This requires extra diligence! If you want to share data, ALL your programs on the RCX need to respect this (Or your data will be overwritten)

How can it work? The only reason this works is because variables are always assigned a location in the memory map based on their order of declaration Similarly, tasks are stored according to the order they are defined in

An Example Let’s look at an example robot – a sumo robot – that has two different behaviours Aggressive Evasive We want to be able to change certain time delays used in these programs at runtime Also, we need to calibrate a light sensor

Programs on a robot Example of Programs for a Robot 1Main program (Aggressive) 2Alternate program (Evasive) 3Setup program (for time delays) 4Battery level display program 5Light sensor calibration program

#1 - Main Program This is the main program that you run when you want to demo your robot It uses these 5 variables: light sensor high & low values mid-point of light sensor values timing delay for activity one timing delay for activity two

#2 - Alternate Program Alternate algorithm or behaviour for your robot It uses these 6 variables: light sensor high & low values mid-point of light sensor values timing delay for activity three timing delay for activity four timing delay for activity five

#3 - Setup Program Use this program to set timing delays used by the main program and alternate program Easiest way to ‘talk’ to this program is with the use of the IR remote control, using messages 1,2, and 3 Use the LCD display to show what is happening (what you’re modifying and its value)

#4 - Battery Level Display I always put the following program in slot #4 on every RCX (no variables are used): // Display Battery Level (Program #4) task main() { SetUserDisplay( BatteryLevel(), 3 ); StopAllTasks(); // leaves the display on }

#5 - Light Sensor Calibration This program ‘floats’ the motors, and for ten seconds collects high and low values on the light sensor During this time the user pushes the robot back and forth over the edge It stores the high & low, and also calculates the mid-point between them which can be used as a threshold

Global Variable Summary VariablesProg 1Prog 2Prog 3Prog 4Prog 5 Light highuses sets Light lowuses sets Light miduses sets time delay1usessets delay 2usessets delay 3usessets delay 4usessets delay 5usessets

Using an Include File Since the RCX is physically attached to one robot, there are many things that will be the same for each program Motor and Sensor meanings Motor directions and sensor setup Global variables It makes sense to put this in one place

Contents of our Include File Use #defines to give meaningful names to all your sensors and motors Declare names for all shared global variables Write a setup function for sensors, etc. Remember, this doesn’t result in any code for your program until you call it; at that point it is ‘inlined’

Include File (Part 1 of 3) // Include file for sumo robot // Sensor definitions #define BUMPER SENSOR_1 #define EDGE_DETECTOR SENSOR_2 #define PROGRESS_METER SENSOR_3 // Motor definitions #define LEFT_WHEEL OUT_A #define RIGHT_WHEEL OUT_B #define RAM OUT_C #define BOTH_WHEELS (LEFT_WHEEL+RIGHT_WHEEL) // SHARED Global variables int LightLow; int LightHigh; int LightMid; int Time1; // program 1: turn time int Time2; // program 1: delay before turning int Time3; // program 2: back up time int Time4; // program 2: turn time int Time5; // program 2: delay after turning void Init( void ) { SetSensor( BUMPER, SENSOR_TOUCH ); SetSensorType( EDGE_DETECTOR, SENSOR_TYPE_LIGHT ); SetSensorMode( EDGE_DETECTOR, SENSOR_MODE_RAW ); SetSensor( PROGRESS_METER, SENSOR_ROTATION ); ClearSensor( PROGRESS_METER ); SetGlobalDirection( RIGHT_WHEEL, OUT_REV ); SetPower( BOTH_WHEELS, OUT_FULL ); Float( BOTH_WHEELS ); Off( RAM ); }

Include File (Part 2 of 3) // SHARED Global variables int LightLow; int LightHigh; int LightMid; int Time1; // program 1: turn time int Time2; // program 1: delay before turning int Time3; // program 2: back up time int Time4; // program 2: turn time int Time5; // program 2: delay after turning

Include File (Part 3 of 3) void Init( void ) { SetSensor( BUMPER, SENSOR_TOUCH ); SetSensorType( EDGE_DETECTOR, SENSOR_TYPE_LIGHT ); SetSensorMode( EDGE_DETECTOR, SENSOR_MODE_RAW ); SetSensor( PROGRESS_METER, SENSOR_ROTATION ); ClearSensor( PROGRESS_METER ); SetGlobalDirection( RIGHT_WHEEL, OUT_REV ); SetPower( BOTH_WHEELS, OUT_FULL ); Float( BOTH_WHEELS ); Off( RAM ); }

Using the Include File Each program loaded on the RCX should start like this: // Sumo Robot – program #1 – Aggressive version #include "Common Sumo Code.h" // Other global variables go here int foo; task main() { Init(); // rest of program }

Example of Calibration Program // Sumo Robot – Light Sensor Calibration – program #5 #include "Common Sumo Code.h" task main() { int tmp; Init(); LightLow = EDGE_DETECTOR; LightHigh = LightLow; Float( BOTH_WHEELS ); ClearTimer(0); while( FastTimer(0) < 1000 ) // for 10 seconds { tmp = EDGE_DETECTOR; if( tmp < LightLow ) LightLow = tmp; else if( tmp > LightHigh ) LightHigh = tmp; } LightMid = (LightLow + LightHigh) / 2; }

Remember the Memory Map? Offsets to program, task & subroutine starting locations 32 global variables 16 ‘local’ variables for task #1 16 ‘local’ variables for task #2 … 16 ‘local’ variables for task #10 Program and datalog storage

Sharing Local Variables in a Task Since the location of local variables in a task are also always in the same place, if you are very careful about how you do things and how you arrange things, you can use that to your advantage as well This is definitely “advanced programming” and not for the faint of heart

Saving Data using Task Locals Another use might be to save data between runs, storing the results in local variables in a task Gus’ Crate Contraption scripting system uses the highest three tasks to store the script in These tasks are not used by the other programs on the RCX

Summary The “feature” of the standard firmware using the same storage locations for all programs allows you to pass information from one program to another You need to be careful that other programs don’t write over your data The best method is to have a standard include file for each program

These slides can be found online at: David/SMART/Presentations/ 03-nov-2004-nqc_intro.ppt