Professional Documents
Culture Documents
LEGO Mindstorms NXT 2.0 Instructions: Instructions For Building The Shooterbot, Robogator, Color Sorter, and Alpha Rex
LEGO Mindstorms NXT 2.0 Instructions: Instructions For Building The Shooterbot, Robogator, Color Sorter, and Alpha Rex
Table of contents
Introduction
Programming the NXT with Linux
Compiling an NXC program
Shooterbot
1. Driving base Building guide Programming guide Test guide
2. Color detection Building guide Programming guide Test guide
3. Shooter Building guide Programming guide Test guide
4. Locate objects Building guide Programming guide Test guide
Robogator
1. Jaws Building guide Programming guide Test guide
2. Eyes Building guide Programming guide Test guide
3. Legs Building guide Programming guide Test guide
4. Tail Building guide Programming guide Test guide
Color Sorter
1. Dispenser Building guide Programming guide Test guide
2. Color detector Building guide Programming guide Test guide
3. Sorting tray Building guide Programming guide Test guide
4. Color catapult Building guide Programming guide Test guide
Alpha Rex
1. Legs Building guide Programming guide Test guide
2. Arms Building guide Programming guide Test guide
3. Head Building guide Programming guide Test guide
4. Color sensor Building guide Programming guide Test guide
Introduction
The instructions for building the four main LEGO Mindstorms NXT 2.0 models are distributed only on software that
does not support Linux. The instructions are reproduced here.
The accompanying programs supplied here are written in NXC. NXC is a simple C-like programming language
supported by NBC. NBC allows you to write NXT programs and download them to the NXT. This configuration guide
for NBC on Linux is useful.
Shooterbot
Can you build and program a Shooterbot to guard your room while you are away?
1. Driving base
Building guide
Can you build a Shooterbot to move forward and then move back again?
Programming guide
Can you program a Shooterbot to move forward on the test pad and then move in reverse to its start position?
// shooterbot_1.nxc
// Move forward and then reverse.
task main ()
{
// Move forward at 75% power for 2 seconds
OnFwd (OUT_BC ,75); Wait (2000);
// Brake
Off (OUT_BC);
}
Test guide
Download your program. Place the Shooterbot on the test pad and run the program.
2. Color detection
Building guide
Can you program a Shooterbot to move forward, find the red line, turn around, and then move back to its start
position?
// shooterbot_2.nxc
// Move forward to the red line, turn around, and move back to the start
// position.
task main ()
{
long t0, t1;
Test guide
Download your program. Place the Shooterbot on the test pad and run the program.
3. Shooter
Building guide
Can you build the Shooterbot to shoot balls at objects? You will have to build the shooter on the front of your
Shooterbot.
Programming guide
Can you program Shooterbot to drive around and shoot while showing different colored lights?
// shooterbot_3.nxc
// Drive around and shoot while showing different colored lights.
#define BULLETS 7
#define FLASH_DURATION 300
#define TURN_TIME 3000
task flashing_lights ()
{
int red_flashes;
task shoot ()
{
while (true)
{
// Check whether to fire a bullet
if (true == fire)
{
// Reset the 'fire a bullet' flag to false
fire = false;
task move ()
{
while (bullets_remaining > 0)
{
// Do a random turn
if (0 == Random (2))
{
// Left turn
OnFwd (OUT_B, 75); OnRev (OUT_C, 75);
Wait (Random (TURN_TIME));
Off (OUT_BC);
Wait (1000);
fire = true;
Wait (1000);
}
else
{
// Right turn
OnFwd (OUT_C, 75); OnRev (OUT_B, 75);
Wait (Random (TURN_TIME));
Off (OUT_BC);
Wait (1000);
fire = true;
Wait (1000);
}
}
task main ()
{
// Make these tasks execute simultaneously after main () is finished
Precedes (flashing_lights, shoot, move);
}
Test guide
Download the program. Place the Shooterbot in the middle of the test pad and run the program.
4. Locate objects
Building guide
Can you build the Shooterbot to protect your room? You will have to add the Ultrasonic Sensor to your Shooterbot.
Programming guide
Can you program the Shooterbot to scan your room and detect objects? If an object is closer than 40cm, the
Shooterbot should shine a warning light and then shoot if the object does not move away.
// shooterbot_4.nxc
// If an object is to close, shine a warning light and then shoot if
// the object does not move away.
#define BULLETS 7
#define FLASH_DURATION 200
#define THRESHOLD 40
#define WARNING_TIME 4000
task main ()
{
long t0;
int bullets_remaining = BULLETS;
Test guide
Download the program. Place the Shooterbot in the middle of the test pad and run the program.
Robogator
Can you build and program a Robogator that attacks objects and protects its territory?
1. Jaws
Building guide
Can you build a robot with jaws that open and close?
Programming guide
Can you program your robot to open and close its jaws?
// robogator_1.nxc
// Open and close the jaws
task motor_speed_monitor ()
{
long mrc_a_0, mrc_a_1; // mrc == motor rotation count
unsigned long t_a_0, t_a_1;
while (true)
{
// Record the rotation count and the time
mrc_a_0 = MotorRotationCount (OUT_A); t_a_0 = CurrentTick ();
Wait (ROTATION_SAMPLE_RESOLUTION);
task bite ()
{
while (true)
{
// Assume the jaws are closed to begin with - open them
OnFwd (OUT_A, 40); Wait (100); // Wait > ROTATION_SAMPLE_RESOLUTION
until (10 > dps_a); // Wait until the jaws have opened and are practically still
Off (OUT_A);
task main ()
{
Precedes (bite, motor_speed_monitor);
}
Test guide
Download and run the program. Your Robogator jaws should open and close.
2. Eyes
Building guide
Can you make your Robogator see objects and attack them if they get too close?
Programming guide
Can you program your robot to bite when objects get too close?
// robogator_2.nxc
// Open and close the jaws when objects are close
task motor_speed_monitor ()
{
long mrc_a_0, mrc_a_1; // mrc == motor rotation count
unsigned long t_a_0, t_a_1;
while (true)
{
mrc_a_0 = MotorRotationCount (OUT_A); t_a_0 = CurrentTick ();
Wait (ROTATION_SAMPLE_RESOLUTION);
task bite ()
{
SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line
while (true)
{
until (SensorUS (S4) < THRESHOLD); // Scan for close objects
task main ()
{
Precedes (bite, motor_speed_monitor);
}
Test guide
Download and run the program. Move your hand towards the Robogator's eyes. The Robogator's jaws
should start biting. Move your hand away and the Robogator should stop biting.
3. Legs
Building guide
Can you program the Robogator to walk forward and start biting when objects get too close?
// robogator_3.nxc
// Open and close the jaws and walk forward when objects are close
task motor_speed_monitor ()
{
long mrc_a_0, mrc_a_1; // mrc == motor rotation count
unsigned long t_a_0, t_a_1;
while (true)
{
mrc_a_0 = MotorRotationCount (OUT_A); t_a_0 = CurrentTick ();
Wait (ROTATION_SAMPLE_RESOLUTION);
task bite ()
{
while (true)
{
until (SensorUS (S4) < THRESHOLD); // Scan for close objects
task walk ()
{
int steps;
while (true)
{
until (SensorUS (S4) < THRESHOLD); // Scan for close objects
task main ()
{
SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line
Test guide
Download the program. Place your alligator on the test pad and run the program.
4. Tail
Building guide
Can you build the tail and add sensors to the Robogator so it knows where its legs are?
Programming guide
Can you program Robogator to protect its territory: lunge forward, bite, and crawl back?
// robogator_4.nxc
// When an object is close: open and close the jaws, walk forward,
// and then move back
task motor_speed_monitor ()
{
long mrc_a_0, mrc_a_1; // mrc == motor rotation count
unsigned long t_a_0, t_a_1;
while (true)
{
mrc_a_0 = MotorRotationCount (OUT_A); t_a_0 = CurrentTick ();
Wait (ROTATION_SAMPLE_RESOLUTION);
task bite ()
{
while (true)
{
until (SensorUS (S4) < THRESHOLD); // Scan for close objects
task walk ()
{
int i, steps;
while (true)
{
// Walk forward while an object is close
steps = 0;
while (SensorUS (S4) < THRESHOLD && steps < MAX_STEPS_FORWARD)
{
RotateMotorEx (OUT_BC, -100, 360, 0, true, true);
steps += 1;
}
task main ()
{
SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line
SetSensorTouch (S1); // Bring the touch sensors on-line
SetSensorTouch (S2);
Test guide
Download the program. Place Robogator on the test pad and run the program.
Color Sorter
Can you build and program the Color Sorter to sort balls by color?
1. Dispenser
Building guide
Can you program the dispenser to tilt and dispense one ball at a time?
// colorsorter_1.nxc
// Dispense one ball at a time
task main ()
{
int i;
i = 0;
while (i++ < BALLS)
{
dispense_ball ();
}
Test guide
Download your program. Place the Color Sorter in the middle of the test pad. Put a couple of balls in the
dispenser and run the program.
2. Color detector
Building guide
Can you build a color detector that can recognise the different ball colors?
Programming guide
Can you make a program that can tell the difference between the colors of the balls?
// colorsorter_2.nxc
// Dispense one ball at a time - display the color of each ball
void dispense_ball ()
{
RotateMotor (OUT_B, 60, 360);
Wait (1000);
}
void show_color ()
{
string ball_color = "";
switch (SENSOR_3)
{
case INPUT_BLUECOLOR:
ball_color = "BLUE";
break;
case INPUT_REDCOLOR:
ball_color = "RED";
break;
case INPUT_GREENCOLOR:
ball_color = "GREEN";
break;
case INPUT_YELLOWCOLOR:
ball_color = "YELLOW";
break;
default:
ball_color = "NONE";
break;
}
task main ()
{
int i;
i = 0;
while (i++ < BALLS)
{
// Show the number of balls left to sort
TextOut (0, LCD_LINE1, "Balls remaining: ", true);
NumOut (50, LCD_LINE3, BALLS - (i-1));
reset_dispenser ();
load_ball ();
show_color ();
dispense_ball ();
}
Test guide
Download your program. Place the Color Sorter in the middle of the test pad. Run the program.
3. Sorting tray
Building guide
Can you build a sorting tray that allows the Color Sorter to sort the four different colored balls into their own
basket?
Programming guide
Can you program the sorting tray to spin around and place balls of the same color in the same basket?
// colorsorter_3.nxc
// Place balls of the same color in the same basket
// Reset the dispensing tray and the basket when these are set to true
bool reset_dispenser_flag = false;
bool reset_basket_flag = false;
void dispense_ball ()
{
RotateMotor (OUT_B, -90, 360);
Wait (1000);
}
switch (SENSOR_3)
{
case INPUT_BLUECOLOR:
ball_color = "BLUE";
break;
case INPUT_REDCOLOR:
ball_color = "RED";
break;
case INPUT_GREENCOLOR:
ball_color = "GREEN";
break;
case INPUT_YELLOWCOLOR:
ball_color = "YELLOW";
break;
default:
ball_color = "NONE";
break;
}
return ball_color;
}
task reset_basket ()
{
while (true)
{
if (true == reset_basket_flag)
{
// Get the basket into a standard position using the touch sensor
OnFwd (OUT_C, 30);
until (0 == SENSOR_2); // Unset the touch sensor if it is set
until (1 == SENSOR_2);
Off (OUT_C);
RotateMotor (OUT_C, -30, 30);
reset_basket_flag = false;
}
}
}
task main ()
{
int i;
string ball_color;
StartTask (reset_basket);
StartTask (reset_dispenser);
while (true)
{
// Clear the display
ResetScreen ();
i = 0;
do
{
// Reset the positions of the dispenser and the baskets in parallel
reset_dispenser_flag = true;
reset_basket_flag = true;
if ("NONE" == ball_color)
{
// Assume no balls are remaining
TextOut (0, LCD_LINE3, "*** FINISHED ***", true);
Wait (4000);
break;
}
align_basket (ball_color);
dispense_ball ();
}
StopAllTasks ();
}
Test guide
Download your program. Place the Color Sorter in the middle of the test pad. Run the program.
4. Color catapult
Building guide
Can you build a color catapult that throws balls in different directions?
Programming guide
Can you program the color catapult to throw balls of the same color in the same direction?
// colorsorter_4.nxc
// Chuck balls of the same color in the same direction
// Reset the dispensing tray and the catapult when these are set to true
bool reset_dispenser_flag = false;
bool reset_catapult_flag = false;
void dispense_ball ()
{
RotateMotor (OUT_B, -90, 360);
Wait (1000);
}
switch (SENSOR_3)
{
case INPUT_BLUECOLOR:
ball_color = "BLUE";
break;
case INPUT_REDCOLOR:
ball_color = "RED";
break;
case INPUT_GREENCOLOR:
ball_color = "GREEN";
break;
case INPUT_YELLOWCOLOR:
ball_color = "YELLOW";
break;
default:
ball_color = "NONE";
break;
}
return ball_color;
}
task reset_catapult ()
{
while (true)
{
if (true == reset_catapult_flag)
{
// Get the catapult into a standard position using the touch sensor
OnFwd (OUT_C, 30); // Unset the touch sensor if it is set by moving toward the front si
until (0 == SENSOR_2);
OnFwd (OUT_C, -30); // Move back until the touch sensor activates
until (1 == SENSOR_2);
Off (OUT_C);
task main ()
{
int i;
string ball_color;
StartTask (reset_catapult);
StartTask (reset_dispenser);
while (true)
{
// Clear the display
ResetScreen ();
i = 0;
do
{
// Reset the positions of the dispenser and the catapult in parallel
reset_dispenser_flag = true;
reset_catapult_flag = true;
if ("NONE" == ball_color)
{
// Assume no balls are remaining
TextOut (0, LCD_LINE3, "*** FINISHED ***", true);
Wait (4000);
break;
}
dispense_ball ();
align_catapult (ball_color);
// Move the catapult back over to the front side, ready for resetting its position
if (ball_color == "GREEN") {
RotateMotor (OUT_C, 30, 45+90);
}else if (ball_color == "BLUE") {
RotateMotor (OUT_C, 30, 90+90);
}
StopAllTasks ();
}
Test guide
Download your program. Place the Color Sorter in the middle of the test pad and make sure the axle is
placed in the centre. Run the program.
Alpha Rex
Can you build and program the Alpha Rex to walk?
1. Legs
Building guide
Can you build a pair of legs that test walk in place, and then walk forwards and backwards?
// alpharex_1.nxc
// Walk in place, and forwards, and backwards.
if (1 == SENSOR_2)
{
OnFwd (OUT_C, 50); Wait (300);
until (0 == SENSOR_2);
Off (OUT_C);
}
OnFwd (OUT_C, 50);
until (1 == SENSOR_2);
Off (OUT_C);
}
void walk (int direction, unsigned long duration)
{
unsigned long t0;
reset_legs ();
Wait (2000);
t0 = CurrentTick ();
switch (direction)
{
case INPLACE:
case FORWARDS:
case BACKWARDS:
Off (OUT_BC);
}
task main ()
{
// Bring the touch sensors on-line
SetSensorTouch (S1); SetSensorTouch (S2);
Test guide
Download the program. Place the Alpha Rex on the test pad. Run the program.
2. Arms
Building guide
Can you build the Alpha Rex with a pair of arms and moveable hands?
Programming guide
Can you program the arms to move back and forth while the hands open and close?
// alpharex_2.nxc
// Move the arms back and forth
task main ()
{
while (true)
{
// turn right - open left hand
RotateMotor (OUT_A, 100, 2*60);
// back to center
RotateMotor (OUT_A, -100, 2*60);
Wait (1000);
// back to center
RotateMotor (OUT_A, 100, 2*60);
Wait (1000);
}
}
Test guide
Download the program. Place the Alpha Rex on the test pad. Run the program.
3. Head
Building guide
Can you build the Alpha Rex to see? You will have to build a head on it!
Programming guide
Can you program the Alpha Rex to move forward when it sees an object?
// alpharex_3.nxc
// Move forward when an object is visible
if (1 == SENSOR_2)
{
OnFwd (OUT_C, 50); Wait (300);
until (0 == SENSOR_2);
Off (OUT_C);
}
OnFwd (OUT_C, 50);
until (1 == SENSOR_2);
Off (OUT_C);
}
t0 = CurrentTick ();
switch (direction)
{
case INPLACE:
case FORWARDS:
case BACKWARDS:
Off (OUT_BC);
reset_legs ();
}
task main ()
{
SetSensorTouch (S1); SetSensorTouch (S2); // Bring the touch sensors on-line
reset_legs ();
Wait (500);
while (true)
{
until (SensorUS (S4) < THRESHOLD);
walk (FORWARDS, 5000); Wait (2000);
}
Test guide
Download the program. Place the Alpha Rex on the test pad. Run the program.
4. Color sensor
Building guide
Can you make the Alpha Rex detect colors by adding the Color Sensor?
Programming guide
Can you program the Alpha Rex to ask for the green ball and then identify it? The Alpha Rex should identify and
reject balls in other colors.
// alpharex_4.nxc
// Ask for a green ball
// TODO Use a voice to ask for a green ball when nxc adds
// support for sound files on Linux
void drop_ball ()
{
RotateMotor (OUT_A, -100, 2*60); Wait (800);
// back to center
RotateMotor (OUT_A, 100, 2*60);
}
if (1 == SENSOR_2)
{
OnFwd (OUT_C, 50); Wait (300);
until (0 == SENSOR_2);
Off (OUT_C);
}
OnFwd (OUT_C, 50);
until (1 == SENSOR_2);
Off (OUT_C);
}
t0 = CurrentTick ();
switch (direction)
{
case INPLACE:
case FORWARDS:
case BACKWARDS:
Off (OUT_BC);
reset_legs ();
}
task main ()
{
SetSensorTouch (S1); SetSensorTouch (S2); // Bring the touch sensors on-line
SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line
SetSensorColorFull (S3); // Bring the color sensor on-line
reset_legs ();
Wait (500);
while (true)
{
until (SensorUS (S4) < THRESHOLD);
// Display a message
TextOut (0, LCD_LINE1, " GIVE ME A ", true);
TextOut (0, LCD_LINE3, " *** GREEN *** ", false);
TextOut (0, LCD_LINE5, " BALL ", false);
Wait (2000);
ResetScreen ();
if (INPUT_GREENCOLOR == SENSOR_3)
{
// Do a dance
walk (INPLACE, 5000);
// Release the ball
drop_ball ();
}
else
{
// Move forward
walk (FORWARDS, 5000);
// Drop the ball
drop_ball ();
// Move back
walk (BACKWARDS, 5000);
}
Wait (1000);
}
}
Test guide
Download the program. Place the Alpha Rex on the test pad. Before running the program, the Alpha
Rex's hands should be closed and pointed forward (do this by turning the gear on the right side of its
back). Run the program.