Recent Changes - Search:

TEAMS Academy Wiki

Explore TEAMS!
for visiting sophomores & juniors

Robotics

EnvBioTech

Bat Design

Assistive Tech


Students


Instructors

TEAMS Forum

TEAMS Calendar

TEAMS Web Site

Wiki Info

edit Student.SideBar

VenandFrancisEggHunt

/* - Ven Satyam & Francis Ambe

	- Cohort 1
	- Egghunt
	- date
	- and overview here 
  • /

// Include the Command Module API

  1. include "create.h"
  2. define min_wall_light (llight > 100 && rlight > 200)
  3. define none_to_min_light (llight > 70 && rlight > 170)
  4. define max_wall_light (llight > 233 || rlight > 333)
  5. define llight (cr8->adc_left1)
  6. define rlight (cr8->adc_right2)
  7. define time (cr8_stopwatch_time() < 1200)
  8. define ldist ((((3 * cr8->adc_left1) + 300) / 4) - 250) //left and right distance between the front switch and the lights
  9. define rdist (((3 * cr8->adc_right2) / 4) - 250)
  10. define lbump (!cr8_dig_get(DIG_LEFT_3))
  11. define rbump (!cr8_dig_get(DIG_RIGHT_3))
  12. define cbump (!cr8_dig_get(DIG_CENTER_3))
  13. define ball_yellow (cr8->adc_left2 >= 610)
  14. define ball_blue (cr8->adc_left2 < 610)

// Pointer to the Sensor Data structure cr8_t *cr8;

// Declare any new function prototypes here:

	void acquire_ball(void);
	void search_net(void);
	void bump(void);
	void lcd_update(void);
	void wait_for_play(void);
	void reject_ball(void);

// Declare GLOBAL constants used in program:

  	const uint8_t green = 0;
	const uint8_t bright = 255;

// Declare GLOBAL Variables used in program:

	uint16_t toggle, timer;

// 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);
	cr8_adc_enable(ADC_CENTER_1);
	cr8_adc_enable(ADC_CENTER_2);
	cr8_adc_enable(ADC_LEFT_1); //left photoresistor
	cr8_adc_enable(ADC_RIGHT_2); //right photoresistor
	cr8_dig_enable(DIG_LEFT_3,DIG_INPUT_PULLUP); //left switch
	cr8_dig_enable(DIG_RIGHT_3,DIG_INPUT_PULLUP); //right switch
	cr8_dig_enable(DIG_CENTER_3,DIG_INPUT_PULLUP); //center switch
	cr8_adc_enable(ADC_LEFT_2); //photoresistor used to detect ball color

  // Declare LOCAL constants used in function main:

	const uint8_t red = 255;

  // Declare LOCAL Variables used in function main:


  // Initialize LOCAL Variables used in function main:


	cr8_stopwatch_start();
	while (time)
		{
		while (time && !cr8->button_play && !cr8->button_advance)
			{
			wait_for_play(); //blink LEDS while waiting for play to be pressed
			cr8_update(cr8);
			}
		if (cr8->button_advance) //press advance to search for yellow balls
			{
			while (time)
				{
				acquire_ball();
				cr8_delay(3000);
				cr8_update(cr8);
				if(ball_yellow)
					{
					cr8_beep_short();
					cr8_set_leds(0,0,green,bright);
					search_net();
					}
				else
					{
					cr8_set_leds(0,0,red,bright);
					reject_ball();
					}
				}
			}
		if (cr8->button_play) //press play to search for blue balls
			{
			while (time)
				{
				acquire_ball();
				cr8_delay(3000);
				cr8_update(cr8);
				if(ball_blue)
					{
					cr8_beep_long();
					cr8_set_leds(0,0,green,bright);
					search_net();
					}
				else
					{
					cr8_set_leds(0,0,red,bright);
					reject_ball();
					}
				}
			}
		}
	cr8_drive(0,0); //when two minutes have passed, stop driving, beep three times, and display TIME UP on LCD
	cr8_beep_error();
	lcd_printf("time up");

  cr8_free(cr8);
  return(0);

} // End of function main

void acquire_ball(void)

	{
	uint16_t timer2 = 0;
	uint16_t last_val = 0;
	uint16_t dy_dx = 0;
	while((cr8_stopwatch_time()<=1200))
		{
		cr8_cricket_servo_set(0, 200);
		cr8_update(cr8);
		timer = cr8_stopwatch_time();
		while(cr8_stopwatch_time()<=(timer+65))
			{
			cr8_drive_direct(-200, -200);
			}
		cr8_update(cr8);
		cr8_cricket_servo_set(0, 200);	
		cr8->revolutions = 0;
		cr8->angle = 0;
		last_val = cr8->adc_center2;
		dy_dx = cr8->adc_center2 - last_val;
		while (time)
			{
			while(time && ((cr8->adc_center2) <= 150 )&&((cr8->angle)+((cr8->revolutions)*360)) <= 360)
				{		
				cr8_beep_error();
				cr8_drive_direct(-100 , 100);
				if((cr8_stopwatch_time() - 2) >= timer2)
					{
					lcd_print_4vars(cr8->adc_center1, cr8->adc_center2,((cr8->angle)+((cr8->revolutions)*360)) , 0, false, false);
					timer2 = cr8_stopwatch_time();
					}
				cr8_delay(50);
				cr8_update(cr8); 	
				}
			while(time && ((cr8->adc_center2) >= 150 )&&((cr8->adc_center2) <= 350 ))
				{
				cr8_drive_direct(-200 , -200);	
				cr8_update(cr8);
				}
			if(cr8->adc_center2 > 350)
				{				
				cr8_drive_direct(0,0);
				cr8_cricket_servo_set(0, 80);
				return;
				}
			}	
		cr8->revolutions = 0;
		cr8->angle = 0;
		if(((cr8->adc_center1) <= 200)&&((cr8->angle)+((cr8->revolutions)*360)) <= 180)
			{
			cr8_drive_direct(-100 , 100);
			}
		}
	}

void search_net(void)

	{
	cr8_cricket_servo_set(0,90); //drop front fence
	while (time && !min_wall_light)
		{
		cr8_drive_direct(-300,-300); //when no trace of the lights are detected, the Create moves backwards at a rate of 300 mm/s
		cr8_update(cr8);
		bump();
		lcd_update();
		}
	if (time && min_wall_light && !max_wall_light) //An if command is used because when the Create gets closer to the lights, the photoresistor readings do not only increase. They fluctuate increasing and decreasing, but as they get closer to lights, they tend to increase more and decrease less.
		{
		while (time && none_to_min_light && !max_wall_light && !lbump && !rbump) //Immediately after min_wall_light is reached, there must be some room for error allowed since the light reading will immediately drop under the min_wall_light level.
			{
			cr8_drive_direct(ldist * 2,rdist * 3); //Proportional control is utilized to maintain its course towards the lights
			cr8_update(cr8);
			bump();
			lcd_update();
			}
		}
	if (time && max_wall_light)
		{
		while (time && min_wall_light)
			{
			cr8_beep_error();
			cr8_drive_direct(-60,-60); //When the Create has detected being extremely close to the lights, it will slowly approach the wall until its front bumper is hit.
			cr8_update(cr8);
			bump();
			lcd_update();
			if (cbump)
				{
				cr8->dist_mm = 0;
				while (time && cr8->dist_mm < 10) //Then the robot will drive away from the wall 10 mm and raise the front fence.
					{
					cr8_drive_direct(60,60);
					cr8_update(cr8);
					}
				cr8_cricket_servo_set(0,150);
				cr8->dist_mm = 70;
				while (time && (cr8->dist_mm > 20)) //Finally, the Create moves 5 cm towards the wall to push the ball into the net. Afterwards, it backs up 5 cm and then rotates 180 degrees clockwise.
					{
					cr8_drive_direct(-60,-60);
					cr8_update(cr8);
					}
				while (time && (cr8->dist_mm < 70))
					{
					cr8_drive_direct(60,60);
					cr8_update(cr8);
					}
				cr8->angle = 180;
				while (time && (cr8->angle < 350) && (cr8->angle > 10))
					{
					cr8_drive(200,1);
					cr8_update(cr8);
					lcd_update();
					if (rbump) //If the right switch is hit while rotating 180 degrees clockwise, the robot will instead rotate counter-clockwise.
						{
						while (time && (cr8->angle > 10))
							{
							cr8_drive(200,-1);
							cr8_update(cr8);
							lcd_update();
							}
						}
					}
				}
			}
		}
		cr8_drive(0,32768);
	return;
	}

void bump()

	{
	if (lbump) //if left switch is triggered, rotate clockwise 40 degrees
		{
		cr8->angle = 180;
		while (time && !min_wall_light && (cr8->angle < 220))
			{
			cr8_drive(200,1);
			cr8_update(cr8);
			lcd_update();
			}
		}
	if (rbump) //if right switch is triggered, rotate coounter-clockwise 40 degrees
		{
		cr8->angle = 180;
		while (time && !min_wall_light && (cr8->angle > 140))
			{
			cr8_drive(200,-1);
			cr8_update(cr8);
			lcd_update();
			}
		}
	if (cbump && !max_wall_light)
		{
		cr8->dist_mm = 0;
		while (time && (cr8->dist_mm < 150) && !(cr8->bumper_left || cr8->bumper_right)) //if the center switch is hit without being near the lights, the robot will first back up 15 cm
			{
			cr8_drive(200,32768);
			cr8_update(cr8);
			}
		cr8->angle = 180;
		while (time && !min_wall_light && !(cr8->bumper_left || cr8->bumper_right) && (cr8->angle < 220) && (cr8->angle > 140)) //then rotate 40 degrees clockwise. if the rotation is interrupted by a switch, the Create will rotate back to its original position and then rotate 40 degrees counter-clockwise
			{
			cr8_drive(200,1);
			cr8_update(cr8);
			lcd_update();
			if (rbump)
				{
				while (time && !min_wall_light && (cr8->angle > 140))
					{
					cr8_drive(200,-1);
					cr8_update(cr8);
					lcd_update();
					}
				}
			}
		if (cr8->bumper_right) //while backing up, if either (factory installed) bumper is pressed, the Create will rotate in the opposite direction and exit the bump() function
			{
			while (time && !min_wall_light && (cr8->angle > 140))
				{
				cr8_drive(200,-1);
				cr8_update(cr8);
				lcd_update();
				}
			}
		if (cr8->bumper_left)
			{
			while (time && !min_wall_light && (cr8->angle < 220))
				{
				cr8_drive(200,1);
				cr8_update(cr8);
				lcd_update();
				}
			}
		}
	return;
	}

void lcd_update(void)

	{
	if (cr8_stopwatch_time() > (timer + 5))
		{
		cr8_update(cr8);
		lcd_print_4vars(ldist,rdist,cr8_stopwatch_time() / 10,0,true,false); //update the LCD every .5 seconds with distance to light for left and right photoresistors and the amount of time that has passed in seconds.
		timer = cr8_stopwatch_time();
		}
	return;
	}

void wait_for_play(void)

	{
	if (cr8_stopwatch_time() < (timer + 5)) //standard "wait for play" feature
		{
		cr8_set_leds((toggle % 2),(toggle % 2),green,bright);
		toggle++;
		timer = cr8_stopwatch_time();
		cr8_update(cr8);
		lcd_update();
		}
	return;
	}

void reject_ball(void)

	{
	cr8_update(cr8);
	cr8_cricket_servo_set(0,150);
	cr8->dist_mm = 0;
	cr8->angle = 0;
	while (time && (cr8->dist_mm < 60))
		{
		cr8_drive(60,32768);
		cr8_update(cr8);
		}
	while (time && (cr8->angle < 40))
		{
		cr8_drive(200,1);
		cr8_update(cr8);
		}
	cr8_drive(0,0);
	return;
	}
Edit - History - Print - Recent Changes - Search
Page last modified on January 25, 2009, at 05:20 PM