NQC / BricxCC Brief Introduction David Schilling
BricxCC Graphical user interface to programming your RCX
BricxCC Editor Syntax highlighting Programming helps Communication with RCX
F1 – Help
Templates
Just click on a command in the template and the function is inserted into the editor with hints for its parameters Press F10 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 SetSensor( 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 typeRange SENSOR_TOUCHtrue / false SENSOR_LIGHT advertised is normal SENSOR_ROTATIONany integer value
Using Names for Sensors Early in your program define a name Use that name instead of the sensor # Benefits 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() { SetSensor( Bumper, SENSOR_TOUCH ); SetSensor( Edge, SENSOR_LIGHT ); }
Using Raw Values With “SetSensor”, 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 SetSensorType and SetSensorMode SetSensorType(SENSOR_1, SENSOR_TYPE_LIGHT ); SetSensorMode(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: On(); Off(); Float(); SetPower(); Fwd(); Rev(); SetDirection(); OnFwd(); OnRev(); OnFor();
Motor States The following functions just take one or more motors as an argument: On();- Turns motor on Off();- Brakes motor Float();- Lets motor float
Motor Power To set a motor’s power level use SetPower( OUT_A, OUT_FULL ); The first argument is one or more motors The second argument is the power level OUT_LOW, OUT_HALF, OUT_FULL or 0 to 7
Motor Direction To set a motor’s direction use SetDirection( OUT_A, OUT_FWD ); The first argument is one or more motors The second argument is the direction 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 SetDirection(); You can also use Toggle( OUT_A ); to change the motor’s direction
Changing a Motor’s Orientation You can rotate the wire 180 degrees You can change every line in your program that references a motor, or You can use SetGlobalDirection(); If your motor goes forward when you want it to go reverse, and vice versa, there are a couple of solutions:
SetGlobalDirection(); SetGlobalDirection( 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() { SetPower( Left+Right, OUT_FULL ); SetDirection( 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)
// 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 Naming sensors and motors
SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); Setting up sensors and motors
Program loop On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( 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() { SetSensor( 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() { SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT );
#define Edge SENSOR_2 #define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C task main() { SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW );
#define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C task main() { SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW );
#define Left OUT_A #define Right OUT_C #define Both OUT_A+OUT_C task main() { SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV );
#define Right OUT_C #define Both OUT_A+OUT_C task main() { SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV );
#define Both OUT_A+OUT_C task main() { SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF );
task main() { SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD );
task main() { SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD );
{ SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both );
SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both ); while( true )
SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both ); while( true ) {
SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper )
SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL );
SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else
SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF );
SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge
SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge {
SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both );
On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 );
On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left );
while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 );
{ if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right );
if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
SetPower( 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() { SetSensor( Bumper, SENSOR_TOUCH); SetSensorType( Edge, SENSOR_TYPE_LIGHT ); SetSensorMode( Edge, SENSOR_MODE_RAW ); SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }