淺談 G-Code 產生器
窮忙一陣子後今天終於可偷閒寫一下文章跟大家分享一下上回談的G-Code產生器了. 我用最簡單的CNC圓形切割來圖文解釋一下, 相信應該可以很快了解其中運作原理.
先解釋一下Entity名詞, Entity在CAD工程上是實體的意思, AutoCAD出圖後的元件我們稱為Entity, 如圓弧, 直線, 雲行線等皆是實體. 上回提過由DWG/DXF檔案 經過程式剖析分解後產生一連串Entity的資料, 如圖一, 是圓的Entity, 將它送入GCodeCircle(Entity) 函式就可很快地產生切圓的G-Code, 下面是GCodeCircle(Entity)程式範例:
//-------------------------------------------------------------------------
public string GCodeCircle(EntityRecord entityrecord)
{
string gCode = "";
Point3D center = new Point3D(entityrecord.Circle.Center.X, entityrecord.Circle.Center.Y, entityrecord.Circle.Center.Z);
double radius = entityrecord.Circle.Radius;
Point3D point = new Point3D(0, 0, 0);
point.X = center.X + radius;
point.Y = center.Y;
gCode += "G00 X" + (point.X - center.X).ToString("#0.0000") + " " + "Y"+ (point.Y - center.Y).ToString("#0.0000") + " " + "Z5.0000"+ "\n";
//此段是為了說明已經簡化許多參數
gCode += "G01" + " " + "Z-1.0000"+ " " + "F200"+ "\n";
gCode += "G02" + " " + "I-"+ radius.ToString("#0.0000");
return gCode;
}
圖二是產生的G-Code, 可直接送去機器做切割一個100mm的正圓.
另外之前有位朋友問我5軸聯動切向跟隨實作問題, 我列出部分程式碼給您參考一下, 基本上我大部分實作都是圓弧直線插補ArcLine()就可完成.
//---------------------------------------------------------------------------
//圓弧直線跟隨插補
//pos : Z軸直線運動位置
//axisNum = 3, 圓弧+直線運動軸數 X Y Z
public boolArcLine(int startx, int starty, int endx, int endy, int cx, intcy, int[] pos, int[] axis, int dir = 0, int axisNum = 3, double acc = 10, doubletgvel = 100, double endvel = 0, double feedRate = 1.0, int wait = 1, intfifo = (int)FIFO_SEL.SEL_PFIFO1, boolbAbs = true)
{
int st;
if(axisNum < 2 || axisNum > MAX_NAXIS)
return false;
//清空PFIFO
st = IMC_Pkg.PKG_IMC_PFIFOclear(gHandle, fifo);
if(st == 0)
return false;
//設置加速度和進給率
if(SetPFIFO(acc, feedRate, fifo) == false)
return false;
//映射軸
st = IMC_Pkg.PKG_IMC_AxisMap(gHandle, axis, axisNum, fifo);
if(st == 0)
return false;
double dx,dy;
dx = startx - cx;
dy = starty - cy;
double r1 = Math.Sqrt(dx * dx + dy * dy);
dx = endx - cx;
dy = endy - cy;
double r2 = Math.Sqrt(dx * dx + dy * dy);
//判斷, 如果起點到圓心距離r1 != 終點到圓心距離r2 表示圓弧軌跡不正確, 跳出不執行
if(r1 != r2)
return false;
//由當前位置移動到指定位置
if(bAbs) //絕對位置
st = IMC_Pkg.PKG_IMC_ArcLine_Pos(gHandle, endx, endy, cx, cy, dir, pos, axisNum - 2, tgvel, endvel, wait, fifo); //絕對位置
else
st = IMC_Pkg.PKG_IMC_ArcLine_Dist(gHandle, endx, endy, cx, cy, dir, pos, axisNum - 2, tgvel, endvel, wait, fifo); //相對位置
if(st == 0)
return false;
return true;
}
圖三是我執行雷射切割的機台, 雷射頭是5.5W藍光雷射, 下回再分享此G-Code雷射切割成果.
p.s. 在我的APP程式直接畫圖我是直接呼叫運動卡驅動CNC, 沒有經過G-Code產生器這道手續, 加快CNC處理速度, 除非是匯入DWG/DXF檔案才會呼叫G-Code 產生器。
黃小法
2019-08-14 14:25:41
Jade Yang
2019-08-14 15:07:20
無私 , 給你讚。字串相加可改用 $”{}”你以後看才不會累