Lego Mindstorms
Tutorials
Date: Jan 2012
There are a number of tools and IDEs you can use to create programs for the NXT Mindstorms. I would argue that the best tool should support most operating systems and involve C or a similar language (there is a limited memory on the brick so good memory management becomes an issue). While
Robot-C is probably the best choice in terms of integration with the brick, it is a commercial tool and it only work on Windows so far. After
comparing a number of options, I chose
NXC as a pretty good alternative to Robot-C. It supports a lot of things like sensor and motor management, playing of sounds, basic 2D and 3D graphics, etc. Most importantly, you can compile your programs from the command line or from XCode. This tutorial assumes knowledge of XCode 4 and of C.
The NXC programming guide is a good reference.
Setting up an XCode project
This blog by Mark C summarises the main points of NXC XCode development and provides a downloadable Hello World program. Rather than repeating what was said before, I would like to give a step-by-step guide to creating a project from scratch. The reason for that is that even if you download somebody else's HelloWorld program, XCode does not allow you to easily change the project name so it is always easier to create a new project.
1. Download
The first step is to download
the latest nxc compiler and
nxt firmware. Put the compiler on your disk, e.g. to the /Developer folder. Also, you might find the
NeXT Tools useful.
Please read the tutorial Setting up NXC Development on a Mac if you are not sure about this step.
2. Create project
In XCode, choose
'Create new project' and select
'External Build System' under Mac OS X -> Other. This will take care of some basic build and target setup. In this example, we name our project 'MyProject'.

3. Create program files
Create folders
'build' and
'source' in the project folder and drag them into XCode to create groups. Next, create the main project file in the 'source' folder, naming it
'MyProject.nxc'. This file should contain the main task, i.e. code that will get executed when the program runs. Put the following into the file:
task main() {
TextOut(0, LCD_LINE2, "Hello world!");
until(ButtonPressed(BTNEXIT, true));
}
This tells the program to display text until the exit (dark gray) button on the brick is pressed. You should always use the 'until' statement or a while loop unless you want your program to exit as soon as it starts.
4. Create a make file
Create file named
'Makefile' (no extension) in the root of the project directory, i.e. one level up from 'source'. The Makefile will automatically be linked to XCode's Build function. Paste in the following:
NXC=/Developer/SDKs/nxt/nbc
OPTIONS=-Z2 -I=../$(SOURCE)/ -EF -d
SOURCE=source
BUILD=build
PROGRAM=MyProject
all: $(PROGRAM).rxe
$(PROGRAM).rxe: $(SOURCE)/$(PROGRAM).nxc Makefile
cd $(SOURCE); \
$(NXC) -O=../$(BUILD)/$(TARGET_NAME).rxe \
$(OPTIONS) \
$(PROGRAM).nxc
clean:
/bin/rm -vf $(BUILD)/$(PROGRAM).rxe
The first part of the file defines some constants and you should change them based on your computer setup. The
NXC and
NXTCOM specify paths to command-line tools you downloaded in Step 1. The
SOURCE and
BUILD constants specify your folder names and
PROGRAM sets the program name, which should be the same as your project and main .nxc file name.
Press build (CMD + B) and the program should build and be downloaded onto the NXT brick.
Some system have problems when using spaces in the Makefile. Download the Makefile if it does not work when you paste the code in.
Including C files
You are free to use .h files that contain C code in your project, although you are limited by what the NXC compiler supports. I personally only create the main task in NXC and continue coding in a .h file. For example:
MyProject.nxc
#include "MyProject.h"
task main() {
runProgram();
}
MyProject.h
void runProgram() {
TextOut(0, LCD_LINE2, "Hello There!");
until(ButtonPressed(BTNEXIT, true));
}
Notice that while standard C syntax is used ('void' instead of 'task'), you can still use all NXC functions (e.g. TextOut).
In order to work with included files, the NXC compiler needs to know where to look for them. The includes folder can be specified in the Makefile using the –I option:
OPTIONS=-Z2 -I=../$(SOURCE)/
In this example, all .h files are located in the same 'source' folder as the main .nxc file. See nxc –-help for more compiler options.
Comments
[12/03/2021]
[19/12/2014]
[19/12/2014]
[13/01/2013]
[13/01/2013]
{Please enable JavaScript in order to post comments}