示例#1
0
文件: lab4.c 项目: eliotbush/Lab4
void main (void) {
	//k is a loop parameter used to determine which byte is currently expected for MIDI in
	k=0;
	USART_Init(BAUD_PRESCALE);
	//pin 0 of D is the MIDI serial in
	//pin 1 is MIDI serial out
	DDRD = 0b11111110;
	//DDRB is connected to the LED bar
	DDRB = 0xFF;
	//DDRA has record on pin 0 and playback on pin 1
	DDRA = 0x00;
	// initialize timer
    timer1_init();	

	bool flush = true;

	//keep looping forever
	while(1){
		//record and playback both off.
		//go to this state to transition from record to playback or playback to playback
		if(PINA == 0){
			//display current state on LEDs
			PORTB = 0x00;
			//reset address and expect a note on to be the next byte
			addr = 0;
			k = 0;
			flush = true;
		}
		//record on, playback off
		if(PINA == 1){
			//display current state
			PORTB = 0x01;
			//record the next MIDI byte
			USART_Recieve();
		}

		//playback on, record off
		if(PINA == 2){
			//display current state
		 	PORTB = eeprom_read_byte(0);
			//play the next MIDI byte from EEPROM 
			USART_playback();
		}
		//playback and record both on
		//use this state to flush the EEPROM so a new melody can be recorded
		//make sure EEPROM flush is done (watch the LEDs) before attempting a record
		if((PINA == 3)||((PINA==1)&&flush)){
			//display current state
			PORTB = 0x03;
			//flush eeprom
			for(i=0; i<256; i++){
				eeprom_write_dword((4*i), 0);
			}
			//reset address and expect a note on to be the next byte
			k = 0;
			addr = 0;
			j = 0;
			for(i=0; i<256; i++){
				if(eeprom_read_dword(i)==0){j++;}
			}
			if(j>=255){
				flush = false;
				j = 0;
			}
		}
	}
}
/**
*	Used to control the robot.
*	Receive and transmit data, measure the distance from object and navigate to the retrieval zone.
**/
int main(void)
{
	lcd_init();
	timer3_init();
	timer_init();
	ADC_init();
	USART_Init();
	oi_t *sensor_data = oi_alloc();
	oi_init(sensor_data);//should turn the iRobot Create's power LED yellow
  
	lcd_init();
	serial_puts("Start");
	//USART_Transmit(13);
	//USART_Transmit(10);
	
	
    int TempAngle[4] = {0,0,0,0};
	int TempIR[4] = {0,0,0,0};
	int pos[4] = {0,0,0,0};
	int AddIR[4] = {0,0,0,0};
	int count[4] = {0,0,0,0};
	int found = 0;
	int x1 = 0;
	int x2 = 0;
	int x3 = 0;
	int x4 = 0;
	unsigned angle = 0;
	unsigned char IR = 0;
	volatile int i=0;
	volatile int x = 0;
	char command;
    char display[100];
	char display1[20];
	char display2[20];
	char display3[20];
	char display4[20];
	char display5[100];
	char display6[100];	
			
	while (1)
	{
		command = USART_Recieve();
		USART_Transmit(command);
		//USART_Transmit(13);
		//USART_Transmit(10);
		
		if (command == '1')
		{
			found = 0;
			angle = 0;
			int t;
			int TempAngle[6] = {0, 0,0,0,0,0};
			int TempIR[6] = {0, 0,0,0,0,0};
			int pos[6] = {0, 0,0,0,0,0};
			int AddIR[6] = {0, 0, 0,0,0,0};
			int count[6] = {0, 0, 0,0,0,0};
			for (angle = 0;angle < 181;angle++)
			{	
				move_servo(angle);
				wait_ms(20);				
				
				IR = 0;
				IR = 42800*pow(ADC_read(2),-1.23);
				
				sprintf(display6, "Angle: %5d		IR: %5d",angle,IR);
				serial_puts(display6);

				if (IR < 80)
				{
				   TempAngle[found]++;
				   count[found]++;
				   AddIR[found]+=IR;
				   TempIR[found]=AddIR[found]/count[found];
				}
				
				else
				{
					if(TempAngle[found] < 5)
					{
						TempAngle[found] = 0;
					}
					
					else
					{
						pos[found] = angle- TempAngle[found]/2;
						if (TempIR[found]*TempAngle[found]< 460)
						{
							USART_Transmit(13);
							USART_Transmit(10);
							for (int i = 0;i<strlen(s8);i++)
							{
								USART_Transmit(s8[i]);
							}
							
							sprintf(display5, "object position: %5d",pos[found]);
							serial_puts(display5);
						}
						sprintf(display, "object position: %5d		IR: %5d		object size: %5d",pos[found],TempIR[found],TempAngle[found]);
						serial_puts(display);
						USART_Transmit(13);
						USART_Transmit(10);
						found++;
					}	
			     }
			}
			OCR3B = 1000-1;				//return to 0 degree	
		}				

		 if (command == 'w')
		 {
			 move_forward(sensor_data,20);
		 }
		 if (command == 's')
		 {
			 move_backforward(sensor_data,20);
		 }
		 if (command == 'a')
		 {
			turn_clockwise(sensor_data,82);
		 }
		 if (command == 'd')
		 {
			turn_counterclockwise(sensor_data,82);
		 }
		 if (command == 'q')
		 {
			 turn_clockwise(sensor_data,38);
		 }
		 if (command == 'e')
		 {
			 turn_counterclockwise(sensor_data, 38);
		 }
		 if (command == '8')
		 {
			 move_forward(sensor_data,5);
		 }
		 if (command == '5')
		 {
			 move_backforward(sensor_data,5);
		 }
		 if (command == 'p')
		 {
			 oi_t* sensor = oi_alloc();
			 oi_init(sensor);
			 
			 load_songs();
			 oi_play_song(songings);
		 }
		 if(command == 'k')
		 {
			oi_update(sensor_data);
			x1 = sensor_data->cliff_left_signal;
			x2 = sensor_data->cliff_right_signal;
			x3 = sensor_data->cliff_frontleft_signal;
			x4 = sensor_data->cliff_frontright_signal; 
			sprintf (display1, "left        = %d",x1);
			sprintf (display2, "right       = %d",x2);
			sprintf (display3, "front left  = %d",x3);
			sprintf (display4, "front right = %d",x4);
			USART_Transmit(13);
			USART_Transmit(10);
	        serial_puts(display1);
	        serial_puts(display3);
	        serial_puts(display4);			
			serial_puts(display2);
			if (x1>500||x2>500||x3>500||x4>500)
			{
				USART_Transmit(13);
				USART_Transmit(10);
				for (int i=0;i<strlen(s6);i++)
				{
					USART_Transmit(s6[i]);
				}
			}						
		}
	}	
}