|
Explore TEAMS!
|
Student /
PurpleCobraFindsTheLight/* Add comment - Andrew Osborne - Blue - Braitenberg Robot - 12/12/08 - For photophilic, if in dark, go into wimp mode to find light and drive toward it based on how close it is. For photophobic, the create will find the light then move away from it at a speed inversely proportional to the amount of light it sees.
// Include the Command Module API
//#define safe (cr8->wheeldrop_left || cr8->wheeldrop_right || cr8->wheeldrop_caster || cr8->cliff_right || cr8->cliff_left||cr8->cliff_frontright || cr8->cliff_frontleft) // Pointer to the Sensor Data structure cr8_t *cr8; //Declares function that sets Create's safe mode. uint8_t safe(void); //Declares function that sets Create's odometers to zero. int reset(void); // Declare GLOBAL Variables used in program: // Initialize GLOBAL Variables used in program: // Declaraion for function "main" int main(void) { // Allocate memory for sensor data structure
cr8 = cr8_alloc();
/* Initialize the Create and Command Module
Sync communications settings. */
cr8_init(cr8);
// Declare LOCAL constants used in function main:
// Declare LOCAL Variables used in function main:
int rw_velocity,lw_velocity;
uint16_t time;
// Initialize LOCAL Variables used in function main:
rw_velocity = 0;
lw_velocity = 0;
time = 0;
time = cr8_stopwatch_time();
cr8_adc_enable(ADC_CENTER_1); // enable right light sensor cr8_adc_enable(ADC_CENTER_2); // enable left light sensor //Run this program forever.
lcd_set_backlight(255);
cr8_update(cr8);
cr8_stopwatch_start();
while(1)
{
//Wait for either play or advance button to decide if photophilic or photophobic.
while ((cr8->button_play == 0) && (cr8->button_advance == 0) && safe())
{
cr8_beep_short();
cr8_update(cr8);
cr8_set_leds(1,1,0,255);
cr8_delay(15);
cr8_set_leds(0,0,0,255);
if(cr8_stopwatch_time() > (time + 5))
{
lcd_print_4vars(cr8->angle,0,cr8->dist_m,cr8->dist_mm,true,false);
time = cr8_stopwatch_time();
}
}
//Play button being pressed means photophilic.
if ((cr8->button_play == 1) && (safe()) && (cr8->button_advance == 0))
{
//While safe, advance button is not pressed, and not close to light, enact photophilic loop.
while((cr8->adc_center1) < 600 && (cr8->adc_center2) < 480 && safe() && (cr8->button_advance == 0))
{
cr8_update(cr8);
//If too little light, enacts a varied wimp mode.
while((cr8->adc_center1) < 200 && (cr8->adc_center2) < 140 && safe() && (cr8->button_advance == 0))
{
cr8_drive_direct(200,200);
cr8_update(cr8);
cr8_set_leds(1, 1,175, 255);
if(cr8_stopwatch_time() > (time + 5))
{
lcd_print_4vars(cr8->angle,0,cr8->dist_m,cr8->dist_mm,true,false);
time = cr8_stopwatch_time();
}
//If right bumper is sensed, rotates counterclockwise.
if((cr8->bumper_right == 1) && (cr8->adc_center1 < 200) && (cr8->adc_center2 < 140) && safe() && (cr8->button_advance == 0))
{
reset();
cr8_update(cr8);
cr8_drive_direct(-100,-100);
cr8_delay(100);
cr8_drive_direct(-200,200);
cr8_set_leds(0, 0, 0, 255);
/*Loop for turning while after a right bump. Turns
until not longer safe, play button pressed,
reached 90 degrees, light sensed or front bumped.*/
while((cr8->adc_center1 < 200) && (cr8->adc_center2 < 140) && safe() && (cr8->button_advance == 0) && (cr8->angle < 90) && (cr8->bumper_left == 0))
{
cr8_update(cr8);
cr8_delay(20);
if((cr8->bumper_right == 1) && safe())
/*If right bumper is pressed,
odometers are set back to zero.*/
{
reset();
}
}
/*Once out of the rotating loop,
all odometers are set to zero.*/
reset();
}
/*If left bumper is sensed, rotates clockwise
and angle is forced set to 180.*/
if((cr8->bumper_left == 1) && (cr8->adc_center1 < 200) && (cr8->adc_center2 < 140) && safe() && (cr8->button_advance == 0))
{
cr8_update(cr8);
cr8->angle = 180;
cr8_drive_direct(-100,-100);
cr8_delay(100);
cr8_drive_direct(200,-200);
cr8_set_leds(0, 1, 0, 0);
/*Loop for turning while after a right bump. Turns
until not longer safe, play button pressed,
less than 90 degrees, light sensed or front bumped.*/
while((cr8->adc_center1 < 200) && (cr8->adc_center2 < 140) && safe() && (cr8->button_advance == 0) && (cr8->angle < 90) && (cr8->bumper_right == 0))
{
cr8_update(cr8);
cr8_delay(20);
if((cr8->bumper_left == 1) && safe())
/*If left bumper is pressed, distance is
set back to zero, but angle is set to 180.*/
{
cr8->angle = 180;
cr8->dist_mm = 0;
}
}
}
}
//Drive in accordance to photosensor readings after speed is calculated to match.
cr8_set_leds(1,1,0,255);
cr8_update(cr8);
cr8_delay(15);
rw_velocity = (cr8->adc_center1*3)/5;
lw_velocity = (cr8->adc_center2*12)/11;
cr8_drive_direct(rw_velocity,lw_velocity);
}
}
if ((cr8->button_advance == 1) && (safe()) && (cr8->button_play == 0))
{
//While safe, advance button is not pressed, and not already in dark, enact photophobic loop.
while((cr8->adc_center1 > 35) && (cr8->adc_center2 > 15) && safe() && (cr8->button_play == 0))
{
cr8_update(cr8);
//If too much light, enacts a varied wimp mode.
while((cr8->adc_center1 > 200) && (cr8->adc_center2 > 150) && safe() && (cr8->button_play == 0))
{
cr8_drive_direct(200,200);
cr8_update(cr8);
cr8_set_leds(1, 1,175, 255);
if(cr8_stopwatch_time() > (time + 5))
{
lcd_print_4vars(cr8->angle,0,cr8->dist_m,cr8->dist_mm,true,false);
time = cr8_stopwatch_time();
}
//If right bumper is sensed, rotates counterclockwise.
if((cr8->bumper_right == 1) && (cr8->adc_center1 > 250) && (cr8->adc_center2 > 170) && safe() && (cr8->button_play == 0))
{
reset();
cr8_update(cr8);
cr8_drive_direct(-100,-100);
cr8_delay(100);
cr8_drive_direct(-200,200);
cr8_set_leds(0, 0, 0, 255);
/*Loop for turning while after a right bump. Turns
until not longer safe,play button pressed,
reached 90 degrees, light mot sensed or front bumped.*/
while((cr8->adc_center1 > 250) && (cr8->adc_center2 > 170) && safe() && (cr8->button_play == 0) && (cr8->angle < 90) && (cr8->bumper_left == 0))
{
cr8_update(cr8);
cr8_delay(20);
if((cr8->bumper_right == 1) && safe())
/*If right bumper is pressed,
odometers are set back to zero.*/
{
reset();
}
}
/*Once out of the rotating loop,
all odometers are set to zero.*/
reset();
}
/*If left bumper is sensed, rotates clockwise
and angle is forced set to 180.*/
if((cr8->bumper_left == 1) && (cr8->adc_center1 > 250) && (cr8->adc_center2 > 170) && safe() && (cr8->button_play == 0))
{
cr8->angle = 180;
cr8_update(cr8);
cr8_drive_direct(-100,-100);
cr8_delay(100);
cr8_drive_direct(200,-200);
cr8_set_leds(0, 1, 0, 0);
/*Loop for turning while after a right bump. Turns
until not longer safe, play button pressed,
less than 90 degrees, light not sensed or front bumped.*/
while((cr8->adc_center1 > 250) && (cr8->adc_center2 > 170) && safe() && (cr8->button_play == 0) && (cr8->angle < 90) && (cr8->bumper_right == 0))
{
cr8_update(cr8);
cr8_delay(20);
if((cr8->bumper_left == 1) && safe())
/*If left bumper is pressed, distance is
set back to zero, but angle is set to 180.*/
{
cr8->angle = 180;
cr8->dist_mm = 0;
}
}
}
}
//Drive away from light in accordance to photosensor readings after speed is calculated to match.
cr8_set_leds(1,1,0,255);
cr8_update(cr8);
cr8_delay(15);
rw_velocity = (cr8->adc_center1*3)/5;
lw_velocity = (cr8->adc_center2*12)/11;
cr8_drive_direct(lw_velocity,rw_velocity);
if(cr8->bumper_left == 1 && (cr8->adc_center1 > 50) && (cr8->adc_center2 > 15) && safe() && (cr8->button_play == 0))
{
cr8_update(cr8);
cr8_drive_direct(-100,-100);
cr8_delay(100);
cr8_drive_direct(200,-200);
cr8_delay(400);
cr8_set_leds(0, 0, 0, 0);
}
if(cr8->bumper_right == 1 && (cr8->adc_center1 > 50) && (cr8->adc_center2 > 15) && safe() && (cr8->button_play == 0))
{
cr8_update(cr8);
cr8_drive_direct(-100,-100);
cr8_delay(250);
cr8_drive_direct(-200,200);
cr8_delay(400);
cr8_set_leds(0, 0, 0, 0);
}
}
}
cr8_drive_direct(0,0);
}
cr8_free(cr8);
return(0);
} // End of function main //Function that sets all odometers to 0. int reset(void) {
cr8->angle = 0;
cr8->dist_mm = 0;
cr8->revolutions = 0;
cr8->dist_m = 0;
return(1);
}
//Safe function made in class. uint8_t safe(void) { cr8_update(cr8);
// Check for unsafe conditions:
// Wheel Drops
// Cliff Detection
if(cr8->wheeldrop_left || cr8->wheeldrop_right ||
cr8->wheeldrop_caster ||
cr8->cliff_right || cr8->cliff_left ||
cr8->cliff_frontright || cr8->cliff_frontleft)
{
cr8_drive_direct(0,0);
cr8_beep_error();
cr8_set_leds(off,off,255,255);
return 0;
}
else {
// No unsafe conditions found
return 1;
}
} |