Download as pdf or txt
Download as pdf or txt
You are on page 1of 240

Contact Home Search Categories BASH C Celestial Lego Various

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.

Programming the NXT with Linux

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.

Compiling an NXC program

A typical command to compile and download an NXC program by USB would be

nbc -d -S=usb filename.nxc

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);

// Reverse at 75% power for 2 seconds


OnRev (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 build a Shooterbot to test colors on the test pad?


Programming 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.

// Adjust this number to suit your surface / battery power


#define TURN_TIME 1750

task main ()
{
long t0, t1;

// Bring the color sensor on-line


SetSensorColorFull (S3);

// Record the time when the forward movement starts


t0 = CurrentTick ();

// Move forward to the red line


OnFwd (OUT_BC ,75);
until (INPUT_REDCOLOR == SENSOR_3);
t1 = CurrentTick (); // Record the time when the forward movement finishes
Off (OUT_BC);

// Turn 180 degrees to the right


OnFwd (OUT_C, 75); OnRev (OUT_B, 75); Wait (TURN_TIME); Off (OUT_BC);

// Move back to the start


OnFwd (OUT_BC ,75); Wait (t1 - t0); Off (OUT_BC);
}

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

int bullets_remaining = BULLETS;


bool fire = false;

task flashing_lights ()
{
int red_flashes;

while (bullets_remaining > 0)


{
// Alternate between the red, green, and blue lamps
SetSensorColorRed (IN_3); Wait (FLASH_DURATION);
SetSensorColorGreen (IN_3); Wait (FLASH_DURATION);
SetSensorColorBlue (IN_3); Wait (FLASH_DURATION);
}

// Indicate that no bullets remain


for (red_flashes = 1; red_flashes <= 10; red_flashes++)
{
SetSensorColorRed (IN_3); Wait (FLASH_DURATION / 2);
SetSensorColorNone (IN_3); Wait (FLASH_DURATION / 2);
}

// Stop the program


StopAllTasks ();
}

task shoot ()
{
while (true)
{
// Check whether to fire a bullet
if (true == fire)
{
// Reset the 'fire a bullet' flag to false
fire = false;

// Decrement the number of bullets remaining


bullets_remaining--;

// To fire, make motor A do a single 360 degree rotation


RotateMotorEx (OUT_A, 75, 360, 0, false, true);
}
}
}

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

#define start_turning(power) OnFwd (OUT_C, power); OnRev (OUT_B, power);

task main ()
{
long t0;
int bullets_remaining = BULLETS;

SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line


SetSensorColorGreen (S3); // Set the lamp to show all-clear

while (bullets_remaining > 0)


{
start_turning (50);
until (SensorUS (S4) < THRESHOLD); // Scan for close objects
Off (OUT_BC); // Stop turning
t0 = CurrentTick ();

while ((CurrentTick () - t0) < WARNING_TIME)


{ // Emit the warning - a flashing red lamp
SetSensorColorNone (S3); Wait (FLASH_DURATION);
SetSensorColorRed (S3); Wait (FLASH_DURATION);
}

if (SensorUS (S4) < THRESHOLD)


{
bullets_remaining--;
RotateMotorEx (OUT_A, 75, 360, 0, false, true); // Shoot
Wait (1000); // Give motor A time to shoot
}

SetSensorColorGreen (S3); // Set the lamp to show all-clear


}

Off (OUT_BC); // Stop turning


TextOut (8, LCD_LINE4, "OUT OF BULLETS", true);
Wait (7000);
StopAllTasks (); // Stop the program
}

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

// Sample the motor rotation speed every this number of milliseconds


#define ROTATION_SAMPLE_RESOLUTION 50

// The approximate 'degrees per second' rotation of motor A


long dps_a = 0;

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);

// Again, record the rotation count and the time


mrc_a_1 = MotorRotationCount (OUT_A); t_a_1 = CurrentTick ();

// Calculate the average rotation rate over that time interval


dps_a = ((mrc_a_1 - mrc_a_0) * 1000) / (t_a_1 - t_a_0);
}
}

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);

// Close the jaws


OnRev (OUT_A, 40); Wait (100);
until (-10 < dps_a); // Reverse rotations are negative
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

// Sample the motor rotation speed every this number of milliseconds


#define ROTATION_SAMPLE_RESOLUTION 50

// Bite objects which come closer than this


#define THRESHOLD 40

// The approximate 'degrees per second' rotation of motor A


long dps_a = 0;

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);

mrc_a_1 = MotorRotationCount (OUT_A); t_a_1 = CurrentTick ();

dps_a = ((mrc_a_1 - mrc_a_0) * 1000) / (t_a_1 - t_a_0);


}
}

task bite ()
{
SetSensorLowspeed (S4); // Bring the ultrasonic sensor on-line

while (true)
{
until (SensorUS (S4) < THRESHOLD); // Scan for close objects

// 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 motor stops
Off (OUT_A);

// Close the jaws


OnRev (OUT_A, 40); Wait (100);
until (-10 < dps_a); // Reverse rotations are negative
Off (OUT_A);
}
}

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 build a robot that can walk?


Programming 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

// Sample the motor rotation speed every this number of milliseconds


#define ROTATION_SAMPLE_RESOLUTION 50

// Bite objects which come closer than this


#define THRESHOLD 40

// Walk forward for up to this many steps when an object is close


#define MAX_STEPS_FORWARD 3

// The approximate 'degrees per second' rotation of motor A


long dps_a = 0;

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);

mrc_a_1 = MotorRotationCount (OUT_A); t_a_1 = CurrentTick ();

dps_a = ((mrc_a_1 - mrc_a_0) * 1000) / (t_a_1 - t_a_0);


}
}

task bite ()
{
while (true)
{
until (SensorUS (S4) < THRESHOLD); // Scan for close objects

// Assume the jaws are closed to begin with


OnFwd (OUT_A, 40); Wait (100); // Wait > ROTATION_SAMPLE_RESOLUTION
until (10 > dps_a); // Wait until the motor stops
Off (OUT_A);
OnRev (OUT_A, 40); Wait (100);
until (-10 < dps_a); // Reverse rotations are negative
Off (OUT_A);
}
}

task walk ()
{
int steps;

while (true)
{
until (SensorUS (S4) < THRESHOLD); // Scan for close objects

// 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

Precedes (bite, motor_speed_monitor, walk);


}

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

// Sample the motor rotation speed every this number of milliseconds


#define ROTATION_SAMPLE_RESOLUTION 50

// Bite objects which come closer than this


#define THRESHOLD 60

// Walk forward for up to this many steps when an object is close


#define MAX_STEPS_FORWARD 3

// The approximate 'degrees per second' rotation of motor A


long dps_a = 0;

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);

mrc_a_1 = MotorRotationCount (OUT_A); t_a_1 = CurrentTick ();

dps_a = ((mrc_a_1 - mrc_a_0) * 1000) / (t_a_1 - t_a_0);


}
}

task bite ()
{
while (true)
{
until (SensorUS (S4) < THRESHOLD); // Scan for close objects

// Assume the jaws are closed to begin with


OnFwd (OUT_A, 40); Wait (100); // Wait > ROTATION_SAMPLE_RESOLUTION
until (10 > dps_a); // Wait until the motor stops
Off (OUT_A);
OnRev (OUT_A, 40); Wait (100);
until (-10 < dps_a); // Reverse rotations are negative
Off (OUT_A);
}
}

task walk ()
{
int i, steps;

// Use the touch sensors to align the legs initially


OnFwd (OUT_B, -100);
until (1 == SENSOR_1);
Off (OUT_B); RotateMotor (OUT_B, -100, 45);

OnFwd (OUT_C, -100);


until (1 == SENSOR_2);
Off (OUT_C); RotateMotor (OUT_C, -100, 45);

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;
}

// Walk back to the original position


Wait (1000);
while (steps > 0)
{
RotateMotorEx (OUT_BC, 90, 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);

Precedes (bite, motor_speed_monitor, walk);


}

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 build a dispenser that separates balls one by one?


Programming guide

Can you program the dispenser to tilt and dispense one ball at a time?

// colorsorter_1.nxc
// Dispense one ball at a time

// The number of balls to be sorted


#define BALLS 4

// Get the dispenser in a standard position


void reset_dispenser ()
{
OnRev (OUT_B, 80);
until (1 == SENSOR_1);
Off (OUT_B);
Wait (1000);
}

// Dispense one ball


void dispense_ball ()
{
reset_dispenser ();
RotateMotor (OUT_B, 60, 720);
Wait (1000);
}

task main ()
{
int i;

// Bring the tilting mechanism's touch sensor on-line


SetSensorTouch (S1);

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

// The number of balls to be sorted


#define BALLS 4

// Get the dispenser in a standard position


void reset_dispenser ()
{
OnRev (OUT_B, 80);
until (1 == SENSOR_1);
Off (OUT_B);
Wait (1000);
}

// Get a ball into the dispenser slot


void load_ball ()
{
RotateMotor (OUT_B, 60, 360);
Wait (1000);
}

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;
}

// Show the ball color


TextOut (30, LCD_LINE3, ball_color, true);
Wait (1000);
}

task main ()
{
int i;

// Bring the tilting mechanism's touch sensor on-line


SetSensorTouch (S1);

// Bring the color sensor on-line


SetSensorColorFull (S3);

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

// Assume there are no balls remaining after this number of retries


#define RETRIES 2

// Reset the dispensing tray and the basket when these are set to true
bool reset_dispenser_flag = false;
bool reset_basket_flag = false;

// Get a ball into the dispenser slot


void load_ball ()
{
Wait (2000);
// Shake up the balls in the dispenser
RotateMotor (OUT_B, 90, 400);
RotateMotor (OUT_B, -90, 400);
Wait (500);
}

void dispense_ball ()
{
RotateMotor (OUT_B, -90, 360);
Wait (1000);
}

// Show the color of the ball on the NXT display


// Return the value of the color of the ball to the calling task
string 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;
}

// Show the ball color


TextOut (30, LCD_LINE3, ball_color, true);
Wait (500);

return ball_color;
}

void align_basket (string ball_color)


{
// RED, YELLOW, GREEN, BLUE
if (ball_color == "YELLOW") {
RotateMotor (OUT_C, 30, 90);
}else if (ball_color == "GREEN") {
RotateMotor (OUT_C, 30, 180);
}else if (ball_color == "BLUE") {
RotateMotor (OUT_C, -30, 90);
}
}

// Get the dispenser in a standard position


task reset_dispenser ()
{
while (true)
{
if (true == reset_dispenser_flag)
{
// Get the dispenser into a standard position using the touch sensor
OnRev (OUT_B, 80);
until (1 == SENSOR_1);
Off (OUT_B);
reset_dispenser_flag = false;
}
}
}

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;

// Bring the tilting mechanism's touch sensor on-line


SetSensorTouch (S1);
// Bring the sorting mechanism's touch sensor on-line
SetSensorTouch (S2);

// Bring the color sensor on-line


SetSensorColorFull (S3);

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;

// Wait until the reset tasks are both finished


until (false == reset_dispenser_flag && false == reset_basket_flag);

load_ball (); // Place a ball in the dispenser exit slot


ball_color = show_color ();

// Check for a ball in the dispenser exit slot


} while (++i < RETRIES && "NONE" == ball_color)

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

// Assume there are no balls remaining after this number of retries


#define RETRIES 2

// Reset the dispensing tray and the catapult when these are set to true
bool reset_dispenser_flag = false;
bool reset_catapult_flag = false;

// Get a ball into the dispenser slot


void load_ball ()
{
Wait (2000);
// Shake up the balls in the dispenser
RotateMotor (OUT_B, 90, 400);
RotateMotor (OUT_B, -90, 400);
Wait (500);
}

void dispense_ball ()
{
RotateMotor (OUT_B, -90, 360);
Wait (1000);
}

// Show the color of the ball on the NXT display


// Return the value of the color of the ball to the calling task
string 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;
}

// Show the ball color


TextOut (30, LCD_LINE3, ball_color, true);
Wait (500);

return ball_color;
}

void align_catapult (string ball_color)


{
// RED, YELLOW, GREEN, BLUE
if (ball_color == "RED") {
RotateMotor (OUT_C, 30, 45);
}else if (ball_color == "YELLOW") {
RotateMotor (OUT_C, 30, 90);
}else if (ball_color == "GREEN") {
RotateMotor (OUT_C, -30, 45);
}else if (ball_color == "BLUE") {
RotateMotor (OUT_C, -30, 90);
}
}

// Get the dispenser in a standard position


task reset_dispenser ()
{
while (true)
{
if (true == reset_dispenser_flag)
{
// Get the dispenser into a standard position using the touch sensor
OnRev (OUT_B, 80);
until (1 == SENSOR_1);
Off (OUT_B);
reset_dispenser_flag = false;
}
}
}

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);

RotateMotor (OUT_C, -30, 15); // A slight adjustment for perfect alignment


reset_catapult_flag = false;
}
}
}

task main ()
{
int i;
string ball_color;

// Bring the tilting mechanism's touch sensor on-line


SetSensorTouch (S1);
// Bring the sorting mechanism's touch sensor on-line
SetSensorTouch (S2);

// Bring the color sensor on-line


SetSensorColorFull (S3);

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;

// Wait until the reset tasks are both finished


until (false == reset_dispenser_flag && false == reset_catapult_flag);

load_ball (); // Place a ball in the dispenser exit slot


ball_color = show_color ();

// Check for a ball in the dispenser exit slot


} while (++i < RETRIES && "NONE" == ball_color)

if ("NONE" == ball_color)
{
// Assume no balls are remaining
TextOut (0, LCD_LINE3, "*** FINISHED ***", true);
Wait (4000);
break;
}

dispense_ball ();
align_catapult (ball_color);

// Fire the catapult


OnFwd (OUT_A, 100); Wait (500); Off (OUT_A);
OnFwd (OUT_A, -100); Wait (500); Off (OUT_A);

// 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 can walk?


Programming 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.

// Three directions for walking


#define INPLACE 0
#define FORWARDS 1
#define BACKWARDS 2

// Get the legs in a standard position


// Motor B & touch sensor 1 ---> right leg
// Motor C & touch sensor 2 ---> left leg
void reset_legs ()
{
if (1 == SENSOR_1)
{
OnFwd (OUT_B, 50); Wait (300);
until (0 == SENSOR_1);
Off (OUT_B);
}
OnFwd (OUT_B, 50);
until (1 == SENSOR_1);
Off (OUT_B);

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);

RotateMotor (OUT_B, 70, 45); // Put right leg down


RotateMotor (OUT_C, -70, 150); // Put left leg up

t0 = CurrentTick ();

switch (direction)
{
case INPLACE:

while (CurrentTick () - t0 < duration)


{
OnFwd (OUT_B, 70); OnFwd (OUT_C, -70);
}
break;

case FORWARDS:

while (CurrentTick () - t0 < duration)


{
OnFwdSync (OUT_BC, 50, 0);
}
break;

case BACKWARDS:

while (CurrentTick () - t0 < duration)


{
OnRevSync (OUT_BC, 50, 0);
}
break;
}

Off (OUT_BC);
}

task main ()
{
// Bring the touch sensors on-line
SetSensorTouch (S1); SetSensorTouch (S2);

walk (INPLACE, 5000); Wait (2000);


walk (BACKWARDS, 5000); Wait (2000);
walk (FORWARDS, 5000); Wait (2000);

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);

// turn left - open right hand


RotateMotor (OUT_A, -100, 2*60);

// 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

// Walk forward when objects come closer than this


#define THRESHOLD 50

// Three directions for walking


#define INPLACE 0
#define FORWARDS 1
#define BACKWARDS 2

// Get the legs in a standard position


// Motor B & touch sensor 1 ---> right leg
// Motor C & touch sensor 2 ---> left leg
void reset_legs ()
{
if (1 == SENSOR_1)
{
OnFwd (OUT_B, 50); Wait (300);
until (0 == SENSOR_1);
Off (OUT_B);
}
OnFwd (OUT_B, 50);
until (1 == SENSOR_1);
Off (OUT_B);

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;

RotateMotor (OUT_B, 70, 45); // Put right leg down


RotateMotor (OUT_C, -70, 150); // Put left leg up

t0 = CurrentTick ();

switch (direction)
{
case INPLACE:

OnFwd (OUT_B, 50); OnFwd (OUT_C, -50);


until (CurrentTick () - t0 > duration);
break;

case FORWARDS:

OnFwdSync (OUT_BC, 50, 0);


until (CurrentTick () - t0 > duration);
break;

case BACKWARDS:

OnRevSync (OUT_BC, 50, 0);


until (CurrentTick () - t0 > duration);
break;
}

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

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

// Ask for a ball when an object is closer than this


#define THRESHOLD 50

// Three directions for walking


#define INPLACE 0
#define FORWARDS 1
#define BACKWARDS 2

void drop_ball ()
{
RotateMotor (OUT_A, -100, 2*60); Wait (800);
// back to center
RotateMotor (OUT_A, 100, 2*60);
}

// Get the legs in a standard position


// Motor B & touch sensor 1 ---> right leg
// Motor C & touch sensor 2 ---> left leg
void reset_legs ()
{
if (1 == SENSOR_1)
{
OnFwd (OUT_B, 50); Wait (300);
until (0 == SENSOR_1);
Off (OUT_B);
}
OnFwd (OUT_B, 50);
until (1 == SENSOR_1);
Off (OUT_B);

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;

RotateMotor (OUT_B, 70, 45); // Put right leg down


RotateMotor (OUT_C, -70, 150); // Put left leg up

t0 = CurrentTick ();

switch (direction)
{
case INPLACE:

OnFwd (OUT_B, 50); OnFwd (OUT_C, -50);


until (CurrentTick () - t0 > duration);
break;

case FORWARDS:

OnFwdSync (OUT_BC, 50, 0);


until (CurrentTick () - t0 > duration);
break;

case BACKWARDS:

OnRevSync (OUT_BC, 50, 0);


until (CurrentTick () - t0 > duration);
break;
}

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 until a ball is placed in the right hand


until (INPUT_BLACKCOLOR != SENSOR_3);

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.

You might also like