Tuesday, March 29, 2011

Lab 6 - Harvester

Lab 6 Write-up

For lab six, we were required to find, detect and acknowledge flat, black circular objects within the playing field followed by searching for strong light sources after a sixty second time period. The goal of this was to find all ten black objects before our sixty seconds were up and we reached the light. The playing field was finite and bounded and the black circles were scattered, so we included the obstacle avoidance and wander functions written in our previous labs.
Our design was simple and easy to implement. It required two reflective sensors pointed directly at the ground under our robot for searching for the black circles to harvest. These two sensors simply looked for any change in reflected light from the surface. This change in light signified the presence of a harvestable object. The remaining sensors were kept the same from previous labs, as their utility had not changed.
In regards to the software aspect of our robot, we had the functions for obstacle avoidance and wander activated until the sixty second timer was up at which point they were turned off. The new functions for registering the reflected light did just that. They looped until some significant change in light level came about and then sent a signal to cause a beep, which acknowledged the harvestable object. At the time when the sixty seconds were up we allowed our robot to keep harvesting while searching for the light as it might get lucky and find a piece of food along the way.
This project brought about some issues with creating new processes/threads within a loop, regardless of the time between each process of thread was called. If it hadn’t been for one of the other groups already encountering this problem, it may have taken us quite a bit of time to discover the source of these problems. Once that issue was cleared up, the rest of the processes/threads seemed to communicate well together allowing a smooth running program.
We feel that our robot competed at a premium level for the competition. Since the wander function was random, it really came down to chance and whose robot had a smoother ride. Although we did not take the competition by a landslide, we are still pleased with the rankings and results.




//code
// Line Follower - Laster, Szelagoski, Niemann


//harvester code lab 6
//laster, szelagowski, niemann

// Global Variables
int MAX_LIGHT = 115;
int MIN_LIGHT = 80;

int rVal = 0;
int lVal = 0;
int bumpCtr = 0;
int flag = 1;
int wFlag = 1;
int lock = 0;

int Lmotor = 2;
int Rmotor = 0;
int Lreflect = 5;//5
int Rreflect = 2;
int Leye = 3;
int Reye = 4;
int Lbumper = 9;
int Rbumper = 10;

int line, lineThreshold;

int main()
{

// Loop For Start Waiting
while( !start_button());

printf( "Harvester\n" );

//start thread to turn off robot when off button is pressed
//start_process(stopper());

//start_process(seek());


//start thread to count for 60 seconds
start_process(count());

//start thread to avoid obstacles
start_process(avoid());

//start thread to wander around
start_process(wander());

//start thread to harvest left
start_process(eatLeft());

//start thread to harvest right
start_process(eatRight());
}


void wander()
{
float rand;
int randDir;

while(wFlag)
{
//check if robot is avoiding
if(!lock)
{
//go straight for 3 sec
motor(Lmotor, 70);
motor(Rmotor, 50);
sleep(3.0);

rand = (float)random(10) / 10.0;
randDir = random(2);

//check if robot is avoiding
if(!lock)
{
//turn right
if(randDir)
{
motor(Lmotor, 50);
motor(Rmotor, -50);
}
//turn left
else if(randDir == 0)
{
motor(Lmotor, -50);
motor(Rmotor, 50);
}
//else don't turn


//turn for random 0.5 < time < 1.5 sec sleep(rand + 0.5); } } } } void seek() { //turn till it senses light motor(Lmotor, -50); motor(Rmotor, 50); while(analog(Leye) > 95 || analog(Reye) > 95)
{
//printf("Leye= %d, Reye= %d\n", analog(Leye), analog(Reye) );
}
start_process(beeper());

off(Lmotor);
off(Rmotor);

motor(Lmotor, 100);
motor(Rmotor, 75);

while(!digital(Lbumper) && !digital(Rbumper));

off(Lmotor);
off(Rmotor);

}

void eatLeft()
{
//loop until time > 60 or off button pressed
while( 1 )
{
//get left reflective sensor value
lVal = analog(Lreflect);

//if the value is > 50 then it sensed a black dot
if( lVal > 50 )
{
//start_process(beeper());
beep();
lVal = analog(Lreflect);
//loop until it passes the dot
while( lVal > 50)
lVal = analog(Lreflect);
}

}


}

void eatRight()
{
//loop until time > 60 or off button pressed
while( 1 )
{
//get right reflective sensor value
rVal = analog(Rreflect);

//if the value is > 50 then it sensed a black dot
if( rVal > 50 )
{
//start_process(beeper());
beep();
rVal = analog(Rreflect);
//loop until it passes the dot
while( rVal > 50)
rVal = analog(Rreflect);
}

}


}

void avoid()
{
while( wFlag )
{
if (digital(Lbumper))//left
{
lock = 1;
avoidLeft();
motor(Lmotor, 70);
motor(Rmotor, 50);
}

if (digital(Rbumper))//right
{
lock = 1;
avoidRight();
motor(Lmotor, 70);
motor(Rmotor, 50);
}
lock = 0;

}
}



void avoidLeft()
{
bk(Lmotor);
bk(Rmotor);
sleep(0.25);
motor(Lmotor, 50);
motor(Rmotor, -50);
sleep(0.5);
}

void avoidRight()
{
bk(Lmotor);
bk(Rmotor);
sleep(0.25);
motor(Rmotor, 50);
motor(Lmotor, -50);
sleep(0.5);
}



void count()
{
//count till 60
sleep(60.);
//turn off wandering
wFlag = 0;
flag = 0;
lock = 1;

//turn off the motors
off(Lmotor);
off(Rmotor);

tone(500.0, 1.);
//start thread to start searching for light
start_process(seek());



}

No comments:

Post a Comment