A Not eXactly C Workshop for Robofest 2009

This workshop was in the morning of January 24th. However,
this Web page will be updated all the way to World Robofest
2009. Please send me your comments and contributions at jmmiller@ltu.edu.
Even if you were not able to come to the workshop, and you have
questions or suggestions for further "missions" that we might
do together on-line, I would be happy to hear from you. Check
back here to see updates like a correction, or a better mission 6
suggested by some team members who were keeping me honest
today.

The programming language used in this workshop is Not eXactly
C. NXC is like its predecessor Not Quite C. NQC was invented
by Dave Baum for his students at the University of Utrecht for
use with the RCX LEGO brick. NXC for the LEGO NXT brick, by
John C. Hansen, continues in this tradition. Visit the Web pages for NXC.

RobotC is another dialect of C for the NXT brick. In keeping
with the Robofest traditional effort to simultaneously maximize
the students' learning while holding down participation costs,
we will not be exploring the proprietary RobotC in this
morning session. (Check the afternoon session for a way to
test drive the RobotC product for free.)

The sample programs during the workshop use the
following connections:

Motor / Sensor

Port

Left Motor

C

Right Motor

B

Left Light Sensor

2

Right Light Sensor

3

Ultrasonic Sensor

4

Touch Sensor

1

3rd Motor

A

The drawing challenge is new to Robofest. In a way it is a
return to the foundations of Robofest, forty years ago in the
work of Seymour Papert.
The text based language Papert wrote for young children is still
easily available as Berkeley
Logo. Monitors were very rare in those times, and so the
original output device was a plastic turtle moving on a sheet
of paper with 2 wheels and drawing with a marker stuck through
its shell. The unit of drawing measurement was 1 turtle-step.
There are lots of Logo commands, but in order to understand
turtle drawing, we just need to consider these:

Command

Abbr.

Function

forward x

fd x

Go forward x turtle-steps.

back x

bk x

Go backward x turtle steps.

left x

lt x

Turn left x degrees.

right x

rt x

Turn right x degrees.

pendown

pd

Put pen down on the paper.

penup

pu

Pick pen up off the paper.

One expense worth including in your total cost of team participation
is a couple of books

NXC is a powerful text-based language that can help your team
excel in Robofest 2008. Text-based languages are important in
a world where a long term job commitment might be 1 year, and
teamwork is much more common than solo work. Donald Knuth's
concept of "literate" programmers is more important than ever.
We need to be able to communicate clearly what our programs do
and how they do it. Otherwise they will be not much help to
our team and useless, as soon as we move to our next job.

Using #define's and // comments in NXC makes programs easier to
read. Learn to use them!

The ability to store intermediate results in a text file is new
in the NXT. Your autonomous robot will be smarter if it can
exploit this new ability to remember its failures and
successes. Not surprisingly, manipulating text files is
actually easier in the text-based NCX than in the icon-based
NXT-G.

Today we will be writing programs to handle some missions on a
laptop and "cross compiling" the programs so that the
executable is suitable to run on the NXT LEGO brick.
Transferring that executable file from the laptop to the brick
we will refer to as downloading. For more on this whole chain
of events see Appendix A below.

Our missions for today (and a few for homework)

Begin by getting our laptops and NXT bricks to work together.

// mission1.nxc
// Since 1972, "Mission 1" has been the Hello,World! program.
task main() // Every program must have a main task.
{
TextOut(0,LCD_LINE3,"Hello, World!");
Wait(10000); // Hold our message on the screen for 10 seconds.
}

The ability to multi-task is vital to robotics. By a small
decrease in flexibility, the icon programming languages make
multitasking more accessible to beginning programmers. We
need work on two concepts today to keep our tasks from
interfering with each other: ordering and synchronization.
For practice at ordering tasks, consider Mission 2.
The ordering of the lines of code on the page is a
separate issue. With a 1-pass compiler things have to
be defined or declared before they can be used or referred
to. Try moving task main() to be the first task mentioned
in the mission2.nxc and see how it the program compiles.

The motors that come with the NXT set have built in rotation
sensors. This mission and the next will explore those new
motors. In his book Hansen points out that motor control in
NXT-G eats up 10 times as much executable memory space as in
NXC. His next point that you cannot see the motor speed in a
simple screen shot of NXT-G, is perhaps even more important
for student programmers.

For the next mission we can try using a subroutine. In
general it helps to separate code, that is repeated in
several parts of your program, and place it in a subroutine.
In contrast, two pieces code that are to be done at the same
time, like going forward while watching for the edge of the
table, need to be in separate tasks, not separate
subroutines.

Appendix A. Writing, compiling,
downloading and running NXC programs. If all your teams'
laptops are running Windows, the Bricx Command Center
IDE is probably all you need to get started. If you use
mixed operating systems, then Emacs may be a help. In any
operating system using Bluetooth wireless is a whole lot easier
than keeping track of USB cables, and is worth the extra effort
to set it up. See the FAQ section for Bricxcc or consider a
.emacs function like this to automate things.