Programming with NQC:

NQC is a higher level programming language than the Lego Mindstorms(LM) language. It is easy to learn and use. With this language, robot developers will be able to unleash the full potential of their robots. NQC stands for “Not Quite C.” As given from the name, this language is very similar to C. Anyone with a background in C++ can catch on in a matter of minutes and be able to write complex programs in a matter of hours.

The reason we went with NQC is that it is more capable than the language supplied with the LM kit. We ran into several problems while programming with the LM kit. We could not accomplish what we wanted. The language was far too low-level and limited.

 

Basics of Programming:

Begin by building your robot, placing sensors and outputs where needed. Get a good idea of what you want your robot to do. Be sure to have the sensors in logical spots and think, “will the sensor read accurately?” Once this is done, it is time to begin programming. Every program written in NQC begins with…

 

task main( )

{

}

 

After this, the program begins. There are several outputs (a,b,c) and inputs (1,2,3) and each one of these can be controlled in the language. To turn any output, follow this structure…

 

task main( )

{

SetPower(OUT_A+OUT_C,2);

OnFwd(OUT_A);

OnRev(OUT_A+OUT_C);

Off(OUT_A+OUT_C);

}

 

This code segment turns output A on in the “forward” direction. (The direction of the output is somewhat arbitrary because “forwards” is not always forwards. It depends on how the end of the wire is attached to the output terminal.) The second command turns both output A and C on in the reverse direction. Motors can be turned off using the “Off” command just as easily as turning them on. As seen from the example, several commands can be connected with a ‘+’. The speed of a motor can be adjusted with the SetPower command. The ‘2’ at the end of the command is the speed setting, it can be adjusted from 1-5. If no speed is selected, it defaults to the fastest speed,5.

 

 

To be sure that these outputs act smart, input controls are needed. Controlling inputs requires code in this format…

 

task main( )

{

SetSensor(SENSOR_1,SENSOR_TOUCH);

OnFwd(OUT_A+OUT_C);

Until (SENSOR_1 = = 1);

Off(OUT_A+OUT_C);

}

 

The first line of code instantiates the sensor and lets the language know that it is a touch sensor. Now whatever comes after this code will run until the sensor is pushed in. The sensor reads ‘1’ if pushed in, and ‘0’ if released. Interesting programs can be achieved by playing with these values, and with the sensors on the robot.

 

 

The light sensor, temperature sensor, and rotation sensor can all be applied in this same fashion. The next bit of useful code is the loop. It is much more basic than in C++, but can be applied in the same way.

 

task main( )

{

#define shift_time 300

int total_time;

SetSensor(SENSOR_1,SENSOR_TOUCH);

repeat(3)

{

do

{

if (SENSOR_1 = = 1)

{

OnRev(OUT_A);

Wait(shift_time);

OnFwd(OUT_A);

total_time += shift_time;

}

}while(total_time < 3000);

}

}

 

This example has many elements that help it work. I began the code with ‘int’s’. These are the same ints found in C++. The value of an int can be changed at any time throughout the code. The define statement creates a constant int and gives it the value assigned after the instantiation, in this case 300 (each tick is 1/100 of a second, 300 is 3 seconds). The “repeat” command is like the “for” loop in C++. It will execute regardless of condition 3 times. Next is a “do while” loop. It executes a set number of commands until the while condition is met. In this example, it will jump out of the loop after 30 seconds, then repeats 2 more times.

This was a basic walkthrough of what can be achieved in NQC. Even with such a basic understanding of this language, complex programs can be created.