// Robot CAR_bot program
//includes #include<avr/io.h> #define F_CPU 1000000UL //define how fast processor is #include <util/delay.h>
//defines #define left_sensor PD0 // Left sensor #define middle_sensor PD1 // Middle sensor #define right_sensor PD2 // Right sensor
#define left_rear PC3 //Left rear sensor #define middle_rear PC4 //Middle rear sensor
#define right_rear PC5 //Right rear sensor
#define sensor_port PORTD #define sensor_pin PIND #define sensor_ddr DDRD
#define range 110 //Range of about 25cm #define rear_port PORTC #define rear_pin PINC
#define Motor_L1 PD7 //Rear motor 1 #define Motor_L2 PD6 //Rear motor 2
#define Motor_R1 PD5 //Front Solanoid 1 #define Motor_R2 PD4 //Front Solanoid 2
//variables uint8_t servodir; //Servo direction
int forward() { PORTD&=~(_BV(Motor_L1)); //turn Off PD6 PORTD|=_BV(Motor_L2)|_BV(Motor_R2)|_BV(Motor_R1); //turn On PD7, PD4 and PD5 return 0; }
int reverse() { PORTD&=~(_BV(Motor_L2)); //turn Off PD7 PORTD|=_BV(Motor_L1)|_BV(Motor_R1)|_BV(Motor_R2); //turn On PD6, PD5 and PD4 return 0; }
int for_left() { PORTD&=~(_BV(Motor_L1)|_BV(Motor_R2)); //turn Off PD7 and PD4 PORTD|=_BV(Motor_L2)|_BV(Motor_R1); //turn On PD6 and PD5 return 0; }
int for_right() { PORTD&=~(_BV(Motor_L1)|_BV(Motor_R1)); //turn Off PD7 and PD5 PORTD|=_BV(Motor_L2)|_BV(Motor_R2); //turn On PD6 and PD4 return 0; }
int rev_left() { PORTD&=~(_BV(Motor_L2)|_BV(Motor_R2)); //turn Off PD6 and PD4 PORTD|=_BV(Motor_L1)|_BV(Motor_R1); //turn On PD7 and PD5 return 0; }
int rev_right() { PORTD&=~(_BV(Motor_L2)|_BV(Motor_R1)); //turn Off PD6 and PD5 PORTD|=_BV(Motor_L1)|_BV(Motor_R2); //turn On PD7 and PD4 return 0; }
int pause() { PORTD&=~(_BV(Motor_L1)|_BV(Motor_R1)|_BV(Motor_L2)|_BV(Motor_R2)); //turn Off all //delay _delay_ms (100); //continue forward forward(); //delay // _delay_ms (1000); return 0; }
int enemy_spotted() { // Enemy is in range? if(ADCH>range) //Object is within range { //time to attack!! switch (servodir) { case 0: //turn left for_left(); //delay _delay_ms (950); //forward forward(); break; case 1: case 3: //Assume that 1 or 3 is ? //forward forward();
break; case 2: //Assume that 2 is right //Enemy to right
//turn right for_right(); //delay _delay_ms (950); //forward forward(); break; } } return 0; }
int main() { //Setup ADC //Config adc; enable, free running, /8 prescaler ADCSRA=1<<ADEN|1<<ADFR|1<<ADPS1|1<<ADPS0; //use internal ref, left adjust, adc channel ADMUX=1<<REFS1|1<<REFS0|1<<ADLAR; //Start conversion ADCSRA|=1<<ADSC;
//Setup servo //config timer 1. 20ms, fast pwm, OC1B ICR1=20000; OCR1B=1500; TCCR1A=1<<COM1B1|0<<COM1B0|1<<WGM11|0<<WGM10; TCCR1B=1<<WGM13|1<<WGM12|0<<CS12|0<<CS11|1<<CS10;
// ***** Setup Ports and sensors DDRB|=1<<PB2; //Servo DDRD|=_BV(PD7)|_BV(PD6)|_BV(PD5)|_BV(PD4); //motor DDRC=0; PORTD|=_BV(PD1)|_BV(PD2)|_BV(PD0); //front sensors PORTC|=_BV(PC3)|_BV(PC4)|_BV(PC5); //Rear sensors // ***** Insert code here to move forward while(1) { // Where is the black line? // if(sensor_pin&(_BV(left_sensor)|_BV(middle_sensor)|_BV(right_sensor))) // if(sensor_pin&(_BV(PD0)|_BV(PD1)|_BV(PD2))) // { // forward(); // }
if(sensor_pin&(_BV(middle_sensor))) { forward(); } else if(sensor_pin&(_BV(left_sensor))) { for_left(); } else if(sensor_pin&(_BV(right_sensor))) { for_right(); } else { reverse(); }
servodir++; // increment srvodir servodir&=0x03; //mask off uneede bits switch (servodir&0x03) //Move servo to postion based on servodir { case 0: //Servo left OCR1B=1100; //1ms Pulse width (need to test values and change) break; case 1: case 3: //Servo forward OCR1B=1600; //1.5ms Pulse width (need to test values and change) break; case 2: //Servo right OCR1B=2300; //2ms Pulse width (need to test values and change) break; } //delay _delay_ms (300); } }
|
|