Example #1
0
int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
//  timerA_start(timing, OUTMOD_SET_RESET); // starte PWM
  INIT_PORTS();
//  INIT_ADC();
//  init_XT2();
  init_PWM_TimerA();
  set_PWM_duty_cycle(0);
  P1OUT |= LED_PWR_ON;
  P1OUT |= LED2;
  //_BIS_SR(LPM3_bits);
  _BIS_SR(LPM0_bits + GIE);                 // Enter LPM0 w/ interrupt

//      while(1)                      // warte bis sich was tut
//    {
//    }
//  return 0;
}
void main()
{
	unsigned int value,value1;
	int a=0,b=0;
	cli();
	INIT_PORTS();										//Initialize ports
	uart0_init();										//Initialize UART0 for xbee communication
	timer5_init();
	sei();

	INIT_PORTS_ROTATE();								//Initialize ports 
	right_position_encoder_interrupt_init();			//Initialize control registers for wheel
	left_position_encoder_interrupt_init();				//           encoders.
			
	init_devices();
	lcd_set_4bit();										//LCD initialization functions.
	lcd_init();

	unsigned char angle = 0;
 	init_devices_servo();								//Initialize servo motors.

	data='0';
	sharp = ADC_Conversion(11);							//Stores the Analog value of front sharp connected to ADC channel 11 into variable "sharp"
	value = Sharp_GP2D12_estimation(sharp);				//Stores Distance calsulated in a variable "value".
//	lcd_print(1,1,value,3);

//	sharp1 = ADC_Conversion(10);						//Stores the Analog value of front sharp connected to ADC channel 11 into variable "sharp"
//	value1 = Sharp_GP2D12_estimation(sharp1);			//Stores Distance calsulated in a variable "value".
//	lcd_print(1,5,value1,3);


// set servo for camera to ground zero
			servo_1(0);								// code to bring the camera to ground state
			_delay_ms(1000);
// set servo for incline to ground zero	
			servo_2(90);							// align camera	
			_delay_ms(500);

	
// the bot rotating slowly trying to find the bot 
	int terminate = 0;
			while(data=='0' && terminate < 150)
			{
				velocity(150,150); 							//If no ball is detected the rotate and scan
				angle_rotate_left(3);							// for ball in the arena.
				_delay_ms(500);
				stop();
				_delay_ms(500);
				sharp = ADC_Conversion(11);					//Stores the Analog value of front sharp connected to ADC channel 11 into variable "sharp"
				value = Sharp_GP2D12_estimation(sharp);		//Stores Distance calsulated in a variable "value".
				terminate++;
			//	lcd_print(1,1,value,3);
			}	

			if(terminate == 60)
			{
					buzzer_on();
					return;
			}

/*If a ball(red colour) is detected then matlab code sends a '5' signal through
 zigbee.If a '5' is received then the robot stops rotating and moves towards the 
 ball*/			

			value2 = 100;
			if(atoi(data) > 5) 
			{
				lcd_print(1,5,value2,2);
			}

			velocity(160,184);

// change value
			while(value>140)
			{
				sharp = ADC_Conversion(11);					//Stores the Analog value of front sharp connected to ADC channel 11 into variable "sharp"
				value = Sharp_GP2D12_estimation(sharp);		//Stores Distance calsulated in a variable "value".
				lcd_print(1,1,value,3);
				forward();
			}
			stop();
			_delay_ms(2000);
		
/* the servo_1 is for the camera and the servo_2 is for the inclined plane */

// rotate 90 at left
			left90_at_place();
			_delay_ms(1000);
// rotate the camera 90 degrees to the right
			servo_1(90);
			_delay_ms(1000);
//rotate in circle
			rotate_in_circle();
			int perceived_height;
			perceived_height = atoi(data);
			lcd_print(1,5,perceived_height,5);
// rotate 90 degrees to the right to face the ball again
			right90_at_place();
			stop();
			_delay_ms(2000);
// rotate camera also to ground state to face the ball again
			servo_1(0);								// code to bring the camera to ground state
			_delay_ms(1000);
		
// set servo for incline

			int angle_of_inclination = get_angle(perceived_height);
			lcd_print(1,2,angle_of_inclination,5);
  			servo_2(angle_of_inclination);
			_delay_ms(1000);

				
	while(1);
}