Selepas seharian buat body untuk mobile robot, akhirnya siap juga.. Ni 1st time aku buat robot aku sendiri menggunakan modal aku sendiri. gembira gile rasanye.. haha..
body yg simple aku buat. Masa ni hanya ada gearbox, tayar, dan set driver L298
Yang ni dah siap pasang. Wayar agak beserabut la.. haha..
LCD panel untuk paparkan bacaan ADC daripada infrared sensor
Pandangan hadapan. Senior aku ckp aku terbalik letak sensor tu. Secara praktikalnya IR transmitter kat depan. Tapi aku buat macam ni jadi kira ok la.. haha
Ni kod yang aku buat untuk mobile robot ni.. Kod ni adalah kod basic untuk line tracking..
#include <16f877a.h>
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt,nolvp,nobrownout
#define use_portd_lcd TRUE
#include <lcd.c>
#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8
long s0,s1,s2,s3,s4;
void ADC()
{
set_adc_channel( 0 );
delay_us(100);
s0 = read_adc();
delay_us(100);
lcd_gotoxy(1,1);
printf(lcd_putc,"0:%lu ",s0);
set_adc_channel( 1 );
delay_us(100);
s1 = read_adc();
delay_us(100);
lcd_gotoxy(6,1);
printf(lcd_putc,"1:%lu ",s1);
set_adc_channel( 2 );
delay_us(100);
s2 = read_adc();
delay_us(100);
lcd_gotoxy(12,1);
printf(lcd_putc,"2:%lu ",s2);
set_adc_channel( 3 );
delay_us(100);
s3 = read_adc();
delay_us(100);
lcd_gotoxy(1,2);
printf(lcd_putc,"3:%lu ",s3);
set_adc_channel( 4 );
delay_us(100);
s4 = read_adc();
delay_us(100);
lcd_gotoxy(7,2);
printf(lcd_putc,"4:%lu ",s4);
}
void linefollowing()
{
if((s0<100)&&(s1<100)&&(s2>100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(250); set_pwm2_duty(250);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1>100)&&(s2>100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(240); set_pwm2_duty(250);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1>100)&&(s2<100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(200); set_pwm2_duty(240);
/* Kiri Kanan*/
}
else if((s0>100)&&(s1>100)&&(s2<100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(100); set_pwm2_duty(200);
/* Kiri Kanan*/
}
else if((s0>100)&&(s1<100)&&(s2<100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(100); set_pwm2_duty(250);
/* Kiri Kanan*/
}
//////////////
else if((s0<100)&&(s1<100)&&(s2>100)&&(s3>100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(250); set_pwm2_duty(240);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1<100)&&(s2<100)&&(s3>100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(240); set_pwm2_duty(200);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1<100)&&(s2<100)&&(s3>100)&&(s4>100))
{
portc=0b1010000;
set_pwm1_duty(200); set_pwm2_duty(100);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1<100)&&(s2<100)&&(s3<100)&&(s4>100))
{
portc=0b1010000;
set_pwm1_duty(250); set_pwm2_duty(100);
/* Kiri Kanan*/
}
}
void main()
{
lcd_init();
SETUP_ADC_PORTS(ALL_ANALOG );
SETUP_ADC(ADC_CLOCK_INTERNAL);
setup_ccp1(CCP_PWM); // Configure CCP1 as PWM
setup_ccp2(CCP_PWM); // configure CCP2 as PWM
setup_timer_2(T2_DIV_BY_4, 255, 1); //20khz frequency
set_tris_c(0x00);
while(1)
{
ADC();
linefollowing();
}
}
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt,nolvp,nobrownout
#define use_portd_lcd TRUE
#include <lcd.c>
#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8
long s0,s1,s2,s3,s4;
void ADC()
{
set_adc_channel( 0 );
delay_us(100);
s0 = read_adc();
delay_us(100);
lcd_gotoxy(1,1);
printf(lcd_putc,"0:%lu ",s0);
set_adc_channel( 1 );
delay_us(100);
s1 = read_adc();
delay_us(100);
lcd_gotoxy(6,1);
printf(lcd_putc,"1:%lu ",s1);
set_adc_channel( 2 );
delay_us(100);
s2 = read_adc();
delay_us(100);
lcd_gotoxy(12,1);
printf(lcd_putc,"2:%lu ",s2);
set_adc_channel( 3 );
delay_us(100);
s3 = read_adc();
delay_us(100);
lcd_gotoxy(1,2);
printf(lcd_putc,"3:%lu ",s3);
set_adc_channel( 4 );
delay_us(100);
s4 = read_adc();
delay_us(100);
lcd_gotoxy(7,2);
printf(lcd_putc,"4:%lu ",s4);
}
void linefollowing()
{
if((s0<100)&&(s1<100)&&(s2>100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(250); set_pwm2_duty(250);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1>100)&&(s2>100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(240); set_pwm2_duty(250);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1>100)&&(s2<100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(200); set_pwm2_duty(240);
/* Kiri Kanan*/
}
else if((s0>100)&&(s1>100)&&(s2<100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(100); set_pwm2_duty(200);
/* Kiri Kanan*/
}
else if((s0>100)&&(s1<100)&&(s2<100)&&(s3<100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(100); set_pwm2_duty(250);
/* Kiri Kanan*/
}
//////////////
else if((s0<100)&&(s1<100)&&(s2>100)&&(s3>100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(250); set_pwm2_duty(240);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1<100)&&(s2<100)&&(s3>100)&&(s4<100))
{
portc=0b1010000;
set_pwm1_duty(240); set_pwm2_duty(200);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1<100)&&(s2<100)&&(s3>100)&&(s4>100))
{
portc=0b1010000;
set_pwm1_duty(200); set_pwm2_duty(100);
/* Kiri Kanan*/
}
else if((s0<100)&&(s1<100)&&(s2<100)&&(s3<100)&&(s4>100))
{
portc=0b1010000;
set_pwm1_duty(250); set_pwm2_duty(100);
/* Kiri Kanan*/
}
}
void main()
{
lcd_init();
SETUP_ADC_PORTS(ALL_ANALOG );
SETUP_ADC(ADC_CLOCK_INTERNAL);
setup_ccp1(CCP_PWM); // Configure CCP1 as PWM
setup_ccp2(CCP_PWM); // configure CCP2 as PWM
setup_timer_2(T2_DIV_BY_4, 255, 1); //20khz frequency
set_tris_c(0x00);
while(1)
{
ADC();
linefollowing();
}
}
Ni video untuk 1st run mobile robot ni..
Setakat ni aku puas hati dapat siapkan.. haha.. nnt ada masa aku akan tambah lagi fungsi-fungsi lain pada mobile robot ni..
2 comments:
mana nak dapat tayar dan motor mcm tu?
Masa ni beli online kt cytron.com.my kalau tinggal kt kawasan KL boleh pergi jalan pasar je. Kt kedai nixie sebelah kedai online component ada jual.
Post a Comment