Download presentation
Presentation is loading. Please wait.
Published byCandice Cobb Modified over 9 years ago
1
NQC / BricxCC Brief Introduction David Schilling
2
BricxCC Graphical user interface to programming your RCX
3
BricxCC Editor Syntax highlighting Programming helps Communication with RCX
4
F1 – Help
6
Templates
7
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
8
Communicating with the RCX
9
NQC Programming language with C-like syntax Uses Lego’s RCX firmware
10
Program skeleton task main() { // your program goes here }
11
Defining Sensors SetSensor( SENSOR_1, SENSOR_TOUCH ); SENSOR_TOUCH SENSOR_LIGHT SENSOR_ROTATION (plus a few rarely used others)
12
Getting a Sensor’s value First define a variable int x; Then assign the value to that variable x = SENSOR_1;
13
Testing a Sensor’s value Alternatively you can just use the sensor value in an expression if( SENSOR_1 > 50 ) { }
14
Sensor Ranges Sensor typeRange SENSOR_TOUCHtrue / false SENSOR_LIGHT 0-100 advertised 45-70 is normal SENSOR_ROTATIONany integer value
15
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
16
Sensor Naming example #define Bumper SENSOR_1 #define Edge SENSOR_2 task main() { SetSensor( Bumper, SENSOR_TOUCH ); SetSensor( Edge, SENSOR_LIGHT ); }
17
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 );
18
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
19
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
20
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 );
21
Motor Commands Common Commands: On(); Off(); Float(); SetPower(); Fwd(); Rev(); SetDirection(); OnFwd(); OnRev(); OnFor();
22
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
23
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
24
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
25
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
26
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:
27
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
28
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 ); }
29
Simple Sumo demo program (This is a terrible program! I’m just using it to illustrate the concepts covered in this presentation)
30
// 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
31
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
32
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 ); }
33
Complete Program...
34
// 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);
35
#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 );
36
#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 );
37
#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 );
38
#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 );
39
#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 );
40
#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 );
41
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 );
42
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 );
43
{ 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 );
44
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 )
45
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 ) {
46
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 )
47
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 );
48
SetGlobalDirection( Left, OUT_REV ); SetPower( Both, OUT_HALF ); SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else
49
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 );
50
SetDirection( Both, OUT_FWD ); On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge
51
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 {
52
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 );
53
On( Both ); while( true ) { if( Bumper ) SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 );
54
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 );
55
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 );
56
{ 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 );
57
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 ); }
58
SetPower( Both, OUT_FULL ); else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
59
else SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
60
SetPower( Both, OUT_HALF ); if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
61
if( Edge > 750 ) // at edge { Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
62
{ Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
63
Rev( Both ); Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
64
Wait( 100 ); Fwd( Left ); Wait( 200 ); Fwd( Right ); }
65
Fwd( Left ); Wait( 200 ); Fwd( Right ); }
69
// 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 ); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.