#include
#include
#include
#include
#include
#include
#include
#define gia_tri_start TCNT0
// khai bao dong co
#define Banh_trai OCR1AL//PWM 4 onboard
#define Banh_phai OCR1BL//PWM 5 onboard
#define Banh_1 OCR1CL//PWM 6 onboard
#define tay_xoay_tr OCR3AL//PWM 1 onboard
#define tay_xoay_ph OCR3BL//PWM 2 onboard
#define pha_a1 bit_is_clear[PINE,7]
#define pha_b1 bit_is_clear[PINE,6]
// KHAI BAO NUT NHAN PHUONG AN
#define cong_tac1 bit_is_clear[PING,0] //0
#define cong_tac2 bit_is_clear[PING,1]// 1
//-=====khai bao bien cho ham do duong moi====
#define cb0 bit_is_set[PINF,0]
#define cb1 bit_is_set[PINF,1]
#define cb2 bit_is_set[PINF,2]
#define cb3 bit_is_set[PINF,3]
#define cb4 bit_is_set[PINF,4]
#define cb5 bit_is_set[PINF,5]
#define cb6 bit_is_set[PINF,6]
#define cb7 bit_is_set[PINF,7]
#define cb PINF
#define un unsigned
#define vo volatile
// ====KHAI BAO CHO DAO CHIYEU DONG CO======
#define banhtrai_dao cbi[PORTB,3];
#define banhtrai_thuan sbi[PORTB,3];
#define banhphai_dao cbi[PORTB,4];
#define banhphai_thuan sbi[PORTB,4];
// Khai bao cho do duong
unsigned char so_line,so_lines;
un char val_pwm,val_pwm_t,nho_line,pwm_speed;
un char tam_pwm,bit_vung,bit_nhanh_cham,pwm_80,pwm_90,pwm_70,pwm_60,pwm_50,pwm_30,pwm_20;
un char L[10],R[10];
un char Ls[10],Rs[10];
un char Lmpu[9],Rmpu[9];
char str[8];
//====================khai bao cho follow line PID========================
//==========================macro set,clear bit============================//
#ifndef cbi
#define cbi[port, bit] [port] &= ~[1 pin_bit] & 1] //ok dung
#endif
#ifndef in_Pin_Init
#define in_Pin_Init[DDRx, DDBx] [DDRx] &= ~[1 PWM2 onboard
OCR3CL=0; //=> PWM3 onboard
//=============================khai bao cho UART====================================
//=========================== USART0 initialization
// USART0 disabled
UCSR0B=0x00;
//============================= USART1 initialization
// Communication Parameters: 8 Data, 2 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud Rate: 9600
UCSR1A=0x00;
UCSR1B=0x98;
UCSR1C=0x0E;
UBRR1H=0x00;
UBRR1L=0x67;
// External Interrupt[s] initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: On
// INT6 Mode: Any change
// INT7: On
// INT7 Mode: Any change
EICRA=0x00;
EICRB=0x50;
EIMSK=0xC0;
EIFR=0xC0;
sei[]; // cho phep ngat toan cuc
//========================DHVT1B==================================//
while[1]
{
chay_do_lai[60,2];
Banh_trai=0;
Banh_phai=0;
while[1];
}
}
// ******************chuong trinh con********************************//
//=======cac ham dieu khien dong co moi==================
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void tien_trai [unsigned char e2]
{
banhtrai_thuan;
Banh_trai=e2;
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxx
void tien_phai [unsigned char e2]
{
banhphai_thuan;
Banh_phai=e2;
}
//================CAC HAM DO LINE============================================//
un char dem_cb[]
{ un char i=0;
if[!cb0] i++; if[!cb1]i++; if[!cb2] i++; if[!cb3] i++;
if[!cb4] i++;if[!cb5] i++;if[!cb6] i++;if[!cb7] i++;
return i;
}
//==============================================
void dem_vach_ko_encoder[]// 1 lai 520 XUNG
{
if[dem_cb[]>4]
{so_line++; }
}
//==============================================
void do_lai[void]// cb7 ben phai , cb0 ben trai
{
// thang nhat ==maxx
if[cb==0 || [!cb3&&!cb4]]
{tien_phai[R[0]];tien_trai[L[0]]; nho_line=4;} //***XX***di max
//===================== ket hop cam bien sau====================
else if [!cb3 && cb2 && cb4]
{tien_phai[R[0]];tien_trai[L[1]];nho_line=4;}//***X****
else if [!cb4 && cb3 && cb5]
{tien_phai[R[1]];tien_trai[L[0]];nho_line=4;}//****X***
else if [!cb3 && !cb2]
{tien_phai[R[0]];tien_trai[L[2]];nho_line=0;}//**XX****
else if [!cb4 && !cb5]
{tien_phai[R[2]];tien_trai[L[0]];nho_line=0;}//****XX**
else if[!cb2 && cb1 && cb3]
{tien_phai[R[0]];tien_trai[L[3]];nho_line=0;}//**X*****
else if [!cb5 && cb4 &&cb6]
{tien_phai[R[3]];tien_trai[L[0]];nho_line=0;}//*****X**
else if [!cb2 && !cb1 ]
{tien_phai[R[0]];tien_trai[L[4]];nho_line=0;}//*XX*****
else if [!cb5 && !cb6 ]
{tien_phai[R[4]];tien_trai[L[0]];nho_line=0;}//*****XX*
else if[!cb1 && cb0 && cb2]
{tien_phai[R[0]];tien_trai[L[5]];nho_line=0;}//*X******
else if [!cb6 && cb7 && cb5]
{tien_phai[R[5]];tien_trai[L[0]];nho_line=0;}//******X*
else if [!cb1 && !cb0 ]
{tien_phai[R[0]];tien_trai[L[6]];nho_line=1;}//******XX
else if [!cb6 && !cb7 ]
{tien_phai[R[6]];tien_trai[L[0]];nho_line=7;}//XX******
else if[!cb0 && cb1]
{tien_phai[R[0]];tien_trai[L[6]];nho_line=1;}//*******X
else if [!cb7 && cb6]
{tien_phai[R[6]];tien_trai[L[0]];nho_line=7;}//X*******
else
{
if[nho_line==4] {tien_phai[20];tien_trai[20];}
else if[nho_line==7] {tien_phai[0];tien_trai[10];}
else if[nho_line==1] {tien_phai[10];tien_trai[0];}
}
}
void speed60[]//120
{ // 34321 0
L[0]=250;L[1]=150;L[2]=100;L[3]=50;L[4]=30;L[5]=20;L[6]=10;
R[0]=250;R[1]=150;R[2]=100;R[3]=50;R[4]=30;R[5]=20;R[6]=10;
}
void speed70[]//120
{ // 34321 0
L[0]=56;L[1]=53;L[2]=53;L[3]=53;L[4]=53;L[5]=53;L[6]=53;
R[0]=60;R[1]=54;R[2]=54;R[3]=54;R[4]=54;R[5]=54;R[6]=54;
}
void speed80[]//120
{ // 34321 0
L[0]=56;L[1]=53;L[2]=53;L[3]=53;L[4]=53;L[5]=53;L[6]=53;
R[0]=60;R[1]=54;R[2]=54;R[3]=54;R[4]=54;R[5]=54;R[6]=54;
}
void set_speed[un char a]
{
if[a==60] speed60[];
else if[a==70] speed70[];
else if[a==80] speed80[];
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void chay_do_lai[unsigned char a, unsigned char line_can_di ]
{
set_speed[a];
so_line=0;
while[1]
{
do_lai[];
dem_vach_ko_encoder[];
if[so_line==line_can_di] break;
}
Banh_trai=0;
Banh_phai=0 ;
}
//=============================================
void stop_pid [void]
{
Banh_trai=2 ;
Banh_phai=2 ;
}