You are on page 1of 5

Homework #3 Team 7

Jeff Pierzynowski, Terrence Reese, Ryan Dodge

Activity 3 a
//Method a) Bump correction inserted into the main Line-Following Code
#define THRESHOLD 60
#define BUMP IN_1
task main()
{
SetSensorLight(IN_3);
SetSensorTouch(BUMP);
OnFwd(OUT_AC, 40);
while (true)
{
if (Sensor(IN_3) > THRESHOLD)
{
OnRev(OUT_C, 40);
OnFwd(OUT_A, 40);
Wait(100);
until(Sensor(IN_3) <= THRESHOLD);
OnFwd(OUT_AC, 40);
}
//Bump Correction
if(SENSOR_1==1)
{
OnRev(OUT_AC, 40);
Wait(500);
OnFwd(OUT_A, 40);
Wait(200);
}
}
}

Disadvantages of this approach are that the functions run together and are not as clearly
separated. As the amount of functions increase in a program the ability to follow what is
happening could get quite difficult.
The Advantages at this point are that for a simple program like this you dont have to worry
about how tasks are getting stopped and started.

Activity 3 b i

//Method b i) using start/stop task commands


#define THRESHOLD 60
#define BUMP IN_1
task linefollow()
{
OnFwd(OUT_AC, 40);
while(true)
{
if (Sensor(IN_3) > THRESHOLD)
{
OnRev(OUT_C, 40);
Wait(100);
until(Sensor(IN_3) <= THRESHOLD);
OnFwd(OUT_AC, 40);
}
}
}
task turnaround()
{
while(true)
{
if(SENSOR_1==1)
{
stop linefollow;
OnRev(OUT_AC, 40);
Wait(500);
OnFwd(OUT_A, 40);
Wait(500);
start linefollow;
}
}
}
task main()
{
SetSensorLight(IN_3);
SetSensorTouch(BUMP);
start linefollow;
start turnaround;
}

Disadvantages with this method are that you must make sure you start and stop tasks correctly
so that the motors are not receiving conflicting commands.

Advantages are that the main task becomes very simple. The separate functions that the robot
does are clearly separated in the program and can be called upon whenever necessary in the
program.

Activity 3 b ii
//Method 3 b ii Using Mutex to coordinate the motor commands
#define THRESHOLD 60
#define BUMP IN_1
mutex moveMutex;
task linefollow()
{
while (true)
{
Acquire(moveMutex);
OnFwd(OUT_AC, 40);
if (Sensor(IN_3) > THRESHOLD)
{
OnRev(OUT_C, 40);
Wait(100);
until(Sensor(IN_3) <= THRESHOLD);
OnFwd(OUT_AC, 40);
}
Release(moveMutex);
}
}
task turnaround()
{
while (true)
{
if(SENSOR_1==1)
{
Acquire(moveMutex);
OnRev(OUT_AC, 40);
Wait(500);
OnFwd(OUT_A, 40);
Wait(500);
Release(moveMutex);
}
}
}
task main()

{
SetSensorLight(IN_3);
SetSensorTouch(BUMP);
start linefollow;
start turnaround;
}

Disadvantages of this method would be similar to the previous method, being that you need to be
careful with the coordination of the mutex controls. Both motor controls cannot have the mutex
at the same time.
Advantages would be that with the mutex command when done correctly, you are basically
assuring in your program which lines of code are controlling the motor at any one time. You are
able to specify exactly when to stop the control from a specific section and allow another portion
of the program to take control of the motors.

Activity 3 c
//Method c) separate subroutine
#define THRESHOLD 60
#define BUMP IN_1
sub turnaround()
{
OnRev(OUT_AC, 40);
Wait(500);
OnFwd(OUT_A, 40);
Wait(500);
}
task linefollow()
{
OnFwd(OUT_AC, 40);
while(true)
{
if (Sensor(IN_3) > THRESHOLD)
{
OnRev(OUT_C, 40);
Wait(100);
until(Sensor(IN_3) <= THRESHOLD);
OnFwd(OUT_AC, 40);
}
if(SENSOR_1==1)
{

turnaround();
}
}
}
task main()
{
SetSensorLight(IN_3);
SetSensorTouch(BUMP);
start linefollow;
}

Disadvantages would be that you cant have more than 256 subroutines and tasks in your
program. This may not be an issue in a simple program but as programs become more involved
it may become a problem.
Advantages would be that you can call the subroutine anywhere you need it in the program. You
can also pass values into the subroutine if needed as well. Subroutines also save memory being
that every time inline functions are used, they are copied into the program thus using up more
memory.

You might also like