Fatos Finais...











Fotos da finalização do projeto, na realização dos testes horas antes da apresentação, com os novos motores, houve alguns problemas, os motores necessitavam de uma corrente alta e o ci acabou não resistindo, havendo então um estouro o qual queimou os ci's e o microcontrolador, sem mais tempo não conseguimos apresentar nosso projeto.

Código Fonte
O programa foi todo desenvolvido em linguagem C.

#define F_CPU 8000000UL /* 8 MHz crystal clock */

/* define as linhas dos sensores */
#define LED_PORT PORTD
#define LED_DDR DDRD
#define LED1 PD3
#define LED2 PD4
#define LED3 PD5
#define LED4 PD6
#define LED5 PD7

/* define controle dos motores */
#define MOTOR_PORT PORTB
#define MOTOR_DDR DDRB
#define M1PULSE PB0
#define M1DIR PB1
#define M2PULSE PB2
#define M2DIR PB3
#define MOTOR_ENABLE PB4

#define MOTOR_FORWARD 0
#define MOTOR_REVERSE 1

#define GO_LEFT 0
#define GO_HARD_LEFT 1
#define GO_FORWARD 2
#define GO_HARD_RIGHT 3
#define GO_RIGHT 4
#define GO_BRAKE 5


#define SW_PORT PORTD
#define SW_BIT PD2
#define SW_DELAY 250

#include
#include
#include

/* prototipos de funcões */
void init_io();
void init_adc();
void enable_motors();
void disable_motors();
void set_motor_direction(uint8_t motor, uint8_t direction);
void steer_robot(uint8_t direction);
uint8_t get_line_sensor_value(uint8_t led_index);
void calibrate_line_sensors(uint8_t *midpoint);
void delay_ms(uint16_t ms);

ISR(INT0_vect)
{

MOTOR_PORT ^= _BV(MOTOR_ENABLE);
delay_ms(SW_DELAY);
GIFR = _BV (INTF0);
}

int
main (void)
{

uint8_t adc_value;
uint8_t midpoint[5] = { 0,0,0,0,0 };
uint8_t last_direction = GO_FORWARD;
uint8_t i;
uint8_t sensor_bits;

init_io();
init_adc();

enable_motors();
steer_robot(GO_HARD_LEFT);
calibrate_line_sensors(midpoint);
disable_motors();

while (1)
{
sensor_bits = 0;


for (i=0; i<5; sensor_bits =" sensor_bits" adc_value =" get_line_sensor_value(i);">= midpoint[i])
{
sensor_bits = _BV(0);
}
else
{
sensor_bits &= ~_BV(0);
}
}



if (sensor_bits == 0x04 sensor_bits == 0x0E sensor_bits == 0x1F)
{
steer_robot(GO_FORWARD);
last_direction = GO_FORWARD;
}
else if (sensor_bits == 0x0C sensor_bits == 0x08)
{
steer_robot(GO_LEFT);
last_direction = GO_LEFT;
}
else if (sensor_bits == 0x02 sensor_bits == 0x06)
{
steer_robot(GO_RIGHT);
last_direction = GO_RIGHT;
}
else if (sensor_bits == 0x10 sensor_bits == 0x18 sensor_bits == 0x1C)
{
steer_robot(GO_HARD_LEFT);
last_direction = GO_HARD_LEFT;
}
else if (sensor_bits == 0x01 sensor_bits == 0x03 sensor_bits == 0x07)
{
steer_robot(GO_HARD_RIGHT);
last_direction = GO_HARD_RIGHT;
}
else
{

steer_robot(last_direction);
}
}
return (0);
}

void
init_io()
{
/* entradas e saidas */
LED_DDR = _BV(LED1) _BV(LED2) _BV(LED3) _BV(LED4) _BV(LED5);
MOTOR_DDR = _BV(M1PULSE) _BV(M1DIR) _BV(M2PULSE) _BV(M2DIR) _BV(MOTOR_ENABLE);

/* resitor pull-up */
SW_PORT = _BV(SW_BIT);
GICR = _BV(INT0);
sei();
}

void
init_adc(void)
{
ADMUX = 0 _BV(ADLAR);
ADCSRA = _BV(ADEN) _BV(ADPS2);
}

void
enable_motors()
{
MOTOR_PORT = _BV(MOTOR_ENABLE);
}

void
disable_motors()
{
MOTOR_PORT &= ~_BV(MOTOR_ENABLE);
}

void
set_motor_direction(uint8_t motor, uint8_t direction)
{
if (direction == MOTOR_REVERSE)
{
MOTOR_PORT = _BV(motor);
}
else
{
MOTOR_PORT &= ~_BV(motor);
}
}

void steer_robot(uint8_t direction)
{


switch (direction)
{
case GO_LEFT:
set_motor_direction(M1DIR, MOTOR_FORWARD);
set_motor_direction(M2DIR, MOTOR_FORWARD);

MOTOR_PORT &= ~_BV(M1PULSE);
MOTOR_PORT = _BV(M2PULSE);
break;

case GO_HARD_LEFT:
set_motor_direction(M1DIR, MOTOR_REVERSE);
set_motor_direction(M2DIR, MOTOR_FORWARD);

MOTOR_PORT &= ~_BV(M1PULSE);
MOTOR_PORT = _BV(M2PULSE);
break;

case GO_FORWARD:
set_motor_direction(M1DIR, MOTOR_FORWARD);
set_motor_direction(M2DIR, MOTOR_FORWARD);

MOTOR_PORT = _BV(M1PULSE);
MOTOR_PORT = _BV(M2PULSE);
break;


case GO_HARD_RIGHT:
set_motor_direction(M1DIR, MOTOR_FORWARD);
set_motor_direction(M2DIR, MOTOR_REVERSE);

MOTOR_PORT = _BV(M1PULSE);
MOTOR_PORT &= ~_BV(M2PULSE);
break;

case GO_RIGHT:
set_motor_direction(M1DIR, MOTOR_FORWARD);
set_motor_direction(M2DIR, MOTOR_FORWARD);

MOTOR_PORT = _BV(M1PULSE);
MOTOR_PORT &= ~_BV(M2PULSE);
break;

case GO_BRAKE:
set_motor_direction(M1DIR, MOTOR_FORWARD);
set_motor_direction(M2DIR, MOTOR_FORWARD);

MOTOR_PORT &= ~_BV(M1PULSE);
MOTOR_PORT &= ~_BV(M2PULSE);
break;
break;
}
}

uint8_t get_line_sensor_value(uint8_t led_index)
{
uint8_t adc_value;
uint8_t led_bit;


switch (led_index)
{
case 0: led_bit = _BV(LED1); break;
case 1: led_bit = _BV(LED2); break;
case 2: led_bit = _BV(LED3); break;
case 3: led_bit = _BV(LED4); break;
case 4: led_bit = _BV(LED5); break;
}


LED_PORT &= ~led_bit;
_delay_ms(5);


ADCSRA = _BV(ADSC);
while (!(ADCSRA & _BV(ADIF)));
adc_value = ADCH;
ADCSRA = _BV(ADIF);


LED_PORT = led_bit;

return adc_value;
}

void calibrate_line_sensors(uint8_t *midpoint)
{
uint8_t adc_value;
uint8_t i, j;
uint8_t thresh_high[5] = { 0,0,0,0,0 };
uint8_t thresh_low[5] = { 255,255,255,255,255 };

for (i=0; i<100; j="0;" adc_value =" get_line_sensor_value(j);"> thresh_high[j]) thresh_high[j] = adc_value;
}
_delay_ms(250);
}

for (i=0; i<5;>





Nenhum comentário: