int main( void ) { uint32_t chargeopportunity = 0; init(); random_walk_disable(); time_wait(TICKS_PER_SEC * 1); /* start charging if touching charger within 1 second */ chargeopportunity = the_time + 20; leds_set(ORANGE); while (the_time < chargeopportunity) { if ( battery_power_good() ) now_parking = !charge_complete; } leds_set(NONE); while(1) { leds_update_mood(); if (battery_critical()) low_power(); /* We may have finished charging */ if( charge_complete ) { food_level = 0; charge_complete = FALSE; now_parking = 0; /* reverse out of the charger */ random_walk_disable(); motor_r = motor_l = 6; motor_mode = MOTOR_BK; time_wait(5); continue; } /* Go to the charger if... */ if( battery_low() /* Or we've reached a defficiency of food */ || ( food_level > FOOD_THRESHOLD ) || ( now_parking ) ) { if (battery_low() ) mood = MOOD_DRIVING_TO_CHARGER_FLATBATT; else if ( food_level > FOOD_THRESHOLD ) mood = MOOD_DRIVING_TO_CHARGER_NOFOOD; now_parking = !charge_complete; parking_update(); continue; } /* Parking involves a static situation, which is incompatible with the watchdog - hence leave it here. */ watchdog_update(); if( hasfood() ) { mood = MOOD_GOT_FOOD; /* Are we at the light source? */ if(light_intensity == 0) { /* Deposit food here */ mood = MOOD_AT_LAMP; leds_update_mood(); random_walk_disable(); motor_r = motor_l = 6; motor_mode = MOTOR_BK; time_wait(10); } /* Do we have a reasonable bearing? */ else if(bearing_strength > 10) { random_walk_disable(); braitenberg_update(); } else /* Random Walk */ random_walk_enable(); } else { /* Not got food, just do random walk */ mood = MOOD_NONE; random_walk_enable(); } } }
int main( void ) { uint32_t chargeopportunity = 0; init(); random_walk_disable(); time_wait(TICKS_PER_SEC * 1); /* start charging if touching charger within 1 second */ chargeopportunity = the_time + 20; leds_set(RED); // red LED on for 2 seconds P4DIR |= (0x09); //P40 and P43 are output IR P4OUT |= (0x09); //all IR leds on (3 top, one bottom) time_wait(TICKS_PER_SEC * 2); leds_set(GREEN); //green LED on for 1 second time_wait(TICKS_PER_SEC * 1); leds_set(ORANGE); //green LED on for 1 second time_wait(TICKS_PER_SEC * 1); leds_set(RED); //green LED on for 1 second time_wait(TICKS_PER_SEC * 1); leds_set(NONE); P4OUT &= ~(0x01); motor_l=1; //left motor running at low speed for 1 second time_wait(TICKS_PER_SEC *1); motor_l=0; motor_r=1; //right motor running at low speed for 1 second time_wait(TICKS_PER_SEC *1); motor_r=0; motor_mode= MOTOR_BK; time_wait(TICKS_PER_SEC *2); motor_l=2; motor_r=2; time_wait(TICKS_PER_SEC *1); motor_l=0; motor_r=0; motor_mode= MOTOR_FWD; /* while (the_time < chargeopportunity) { if ( battery_power_good() ) { now_parking = !charge_complete; } } */ leds_set(NONE); while(1) { leds_update_mood(); if (battery_critical()) { low_power(); } /* // We may have finished charging if( charge_complete ) { food_level = 0; charge_complete = FALSE; now_parking = 0; // reverse out of the charger random_walk_disable(); motor_r = motor_l = 6; motor_mode = MOTOR_BK; time_wait(5); continue; } */ /* // Go to the charger if... if( battery_low() // Or we've reached a defficiency of food || ( food_level > FOOD_THRESHOLD ) || ( now_parking ) ) { if (battery_low() ) { mood = MOOD_DRIVING_TO_CHARGER_FLATBATT; } else if ( food_level > FOOD_THRESHOLD ) { mood = MOOD_DRIVING_TO_CHARGER_NOFOOD; } now_parking = !charge_complete; parking_update(); continue; } */ /* Parking involves a static situation, which is incompatible with the watchdog - hence leave it here. */ watchdog_update(); /* if( hasfood() ) { mood = MOOD_GOT_FOOD; */ /* Are we at the light source? */ if(light_intensity == 0) { /* Deposit food here */ mood = MOOD_AT_LAMP; leds_update_mood(); random_walk_disable(); motor_r = motor_l = 6; motor_mode = MOTOR_FWD; time_wait(10); } /* Do we have a reasonable bearing? */ else if(bearing_strength > 10) { random_walk_disable(); braitenberg_update(); } else { /* Random Walk */ random_walk_enable(); } /* } else { // Not got food, just do random walk mood = MOOD_NONE; random_walk_enable(); } */ } }