Jump to content
ELFORUM - Forumul electronistilor

Line follower (ATMega16)


Recommended Posts

Zilele astea am inceput sa lucrez la un line follower cu uC ATmega16.

Sursa:

#include <avr\io.h>#include <avr\interrupt.h>#define MOTOR1_DDR  DDRA#define MOTOR1_PORT PORTA#define ACTVIVARE1 PA0#define INTRARE1 PA1#define PWM1_DDR  DDRD#define PWM1_PORT PORTD#define PWM1 PD5#define MOTOR2_DDR  DDRA#define MOTOR2_PORT PORTA#define ACTIVARE2 PA3#define INTRARE2 PA4#define PWM2_DDR  DDRD#define PWM2_PORT PORTD#define PWM2 PD4#define SENZOR_DDR DDRC#define SENZOR_PIN PINC#define S1 PC0#define S2 PC1#define S3 PC2#define IN_AFARA_LINIEI (sensors == 0)#define AFARA_CENTRU_DREAPTA (sensors == 1)#define AFARA_CENTRU_STANGA (sensors == 4)#define AFARA_DREAPTA (sensors == 3)#define AFARA_STANGA (sensors == 6)#define PE_LINIE (sensors == 7)void stop1(){	OCR1A = 0;	MOTOR1_PORT &= ~(_BV(INTRARE1));}void stop2(){	OCR1B = 0;	MOTOR2_PORT &= ~(_BV(INTRARE2));}void mergiINAINTE1(float speed){	MOTOR1_PORT &= ~(_BV(INTRARE1));	OCR1A = (int)(255 * speed);}void mergiINAINTE2(float speed){	MOTOR2_PORT &= ~(_BV(INTRARE2));	OCR1B = (int)(255 * speed);}void mergiINAPOI1(){	MOTOR1_PORT |= _BV(INTRARE1);	OCR1A = 0;}void mergiINAPOI2(){	MOTOR2_PORT |= _BV(INTRARE2);	OCR1B = 0;}void vireazaSTANGAbrusc(){	stop1();	mergiINAINTE2(1);}void vireazaDREAPTAbrusc(){	stop2();	mergiINAINTE1(1);}void vireazaSTANGAlin(){	mergiINAINTE1(0.5);	mergiINAINTE2(1);}void vireazaDREAPTAlin(){	mergiINAINTE2(0.5);	mergiINAINTE1(1);}void init(){	TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10);	TCCR1B = _BV(WGM12) | _BV(CS10) | _BV(CS12);	MOTOR1_DDR = MOTOR2_DDR = PWM1_DDR = PWM2_DDR = SENSOR_DDR = 0;	MOTOR1_DDR |= _BV(ACTIVEAZA1) | _BV(INTRARE1);	MOTOR2_DDR |= _BV(ACTIVEAZA2) | _BV(INTRARE2);	PWM1_DDR |= _BV(PWM1);	PWM2_DDR |= _BV(PWM2);	SENSOR_PIN &= ~(_BV(S1) | _BV(S2) | _BV(S3));	MOTOR1_PORT |= _BV(ACTIVEAZA1);	MOTOR2_PORT |= _BV(ACTIVEAZA2);	mergiINAINTE1(1);	mergiINAINTE2(1);}	int main(){	init();	unsigned char sensors;	while (1)	{		sensors = SENSOR_PIN & (_BV(S1) | _BV(S2) | _BV(S3));		if (AFARA_STANGA)		{			vireazaDREAPTAlin();			continue;		}		if (AFARA_DREAPTA)		{			vireazaSTANGAlin();			continue;		}		if (AFARA_CENTRU_STANGA)		{			vireazaDREAPTAbrusc();			continue;		}		if (AFARA_CENTRU_DREAPTA)		{			vireazaSTANGAbrusc();			continue;		}		if (IN_AFARA_LINIEI)		{			mergiINAPOI1();			mergiINAPOI2();			continue;		}		mergiINAINTE1(1);		mergiINAINTE2(1);	}	return 0;}

Posted Image

 

 

Posted Image

 

 

Posted Image

Link to comment
  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines