機(jī)床數(shù)控課設(shè)(插補(bǔ))
一、 課設(shè)任務(wù)用DDA法插補(bǔ)第二象限直線。用逐點(diǎn)比較法插補(bǔ)第三到第二象限順圓弧。二、課設(shè)要求 1、具有數(shù)據(jù)輸入界面,有起點(diǎn)、終點(diǎn)、半徑及插補(bǔ)步長等; 2、具有單步插補(bǔ)過程的動(dòng)態(tài)顯示功能; 3、插補(bǔ)的步長可調(diào);三、編程語言Visual Basic四、功能說明本程序用逐點(diǎn)比較法插補(bǔ)第二象限的直線,及第三二象限的順圓弧,可進(jìn)行連續(xù)插補(bǔ)或單步插補(bǔ)。1、直線插補(bǔ):用逐點(diǎn)比較法實(shí)現(xiàn)第二象限任意直線的插補(bǔ),需要輸入起點(diǎn)、終點(diǎn)坐標(biāo)及步長。2、圓弧插補(bǔ):用逐點(diǎn)比較法實(shí)現(xiàn)第三二象限的順圓弧段的插補(bǔ),需要輸入起點(diǎn)、終點(diǎn)坐標(biāo)、半徑和步長。3、插補(bǔ)步長可調(diào)。4、可以單步執(zhí)行所有插補(bǔ)動(dòng)作,單擊一次按鈕執(zhí)行一次插補(bǔ)。五、程序內(nèi)容 DDA法插補(bǔ)第二象限直線1、源程序: Dim A, B, C, D, E, F, G, I, J, E, F As Single*連續(xù)直線插補(bǔ)*Private Sub Command1_Click()If (-C + A) > (D - B) Then F = -C + A Else: F = D - BG = 1Do While (2 G) <= FG = G + 1 LoopFor H = 0 To (2 G) Step 1I = I + (-C + A)x1 = x2If (I / (2 G) >= E Then I = I - E * (2 G): x2 = x2 - EJ = J + (D - B)y1 = y2If (J / (2 G) >= E Then J = J - E * (2 G): y2 = y2 - EPicture1.Line (x2 * Text6.Text, y2 * Text6.Text)-(x1 * Text6.Text, y1 * Text6.Text), vbGreenNext HPicture1.Line (C * Text6.Text, -D * Text6.Text)-(A * Text6.Text, -B * Text6.Text), vbRedEnd Sub*單步直線插補(bǔ)*Private Sub Command2_Click()If (-C + A) > (D - B) Then F = -C + A Else: F = D - BG = 1Do While (2 G) <= FG = G + 1Picture1.Line (C * Text6.Text, -D * Text6.Text)-(A * Text6.Text, -B * Text6.Text), vbRedLoopI = I + (-C + A)J = J + (D - B)If (I / (2 G) >= E And (J / (2 G) >= E ThenPicture1.Line (x2 - E) * Text6.Text, (y2 - E) * Text6.Text)-(x2 * Text6.Text, y2 * Text6.Text), vbGreenI = I - E * (2 G)J = J - E * (2 G)x2 = x2 - Ey2 = y2 - EElseIf (I / (2 G) >= E And (J / (2 G) < E ThenPicture1.Line (x2 - E) * Text6.Text, y2 * Text6.Text)-(x2 * Text6.Text, y2 * Text6.Text), vbGreenI = I - E * (2 G)x2 = x2 - EElseIf (I / (2 G) < E And (J / (2 G) >= E ThenPicture1.Line (x2 * Text6.Text, (y2 - E) * Text6.Text)-(x2 * Text6.Text, y2 * Text6.Text), vbGreenJ = J - E * (2 G)y2 = y2 - EEnd IfEnd Sub3. DDA法插補(bǔ)第二象限直線的流程圖:YNYNYNMm-1XeXeXeYeYeYe初始化xeXe,yeYe,累加次數(shù)mM,Xe、Ye清零M=0嗎?Ye有溢出嗎?Xe有溢出嗎?+Y向走一步-X向走一步結(jié)束逐點(diǎn)比較法插補(bǔ)第三到第二象限順圓弧1. 源程序:Dim X0, Y0, CXY, CD, CX1, CY1, CX2, CY2, CX11, CY11, CX22, CY22, ci, R, a1, b1, c1, d1, e1, x1, y1, x2, y2 As SingleCONST Pi=3.1415926*連續(xù)圓弧插補(bǔ)*Private Sub Command6_Click()d1 = 1 / 2 * (CX22 2 + CY22 2 - CX11 2 - CY11 2) / (CY22 - CY11)e1 = (CX11 - CX22) / (CY22 - CY11)a1 = 1 + e1 2b1 = 2 * d1 * e1 - 2 * CX11 - 2 * CY11 * e1c1 = CX11 2 + CY11 2 + d1 2 - 2 * d1 * CY11 - R 2X0 = (-b1 + Sqr(b1 2 - 4 * a1 * c1) / (2 * a1)Y0 = d1 + e1 * X0 *求得圓心If CX1 <> X0 And CX2 <> X0 Theno = Atn(CY1 - Y0) / (CX1 - X0) + Pi *起始角o1 = Atn(CY2 - Y0) / (CX2 - X0) + Pi *終止角Picture2.Circle (X0, Y0), R, vbRed, o1, oElsePicture2.Circle (X0, Y0), R, vbRed, 2 / Pi, 3 * Pi / 2End If *畫圓 again: *開始插補(bǔ)過程If CY1 < Y0 ThenIf (CX1 - X0) 2 + (CY1 - Y0) 2 <= R 2 ThenPicture2.Line (CX1, CY1)-(CX1 - ci, CY1)CX1 = CX1 - ciElsePicture2.Line (CX1, CY1)-(CX1, CY1 + ci)CY1 = CY1 + ciEnd IfElseIf (CX1 - X0) 2 + (CY1 - Y0) 2 <= R 2 ThenPicture2.Line (CX1, CY1)-(CX1, CY1 + ci)CY1 = CY1 + ciElsePicture2.Line (CX1, CY1)-(CX1 + ci, CY1)CX1 = CX1 + ciEnd IfEnd If If (CX1 - CX2) 2 + (CY1 - CY2) 2 > (CX1 - CX2 + ci) 2 + (CY1 - CY2) 2 Or (CX1 - CX2) 2 + (CY1 - CY2) 2 > (CX1 - CX2 - ci) 2 + (CY1 - CY2) 2 Or (CX1 - CX2) 2 + (CY1 - CY2) 2 > (CX1 - CX2) 2 + (CY1 - CY2 + ci) 2 Or (CX1 - CX2) 2 + (CY1 - CY2) 2 > (CX1 - CX2) 2 + (CY1 - CY2 - ci) 2 ThenGoTo againElseEnd IfEnd Sub*單步圓弧插補(bǔ)*Private Sub Command7_Click()d1 = 1 / 2 * (CX22 2 + CY22 2 - CX11 2 - CY11 2) / (CY22 - CY11)e1 = (CX11 - CX22) / (CY22 - CY11)a1 = 1 + e1 2b1 = 2 * d1 * e1 - 2 * CX11 - 2 * CY11 * e1c1 = CX11 2 + CY11 2 + d1 2 - 2 * d1 * CY11 - R 2X0 = (-b1 + Sqr(b1 2 - 4 * a1 * c1) / (2 * a1)Y0 = d1 + e1 * X0 *求得圓心If CX1 <> X0 And CX2 <> X0 Theno = Atn(CY1 - Y0) / (CX1 - X0) + Pi *終止角o1 = Atn(CY2 - Y0) / (CX2 - X0) + Pi *終止角Picture2.Circle (X0, Y0), R, vbRed, o1, oElsePicture2.Circle (X0, Y0), R, vbRed, 2 / Pi, 3 * Pi / 2End If *畫圓If (CX1 - CX2) 2 + (CY1 - CY2) 2 > (CX1 - CX2 + ci) 2 + (CY1 - CY2) 2 Or (CX1 - CX2) 2 + (CY1 - CY2) 2 > (CX1 - CX2 - ci) 2 + (CY1 - CY2) 2 Or (CX1 - CX2) 2 + (CY1 - CY2) 2 > (CX1 - CX2) 2 + (CY1 - CY2 + ci) 2 Or (CX1 - CX2) 2 + (CY1 - CY2) 2 > (CX1 - CX2) 2 + (CY1 - CY2 - ci) 2 ThenElseGoTo cover *開始插補(bǔ)過程End IfIf CY1 < Y0 ThenIf (CX1 - X0) 2 + (CY1 - Y0) 2 <= R 2 ThenPicture2.Line (CX1, CY1)-(CX1 - ci, CY1)CX1 = CX1 - ciElsePicture2.Line (CX1, CY1)-(CX1, CY1 + ci)CY1 = CY1 + ciEnd IfElseIf (CX1 - X0) 2 + (CY1 - Y0) 2 <= R 2 ThenPicture2.Line (CX1, CY1)-(CX1, CY1 + ci)CY1 = CY1 + ciElsePicture2.Line (CX1, CY1)-(CX1 + ci, CY1)CX1 = CX1 + ciEnd IfEnd Ifcover:End Sub2.變量說明: CX1=圓弧起點(diǎn)的橫坐標(biāo)值,CY1=圓弧起點(diǎn)的縱坐標(biāo)值 CX2=圓弧終點(diǎn)的橫坐標(biāo)值,CY2=圓弧終點(diǎn)的縱坐標(biāo)值 X0=圓弧半徑的橫坐標(biāo)值, Y0=圓弧半徑的縱坐標(biāo)值R=圓弧半徑, ci=圓弧插補(bǔ)的步長a1,b1,c1,d1為計(jì)算圓點(diǎn)的中間變量3.逐點(diǎn)比較法插補(bǔ)第三象限順圓弧程序流程圖NYNY+Y向走一步初始化xeX,yeY,E=NF0?-X向走一步E=0?結(jié)束起始FF-2Y+1FF-2X+1EE-1逐點(diǎn)比較法插補(bǔ)第二象限順圓弧程序流程圖: NYNY+X向走一步初始化xeX,yeY,E=NF0?+Y向走一步E=0?結(jié)束起始FF+2X+1FF+2Y+1EE-1六、程序界面及運(yùn)行結(jié)果DDA插補(bǔ)第二象限直線運(yùn)行界面DDA插補(bǔ)第二象限直線運(yùn)行結(jié)果逐點(diǎn)比較法插補(bǔ)二三象限順圓弧運(yùn)行界面逐點(diǎn)比較法插補(bǔ)二三象限順圓弧運(yùn)行結(jié)果七、課設(shè)中遇到的問題及解決辦法1、在進(jìn)行單步插補(bǔ)時(shí)出現(xiàn)問題,單步插補(bǔ)出不來? 定義的變量應(yīng)為全局變量,通過按鈕來實(shí)現(xiàn)單步操作。按第一次按鈕,程序執(zhí)行一步,按第二次時(shí),程序執(zhí)行兩步,及將第一步做的覆蓋后在多做一步。依此,每次按下按鈕都比前一次多做一步,這樣,作圖時(shí)就表現(xiàn)為單步運(yùn)行。2、在圓弧插補(bǔ)是畫出的圓跟圓弧的插補(bǔ)在起點(diǎn)和終點(diǎn)處不重合? 在畫圖的pitcure屬性中的scaletop和scalewidth的值應(yīng)相等,否則畫出的橫縱坐標(biāo)不等,導(dǎo)致在橫坐標(biāo)和縱坐標(biāo)上的長對不等使得與圓弧不重合。3、如何解決起點(diǎn)不是原點(diǎn)的直線的插補(bǔ) 若是起點(diǎn)不是原點(diǎn)的話,可以將起點(diǎn)平移到原點(diǎn)位置,而終點(diǎn)也相應(yīng)的平移即可,這樣就解決了起點(diǎn)不是原點(diǎn)的問題。根據(jù)這個(gè)思想在判斷象限時(shí)就拿終點(diǎn)坐標(biāo)和起點(diǎn)坐標(biāo)比較來判斷終點(diǎn)屬于哪個(gè)象限。 和插補(bǔ)直線一樣,當(dāng)圓心不是原點(diǎn)時(shí),可以將圓心平移到原點(diǎn)位置,圓上各點(diǎn)相應(yīng)的平移即可。八、心得體會(huì)通過這次的機(jī)床數(shù)控課程設(shè)計(jì),使我加深了對數(shù)控機(jī)床的插補(bǔ)的了解,更好的掌握了逐點(diǎn)比較法的插補(bǔ)原理。這次課程設(shè)計(jì)選擇了Visual Basic作為編程語言,由于是初次接觸,不能熟練得掌握,所以使用起來不是很順利,對整體進(jìn)度有一些影響。通過這次課程設(shè)計(jì),增加了知識(shí),鍛煉了自己的實(shí)際動(dòng)手和操作能力,對以后的學(xué)習(xí)有著深遠(yuǎn)的影響。九、參考資料 陸慰民. 2000 . VisualBasic程序設(shè)計(jì)教程(6.0版). 北京:高等教育出版社 張勇. 2002 . VisualBasic課程設(shè)計(jì)案例精編. 北京:中國水利水電出版社 段興. 2002. VisualBasic實(shí)用程序100例. 北京:人民郵電出版社