Code Software trên C# chường trình điều khiểm máy CNC 3D public SoundPlayer ketthuc = new SoundPlayer"hoanthanh.wav"; private void groupBox1_Enterobject sender, EventArgs e TTA1.Clear;
Trang 1P.1 Code Software trên C# chường trình điều khiểm máy CNC 3D
public SoundPlayer ketthuc = new SoundPlayer("hoanthanh.wav");
private void groupBox1_Enter(object sender, EventArgs e)
TTA1.Clear(); TTA2.Clear(); TTA3.Clear();
TTB1.Clear(); TTB2.Clear(); TTB3.Clear();
Trang 3// ***** Gửi dữ liệu nội suy cung tròn*************
private void guict_Click(object sender, EventArgs e)
//********************** Xóa dữ liệu cung tròn*************
private void xoact_Click(object sender, EventArgs e)
Trang 4string q;
if (LbStatus.Text == "Connect")
{
if (bluZ.Text == "" || bluX.Text == "" || bluY.Text == "" )
{ MessageBox.Show("Chưa có dữ liệu!", "Thông Báo"); }
Trang 5}
// ************** Nội Suy Cung Tròn ****************************** private void noisuycungtron(float X0, float Y0, float Xf, float Yf, string chieu) {
float BLU = 1;
float[] mangx = new float[1000];
float[] mangy = new float[1000];
float Axf,Ayf,Ax,Ay,D;
int dem1 = 0;
if (chieu == "1") { mangy[0] = Y0; mangx[0] = X0; }
if (chieu == "2") { mangy[0] = Yf; mangx[0] = Xf; }
//////////////////////////////////////////////////////////////////////
if (X0 >= Xf) { Axf = X0; } else Axf = Xf;
if (Y0 >= Yf) { Ayf = Y0; } else Ayf = Yf;
mangy[dem1 + 1] = mangy[dem1] - BLU;
mangx[dem1 + 1] = mangx[dem1]; dem1++;
mangy[dem1 + 1] = mangy[dem1] + BLU;
mangx[dem1 + 1] = mangx[dem1]; dem1++;
}
}
} while (Ayf != 0);
noisuyx.Clear(); noisuyy.Clear(); noisuyz.Clear(); STT.Clear();
for (int j = 0; j < dem1; j++)
Trang 6trucY = xungy[net];
trucZ = xungz[net];
Com.Write("?");
//************************ Nội Suy tuyến tính********************
private void noisuytuyentinh(float a1, float a2, float a3, float b1,float b2, float b3,float f) {
// Noi suy tuyen tinh
float[] MA = new float [n];
float[] MB = new float[n];
float[] MC = new float[n];
float A=a1; float B=a2; float C=a3;
Trang 7}
string[] xungx = { "X-8", "X+3200","X+6144", "X+0", "X+0", "X-0" };
string[] xungy = { "Y+6", "Y+3200","Y-4096", "Y+0", "Y+0", "Y+0" };
string[] xungz = { "Z-7", "Z+3200","Z+0", "Z+2049", "Z+2052", "Z+2055" }; lapthuN = "L100";
char ch = mangkytu[i]; //đọc ký tự thứ i và gán vào biến ch
string linh = ch.ToString(); //ep char sang string
Trang 8if (c == "&") { kj++; m = 0; dem++; truyengcode(); if (kj == 3) { txtnhan.Clear();
if (it <= tangdem) { Com.Write(CT[it]); }
if (it > tangdem) { Com.Write("g"); it = 1; tangdem = 1; }
it++; vtz.Text = it.ToString();
Trang 9#region di chuyen tro
public void fcus(int vitri)
Trang 13//************** Ham xu ly vi tri hien tai dong co***************
private void bluZ_TextChanged(object sender, EventArgs e)
OpenFileDialog gcode = new OpenFileDialog();
private void btnopen_Click(object sender, EventArgs e)
{
gcode.Title="OPEN GCODE";
gcode.InitialDirectory = @"Desktop";
Trang 14// chuyen file gcode thành đọc từng hàng ghép vào textbox xulygcode
int i, dem = 1, dems1 = 0;
private void docgcode(int solan)
if (dems1 < 10) { dems1++; goto ll; }
if (dems1 == 10) { xulygcode.Text = "End;"; dem = 1; TDUNG = 1; MessageBox.Show("Hoàn Thành Đọc Gcode !"); break; }// reset lai dem và thoát
}} }
int ij;
private void tachcode(string code)
Trang 15lenh.Clear();// xóa mã lệnh trước
lenh.Text = malenh;// hiển thị mã lệnh lên màng hình
for (ij = nho+1; ij <code.Length; ij++)// tách tọa độ
{
matoado = matoado + mangcode[ij].ToString();// ghép tọa độ
}
toado.Clear();// xóa tọa độ gốc
toado.Text = matoado;// hiển thị tọa độ mới
if (malenh != "S") { tachtoado(matoado); } // chuyển tọa độ đi tách thành tọa độ các trục
}
private void magcode_TextChanged(object sender, EventArgs e)
{ }
// *****************Hàm tách tọa độ thành tọa độ các trục*************** private void tachtoado(string td)
if (demtd == 1) { nhox = it; }
if (demtd == 2) { nhoy = it; }
if (demtd == 3) { nhoi = it; }
}
Trang 18float driveryf = float.Parse(drivery.Text);
float driverzf = float.Parse(driverz.Text);
float driverxf = float.Parse(driverx.Text);
float driveryf = float.Parse(drivery.Text);
float driverzf = float.Parse(driverz.Text);
minstepx.Text = ((vmxf / 360F) * (stxf / driverxf)).ToString();
minstepy.Text = ((vmyf / 360F) * (styf / driveryf)).ToString();
minstepz.Text = ((vmzf / 360F) * (stzf / driverzf)).ToString();
// Dich chuyen truc Z
float ccp = float.Parse(CCP.Text) / float.Parse(minstepx.Text);
Trang 19float c = float.Parse(TTB3.Text);
float Tipo;
float Vf = 10; // chương trinh offset
// do dai duong thang AB
double L = Math.Sqrt(Math.Abs(a - x) * Math.Abs(a - x) + Math.Abs(b - y) * Math.Abs(b - y) + Math.Abs(c - z) * Math.Abs(c - z));
// Gửi dữ liệu đi tính xung cho mỗi trục động cơ
tinhxung(Ax, Ay, Az, N);
}
//Hàm tính xung cho mỗi trục sau nội suy
private void tinhxung(double x1, double y1, double z1, int lanlapN)
float minx = float.Parse(minstepx.Text);
float miny = float.Parse(minstepy.Text);
float minz = float.Parse(minstepz.Text);
double ttx = (x1 / minx); if (ttx < 0) { ttx = Math.Abs(ttx); nhox = 1; } int ttxn = (int)ttx;
double tty = (y1 / miny); if (tty < 0) { tty = Math.Abs(tty); nhoy = 1; } int ttyn = (int)tty;
// double ttz = (z1 / minz); if (ttz < 0) { ttz = Math.Abs(ttz); nhoz = 1; } int ttzn = (int)ttz;
if(ttxn>0){ solanthieux=lanlapN/ttxn;}
if(ttyn>0){solanthieuy = lanlapN / ttyn;}
thieux = (ttx - ttxn) * solanthieux;
Trang 20// vtx.Text = thieux.ToString();
thieuy = (tty - ttyn) * solanthieuy;
// vtx.Text = thieuy.ToString();
if (thieux >= thieuy) { lanlapN = (lanlapN + (int)thieux); }
if (thieuy > thieux) { lanlapN = (lanlapN + (int)thieuy); }
// chằn tọa độ x,y,z, vào
if (nhox==0) { trucX = "X+" + ttxn.ToString(); }
if (nhox==1) { trucX = "X-" + ttxn.ToString(); }
if (nhoy==0) { trucY = "Y+" + ttyn.ToString(); }
if (nhoy==1) { trucY = "Y-" + ttyn.ToString(); }
// if (nhoz==0) { trucZ = "Z+" + ttzn.ToString(); }
// if (nhoz==1) { trucZ = "Z-" + ttzn.ToString(); }
if ((xd0==1)&&(xd1==0)) { trucZ = "Z+" + chieusau.ToString(); }
if ((xd0 == 0) && (xd1 == 1)) { trucZ = "Z-" + chieusau.ToString(); }
float Vf = 10; // chương trinh offset
// do dai duong thang AB
double L = Math.Sqrt(Math.Abs(a - x) * Math.Abs(a - x) + Math.Abs(b - y) * Math.Abs(b - y) + Math.Abs(c - z) * Math.Abs(c - z));
Trang 21// Do dai dich chuyen cua moi truc
// Gửi dữ liệu đi tính xung cho mỗi trục động cơ
tinhxung(Ax, Ay, Az, N);
Trang 22if (Y0 > Yf) { GOC = 1; }
if (Y0 < Yf) { GOC = 2; }
}
if (Xf < X0)
{
if (Yf < Y0) { GOC = 4; }
if (Yf > Y0) { GOC = 3; }
Trang 23Xj = X0 + Xi; //vty.Clear(); vty.Text += Xj;
Yj = Y0 + Yi; //vtz.Clear(); vtz.Text += Yj;
Trang 24double R = Math.Sqrt(Math.Pow(X0 - Xj, 2) + Math.Pow(Y0 - Yj, 2)); X0 = X0 - Xj;
if (Yf < Y0) { GOC = 4; }
if (Yf > Y0) { GOC = 3; }
Trang 25// Nội suy cung tròn ngược chiều kim đồng hồ private void G03()
Xj = X0 + Xi; vty.Clear(); vty.Text += Xj;
Yj = Y0 + Yi; vtz.Clear(); vtz.Text += Yj;
Trang 26double R = Math.Sqrt(Math.Pow(X0 - Xj, 2) + Math.Pow(Y0 - Yj, 2)); X0 = X0 - Xj;
if (Yf < Y0) { GOC = 2; }
if (Yf > Y0) { GOC = 1; }
Trang 27// Ham dua dao ve toa do x0y0z0
private void btG0_Click(object sender, EventArgs e) {
if (LbStatus.Text == "Connect") { Com.Write("O"); } else
MessageBox.Show("Bạn chưa kết nối cổng Com !"); }
// Hàm điều khiển tốc độ động cơ trục chính
Trang 28tangbiendem++; TTA1.Text = TTB1.Text; TTA2.Text = TTB2.Text; TTA3.Text =
TTB3.Text; } // tuyen tinh
if (lenh.Text == "G1" || lenh.Text == "G01") { xd0 = 0; xd1++; G01();
tangbiendem++; TTA1.Text = TTB1.Text; TTA2.Text = TTB2.Text; TTA3.Text =
TTB3.Text; }// tuyen tính ăn dao
if (lenh.Text == "G02") { xdct = 0; if (xd0 != 0) { xdct = 1; xd0 = 0; xd1 = 1; Com.Write("r"); } G02(); tangbiendem++; TTA1.Text = CTA1.Text; TTA2.Text =
CTA2.Text; }// nọi suy cung tròn theo chiều kim đồng hồ
if (lenh.Text == "G03") { xdct = 0; if (xd0 != 0) { xdct = 1; xd0 = 0; xd1 = 1; Com.Write("r"); } G03(); tangbiendem++; TTA1.Text = CTA1.Text; TTA2.Text =
CTA2.Text; } // nội suy cung tròn ngược chiều kim đồng hồ
if (lenh.Text == "S") { Spindle.Text = toado.Text; dongcotrucchinh();
Com.Write("F"); tangbiendem++; }// tốc độ quay động cơ trục chính
if (lenh.Text == "M02") { tangbiendem++; }// reset thông số veef ban đầu
if (lenh.Text == "M03") { tangbiendem++; Com.Write("T"); }// động cơ trục chính theo chiều kim đồng hồ
if (lenh.Text == "M05") { tangbiendem++; Com.Write("t"); }// dừng động cơ trục chính
if (lenh.Text == "M30") { TDUNG = 1; ketthuc.Play(); MessageBox.Show(" Chạy Xong mã code-Kết thúc gia công"); Com.Write("^"); }// kết thúc chương trình
}
// bắt đầu chạy chương trình
private void star_Click(object sender, EventArgs e)
// Truyền Gcode co vi ddieu khiển
private void truyengcode()
{
CL: if (TDUNG == 0)// nếu đã nhấn star
{
docgcode(dem); // doc Gcode
chaymalenh(); // doc mã lệnh & chạy chuong trình
Trang 29if (tangbiendem == 0) // sau khi đọc mã lệnh nếu không pải mã thuwck thi thì tăng biến đêm và doc tiep
Trang 31{
} } }