1 / 26

NQC

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.

urbain
Télécharger la présentation

NQC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NQC Brief Introduction – Part 3 David Schilling

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

  3. RCX Memory Map This is schematic; not an actual layout

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

  5. 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)

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

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

  8. Programs on a robot

  9. #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

  10. #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

  11. #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)

  12. #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 }

  13. #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

  14. Global Variable Summary

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

  16. 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’

  17. 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 ); }

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

  19. 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 ); }

  20. 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 hereint foo;task main() { Init();// rest of program }

  21. 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; }

  22. Remember the Memory Map?

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

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

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

  26. These slides can be found online at: http://www.brickshelf.com/gallery/David/SMART/Presentations/03-nov-2004-nqc_intro.ppt

More Related