- Topic 1
NQC - Not Quite C
What is NQC
NQC is a structured programming language written especially for the Lego RCX brick. It is an alternative to the LabView software that comes with the RCX.
It is viable for several LEGO MINDSTORMS products including the RCX, CyberMaster, and Scout. NQC's syntax is very similar to the C programming langauge, so experienced C programmers (and Java programmers) should find it very easy to get started with. Even if you aren't an experienced programmer, NQC is relatively easy to learn.
This is important!
RCX was released onto the market in 1998. As a result, it has been superceded long ago, so it is possible to find these very cheap around schools in back cupboards and in dark, secret store rooms. There is also a selection of legacy documentation hiding in corners around the 'net.
Setting up RCX as RCX2 in Windows
This is a summary of the advice on the net.
1. My Windows machine comes with preloaded anti virus software. To run the following, boot into SAFE MODE then edit autoexec.bat usng sysedit from the command prompt.
NQC uses the same firmware as LEGO's standard tools (RCX Code and Robolab). This means it is possible to have RCX Code, Robolab, and NQC programs loaded onto the RCX at the same time. NQC also benefits from the stability and user interface (e.g. the View button) provided by the standard firmware. On the minus side, NQC must live within the constraints of the standard firmware. For example, since the firmware does not provide floating point support, NQC cannot provide it either. Other altermatives for programming the RCX (notably legOS and pbForth) do not have such restrictions.
- Kids like Lego
- Using Lego, the robot build is very quick
- ncq is a structured language
- It is free and comes with tutorials and documentation
- There is legacy teaching material available on the 'net
- The usual learning curve
- Old bricks might not work (Check the battery contacts for corrosion.
- RCX lead may have degraded
However, around the world, there are adult groups who continue to enthuse about Lego
The specifics of setting environment variables depends on your operating system and shell. For example, under Windows you could type the following in a command shell:
If you want to always use RCX 2.0, then you can make this setting permament by editing the AUTOEXEC.BAT file and adding the above command to the end of the file. This way, the variable will get set every time Windows is started.
2. On loading the Command Center (CC) choose RCX2 or within CC, go to Edit => Preferences = > Startup => RCX2
3 Compile and download.
NQC is a really good introduction to the programming language used in the Arduino environment. It's major advantage is that the build is quick and easy so users can get to programming straight away.
- Topic 2
What is LEGO MINDSTORMS?
LEGO MINDSTORMS is a series of LEGO sets that use special programmable bricks to allow construction of robots.
The most versatile programmable brick is the RCX, which can be found in the Robotics Invention System set.
A simpler brick, the Scout, can be found in the Robotics Discovery set.
The simplest brick, Micro Scout, is featured in two sets with a Star Wars theme: Droid Developer Kit, and Dark Side Developer Kit.
Even though it is the most expensive, the RCX based set is still the best value due to the versatility of the RCX itself. The Scout is a bit more limited, but still can be programmed (using NQC) to do some interesting things. The Micro-Scout is extremely limited, and it useful mainly as an accessory to another programmable brick such as the RCX or Scout.
Further information on MINDSTORMS sets can be found at the official site: www.legomindstorms.com.
- Topic 3
Download the software from Sourceforge. The documentation is available both on Brickx site and as part of the help in the editor.
The life cycle of any project has some distinct steps. For computer related pursuits it goes :
- Goal setting
- The build
The goal here is to build, program and test and tankbot. A smart place to start is to see what someone else has done and maybe modify it.
Manual at bricxcc.sourceforge.org Download .doc to follow the following modules.
- Topic 4
Up and running
The RCX brick uses an infra-red tower to commnicate with the brick. To get the tower driver for your computer, you might need to hunt round the net.
For Windows, try the SDK25 link. Or if you have your mindstorms disk, try that.
The SDK25 also has the Firm038.lgo file which is the firmware or operating system for the brick.
You can run RCX2 commands on an RCX1 brick by downloading the appropriate firmware.
When writing code for RCX, make sure you save it as a .nqc file. Otherwise it won't recognise it.
Hints and tips
- Make sure your brick is turned on. It will beep. It also goes to sleep.
- Look at Tools => Diagnostics to see if your brick is alive
- Choose to download firmware. Sometimes the firmware is lost and you have to reload it.
When you go to download your code, make sure you save it in the format for your brick.
ie it is likely if you are using an RCX 1.0 you need to upload code saved as ncq.
If the name Mark Overmarrs (Gamemaker author) is on a a tutorial, you know it is a good one.
Work through the exercises. Then if you want to extend yourself, go to ncq Programmer's Guide by Dave Baum & John Hansen which is available either in help or on the Brickx web site
RIS = Robot invention system
- Topic 5
Section 1 : Writing you first program
Tutorials on structured programming follow pretty much the same path.
- Why do programming
- Reserved words
- Declaring variables
- Sub procedures
- Functions and procedures
- Parameters and parameter passing
There is then a general divestment into things like
- Str handling (Text)
- Include file
pp 5 - 8
Remember, NCQ is written for the RCX.
We start with the inevitable robotic equivalence of "Hello world"
A summary of commands
Command center, RCX, sequence
It is best to work as if all NQC is case sensitive, including the CamelCase statements
OnFwd(), Wait(), OnRev(), Off(), SetPower()
- Topic 6
Section 2 : A more interesting program. Iteration
There are two ways to make your brick turn. Using only one motor ( sweep turn) or making two motors act in oposite directions (point turn) pp 9 - 11
It is surprising how easy one forgets. Your program should include three parts (to start with)
The comments at the head of the program should
- task main()
- Explain what the program is about.
- Other relevant information.
Turn, point, sweep, repeat, loop, nested loop, comment, constant
You can only store 32 variables and they only allow integers.
- Constants in CAPITALS
- Variables all lower case
- Functions in CamelCase
- coontrol structes in lower case
#define, repeat(), Random(), while()
- Topic 7
Section 3 : Using variables
A variable is a value stored in a memory location in the RCX memory. This value is is allowed to change and overwrite the value in the memory location.
pp 12 - 13
Find out the rules for namimg variables.
Later on we will see how variables are passed around s parameters.
int, Random(), while()
integer, operations, random, Boolean operator,
- Topic 8
Section 4 : Control structures
Cotrol structues do exactly that, control the way a program runs. They are used to wait for some sort of trigger, then change the path the code follows.
They are used to repeat parts of the code
You can set a condition inside a loop using a sentinal
- Test at top of loop (if .. then ... else)
- A fixed number of times (repeat())
- Test at bottom of loop (do ... while())
Especially useful in the way you use sub-procedures.
pp 14 - 15
Named after George Boole, secret author of "Alice in Wonderland". True, false etc.
if, else, do, while
While vs Do
Look at the notes pp 14-15.
The while tests at the start of code, the do executes at the bottom of the code.
Thedo loop must execute at least once, while can be set not to run at all.
decision, test, execute, counter, incriment
- Topic 9
section 5 : Sensors
A sensor is a device that changes according to its state then sends a signal to measuring device.
There are four sensors that come with RCX
However, there is a way to calibrte you brick to use anything for a sensor
pp 16 - 18 You can make an RIS kit using black insulation tape on the back of a white board. I use old beer signs from a hotel.
sensor, actuator, feedback loop, condition,
SetSensor(), SENSOR_TOUCH, SENSOR_LIGHT, SENSOR_1,
- Topic 10
Section 6 : Tasks and subroutines
We are now getting to the fun part of the exercise.
The RCX has limited memory, so if programs can be written so parts can be reused, memory space can be saved.
Later, we will see how values (called parameters) can be passed between sub procedures
pp 19 - 22
There are four ways you can break code up in NCQ
- macros (short for macroinstuctions)
- inline functions
- sub procedures
Inline functions allow for parameters to be passed to them.
Can you figure out how these are all different?
macro, task, sub routine, inline function,
- Topic 11
Section 7 : Making music
Your RCX brick is capable of sound. pp 23 - 24 An old exercise is to write a song such as "Three blind mice" where the rounds are written as subprocedures, then who can produce the sone with the smallest amount of code
frequency, Hertz, time
- Topic 12
Section 8 : More about motors
You can operate motors in a more precise way.
pp 25 - 26
This will bear more investigation to see if ticks and 360 degrees are related (ie is an RCX motor a stepper)
Unfortunately, Lego motors are prone to seizing. Literature suggests the motors are fine but the gears are jammed.
On(), Of()f, Float(), Fwd(), Rev(), Toggle(), OutFor(),
- Topic 13
Section 9 : More about sensors
There are four sensor modes
pp 27 - 29
SENSOR_MODE_RAW may be the most useful as it reads a sensor to a value of betwee 0 - 1023.
This allows you to use a range of sensors not necessarily defined in NCQ
The documention around the web suggests many ways you can build or modify sensors.
There is also some Youtube videos showing RCX bricks being operated from TV and Wii remotes.
- Topic 14
Section 10 : Parallel tasks
Here is where the difference between a task and a sub-procedure comes to the fore. pp 31 - 33
- Topic 15
Section 11 : Communication between robots
Swarm technology is a new and exciting field of study
- Topic 16
Section 12 : More commands
This code sets a sensor to temperature and reads a sensor 50 times. Find the datalog icon 5 from the right on the bottommenu in bricx cc to upload and analyse data.
Off(OUT_A); // connect LED to OUT_A now OFF
SelectDisplay(DISPLAY_SENSOR_2); Wait(100); // Display readings on RCX display
OnFwd(OUT_A); // LED ON while working
CreateDatalog(50); // create space for logging
repeat (50) // 50 records
Off(OUT_A); // LED OFF
pp 37 - 39
The Lego temp sensor may not be the most accurate.
- Topic 17
I remember when the NCQ was first released, we could steer it by suspending a brick above it and operating it in direct mode
We could also program it in Visual Basic usiing SPIRIT.OCX
All pretty cool.
- Topic 18
- Topic 19
- Topic 20
- Topic 21
- Topic 22
/* This sketch reads a temp probe and turns a motor on and off
as the temperature changes
It demonstrates a feedback mechanism using 2.25L PET bottle, RCX
brick, RCX temp pobe and CPU fan.
Plus some dodgy homebrew modifications.
int Threshhold = 220 ; // approx 22 degrees centigrade
Off(OUT_A); // set fan to Off
while(true) // endless loop
Wait(10); // Display readings on RCX display
x = SENSOR_2; // reads port 2
if (x > Threshold) // it seems the value is T_hold*10
OnFwd(OUT_A); // fan on here
Off(OUT_A) // fan off here
- Topic 23
- Topic 24
- Topic 25