|
THS
|
Robots /
MarkLab5Code
/*
Lab5 Brightenburg by Mark Troutt
*/
// Include the Command Module API
#include "create.h"
// Pointer to the Sensor Data structure
cr8_t *cr8;
// Declare any new function prototypes:
int safe(void);
void turn(int right, int time);
void backUp(void);
void UpdateSensors();
int right = 0, left = 0, done = 0;
// Main function
int main(void)
{
// Allocate memory for sensor data structure
cr8 = cr8_alloc();
// Initialize the Create and Command Module
// Sync communications settings.
cr8_init(cr8);
//Code goes here
cr8_update(cr8);
cr8_stopwatch_start();
cr8_adc_enable(ADC_RIGHT_2);
cr8_set_leds(on, on, 0, 255);
cr8_beep_long();
cr8_delay(500);
while(!(cr8->button_play && !cr8->button_advance)){
while(safe())
{
UpdateSensors();
if(right > 210 || left > 210){
cr8_drive_direct(0, 0);
}
else if(left < right + 10 && right < left + 10 && left < 45 && right < 45) {
cr8_drive_direct(-70, 70);
}
else if(right > left + 18) {
cr8_drive_direct(80,35);
cr8_set_leds(on, off, 0, off);
}
else if(left > right + 18) {
cr8_drive_direct(35,80);
cr8_set_leds(off, on, 0, off);
}
else if(left < right + 18 && right < left + 18) {
cr8_drive_direct(60, 60);
}
else{
cr8_drive_direct(50,-50);
}
if(cr8_stopwatch_time() % 10) {
cr8_cricket_display(right,true,false);
}
}
while(!safe()) //stop if not safe
{
cr8_drive_direct(0, 0);
cr8_set_leds(off, off, 0, off);
}
}
cr8_set_leds(0, 0, 0, 0); //turn off create lights
cr8_set_mc_led0(0); //turn off command module lights
cr8_set_mc_led1(0);
cr8_drive_direct(0, 0); //stop moving
//End main Code
// Release the sensor data structure memory and exit the program
cr8_free(cr8);
return 0;
} // End of main()
// safe() checks if create is safe and returns status.
// If create is at a cliff it backs up.
int safe(void){
int x = true;
if(cr8->wheeldrop_right || cr8->wheeldrop_left ||
cr8->wheeldrop_caster )
{
x = false;
cr8_drive_direct(0, 0);
}
else if(cr8->cliff_left || cr8->cliff_right || cr8->cliff_frontleft ||
cr8->cliff_frontright){
cr8_drive_direct(-50, -40);
}
else if(cr8->bumper_right)
{
cr8_stopwatch_start();
while(cr8_stopwatch_time() < 25){
cr8_drive_direct(-70, -70); }
while(cr8_stopwatch_time() < 40){
cr8_drive_direct(-70, 70); }
cr8_stopwatch_start();
}
else if(cr8->bumper_left)
{
cr8_stopwatch_start();
while(cr8_stopwatch_time() < 25){
cr8_drive_direct(-70, -70); }
while(cr8_stopwatch_time() < 40){
cr8_drive_direct(70, -70); }
cr8_stopwatch_start();
}
return x;
} //end safe()
void UpdateSensors()
//Moves the "eye" in front and updates the sensor readings.
{
cr8_update(cr8);
if(cr8_stopwatch_time() % 10 == 0)
{
left = cr8->adc_left2/4;
cr8_cricket_servo_set(0,0);
cr8_cricket_display(left, false, false);
}
else if(cr8_stopwatch_time() % 5 == 0)
{
right = cr8->adc_left2/4;
cr8_cricket_servo_set(0,130);
cr8_cricket_display(right, false, false);
}
}//End UpdateSensors()
|