Write up
For our final lab, we were required to find, detect and acknowledge golf balls within the playing field followed by searching for a black circular disk in the middle of the arena. The main goal was to collect as many golf balls as you could and drop them off within the black circle in the middle. The playing field was finite and bounded and the golf balls were scattered; six balls on our side and six balls on our opponents side to start. To accomplish these tasks 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 drop off our load of golf balls. These two sensors simply looked for any change in reflected light from the surface. This change in light signified the presence of the drop off zone. The remaining sensors were kept the same from previous labs, as their utility had not changed (i.e., two bump sensors for obstacle avoidance).
In regards to the software aspect of our robot, we had the functions for obstacle avoidance and wander activated until either one team captured and dropped off a majority of the golf balls or the current game timer ran out. The 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 the robot to center and drop the golf balls off, which in turn activated our gate mechanism. The entire program consisted of a total of three main processes which were constantly active for each of the main tasks; obstacle avoidance, wander and drop golf balls off.
The main issues that presented themselves during this project were mainly in the design of our robot as the code was pretty much taken from previous labs with minor modifications. We had to construct and deconstruct our robot in order to make it vulnerable enough to withstand some of the torment the other robots brought about. Although we were not allowed to equip our robot with offensive weaponry, we were allowed to use the legos within our lego box to make it sturdy. Another issue we had was our battery life. Our battery seemed to go dead rather quickly, which presented significant problems while testing and during the actual competition.
We feel that our robot competed at a premium level for the competition. I believe that the outcome of the tournament were as expected. It really came down to who was able to get the balls to the center first which the winner seemed to do flawlessly.
Code
// Robo Mini Golf - Laster, Szelagowski, Niemann
// Final Competition
////////////// Global Variables
int MAX_LIGHT = 115;
int MIN_LIGHT = 80;
float _timer;
int RIGHT_EYE = 4;
int LEFT_EYE = 3;
int RIGHT_BUMP = 10;
int LEFT_BUMP = 9;
int rVal = 0;
int lVal = 0;
int bumpCtr = 0;
int aFlag = 1;
int lock = 0;
int onLine = 0;
int LEFT_MOTOR = 0;
int RIGHT_MOTOR = 2;
int GATE_MOTOR = 3;
int OPEN_GATE = 100;
int CLOSE_GATE = -100;
float centerTime = 2.5;
int HOLE_SENSOR = 4;
// Main Function
void main()
{
int time;
// Loop For Start Waiting
while( !start_button());
start_process(open());
reset_timer();
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, 75);
start_process(avoid());
start_process(sense());
}
void sense()
{
while(1)
{
if(normalize(analog(LEFT_EYE)))
{
close();
aFlag = 0;
if(!normalize(analog(RIGHT_EYE)))
{
motor(LEFT_MOTOR, -50);
motor(RIGHT_MOTOR, 50);
sleep(.75);
}
off(RIGHT_MOTOR);
off(LEFT_MOTOR);
sleep(.3);
open();
motor(LEFT_MOTOR, -50);
motor(RIGHT_MOTOR, -50);
sleep(2.0);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, -75);
sleep(1.0);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, 75);
}
else if( normalize(analog(RIGHT_EYE)) )
{
close();
aFlag = 0;
if(!normalize(analog(LEFT_EYE)))
{
motor(LEFT_MOTOR, 50);
motor(RIGHT_MOTOR, -50);
sleep(.75);
}
off(RIGHT_MOTOR);
off(LEFT_MOTOR);
sleep( .3);
open();
motor(LEFT_MOTOR, -50);
motor(RIGHT_MOTOR, -50);
sleep(2.0);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, -75);
sleep(1.0);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, 75);
}
aFlag = 1;
}
}
void avoid()
{
while(1){
while( aFlag )
{
if( digital( LEFT_BUMP )) // Left Motor
{
if( timer() < 5. )
{
if( bumpCtr == 2 )
{
//left
randomTurn(1);
bumpCtr=0;
}
else
{
goRight();
//goLeft();
bumpCtr++;
}
}
else
{
goRight();
//goLeft();
bumpCtr = 1;
reset_timer();
}
open();
}
if( digital( RIGHT_BUMP )) // Right Motor
{
if( timer() < 5. )
{
if( bumpCtr==2 )
{
randomTurn(2);
bumpCtr=0;
}
else
{
goLeft();
//goRight();
bumpCtr++;
}
}
else
{
goLeft();
//goRight();
bumpCtr=1;
reset_timer();
}
open();
}
}
}
}
void goRight()
{
// Turning right
close();
sleep(0.2);
goBack(0.4);
if(aFlag)
{
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, -75);
sleep(0.5);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, 75);
}
}
void goLeft()
{
// Turning left
close();
sleep(0.2);
goBack(0.4);
if(aFlag)
{
motor(RIGHT_MOTOR, 75);
motor(LEFT_MOTOR, -75);
sleep(0.5);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, 75);
}
}
void goBack(float val)
{
motor(RIGHT_MOTOR, -40);
motor(LEFT_MOTOR, -40);
sleep(val);
}
void randomTurn(int val)
{
float randNum;
int randMotor;
int i;
close();
randNum = (float)random(3)/10.0 + .2;
fd(LEFT_MOTOR);
fd(RIGHT_MOTOR);
tone(940.0, 0.7);
if(aFlag)
{
if(val == 1)
{
motor(LEFT_MOTOR, -100);
motor(RIGHT_MOTOR, 100);
sleep(randNum);
}
else
{
motor(LEFT_MOTOR, 100);
motor(RIGHT_MOTOR, -100);
sleep(randNum);
}
motor(RIGHT_MOTOR, 75);
motor(LEFT_MOTOR, 75);
}
}
void open()
{
motor(GATE_MOTOR, OPEN_GATE);
sleep(.3);
motor(GATE_MOTOR, 30);
}
void close()
{
motor(GATE_MOTOR, CLOSE_GATE);
sleep(.3);
motor(GATE_MOTOR, -30);
}
void reset_timer()
{
_timer= seconds();
}
float timer()
{
return seconds() - _timer;
}
int normalize( int light )
{
if(light > 60)
{
return 1;
}
else //( light > 50)
{
return 0;
}
}