NQC Bricx CC Brief Introduction David Schilling Bricx
NQC / Bricx. CC Brief Introduction David Schilling
Bricx. CC Graphical user interface to programming your RCX
Bricx. CC Editor Syntax highlighting Programming helps Communication with RCX
F 1 – Help
F 1 – Help
Templates
Templates Just click on a command in the template and the function is inserted into the editor with hints for its parameters Press F 10 to go to the next one
Communicating with the RCX
NQC Programming language with C-like syntax Uses Lego’s RCX firmware
Program skeleton task main() { // your program goes here }
Defining Sensors Set. Sensor( SENSOR_1, SENSOR_TOUCH ); SENSOR_TOUCH SENSOR_LIGHT SENSOR_ROTATION (plus a few rarely used others)
Getting a Sensor’s value First define a variable int x; Then assign the value to that variable x = SENSOR_1;
Testing a Sensor’s value Alternatively you can just use the sensor value in an expression if( SENSOR_1 > 50 ) { }
Sensor Ranges Sensor type Range SENSOR_TOUCH true / false SENSOR_LIGHT 0 -100 advertised 45 -70 is normal SENSOR_ROTATION any integer value
Using Names for Sensors Early in your program define a name Use that name instead of the sensor # Benefits n n can easily move sensor to a different port by only making one change code becomes more self-explanatory
Sensor Naming example #define Bumper SENSOR_1 #define Edge SENSOR_2 task main() { Set. Sensor( Bumper, SENSOR_TOUCH ); Set. Sensor( Edge, SENSOR_LIGHT ); }
Using Raw Values With “Set. Sensor”, The light sensor range (a ‘percentage’ from 0 to 100) is very limited Usually you’ll only get a value from around 45 to 70 or so To get a greater range, use Set. Sensor. Type and Set. Sensor. Mode Set. Sensor. Type(SENSOR_1, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode(SENSOR_1, SENSOR_MODE_RAW );
Raw Sensor Values The range changes to something like 600 – 800 However, light and dark are reversed: A dark reading of 40 becomes 800 A light reading of 70 becomes 600
Motors Default names for motors are OUT_A, OUT_B and OUT_C Motor state can be ON, OFF, or FLOAT Motor direction and power can be set Just like with sensors, I recommend giving motors names
Motor Commands You can just use motors (since all motors are identical to the RCX there is no need to define them like sensors) eg: On( OUT_A ); You can combine multiple motors in a command by using the “+” sign eg: Off( OUT_A+OUT_C );
Motor Commands Common Commands: n n On(); Off(); Float(); Set. Power(); Fwd(); Rev(); Set. Direction(); On. Fwd(); On. Rev(); On. For();
Motor States The following functions just take one or more motors as an argument: n n n On(); Off(); Float(); - Turns motor on - Brakes motor - Lets motor float
Motor Power To set a motor’s power level use Set. Power( OUT_A, OUT_FULL ); The first argument is one or more motors The second argument is the power level n n OUT_LOW, OUT_HALF, OUT_FULL or 0 to 7
Motor Direction To set a motor’s direction use Set. Direction( OUT_A, OUT_FWD ); The first argument is one or more motors The second argument is the direction n OUT_FWD, or OUT_REV
Motor Direction A simpler way to set a motor’s direction is to use: Fwd( OUT_A ); or Rev( OUT_A ); These work the same as Set. Direction(); You can also use Toggle( OUT_A ); to change the motor’s direction
Changing a Motor’s Orientation If your motor goes forward when you want it to go reverse, and vice versa, there a couple of solutions: You can rotate the wire 180 degrees You can change every line in your program that references a motor, or You can use Set. Global. Direction();
Set. Global. Direction(); Set. Global. Direction( OUT_A, OUT_REV ); This command will change a motor’s direction from now on, so that “FWD” means “REV” and vice versa “OUT_FWD” as the second argument restores a motor back to normal This is a very clean way to write your programs: “FWD” means forward
Motor Example #define Left OUT_A #define Right OUT_C task main() { Set. Power( Left+Right, OUT_FULL ); Set. Direction( Left+Right, OUT_FWD ); On( Left+Right ); Wait( 100 ); }
Simple Sumo demo program (This is a terrible program! I’m just using it to illustrate the concepts covered in this presentation)
Naming sensors and motors // Simple Sumo demo program #define Bumper SENSOR_1 #define Edge SENSOR_2 #define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C
Setting up sensors and motors Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD );
Program loop On( Both ); while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); } }
Complete Program. . .
// Simple Sumo demo program #define Bumper SENSOR_1 #define Edge SENSOR_2 #define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C task main() { Set. Sensor( Bumper, SENSOR_TOUCH);
#define Bumper SENSOR_1 #define Edge SENSOR_2 #define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C task main() { Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT );
#define Edge SENSOR_2 #define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C task main() { Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW );
#define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C task main() { Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW );
#define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C task main() { Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV );
#define Right OUT_C #define Both OUT_A+OUT_C task main() { Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV );
#define Both OUT_A+OUT_C task main() { Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF );
task main() { Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD );
task main() { Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD );
{ Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD ); On( Both );
Set. Sensor( Bumper, SENSOR_TOUCH); Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD ); On( Both ); while( true )
Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD ); On( Both ); while( true ) {
Set. Sensor. Type( Edge, SENSOR_TYPE_LIGHT ); Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper )
Set. Sensor. Mode( Edge, SENSOR_MODE_RAW ); Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL );
Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL ); else
Set. Global. Direction( Left, OUT_REV ); Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF );
Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge
Set. Power( Both, OUT_HALF ); Set. Direction( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge {
Set. Direction( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both );
On( Both ); while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 );
On( Both ); while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left );
while( true ) { if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 );
{ if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right );
if( Bumper ) Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
Set. Power( Both, OUT_FULL ); else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); } }
else Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); } } }
Set. Power( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); } } }
if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); } } }
{ Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); } } }
Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); } } }
Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); } } }
Fwd( Left ); Wait( 200 ); Fwd( Right ); } } }
// Simple Sumo demo program #define Bumper SENSOR_1 #define Edge SENSOR_2 #define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C task main() { Set. Sensor( Bumper, SENSOR_TOUCH);
- Slides: 69