Beefhouse v2.0
From MARS Wiki
/*
Title: Beef_House.c
Authors: James O'Carroll (jcoarrol@purdue.edu), Chris Cooper, Craig Benz, Trent Nelson
Microcontroller: ATMega16
Compiler: CodeVisionAVR
Based on previous code by Nathan Smith, Dave Matthews, and MARS members 2003-2005
The main control routine for the sumo robot Beef House.
Revision History:
Date: 12-05-05
Changes: Initial conversion of code from older file to support PerfBoard and more stable,
modular operation. main(), Read_Infrared(), Go_Forward(),
Go_Reverse(), Go_Left(), and Go_Right() written. Read_Ultrasonic() taken from
SRF04.c.
Design Notes: Port A: Nada Bit 7 - Nothing
6 - Nothing 5 - Nothing 4 - Nothing 3 - Nothing 2 - Nothing 1 - Nothing 0 - Nothing
Port B: IR Sensors for Detecting Ring Edge Bit 7 - Input, IR Sensor Front Right
6 - Input, IR Sensor Rear Right 5 - Input, IR Sensor Rear Left 4 - Nothing 3 - Nothing 2 - Nothing 1 - Nothing 0 - Input, IR Sensor Front Left
Port C: Outputs to Motor Mind C Bit 7 - Nothing
6 - Nothing 5 - Nothing 4 - Nothing 3 - Output, Right Motor 2 - Output, Right Motor 1 - Output, Left Motor 0 - Output, Left Motor
Port D: Interface to Ultrasonic Sensor(s) Bit 7 - Output, Trigger Pulse to Ultrasonic Sensor
6 - Input, Echo Pulse from Ultrasonic Sensor 5 - (Tentative) Output, Left Motor //ULTRA SONIC 2 IS ON HERE 4 - (Tentative) Input, Echo Pulse from Ultrasonic Sensor //ULTRA SONIC 2 IS ON HERE 3 - Nothing 2 - Nothing 1 - Nothing 0 - Nothing
- /
- include <mega16.h>
- include <stdio.h>
- include <delay.h>
- define Clock_Freq_MHz (_MCU_CLOCK_FREQUENCY_ / 1000000) // _MCU_CLOCK_FREQUENCY_ is the
// Programmed Clock Frequency in Hz
int Rollover = 0;
int Read_Infrared(); int Read_Ultrasonic(); int Read_Ultrasonic2(); void Go_Forward(); void Go_Reverse(); void Go_Left(); void Go_Right();
//Timer 0 Overflow Interrupt Service Routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
//TCNT0 = 0; //reload for 250 us ticks Rollover++; return;
}
int main() {
int Command;
int debug;
UCSRA = 0x00;
UCSRB = 0x18; // Initialized for both input and output Serial Communications
UCSRC = 0x86;
UBRRH = 0x00;
UBRRL = _MCU_CLOCK_FREQUENCY_ / (16 * 9600) - 1; // Set for 9600 baud at any clock frequency
DDRA = 0b00000000;
DDRB = 0b00000000;
DDRC = 0b00001111;
DDRD = 0b10100000;
debug=0;
if(debug ==1)
{
printf("Beginning five second delay for match beginnning\n\r");
}
delay_ms(5000);
if(debug==1)
{
printf("Delay complete, proceeding to kick bot\n\r");
}
Command = 0;
Command = Read_Infrared();
if (Command == 0)
{
Command = Read_Ultrasonic2();
}
switch (Command)
{
case 1:
Go_Forward();
case 2:
Go_Reverse();
case 3:
Go_Right();
case 4:
Go_Left();
default:
printf("This shouldn't happen\n\r");
}
while(1)
{
Command = 0;
Command = Read_Infrared();
if (Command == 0)
{
Command = Read_Ultrasonic();
}
switch (Command)
{
case 1:
Go_Forward();
case 2:
Go_Reverse();
case 3:
Go_Right();
case 4:
Go_Left();
default:
printf("This shouldn't happen\n\r");
}
}
return 0;
}
int Read_Infrared() { /*
Reads the state of the infrared sensors. Returns an integer command to be used by the main function.
- /
int Infrared_Command = 0;
if ((PINB.7 == 1) || (PINB.0 == 1))
{
if(debug ==1 ) {printf("Infrared says go reverse\n\r");}
Infrared_Command = 2; // Tell main() function to call Go_Reverse()
}
if ((PINB.6 == 1) || (PINB.5 == 1))
{
if(debug ==1 ) {printf("Infrared says go forward\n\r");}
Infrared_Command = 1; // Tell main() function to call Go_Forward()
}
return Infrared_Command;
}
int Read_Ultrasonic() { /*
Activates the ultrasonic sensor and measures how long the returned pulse length is. Returns an integer to tell the main function what directional function to call. Is only called if Read_Infrared() returns 0.
- /
int Pulse_Command = 0;
unsigned int Pulse_Length = 0;
unsigned int Pulse_Len = 0;
unsigned int Temp1 = 0;
unsigned int Temp2 = 0;
PORTD.7 = 0;
TCCR0 = 0x01;
TIMSK = 0x01;
if(debug ==1 ) {printf("Clock freq is %d, UBRRL is %d\n\r", Clock_Freq_MHz, UBRRL);}
if(debug ==1 ) {printf("Activating Sensor Now\n\r");}
#asm("sei")
TCNT0=0;
PORTD.7=1;
while(TCNT0<20)
{
}
PORTD.7=0;
while (PIND.6 == 0)
{
}
TCNT0 = 0;
Rollover = 0;
while (PIND.6 == 1)
{
}
Pulse_Length = 256 * Rollover + TCNT0;
Pulse_Len = (2000 / Clock_Freq_MHz) * Rollover + TCNT0;
#asm("cli");
delay_ms(10);
Temp1 = Pulse_Length/74;
Temp2 = Pulse_Len/74;
if(debug ==1 ) {printf("Pulse length is %d, Pulse len is %d\r", Temp1, Temp2);}
if (Pulse_Length < 200) // 200 Is a temporary number
{
if(debug ==1 ) {printf("Ultrasonic says go forward\n\r");}
Pulse_Command = 1; // Tell main() to call Go_Forward()
}
else
{
Pulse_Command = 4; // Tell main() to call Go_Left()
}
return Pulse_Command;
}
int Read_Ultrasonic2() { /*
Activates the ultrasonic sensor and measures how long the returned pulse length is. Returns an integer to tell the main function what directional function to call. Is only called if Read_Infrared() returns 0.
- /
int Pulse_Command = 0;
unsigned int Pulse_Length = 0;
unsigned int Pulse_Len = 0;
unsigned int Temp1 = 0;
unsigned int Temp2 = 0;
PORTD.5 = 0;
TCCR0 = 0x01;
TIMSK = 0x01;
if(debug ==1 ) {printf("Clock freq is %d, UBRRL is %d\n\r", Clock_Freq_MHz, UBRRL);}
if(debug ==1 ) {printf("Activating Sensor Now\n\r");}
#asm("sei")
TCNT0=0;
PORTD.5=1;
while(TCNT0<20)
{
}
PORTD.5=0;
while (PIND.4 == 0)
{
}
TCNT0 = 0;
Rollover = 0;
while (PIND.4 == 1)
{
}
Pulse_Length = 256 * Rollover + TCNT0;
Pulse_Len = (2000 / Clock_Freq_MHz) * Rollover + TCNT0;
#asm("cli");
delay_ms(10);
Temp1 = Pulse_Length/74;
Temp2 = Pulse_Len/74;
if(debug ==1 ) {printf("Pulse length is %d, Pulse len is %d\r", Temp1, Temp2);}
if (Pulse_Length < 200) // 200 Is a temporary number
{
if(debug ==1 ) {printf("Ultrasonic2 turn right\n\r");}
Pulse_Command = 3; // Tell main() to call Go_RIGHT()
}
else
{
if(debug ==1 ) {printf("Ultrasonic says go left\n\r");}
Pulse_Command = 4; // Tell main() to call Go_Left()
}
return Pulse_Command;
}
void Go_Forward() {
if(debug ==1 ) {printf("Going forward\n\r");}
PORTC.0 = 1;
PORTC.1 = 1;
PORTC.2 = 1;
PORTC.3 = 1;
return;
}
void Go_Reverse() {
if(debug ==1 ) {printf("Going reverse\n\r");}
PORTC.0 = 0;
PORTC.1 = 0;
PORTC.2 = 0;
PORTC.3 = 0;
return;
}
void Go_Left() {
if(debug ==1 ) {printf("Going left\n\r");}
PORTC.0 = 1;
PORTC.1 = 1;
PORTC.2 = 0;
PORTC.3 = 0;
return;
}
void Go_Right() {
if(debug ==1 ) {printf("Going left\n\r");}
PORTC.0 = 0;
PORTC.1 = 0;
PORTC.2 = 1;
PORTC.3 = 1;
return;
}
