Lampiran 1: Listing Program Mobile Robot.ASM
Ka_maju bit P1.3 Ka_mundur bit P1.2 Ki_maju bit P1.5 Ki_mundur bit P1.4 Speedka equ 10 Speedki equ 11 Speed equ 12
org 0 ajmp Start
org 0Bh ajmp Pwm_on
org 23h ajmp Serial_on org 100h
Pwm_on ;prosedur untuk menjalankan PWM
clr tr0 push a
mov a,Speedka cjne a,Speed,P_1 setb Ka_maju setb Ka_mundur
P_1 mov a,Speedki
cjne a,Speed,P_2 setb Ki_maju setb Ki_mundur
P_2 mov a,#10
cjne a,Speed,P_3 mov Speed,#0 cjne r5,#1,P_2a acall Maju P_2a cjne r5,#2,P_2b
acall Kanan P_2b cjne r5,#3,P_2C
acall Kiri
P_2c cjne r5,#4,P_2d acall Mundur P_2d cjne r5,#5,P_2e
acall Majukanan P_2e cjne r5,#6,P_2f
acall Majukiri P_2f cjne r5,#7,P_2g
acall Mundurkanan P_2g cjne r5,#8,P_3
acall Mundurkiri
P_3 inc Speed
mov th0,#0FCh mov tl0,#39h pop a
setb tr0 reti
Serial_on ;prosedur yang dijalankan saat menerima
push a ;data serial
jnb ri,X_serial clr ri
mov a,sbuf FFast cjne a,#1,FLow
mov r2,#0 mov r3,#1 mov r4,#0 acall Maju sjmp X_serial FLow cjne a,#2,RotL
mov r2,#0 mov r3,#0 mov r4,#0 acall Majupelan sjmp X_serial RotL cjne a,#3,RotR
mov r2,#0 mov r3,#2 acall Kiripelan sjmp X_serial RotR cjne a,#4,TurnL
mov r2,#0 mov r3,#3 acall Kananpelan sjmp X_serial TurnL cjne a,#5,TurnR
mov r2,#0 mov r3,#4
acall Majukiripelan sjmp X_serial TurnR cjne a,#6,Reverse
mov r2,#0 mov r3,#5
acall Majukananpelan
Lampiran 1: Listing Program Mobile Robot.ASM (sambungan)
sjmp X_serial
Reverse cjne a,#7,TurnbackL mov r2,#0
mov r3,#6
acall Mundurpelan sjmp X_serial
TurnbackL cjne a,#8,TurnbackR mov r2,#0
mov r3,#7
acall Mundurkiripelan sjmp X_serial
TurnbackR cjne a,#9,Random mov r2,#0
mov r3,#8
acall Mundurkananpelan sjmp X_serial
Random cjne a,#10,Brakes acall Gerakbebas sjmp X_serial Brakes cjne a,#0,X_serial
mov r2,#0 acall Diam X_serial pop a
reti
Maju clr tr0
setb Ka_mundur setb Ki_mundur clr Ka_maju clr Ki_maju ret
Kanan clr tr0
setb Ki_mundur setb Ka_maju clr Ki_maju clr Ka_mundur ret
Kiri clr tr0
setb Ki_maju setb Ka_mundur clr Ki_mundur clr Ka_maju ret
Mundur clr tr0 setb Ka_maju setb Ki_maju clr Ka_mundur clr Ki_mundur ret
Majukanan clr tr0
setb Ki_mundur setb Ka_mundur setb Ka_maju clr Ki_maju ret
Majukiri clr tr0
setb Ki_mundur setb Ka_mundur setb Ki_maju clr Ka_maju ret
Mundurkanan
clr tr0 setb Ki_maju setb Ka_maju setb Ka_mundur clr Ki_mundur ret
Mundurkiri
clr tr0 setb Ki_maju setb Ka_maju setb Ki_mundur clr Ka_mundur ret
Diam clr tr0
setb Ka_mundur setb Ki_mundur setb Ka_maju setb Ki_maju ret
Lampiran 1: Listing Program Mobile Robot.ASM (sambungan)
Majucepat mov Speedka,#8 mov Speedki,#8 mov r5,#1 acall Maju mov th0,#0FCh mov tl0,#39h setb tr0 ret
Majupelan mov Speedka,#5 mov Speedki,#5 mov r5,#1 acall Maju mov th0,#0FCh mov tl0,#39h setb tr0 ret
Mundurpelan
mov Speedka,#5 mov Speedki,#5 mov r5,#4 acall Mundur mov th0,#0FCh mov tl0,#39h setb tr0 ret
Kananpelan mov Speedka,#4 mov Speedki,#4 mov r5,#2 acall Kanan mov th0,#0FCh mov tl0,#39h setb tr0 ret
Majukananpelan
mov Speedka,#5 mov Speedki,#8 mov r5,#1 acall Maju mov th0,#0FCh mov tl0,#39h setb tr0 ret
Majukiripelan
mov Speedka,#8 mov Speedki,#5 mov r5,#1 acall Maju mov th0,#0FCh mov tl0,#39h setb tr0 ret
Mundurkananpelan
mov Speedka,#5 mov Speedki,#8 mov r5,#4 acall Mundur mov th0,#0FCh mov tl0,#39h setb tr0 ret
Mundurkiripelan
mov Speedka,#8 mov Speedki,#5 mov r5,#4 acall Mundur mov th0,#0FCh mov tl0,#39h setb tr0 ret
Gerakbebas
inc r2
cjne r3,#0,Gb0 acall Diam cjne r2,#7,Gb0b mov r2,#0 cjne r4,#0,Gb0a mov r3,#6 acall Majupelan Gb0a cjne r4,#1,Gb0b
mov r3,#2 acall Kananpelan Gb0b sjmp End_Gb
Gb0 cjne r3,#1,Gb1
acall Mundurpelan cjne r2,#7,Gb1a
Lampiran 1: Listing Program Mobile Robot.ASM (sambungan)
mov r2,#0 mov r3,#2 acall Kananpelan Gb1a sjmp End_Gb
Gb1 cjne r3,#2,Gb2
acall Kananpelan cjne r2,#50,Gb2a mov r2,#0
mov r3,#3 acall Kiripelan Gb2a sjmp End_Gb
Gb2 cjne r3,#3,Gb3
acall Kiripelan cjne r2,#60,Gb3a mov r2,#0
mov r3,#4
acall Mundurkananpelan Gb3a sjmp End_Gb
Gb3 cjne r3,#4,Gb4
acall Mundurkananpelan cjne r2,#10,Gb4a mov r2,#0
mov r3,#7
acall Majukiripelan Gb4a sjmp End_Gb
Gb4 cjne r3,#5,Gb5
acall Mundurkiripelan cjne r2,#10,Gb5a mov r2,#0
mov r3,#6 acall Majupelan Gb5a sjmp End_Gb
Gb5 cjne r3,#6,Gb6
acall Majupelan cjne r2,#10,Gb6a mov r2,#0
mov r4,#1 mov r3,#0 acall Diam Gb6a sjmp End_Gb
Gb6 cjne r3,#7,End_Gb
acall Majukiripelan cjne r2,#10,Gb7a mov r2,#0
mov r3,#1
acall Mundurpelan
Gb7a sjmp End_Gb End_Gb
ret
Initial ;inisialisasi awal
mov Speed,#0 mov r2,#0 mov r5,#0 acall Diam mov pcon,#00h
mov tmod,#0A1h ;timer1 = 8bit auto reload timer untuk serial
;timer0 = 16bit untuk PWM mov th0,#0FCh
mov tl0,#39h
mov th1,#0FDh ;9600 bps @12Mhz mov tl1,#0FDh
mov scon,#01010001b
setb tr1 ;mengaktifkan timer1
setb et0 ;enable timer0 overflow interrupt setb es ;enable serial interrupt
setb ps ;serial port interrupt priority level setb ea ;enable interrupt
ret
Start
mov sp,#30h acall Initial sjmp $
Lampiran 2: Listing Program Pengenalan Obyek
void CCamCapDlg::CallBackCam1(IplImage *frame) {
if (mulai) {
IplImage *img_hsv, *img_h, *img_s, *img_v,
*img_calcback_h, *img_and_sv;
CvSize size_image;
size_image.width = frame->width;
size_image.height = frame->height;
// 2. Create a temporary grey image the same size, flip it and set the origin to // Bottom left (DIB's are upside down to normal images!)
img_hsv = cvCreateImage(size_image, 8, 3);
iplMirror(img_hsv, img_hsv, 0);
img_hsv->origin = IPL_ORIGIN_BL;
// Frame RGB di konvert menjadi HSV iplRGB2HSV(frame, img_hsv);
img_h=cvCreateImage(size_image, 8, 1);
iplMirror(img_h, img_h, 0);
img_h->origin = IPL_ORIGIN_BL;
img_s=cvCloneImage(img_h);
img_v=cvCloneImage(img_h);
img_calcback_h = cvCloneImage(img_h);
// Masing-masing channel dipisahkan
cvCvtPixToPlane(img_hsv, img_h, img_s, img_v, NULL);
// Image dibandingkan dengan sample
cvCalcBackProject(&img_h, img_calcback_h, hist_h);
// Image S dan V di threshold
cvThreshold(img_s, img_s, 90, 255, CV_THRESH_BINARY);
cvThreshold(img_v, img_v, 80, 255, CV_THRESH_BINARY);
// Image S dan V digabung dengan fungsi 'AND' img_and_sv = cvCloneImage(img_h);
iplAnd(img_s, img_v, img_and_sv);
IplImage *temp, *temp1, *temp2, *temp3, *temp4;
temp=cvCloneImage(img_h);
// Image hasil Calcbackproject digabung dengan image gabungan S dan V iplAnd(img_calcback_h, img_and_sv, temp);
// Image difilter menggunakan median filter temp1=cvCloneImage(temp);
iplMedianFilter(temp1, temp, 3, 3, 1, 1);
// Image di threshold
cvThreshold(temp,temp,10,255,CV_THRESH_BINARY);
temp2=cvCreateImage(size_image, 8, 1);
temp3=cvCloneImage(temp2);
temp4=cvCloneImage(temp2);
cvvShowImage("Display",temp);
// Mendeteksi adanya obyek yang dicari atau tidak unsigned char pixel[1];
int x,y,keliling,luas,arah,xmin,xmax,ymin,ymax,count;
int xtemp,ytemp,xstart,ystart;
bool carikeliling,cariluas,carititikberat,keluar,ketemu;
carikeliling = FALSE;
cariluas = FALSE;
carititikberat = FALSE;
for (y=0; y<120; y++)
for (x=0; x<160; x++) {
iplGetPixel(temp, x, y, &pixel[0]);
if (pixel[0]==255) {
xtemp = x;
ytemp = y;
xmin = x; xmax = x;
ymin = y; ymax = y;
pixel[0]=100;
iplPutPixel(temp, x, y, &pixel[0]);
pixel[0]=255;
iplPutPixel(temp2, x, y, &pixel[0]);
x = 160;
y = 120;
carikeliling = TRUE;
} }
// prosedur mencari keliling obyek x = xtemp;
y = ytemp;
xstart=x;
ystart=y;
arah = 0;
keliling = 0;
Lampiran 2: Listing Program Pengenalan Obyek (sambungan)
while (carikeliling) {
ketemu = FALSE;
keluar = FALSE;
count = 0;
while (!ketemu && !keluar) {
switch (arah) {
case 0:
xtemp = x + 1;
ytemp = y;
break;
case 1:
xtemp = x + 1;
ytemp = y - 1;
break;
case 2:
xtemp = x;
ytemp = y - 1;
break;
case 3:
xtemp = x - 1;
ytemp = y - 1;
break;
case 4:
xtemp = x - 1;
ytemp = y;
break;
case 5:
xtemp = x - 1;
ytemp = y + 1;
break;
case 6:
xtemp = x;
ytemp = y + 1;
break;
case 7:
xtemp = x + 1;
ytemp = y + 1;
break;
}
if (xtemp>=0 && xtemp<160 && ytemp>=0 &&
ytemp<120)
{
iplGetPixel(temp, xtemp, ytemp, &pixel[0]);
if (pixel[0]==255) {
iplPutPixel(temp2, xtemp, ytemp,
&pixel[0]);
pixel[0]=100;
iplPutPixel(temp, xtemp, ytemp, &pixel[0]);
if (xtemp < xmin) xmin = xtemp;
if (xtemp > xmax) xmax = xtemp;
if (ytemp < ymin) ymin = ytemp;
if (ytemp > ymax) ymax = ytemp;
x = xtemp;
y = ytemp;
if (arah == 0) arah = 1;
else if (arah == 1) arah = 3;
else if (arah == 2) arah = 3;
else if (arah == 3) arah = 5;
else if (arah == 4) arah = 5;
else if (arah == 5) arah = 7;
else if (arah == 6) arah = 7;
else if (arah == 7) arah = 1;
ketemu = TRUE;
keliling++;
}
else if (xtemp==xstart && ytemp==ystart) {
keluar = TRUE;
carikeliling = FALSE;
cariluas = TRUE;
} }
if (!ketemu) {
arah--;
if (arah < 0) arah = 7;
count++;
if (count == 7) {
count = 0;
keluar = TRUE;
carikeliling = FALSE;
} }
} }
Lampiran 2: Listing Program Pengenalan Obyek (sambungan)
// prosedur mencari luas obyek float shape;
luas = 0;
shape = 0;
while (cariluas) {
luas = 0;
for (y=ymin; y<=ymax; y++)
for (x=xmin; x<=xmax; x++) {
iplGetPixel(temp, x, y, &pixel[0]);
if (pixel[0] != 0) {
pixel[0]=255;
iplPutPixel(temp3, x, y, &pixel[0]);
luas++;
} }
cariluas = FALSE;
shape = float(keliling * keliling) / float(luas);
if (shape >= 10 && shape <= 15 && luas > 100) carititikberat = TRUE;
}
// menampilkan nilai keliling, luas, dan shape factor CString value;
value.Format("%d",luas);
SetDlgItemText(IDC_LUAS,value);
value.Format("%d",keliling);
SetDlgItemText(IDC_KELILING,value);
value.Format("%f",shape);
SetDlgItemText(IDC_SHAPE,value);
// prosedur mencari titik berat int sx,sy,n;
while (carititikberat) {
sx = 0;
sy = 0;
n = 0;
for (y=ymin; y<=ymax; y++)
for (x=xmin; x<=xmax; x++) {
iplGetPixel(temp, x, y, &pixel[0]);
if (pixel[0] != 0) {
sx = sx + x;
sy = sy + y;
n = n + 1;
} }
if (n != 0) {
sx = sx / n;
sy = sy / n;
}
CvPoint pt1, pt2;
pt1.x = sx;
pt1.y = sy;
pt2.x = sx;
pt2.y = sy;
cvRectangle(temp4,pt1,pt2,CV_RGB(255,255,255),2);
carititikberat = FALSE;
}
// Menampilkan nilai koordinat posisi obyek value.Format("%d",sx);
SetDlgItemText(IDC_XPOS,value);
value.Format("%d",sy);
SetDlgItemText(IDC_YPOS,value);
// Gambar sensor posisi obyek CvPoint pt1, pt2;
pt1.x = 0;
pt1.y = 0;
pt2.x = 50;
pt2.y = 70;
cvRectangle(temp4,pt1,pt2,CV_RGB(100,100,100),3);
pt1.x = 0;
pt1.y = 70;
pt2.x = 40;
pt2.y = 120;
cvRectangle(temp4,pt1,pt2,CV_RGB(100,100,100),3);
pt1.x = 110;
pt1.y = 0;
pt2.x = 160;
pt2.y = 70;
cvRectangle(temp4,pt1,pt2,CV_RGB(100,100,100),3);
pt1.x = 120;
pt1.y = 70;
pt2.x = 160;
pt2.y = 120;
Lampiran 2: Listing Program Pengenalan Obyek (sambungan)
cvRectangle(temp4,pt1,pt2,CV_RGB(100,100,100),3);
pt1.x = 50;
pt1.y = 55;
pt2.x = 110;
pt2.y = 55;
cvRectangle(temp4,pt1,pt2,CV_RGB(100,100,100),3);
cvvShowImage("Keliling",temp2);
cvvShowImage("Luas",temp3);
cvvShowImage("Posisi Bola",temp4);
// Prosedur penentuan arah gerakan mobile robot if (sy>0 && sy<=55)
{
if (sx>0 && sx<50) //belok kiri com->sendChar(5);
else if (sx>110 && sx<160) //belok kanan com->sendChar(6);
else com->sendChar(2); //maju }
else if (sy>55 && sy<120) {
if (sx>0 && sx<=40) //balik kiri com->sendChar(3);
else if (sx>=120 && sx<160) //balik kanan com->sendChar(4);
else com->sendChar(1); // maju cepat }
else com->sendChar(10);
//cv 14-9
cvReleaseImage(&temp);
cvReleaseImage(&temp1);
cvReleaseImage(&temp2);
cvReleaseImage(&temp3);
cvReleaseImage(&temp4);
cvReleaseImage(&img_calcback_h);
cvReleaseImage(&img_and_sv);
cvReleaseImage(&img_v);
cvReleaseImage(&img_s);
cvReleaseImage(&img_h);
cvReleaseImage(&img_hsv);
} }
void CCamCapDlg::OnStart() {
// TODO: Add your control notification handler code here
IplImage *sample, *sample_hsv, *sample_h, *sample_s, *sample_v;
int histh_dim[1] = {255};
int hists_dim[1] = {255};
int histv_dim[1] = {255};
CvSize size_sample;
//load sample warna switch (m_iColor) {
case 0:
sample = cvvLoadImage("BolaMerah.jpg");
break;
case 1:
sample = cvvLoadImage("BolaKuning.jpg");
break;
}
size_sample.width = sample->width;
size_sample.height = sample->height;
sample_hsv = cvCreateImage(size_sample, IPL_DEPTH_8U, 3);
// Image sample RGB dikonvert ke HSV iplRGB2HSV(sample, sample_hsv);
sample_h=cvCreateImage(size_sample, IPL_DEPTH_8U, 1);
sample_s=cvCreateImage(size_sample, IPL_DEPTH_8U, 1);
sample_v=cvCreateImage(size_sample, IPL_DEPTH_8U, 1);
// Masing-masing channel dipisahkan
cvCvtPixToPlane(sample_hsv, sample_h, sample_s, sample_v, NULL);
// Menghitung nilai histogram dari hue sample float thresh_h[1][2] = { {0, 255} };
float* pthresh_h[1] = { thresh_h[0] };
hist_h = cvCreateHist ( 1, histh_dim, CV_HIST_ARRAY, 0, 1);
cvSetHistBinRanges( hist_h, pthresh_h, 1);
cvCalcHist(&sample_h, hist_h, 0, 0);
// siapkan window
cvvNamedWindow("Display",1);
cvvNamedWindow("Keliling",1);
cvvNamedWindow("Luas",1);
cvvNamedWindow("Posisi Bola",1);
Lampiran 3: Listing Program Pengolahan Image Sample (sambungan)
cvReleaseImage(&sample);
cvReleaseImage(&sample_hsv);
cvReleaseImage(&sample_h);
cvReleaseImage(&sample_s);
cvReleaseImage(&sample_v);
mulai=true;
}