Report 4-Bug Bot - Wayne State University

advertisement
Bug Bot
Group 3: Krystal Reinwand, Yannick Djoumbi, Derek Wedley
Bug Bot 1:____________________________________________________________________
//huntbot.nxc
task hunt() // hunt for objects
{
while(true) // will make following code repeat forever
{
OnFwd(OUT_AC,65); // both motors forward and 65%
Wait(1000+Random(2000)); // randomly move forward between 1 and 3 seconds
OnRev(OUT_C,50); // left motor reverse at 50%
Wait(Random(1000)); //reverse random amount between 0 and 999
}
}
task detect_bump() // enables touch center
{
while (true) // subsequent code repeat forever
{
if(SENSOR_1==1) // if touch sensor output one pressed
{
stop hunt; // stops task hunt so arguments do not get confused
OnRev(OUT_AC,60); // reverse both wheels at 60%
Wait(500); // wait half second
OnFwd(OUT_A,60); // output A forward to turn right
Wait(850); // wait 850 milliseconds
start hunt; // resume hunt
}
if(SENSOR_4==1) // if touch sensor output four pressed
{
stop hunt; // stop task hunt
OnRev(OUT_AC,60); // reverse both wheels
Wait(500); // wait half second
OnFwd(OUT_C,60); // output C forward to turn left
Wait(850); // wait 850 milliseconds
start hunt; // resume hunt
}
}
}
task main() // enables bot tasks
{
SetSensorTouch(IN_1); // touch sensor in input 1
SetSensorTouch(IN_4); // touch sensor in input 2
start detect_bump; // start detect bump task
start hunt; // start hunt task
PlayFileEx("starwars.rso", 4, TRUE); Wait(2000); // play star wars on loop
In the first bug bot iteration, the main change we made was adding an extra touch sensor
and creating a code for the correct input. We added an extra touch sensor in input 4, creating a
double touch sensor bot that reacts to two different touch sensors. When the left sensor is pressed,
the bot is to back up for 500 milliseconds, then output A is to turn on and turn the bot to the right.
The same goes for the right sensor, except that when it is activated it is to turn left.
Why is Bug Bot better than Bump bot?____________________________________________
Bug bot is better than bump bot because there are two sensors attached rather than just one.
The fact that it has two touch sensors allows it to maneuver between obstacles better. This allows
the bug bot to avoid going through narrow openings and possibly getting stuck. It also allows the
bug bot to react to its surrounding better.
Bug Bot 2_____________________________________________________________________
//huntbot.nxc
inline void BumpTurn_Right()
{
OnRev(OUT_AC,60); // reverse both wheels at 60%
Wait(500); // wait 500 milliseconds
OnFwd(OUT_A,60); // output A forward to turn right
Wait(850); // waith 850 mulliseconds
}
inline void BumpTurn_Left()
{
OnRev(OUT_AC,60); //reverse both wheels at 60%
Wait(500); // wait 500 milliseconds
OnFwd(OUT_C,60); // output C forward to turn left
Wait(850); // wait 850 milliseconds
}
task hunt() // hunt for objects
{
while(true) // will make following code repeat forever
{
OnFwd(OUT_AC,65); // both motors forward and 65%
Wait(1000+Random(2000)); // randomly move forward between 1 and 3 seconds
OnRev(OUT_C,50); // left motor reverse at 50%
Wait(Random(1000)); //reverse random amount between 0 and 999
}
}
task detect_bump() // enables touch center
{
while (true) // folling code repeat forever
{
if(SENSOR_1==1) // if touch sensor output one pressed
{
stop hunt; // stops task hunt so arguments do not get confused
BumpTurn_Right();
start hunt; // resume hunt
}
if(SENSOR_4==1) // if touch sensor output four pressed
{
stop hunt; // stop task hunt
BumpTurn_Left();
start hunt; // resume hunt
}
}
}
task main() // enables bot tasks
{
SetSensorTouch(IN_1); // touch sensor in input 1
SetSensorTouch(IN_4); // touch sensor in input 2
start detect_bump; // start detect bump task
start hunt; // start hunt task
PlayFileEx("starwars.rso", 4, TRUE); Wait(2000); // play star wars on loop
In Bug bot 2, we added inline functions to help better organize the code. We decided to
add inline functions for each sensor, helping create a better organization under each statement. As
with the first bug bot, when the right sensor is pressed, the bot turns left, and when the left sensor
is pressed, the bot turns right. Other than organization to the code, the bot performs the same
functions as before.
Explain why it is a good idea to have inline functions in the code_________________________
It is a good idea to add inline functions in a code because it helps with organization, making
the code easier to read. When the code is organized and easier to read, there is slightly less stress
when trying to fix a line of code that could be wrong.
Bug Bot 3_____________________________________________________________________
//huntbot.nxc
inline void MoveTurn_Right()
{
OnRev(OUT_AC,60); // reverse both wheels at 60%
Wait(500); // wait half second
OnFwd(OUT_A,60); // output A forward to turn right
Wait(850); // waith 850 mulliseconds
}
inline void MoveTurn_Left()
{
OnRev(OUT_AC,60); // reverse both wheels
Wait(500); // wait half second
OnFwd(OUT_C,60); // output C forward to turn left
Wait(850); // wait 850 milliseconds
}
inline void MoveHunt()
{
OnFwd(OUT_AC,65); // motors forward at 65%
Wait(1000+Random(2000)); // between 1 and 3 seconds
if (Random() >= 0) // random to turn right or left
{
OnRev(OUT_A,60); // turn right
}
else
{
OnRev(OUT_C,60); // turn left
}
Wait(Random(1000)); // between 0 and 1 seconds
}
task hunt() // hunt for objects
{
while(true) // will make following code repeat forever
{
MoveHunt(); // recall inline function MoveHunt
}
}
task detect_bump() // enables touch center
{
while (true) // following code repeat forever
{
if(SENSOR_1==1) // if touch sensor output one pressed
{
stop hunt; // stops task hunt so arguments do not get confused
MoveTurn_Right(); // recall inline function MoveTurn_Right
start hunt; // resume hunt
}
if(SENSOR_4==1) // if touch sensor output four pressed
{
stop hunt; // stop task hunt
MoveTurn_Left(); // recall inline function MoveTurn_Left
start hunt; // resume hunt
}
}
}
task main() // enables bot tasks
{
SetSensorTouch(IN_1); // touch sensor in input 1
SetSensorTouch(IN_4); // touch sensor in input 2
start detect_bump; // start detect bump task
start hunt; // start hunt task
PlayFileEx("starwars.rso", 4, TRUE); Wait(2000); // play star wars on loop
In the Random Walk bug bot code, there was a random if/else statement inserted into the
move hunt task. While this was active, the bug bot would randomly turn left or right, depending
on when the random integer was positive or negative. This random movement helps the bug bot
hunt more efficiently because it is now able to hunt left and hunt right, rather than just one or the
other.
How well does the random walk work?_____________________________________________
The random walk works well. When there are no obstacles in the bug bot’s way, it now
turns left or right depending on what random integer it falls on. It continues to turn left or right
until it finds an obstacle. Then it turn left or right, depending on what touch sensor hits the obstacle.
Does changing the parameters of the Random() function for the distance moved and the
angle turned affect the field the bot can sweep?______________________________________
Changing the parameters of the Random() function for greater would increase the distance
and turn angle, allowing for a greater area the bot can sweep. However, when the Random()
function is decreased, the area swept by the bot will be less.
Bug Bot 4_____________________________________________________________________
//huntbot.nxc
int bumps = 0;
inline void MoveHunt()
{
OnFwd(OUT_AC,65); // both motors forward and 65%
Wait(1000+Random(2000)); // randomly move forward between 1 and 3 seconds
if (Random(2) == 1) // randomly search
OnRev(OUT_A,60); // reverse out A
else
{
OnRev(OUT_C,60); // or reverse out C
}
Wait(Random(1000)); //reverse random amount between 0 and 999
}
inline void MOVE_HIT_LEFT()
{
OnRev(OUT_AC,60); // reverse both wheels at 60%
Wait(500); // wait half second
OnFwd(OUT_A,60); // output A forward to turn left
Wait(350); // waith 850 mulliseconds
}
inline void MOVE_HIT_RIGHT()
{
OnRev(OUT_AC,60); // reverse both wheels
Wait(500); // wait half second
OnFwd(OUT_C,60); // output C forward to turn right
Wait(350); // wait 350 milliseconds
}
task watchT()
{
PlayToneEx(440, 500,4,FALSE); // play tone on start
Wait(500); // wait 500 milliseconds
PlayToneEx(220, 500,4,FALSE); // play same tone again
Wait(500); // wait 500 milliseconds
long t3; // variable
while(true) // repeat forever
{
t3 = CurrentTick(); // current tick
until((bumps == 4) ||
(CurrentTick()-t3 > 10000)); // current tick - t3 greater than 1000
PlayToneEx(220, 2000, 4, FALSE); play tone on bumps greater than 4
Wait(1000); // wait one second
bumps=0; // reset bumps to 0
}
}
task hunt() // hunt for objects, runs parallel with task WatchT()
{
while(true) // will make following code repeat forever
{
MoveHunt(); // recall inline void MoveHunt
}
}
task detect_bump() // enables touch center
{
while (true) // folling code repeat forever
{
if(SENSOR_1==1) // if touch sensor output one pressed
{
stop hunt; // stops task hunt so arguments do not get confused
bumps=bumps+1; // increment bumps
if (bumps == 4) // if bumps equals 4
{
OnRev(OUT_A,75); // reverse out A to turn
Wait(500); // pull 360
}
else
{
MOVE_HIT_LEFT(); // do this until bumps equals 4
}
start hunt; // resume hunt
}
if(SENSOR_4==1) // if touch sensor output four pressed
{
stop hunt; // stop task hunt
bumps=bumps+1; // increment bumps
if (bumps == 4) // if bumps equals 4
{
OnRev(OUT_C,75); // reverse out C
Wait (500); // pull 360
}
else
{
MOVE_HIT_RIGHT(); // recall inline void turn left
}
start hunt; // resume hunt
}
}
}
task main() // enables bot tasks
{
SetSensorTouch(IN_1); // touch sensor in input 1
SetSensorTouch(IN_4); // touch sensor in input 2
start watchT; // start task watch
start detect_bump; // start detect bump task
start hunt; // start hunt task
//PlayFileEx("starwars.rso", 4, TRUE); Wait(2000); // play star wars on loop
In this final iteration of the bug bot, we added a new task name task watchT(). The new
task is meant to run parallel with detect bump. In task watchT(), we added a tick counter to help
the bot get out of a corner if needed. In the code, we stated that if there are 4 hits, both sensors
combined, in 10 seconds or less, the bot should turn around to get out of the corner. Also, to help
us notice when the 4 hits occurred, we added a tone. The tone was there for our purposed, helping
us notice when the bot had hit 4 times in 10 seconds and starting its 360 degree turn. Finally, in
task detcect_bump(), we added increments to the variable bump, helping the bot keep track of
when the 4 bumps occurred and if they occurred in 10 seconds or less.
How did it work?_______________________________________________________________
The bug bot worked completely as planned. We got the bot into a corner and turned it on.
The bot proceeded into a corner and hit the walls, 4 times in less than 10 seconds. When hit the
fourth time, the bot did a 360 degree turn and evaded the corner, all as planned.
Describe the way the parallel tasks work. Explain the difference between parallel and
sequential execution. ____________________________________________________________
Parallel tasks work in conjunction. The tasks will not behave correctly without one another.
An example of this would be in the final bug bot iteration when we added the current tick task.
That was meant to work parallel with the detect bump task. Without one or the other, the tasks
would not work correctly. One of the codes may be read, but the bot would not be able to maneuver
out of a corner. The difference between a parallel task and a sequential execution is that parallel
tasks need one another in order to function, while a sequential task can be read on its own. An
example of a sequential task is the move hunt task. That is meant to work on its own. This can be
seen in the detect bump task. In the detect bump task, we stop the move hunt task. We do this
because they are meant to be sequential. They can work without one another. However, we see a
the end of the detect bump, that the move hunt task is restarted.
Download