Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Team 6

BE 1200 Midterm
Dr. Shreve

NXT Lego Mindstorms Midterm: 10 Step Design Process


1. Clarify Objectives:
The objective of this assignment is to construct the NXT with a line
following code in order for it to move throughout the track. While on the
track, the NXT must be able to detect and maneuver around an object at
least one vehicle length in distance. Once the object has been avoided, the
NXT must re-find the track and proceed with its line following command until
the NXT surfaces the green square. While detecting the green square, the
NXT must stop on the green square and play a musical note for more than
five seconds.
Theres also 2 other objectives that we could achieve for extra credit.
One objective is to have the NXT maneuver around the obstacle twice and
the second objective is to have the NXT execute the track a second time.
2. User requirements:
The user must not only turn on the NXT using the orange button, but
must place it on the green square until two sounds are made (This
commands the light sensor to read values for the variable for the Green
Square). Afterward, the user must put the bot on a completely white part of
the board to also acquire a value. Once a sound is heard, the bot must be
placed on the black line to acquire the value in order for the NXT to begin
following the line.
3. Identify Constraints:
Constraints with this project is the amount of materials provided to us
in the LEGO Mindstorms Kit that we received. Because of the limit materials,
we were limited to what we could build and construct, in other words, we had
to be more creative. The amount of coding that the NXT could manage to
hold within its software is another constraint, so we checked the NXT of
previous programs and any other files that may be unnecessary for our
team.
4. Establish Functions:
First, the NXT will store the threshold values for the green square,
white board, and black line. It will follow the black line using a shuffling
motion. Once the NXT hits the red can with its bumper, it will back up a bit,
turn slightly to the left and begin to use the ultrasonic sensor to follow the
red can at a safe distance. As it goes over the black lines while following the
can, an integer value will continue to increase until it reaches a specified
value. At this point, the NXT will continue to follow the black line again. After
it goes on the green square, the bot will stop.

Team 6
BE 1200 Midterm
Dr. Shreve

5. Establish Design Specifications:


In terms of design specifications, for this specific assignment, the
position of the sensors was quite important. Firstly, though we started with a
vertically positioned ultrasonic and were advised to do this, we ended up
positioning the ultrasonic sensor horizontally as this worked better for our
specific design and location of the sensor. The bumper was placed in the
front as was the light sensor. The bumper was placed significantly in front of
the light sensor so that it would fully be able to protect the bot.
6. Generate Alternatives:
Initially the design included an ultrasonic sensor that was placed much
higher on the bot. However, as the can was not this tall, we had to change
the mechanical design of the bot so that the ultrasonic sensor would be fully
utilized. Also, with the ultrasonic sensor on top of the NXT, having the NXT
avoid the obstacle would be difficult. Instead we installed the ultrasonic
sensor on the right side of the NXT, which allowed a more efficient way of
maneuvering around the obstacle. Additionally, we had the option to use two
light sensors in the place of one, but since our bot was following the black
line fine without the second light sensor, we decided not to add the second
light sensor to keep the mechanical design and program code simple.
7. Model and Analyze Design:
In order to keep the design as simple as possible, we decided to only
use the sensors that would be absolutely necessary: one light sensor, one
ultrasonic sensor, and one touch sensor. The touch sensor was placed in
front of the light sensor. Both of these are placed directly in front of the bot.
This is ideal as it can follow the line because the wheels are oriented in the
same direction. The touch sensor serves as a bumper to protect the rest of
the bot. Lastly, the ultrasonic sensor was placed on the bots right side to
allow it to move adjacent to the can. It would be impossible to get it to move
around the can were the ultrasonic sensor on the front of the bot!
8. Test and Evaluate Design:
Refer to Testing Table: Testing Results and Refinements
9. Refine and Optimize Design:
Refer to Testing Table: Retesting of Final Results
10.
Design Communication/Document Design:
Refer to Team Website for Videos
Photos of 1st Construct:

Team 6
BE 1200 Midterm
Dr. Shreve

Team 6
BE 1200 Midterm
Dr. Shreve

Photos of 2nd Construct:

Team 6
BE 1200 Midterm
Dr. Shreve

Implementation Plan and Debugging Procedure


Implementation Plan:
For the midterm, our objective was to program the NXT to follow a black line around the loop
and proceeding clockwise to the end point designated by the Green Square. The robot must
avoid the can which is on the black line and must be able to re-find the line afterward. For
extra credit, our team decided to aim for the NXT to go around the can a total of two times.
After the bot stops on the green square, we must also get it to play a musical sound for more
than five seconds.
1. As our Homework 4 assignment was to line follow and stop on the green square, we
were able to reuse parts of our program from Homework 4. First we needed to create
task main and set up the sensors we are using for this assignment. We set up the
Touch Sensor, the Light Sensor, and the Ultrasonic Sensor. Within task main, we also
set up a mutex, Precedes, with the tasks we want the NXT to continuously execute.
Well get back to that later on after we create more tasks.
2. Now we want to the NXT to follow the track, which consists of black tape. In order to
do that, we use variables. Since there are more than one color that the NXT will
basically recognize throughout the course, we must declare 3 different types of
variables we want the Light Sensor value. So we type int (color) above the task
main. Now there will be 3 integers that are declared and those integers are Green,
Black, and White. We still have to create the setup for the Light Sensor to determine
what those values are for the integers, so we create a inline function called setup

Team 6
BE 1200 Midterm
Dr. Shreve

3.

4.

5.
6.

7.
8.

9.

and within this function, we want to say that when the Light Sensor is placed on the
green, play a sound for 3 seconds and until the next sound is played, place it on the
next color, and then the last color. Also, we want to define the Light Sensor in input 3
on the NXT as EYE to make coding easier throughout this program.
With the Line Following task, we want the NXT to move forward on the Black Line, but
while true, if the NXT falls off track, and notices that its falling off the track (Or the
value of the Light Sensor is less than the value of the Black Line), to turn right and
turn left when the value of the Light Sensor is greater than the White value. If
everything seems to be correct with the Light Sensor values, the NXT will move
forward on the track.
Besides the NXT following the track, we want the NXT to stop on the Green Square
when the Light Sensor detects it. However, we also want the NXT to go past the
Green Square the first time around the track and stop on it the second time around
the track. In order to do that, we want the NXT to count the Green Square twice for
each time it detects it. To do this, we first compose an integer, call it green count,
and place it before the while true statement were about to have. Within the while
true statement, if the Light Sensor detects a value of between the green value minus
1 (Minimum Green Value) and the green value plus 1 (Maximum Green Value), wait
10/1000 of a second, then if the Light Sensor detects that same value in between the
minimum and maximum value of our specified values, play a sound, turn off both
motors, and begin counting increments of 1 and play a tone for each time the Light
Sensor detects green. If the green count becomes greater than or equal to 2, the NXT
will play 5 different tones for 5 seconds at 1 second for each tone, turn off both
motors, and finally stop all tasks from executing. Add the line following task in the
Precedes command within task main.
Test the program, check for errors, and record your results.
For the obstacle, we want to use the Touch Sensor, so we create a task for the Touch
Sensor that says while true, if the Touch Sensor is pressed, to stop the line follow
task, turn off both motors, play a sound to indicate the obstacle is pressed, put the
motors in reverse for a certain amount of distance, have motor A move forward and
have motor C move backwards until the Light Sensor recognizes the Black Line. This
allows the NXT to turn itself with the Ultrasonic Sensor facing the obstacle. Then once
it does recognize the Black Line, for the NXT to start the tasks for the Ultrasonic
Sensor and for the Light Sensor to count how many times it goes over the Black Line.
Add this task for the Touch Sensor to the Precedes command in task main.
Test the program, check for errors with the Touch Sensor and the reaction the NXT
does with the motor commands that were designed.
The Ultrasonic Sensor should be designed to circle the can whether its getting closer
or farther away from the can. Since we want 1 vehicle length away from the obstacle,
we define the closest distance to the obstacle as NEAR and define its value at 8cm
and for the farthest distance as FAR and its value at 18cm. This establishes a range
for the NXT to stay within when circling the obstacle. Create a task for the NXT to
circle the obstacle. So, white true, if the Ultrasonic Sensor is between the range we
defined, to have both motors move forward, but with motor C moving slightly faster,
getting closer to the obstacle. Or else, if the Ultrasonic Sensor detects that its value
is below the NEAR value, for the motor outputs to switch. In other words, both motors
will move forward, but motor A will move slightly faster instead of motor C, causing
the NXT to move away from the obstacle.
Next we construct a task for the Light Sensor to count the number of times it crosses
over the Black Line because we want the NXT to circle the obstacle twice. We
construct a code that sets an integer, well call it count, equal to 0. Then while true, if
the Light Sensors value is below the Black Lines value, begin counting increments of
1 and play a sound for each time the Light Sensor recognizes the Black Line. Then if

Team 6
BE 1200 Midterm
Dr. Shreve

7
the count becomes greater than or equal to 12, stop the ultrasonic task and exit this
line counting task to the line following task. This will allow the NXT to begin re-finding
the Black Line after it has maneuvered around the obstacle and continue with the line
following task.
10. Test the program with the updated tasks, check for any errors within the newly
written tasks, and record your results.
11. After all of these steps have been completed, youre NXT should be able to perform a
dynamic line following system, be able to detect an obstacle, maneuver around the
obstacle a certain amount of times you prefer, re-find the track and perform the line
following command, and finally either stop on the Green Square or have the NXT
make a second lap around the track and then stop on the Green Square then. If all
errors have been corrected and all results recorded, the assignment is now
completed!

Debugging Procedures:
1. The first issue that we ran into was the Ultrasonic Sensor and its task of circling the
obstacle. We added the Ultrasonic Sensor to the NXT on a vertical standpoint. That
seemed to work, but the probability of it actually working properly was very low. The
NXT would run into the obstacle and would get fastened to the obstacle itself while
the motors were running. So we reconstructed it to be placed horizontally and
because of this change, the NXT began to maneuver much better around the
obstacle without causing any unwanted collisions.
2. Second problem we faced was that the distance of what is NEAR and FAR played a
minor role in how well the NXT would maneuver around the can. If the initial distance
of the Ultrasonic Sensor was too far then the NXT would begin coasting off track and
wouldnt be able to recognize the obstacle itself. If the initial distance was too close,
the NXT would again, cause some soft of collision with the obstacle. We changed the
values a few times and found out changing them to 8cm (NEAR) and 18cm (FAR)
were suitable values for our program.
3. Another complication we dealt with was during the second lap around the track, the
task for the Touch Sensor was not being executed. When the Touch Sensor is pressed
a second time, the NXT would move forward, reverse, move forward, reverse, and
move forward again as if it were looking for a line to follow. Thus, meaning the task
for counting the number of times crossing over the Black Line was not being stopped
nor was the line following task. The answer to this fix was within the code for the line
counting itself. We had a stop command to stop the line counting itself, but in the
NXC tutorial, a task can not be stopped within itself. So we removed that stop
command, added an ExitTo(name of task) and the NXT performed the Touch Sensor
task properly. Causing the NXT to make the second lap without any errors.
4. Our final dilemma consisted of stopping on the Green Square due to the fact that the
integer count on the Green Square was random by the Light Sensors detection. Each
time we tested the program, the Light Sensor would give us different readings of how
many times it counted the Green Square, whether it was 2 or 4 times. We changed
the specified count value to 3 so that if the Green Square was counted for twice or
once at the end of the first lap, the third count would be a constant given to the Light
Sensor and the NXT would stop on the Green Square at the end of the second lap.

Team 6
BE 1200 Midterm
Dr. Shreve

Midterm NXC Program Code


#define EYE SENSOR_3
//defines Light Sensor
#define NEAR 8
//defines the distance for Ultrasonic Sensor
#define FAR 18
//defines the distance for Ultrasonic Sensor
int Green;
int Black;
int White;
void setup()
{
Green = EYE;
PlaySound(SOUND_UP);
Wait(3000);
White = EYE;
PlaySound(SOUND_UP);
Wait(3000);
Black = EYE;
PlaySound(SOUND_UP);
Wait(3000);

//variable for Green Square


//variable for Black Line
//variable for White Board
//This function gives definition to variables

int delta = (White - Black) / 3;


Black += delta;
White -= delta;
}
task line_follow()
//Task to follow the Black Line and Stop on the Green Square
{
OnFwd(OUT_AC, 50);
int greencount = 0;
while(true)
{
if(EYE <= Black)
//Turn RIGHT if NXT goes off track
{
Off(OUT_A);
OnFwd(OUT_C, 60);
}
else if (EYE >= White)
//Turn LEFT if NXT goes off track
{
Off(OUT_C);
OnFwd(OUT_A, 60);
}
else
{
OnFwd(OUT_AC, 60);
//Move Forward if on track
}
if((EYE >= Green - 1) && (EYE <= Green + 1))
//1st check of the Green Square
{
Wait(20);
if((EYE >= Green - 1) && (EYE <= Green + 1))
//2nd check of the Green Square, then STOP on
the Green Square
{
PlaySound(SOUND_DOWN);
Off(OUT_AC);

Team 6
BE 1200 Midterm
Dr. Shreve

9
greencount ++ ;
PlayTone(1000, 30);
Wait(300);
}
if(greencount >= 2)
{
PlayTone(262,400);
PlayTone(294,400);
PlayTone(330,400);
PlayTone(294,400);
PlayTone(220,400);
Off(OUT_AC);
StopAllTasks();
}
}
}
}

Wait(1000);
Wait(1000);
Wait(1000);
Wait(1000);
Wait(1000);

task ultra_sound()
//Task to CIRCLE the can
{
while(true)
{
if((SensorUS(IN_4) >= NEAR) && (SensorUS(IN_4) < FAR))
//If NXT is in between the range, Turn
slightly RIGHT
{
OnFwd(OUT_A, 20);
OnFwd(OUT_C, 32);
}
else if (SensorUS(IN_4) <= NEAR)
//If NXT is too close to can, Turn slightly LEFT or away from the
can
{
OnFwd(OUT_A, 32);
OnFwd(OUT_C, 20);
}
}
}
task black_count()
//Task to re-find the Black Line and follow the Black Line
{
int count = 0;
//Set count number to 0; This will determine the number of times the NXT
goes over the Black Line
while(true)
{
if(SENSOR_3 < Black)
//If NXT goes over the Black Line, Add the number of times it
goes over each time
{
count ++ ;
PlaySound(SOUND_UP);
Wait(400);
}
if (count >= 12)
//When the count is greater than the significant number of counts specified,
STOP other tasks, START line follow task
{
StopTask(ultra_sound);
ExitTo(line_follow);
//Exits to line following task when the Black Line count is greater than 8
}
}
}

Team 6
BE 1200 Midterm
Dr. Shreve

10
task turn_around()
//Task for when the NXT hits an object, Reverse and turn LEFT
{
SetSensorTouch(IN_1);
while(true)
{
if (SENSOR_1 == 1)
//When NXT hits obstacle, Turn slightly LEFT, then Start tasks ultra
sound and refind line
{
StopTask(line_follow);
Off(OUT_AC);
PlaySound(SOUND_DOUBLE_BEEP);
OnRev(OUT_AC, 50);
Wait(350);
OnFwd(OUT_A, 25);
Wait(100);
OnRev(OUT_C, 25);
until(EYE <= Black);
StartTask(ultra_sound);
StartTask(black_count);
}
}
}
task main()
//Main Task that sets up all Sensors, variable set up, and Starts line follow and
turn around tasks
{
SetSensorLight(IN_3);
SetSensorTouch(IN_1);
SetSensorLowspeed(IN_4);
setup();
Precedes(line_follow, turn_around);
}

Programming Table:

Preprocessor Directives
EYE

Used to call the light sensor in port 3 conveniently

NEAR

Used to call the closer-end of the distance for the ultrasonic


sensor in port 4 conveniently

FAR

Used to call the further-end of the distance for the ultrasonic


sensor in port 4 conveniently

Global Variables

Team 6
BE 1200 Midterm
Dr. Shreve

11
Green

Data value indicating the percentage of light reflected by the


Green Square.

Black

Data value indicating the percentage of light reflected by the


Black line.

White
Data value indicating the percentage of light reflected by the
White posterboard.

greencount
Variable used as a counter to sense the amount of times the bot
has passed over the green square

count
Variable used as a counter to sense the amount of times the bot
has passed over the black line while circling the can.

Tasks
Line_follow()

Task to follow the edge of the black line using the variables
BLACK and WHITE as defined in setup() function. The bot uses
both variables to correct its course if off track, otherwise,
continues in a straight line on the edge of the black line. The bot
will continue to move forward unless the if conditions are met.
If the bot goes too far left, onto the line, it will correct its course
by turning right. If the bot goes too far right, onto the
posterboard, it will correct its course by turning left. Used in the
beginning of the program after void setup() until the
turn_around() task. Continues after the black_count() task.

Ultra_sound()

black_count()

Task to turn around the edge of the can.This task uses the
preprocessor directives NEAR and FAR to circle the can, by
continuously turning slightly right if the can is in the NEAR and
FAR range. However if the can is too close, it will turn slightly
left, guiding itself away from the car. This task is stopped by the
task black_count() once the bot has circled the can twice. Task
black_count() will then exit to task line_follow().
Task to count the number of times the bot crosses over the
black line. It uses the count variable, with an initial value of 0 to
count the line, and adds each time the bot crosses the black
line. When the count is greater than the number specified, it
stops all tasks. It will then start the line_follow() task again to refind the line.

Team 6
BE 1200 Midterm
Dr. Shreve

12
turn_around()
Task to back up and turn left once the bot hits the number. This
task works with the touch sensor in port 1. When the bot hits an
obstacle, it will back up and slightly turn left. At this time it will
also start tasks ultra_sound(), and black_count(). Once task
black_count() has reached a specified number, it will exit to task
line_follow().

Functions
void_setup()

Before the main task is started, this setup gives definition to the
variables GREEN, BLACK, and WHITE. The bot is first placed
on the Green Square, where it saves the reflected light as
GREEN and plays a sound to confirm. Within three seconds,
the bot must be placed on the White posterboard, where it
saves the light reflected as WHITE, and plays a sound to
confirm. Within three seconds, the bot must be placed on the
Black line where it says the light reflected as BLACK, and plays
a sound to confirm. Within the next three seconds, the bot
executes the main task.

Testing Table:

Performance
Parameters

Testing Results

Refinements

Retest/Explanations

Line following (accurate


line following, following
sharp turns), object
sensing, programming
options (timer, variables,
advanced motors)

Number of Trials and


Results

Adjustments

Final Results

Team 6
BE 1200 Midterm
Dr. Shreve

13
Touch Sensor Detecting Can (touching
can then reversing and
trying to detect the can
with the ultrasound
sensor)

1. Bad, too far back.


2. Bad, still too far back.
3. Good, enough distance.
4. Bad, too close.
5. Good.
6. Good.
7. Good better than
before.
8. Perfect.

1.
2.
3.
4.
5.
6.
7.
8.

Distance
Distance
Distance
Distance
Distance
Distance
Distance
Distance

was
was
was
was
was
was
was
was

1000.
700.
300.
100.
180.
250.
320.
350.

Overall: 100%

Overall: 62.5%
Extra Credit:
Overall: 33.33%
1. Bad, NXT doesnt turn
around after hitting the
can the second time.
2. Bad, NXT doesnt turn
around after hitting the
can the second time.
3. Good, NXT maneuvered
around the can during
second lap.

Ultrasonic Sensor Maneuvering around Can


(Sensing the can and
being able to go around
the can without actually
touching it)

1. Bad, did not work.


2. Bad, did not work.
3. Bad, did not work.
4. Bad, did not work.
5. Bad, too close to can.
6. Good, still too close to
can.
7. Good, too far from can.
8. Good, too far from can.
9. Good, too close to can.
10. Good, perfect.
11. Good, but hit can,
then followed (new can).
12. Good, but hit can,
then followed.

Extra Credit:
1. Added task
turnaround2 to Precedes
command.
2. Separated task
greensquare to its own
and set it from precedes.
3. Replaced
StopTask(black_hunt);
and
StartTask(line_follow);
with
ExitTo(line_follow);.

Extra Credit:
Overall: 100%
1. NXT shut down after
hitting the can (did
this several times on
3/23--too many tasks
trying to control the
bot).
2. NXT did not can
follow and ultrasound
task did not work. NXT
just started turning
without aim.
3. NXT conducted
second turn around
task properly.

1. Only used NEAR value.


2. Only used NEAR value.
3. Only used NEAR value.
4. Only used NEAR value.
5. Range was too wide.
6. NEAR value too wide.
7. FAR value too wide.
8. FAR value too wide.
9. NEAR value too wide.
10. NEAR = 8; FAR = 15.
11. Increasing reverse
time so the circle would
be a bit larger.

Overall: 100%

Extra Credit:
1. Added ultrasound2
task.
2. Put task refind_line
(name later changed to
black_count) in task
turn_around.

Extra Credit:
Overall: 90%
1. Circles the can
infinitely.
2. Circles the can
infinitely.
3. Only circled can

10/10 Circle around


the can is a bit larger.

Overall: 59%
Extra Credit:
Overall: 20%
1. Bad, Stops after hitting
can second time.
2. Bad, Keeps circling the
can without stopping.

Team 6
BE 1200 Midterm
Dr. Shreve

14
3. Bad, Circles can
infinitely.
4. Bad, Does not can
follow during the second
lap.
5. Good.

3. Added variable bump


in task turn_around to
get that task to keep
looping while the number
of times that the bot hits
something is less than 2.
4. Added
ExitTo(line_follow) from
task black_count.
5. Increased the count
value in the condition
statement to 12.

once and then


stopped on green.
4. NXT was able to can
follow during the
second lap and it refound the line
afterward. Only
problem was that it
did not stop on the
green square.

Light Sensor - Line


Following (Follows the
black line - Light sensor
is edge-line reading)

1. Good.

No adjustments needed.

Overall: 100%

Re-finding Black Line


(After NXT circles can,
NXT can re-find black
line)

1. Bad, did not work.


2. Bad, did not work.
3. Bad, did not work.
4. Good, noticed the line
but did not follow it.
5. Good, noticed the line
but went in opposite
direction.
6. Good, noticed the line
but still went in opposite
direction.
7. Good, noticed the line
and followed.
8. Good, perfect run.

1. Incorrect program.
2. Incorrect program.
3. Incorrect program.
4. Changed START/STOP
tasks.
5. Changed START/STOP
tasks.
6. Changed START/STOP
tasks.
7. Added count
variable so every time
light sensor noticed the
black line, NXT added 1
count.
8. No adjustments.

Overall: 100%

Overall: 100%

Overall: 62.5%
Extra Credit:
Overall: 25%
1. Bad, Cracks in line
detected as black line.
2. Bad, NXT cannot re-find
line after hitting the can.
3. Bad, Stops on black line
right after being on green
(counts the green square
twice).
4. Good.

Extra Credit:
1. Increased count
requirements to get it to
follow the line again.
2. Changed green
thresholds threshold.
3. Count value in if
statement changed.
4. Changed reversing
time to get the turn to
be. Smoother but it got
caught on the handle.

Extra Credit:
Overall: 100%

Team 6
BE 1200 Midterm
Dr. Shreve

15
Green Square - NXT will
stop on the Green
Square

1. Bad, NXT went around


the Green square.
2. Bad, NXT went around
the Green square.
3. Good, NXT stopped on
the Green square.
4. Good, NXT stopped on
Green square.
5. Good, perfect run.
Overall: 60%
Extra Credit:
Overall: 50%
1. Bad, NXT will not stop
on green after finishing
the second lap.
2. Good, the NXT stopped
after the first lap.

1. Made finding green


square a task on its own.
2. Made finding green
square a task on its own.
3. Made green square
task in line follow task.
4. Made green square
task in line follow task
and had light sensor
double check the green
square.
5. No adjustments.
Extra Credit:
1. Decreasing the
greencount to 2 from 4.
2. Increased the
greencount to 3.

Overall: 100%

Extra Credit:
Overall: 50%
1. Stopped after first
lap.
2. Did not even count
the green square a
single time during
both laps.

You might also like