Migrating from NQC to NXC

This is a Web page for coaches and their teams that have Lego
NXT robots and have had some experience with Not Quite C for
the Lego RCX robots. Not eXactly C is a similar language,
perhaps a little closer to C, that will help you take full
advantage of your new robots' strengths.

As I write, NXC is still in beta, Ver.1.0.1 b25 to be exact.
This page is thus also in beta. Please check back often and
email suggestions and questions to me jmmiller@ltu.edu.

Doing the missions that are mentioned in Dr. Chung's workshops
loosely based on Robofest 2007, Miner Rescue: (For these
missions we will use the "Tribot" NXT setup. There is a touch
sensor on port 1, a light sensor on port 3, and a ultrasonic
sensor on port 4. The right motor runs from port B and the left
motor runs from port C.)

Go forward for 2 seconds and come back by reversing the motors.
Here we will use the RCX notion of controlling the distance
traveled by setting the travel time.

// mission1.nxc
#include "NXCDefs.h"
// The required #include is new for NXC
#define LEFT OUT_C
#define RIGHT OUT_B
#define BOTH OUT_BC
// In NQC this would have been #define BOTH OUT_B + OUT_C
task main()
{
OnFwd(BOTH,75);
Wait(2000); // Time in NXC is in thousandths and not hundredths of a second.
OnRev(BOTH,75);
Wait(2000);
Off(BOTH);
}

Go forward 1.5 seconds and come back by spinning left.
Experiment with controlling the motors by degrees of
rotation. You can continue to figure distance as speed
× time as you did with the RCX, but speed can vary
during a competition as your batteries wear down.

Stop at edge of the table. First use your View menu to take
light sensor readings from the white table top, the black
lines and looking down over the edge of the table. You might
find numbers like 62% over white, 40% over black and 28% off
the edge with a dark floor..

Stop the robot when it sees a 2nd black line on the Miner
Rescue field. Use a variable to count to 2. Be careful not
to count the same line many times by beginning to count
during the time spent crossing the line. Remember even a
small computer counts pretty quickly.

Loop until a touch sensor is pressed. Use separate tasks.
This mission, each loop will be completed before stopping.
In the next mission we will try to stop the motors right
away. Notice that the power and direction setting is an
expression: 75 * direction. This would not have been
allowed in NQC because the RCX firmware required constants
for parameters like these.

Follow a black line until the touch sensor is pressed. This
time, try to stop right away so that the robot does not run
into whatever pressed the touch sensor. To do this we will
experiment with the if-else-if form of the decision making
if statement. To follow the line, try following one edge of
the line. That means to stay in the zone where the
reflected light is a little brighter than over the center of
the black line, but still a little darker than over the
white background.

Follow the line until it detects the foil. First we
use the view menu to find that the reflected light over
the foil is 76%. Next we learn to use a function to
set the limits for line following "dynamically." This time
the robot starts with its light sensor directly over the
black line. This is important because light sensors, like
motor speed, change as your batteries wear down.

Follow the line until it climbs back to the home base. This
is an advanced mission where we can use the rotation sensors
built into the NXT motors to tell when we have made a
90° turn. Back in Mission 2 we turned 180° by
turning each wheel 370° in opposite directions. Where
did our guess of 370 come from. The Tribot has 56 mm.
tires and a 115 mm. wheel base. To turn exactly 180&deg,
each wheel would have to travel
115 × π / 2 mm.; and thus,
since each wheel revolution covers 56 × π
mm., 115 / (56 × 2) rotations or
369.6°. The difference between the wheels' rotations is
370 - (-370) or 740° for half-turn or 370°
for a quarter-turn. For this mission, as we are coming
around the perimeter black line and are on the segment
approaching the foil patch, we would like to know where we
are before we get to the foil where we might crash
into our partner robot -- killing the miner we just rescued.

Follow the line until the ultrasonic sensor detects
something 6 inches away. This is also an advanced mission
for using NXC. I am not sure if there might be problems
if two robot partners are close together and using their
ultrasonic sensors at the same time. Slowing down at 10-12
inches away might be a useful tactic.

For this last advanced mission the robot will look for a
miner that is not near a black line. These miners are a bit
short, so you will have to point the ultrasonic sensor down
a bit compared to the standard Tribot.

The Bricx Command Center works well for writing and managing
NQC programs on a Windows XP laptop. For NXT there are some
issues with properly installing the Fantom drivers in order
to use Bluetooth connections. Hopefully Bricx CC will be
out of testing and there will be some experience with
Windows Vista. In the meantime, if you can produce an .rxe
file from your .nxc file, using Bricx CC/NBC, then you can
always use the memory tab of the NXT Window in the NXT LEGO
software, and simply click the "Download" button to select
and download the .rxe file.

There is no MacNXC yet. However, the NBC compiler runs on
Windows, Mac and Linux systems.

For a PPC Mac, you can use the LEGO software to download
your .rxe programs as described above. Unfortunately,
newer Intel Macs and Linux boxes are still not supported
by LEGO. Maybe soon.

Downloading your .rxe program file to your NXT robot is
relatively easy in Ruby. I have tested this on a PPC Mac and
hope to have details on this and testing on other platforms
available here soon. You are welcome to use this, but please
write and tell me if you have any problems. For example,
to compile and download mission1.nxc: