⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 一个雕刻机的源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
MainForm->ExecuteButton->Enabled = true;
MainForm->StartButton->Enabled = true;
MainForm->StepButton->Enabled = true;
MainForm->OpenButton->Enabled = true;
MainForm->ResetButton->Enabled = true;
controlsenabled = 1;
}
//---------------------------------------------------------------------------
void DisableMotionControls()
{
MainForm->XPlusButton->Enabled = false;
MainForm->YPlusButton->Enabled = false;
MainForm->ZPlusButton->Enabled = false;
MainForm->XMinusButton->Enabled = false;
MainForm->YMinusButton->Enabled = false;
MainForm->ZMinusButton->Enabled = false;
MainForm->ExecuteButton->Enabled = false;
MainForm->StartButton->Enabled = false;
MainForm->StepButton->Enabled = false;
MainForm->ResetButton->Enabled = false;
controlsenabled = 0;
}
//---------------------------------------------------------------------------
//Compensates for current tool length
int StartRapid(float x, float y, float z)
{
long int pos, vel;
byte mode;

prevrx = x; prevry = y; prevrz = z;

mode = LOAD_POS | LOAD_VEL | ENABLE_SERVO | START_NOW;

vel = (long int)(0x10000*rapidoverride*xscale*xmaxvel/(1953.12*100));
if (vel<0) vel= -vel;
pos = (long int)( xscale*(x + xorg));
ServoLoadTraj(xaxis, mode, pos, vel, 0, 0);

vel = (long int)(0x10000*rapidoverride*yscale*ymaxvel/(1953.12*100));
if (vel<0) vel= -vel;
pos = (long int)( yscale*(y + yorg));
ServoLoadTraj(yaxis, mode, pos, vel, 0, 0);

vel = (long int)(0x10000*rapidoverride*zscale*zmaxvel/(1953.12*100));
if (vel<0) vel= -vel;
pos = (long int)( zscale*( z + zorg + toollen[toollennum] ));
ServoLoadTraj(zaxis, mode, pos, vel, 0, 0);

opmode = RAPID;

return(0);
}
//---------------------------------------------------------------------------
//Display G code in list box with line n selected
void DisplayGCode(int n)
{
int i, s, f, sel;  //starting line, finishing line, selected

MainForm->GList->Items->Clear();

sel = 3;

if (n>=3) s = n-3;
else
  {
  s = 0;
  sel = n;
  }

f = s+7;
if (f>numlines) f = numlines;

for (i=s; i<f; i++)
  MainForm->GList->Items->Add( &(gcode[line[i]]) );

MainForm->GList->ItemIndex = sel;  //Select the first line
}
//---------------------------------------------------------------------------
//
// G-Code Functions
//
//---------------------------------------------------------------------------
//Returns -1 if file not found, -2 if too many chars, -3 if too many lines
int ReadGCodeFile(char *name)
{
FILE *gfile;
int i, testchar;

i = 0;                //char counter
numlines = 1;         //line counter
line[0] = 0;

if ( (gfile=fopen(name,"r")) == NULL ) return(-1);

Screen->Cursor = crHourGlass;

while (1)
  {
  testchar = fgetc(gfile);
  if (testchar == '\r') gcode[i] = '\0';      //convert CR to null
  else if (testchar == '\n') gcode[i] = '\0'; //convert LF to null
  else if (testchar == EOF)                   //convert EOF to null & punt
    {
    gcode[i] = '\0';
    break;
    }
  else
    {
    gcode[i] = (char)testchar;
    if (i>0 && gcode[i-1] == '\0')
      {
      line[numlines] = i;
      numlines++;
      }
    }

  i++;
  if (i >= MAXCHARS) return(-2);
  if (numlines >= MAXLINES) return(-3);
  } //END while 1

fclose(gfile);

Screen->Cursor = crDefault;

return(0);
}
//---------------------------------------------------------------------------
void SetLimitedFeedrate(float fr)
{
if (fr>maxfr) fr = maxfr;
SetFeedrate(fr);
}
//---------------------------------------------------------------------------
//Scan a gcode string and fill in the global tokens
//Returns  0 on success
//		   1 on success & some code causes a break in a continuous path
//        -4 on syntax error
//
int TokenScan(char *codestr)
{
unsigned int i, numtok;
int tokloc[40];  //token locations
int gcode;
int pathbreak;
int commentmode;

pathbreak = 0;
commentmode = 0;

//Scan for letter codes:
numtok = 0;
for (i=0; i<strlen(codestr); i++)
  {
  if ( codestr[i] == '(' ) commentmode++;
  if ( codestr[i] == ')' ) commentmode--;
  if ( codestr[i]>='A' && codestr[i]<='Z' && !commentmode )
    {
    tokloc[numtok] = i;
    numtok++;
    }
  }

//Fill in the global values
for (i=0; i<numtok; i++)
  {
  switch (codestr[tokloc[i]])
  	{
    case 'G':   if (sscanf(codestr+tokloc[i]+1,"%d",&gcode) != 1) return(-4);
    			//assign the gcode to the proper group
    			switch (gcode)
                  {
                  case 4:  g0code = gcode;    //dwell
                  		   pathbreak = 1;
                  		   break;
                  case 0:  pathbreak = 1;     //rapid
                  case 1:                     //lines & arcs
                  case 2:
                  case 3:  g1code = gcode;
                  		   break;
                  case 17:                    //set arc planes
                  case 18:
                  case 19: g2code = gcode;
                  		   break;
                  case 43: g8code = gcode;    //set tool offset
                  		   pathbreak = 1;
                  		   break;
                  }
    			break;
    case 'M':   if (sscanf(codestr+tokloc[i]+1,"%d",&mcode) != 1) return(-4);
    			pathbreak = 1;
    			break;
    case 'X':   if (sscanf(codestr+tokloc[i]+1,"%f",&gx) != 1) return(-4);
    			if (g1code == -1) g1code = oldg1code;
    			break;
    case 'Y':   if (sscanf(codestr+tokloc[i]+1,"%f",&gy) != 1) return(-4);
    			if (g1code == -1) g1code = oldg1code;
    			break;
    case 'Z':   if (sscanf(codestr+tokloc[i]+1,"%f",&gz) != 1) return(-4);
    			if (g1code == -1) g1code = oldg1code;
                dgz = 1;  //flag change in z coord.
    			break;
    case 'I':   if (arcmode == ARC_R) return(-4);
    			if (sscanf(codestr+tokloc[i]+1,"%f",&arci) != 1) return(-4);
                arcmode = ARC_IJK;
    			break;
    case 'J':   if (arcmode == ARC_R) return(-4);
    			if (sscanf(codestr+tokloc[i]+1,"%f",&arcj) != 1) return(-4);
                arcmode = ARC_IJK;
    			break;
    case 'K':   if (arcmode == ARC_R) return(-4);
    			if (sscanf(codestr+tokloc[i]+1,"%f",&arck) != 1) return(-4);
                arcmode = ARC_IJK;
    			break;
    case 'R':   if (arcmode == ARC_IJK) return(-4);
    			if (sscanf(codestr+tokloc[i]+1,"%f",&arcr) != 1) return(-4);
                arcmode = ARC_R;
    			break;
    case 'P':   if (sscanf(codestr+tokloc[i]+1,"%f",&dwelltime)!= 1) return(-4);
    			break;
    case 'F':   if (sscanf(codestr+tokloc[i]+1,"%f",&fripm) != 1) return(-4);
    			pathbreak = 1;
    			break;
    case 'T':   if (sscanf(codestr+tokloc[i]+1,"%d",&ptoolnum) != 1) return(-4);
    			break;
    case 'H':   if (sscanf(codestr+tokloc[i]+1,"%d",&ptoollennum) != 1) return(-4);
    			break;
    } //END switch
  } //END fill in global values

return(pathbreak);
}
//---------------------------------------------------------------------------
//Process arc using current interpreter globals, and adds it to the segment
//list.  Resets the g1code to -1 on completion.
//Returns 0 on success
//       -1 if not tangent to prev. segment
//       -2 if segment list is full
//		 -3 if arc data invalid
//
int ProcessArc()
{
int res;

float xc, yc, zc;  //arc center point

if (arcmode == ARC_IJK)
  {
  xc = oldgx + arci;
  yc = oldgy + arcj;
  zc = oldgz + arck + toollen[toollennum];
  }
else if (arcmode == ARC_R)
  {
  //Not yet supported
  return(-3);
  }
else return(-3);

if (g1code == 3)
  res = AddArcSeg( gx, gy, gz+ toollen[toollennum],   //end point
                   xc, yc, zc,     					  //center point
           		   arcnormx, arcnormy, arcnormz );    //normal
else if (g1code == 2)
  res = AddArcSeg( gx, gy, gz+ toollen[toollennum],   //end point
                   xc, yc, zc,     					  //center point
           		   -arcnormx, -arcnormy, -arcnormz );    //normal
else return(-3);

if (res < 0) return(res);  //pass thru error code on failure
else return(0);
}
//---------------------------------------------------------------------------
//codestr = null terminated string with G Codes
//append = flag to append segment to existing path
//	if append = 0, clear seg list, add segment, and then execute
//	if append = 1,
//		if not pathappendmode, clear seg list and set pathappend mode
//		add segment
//		if segment not tangent or if feedrate change
//			then execute prev path, clear pathappendmode
//      if mcode, execute gcodes, clear pathappendmode
//Returns 0 on successful execution
//		 -1 Break in continuous path (due to tangency error or mode change)
//       -2 Segment list is full
//       -3 invalid arc data or other data
//       -4 invalid syntax
int ExecuteGCode(char *codestr, int append)
{
int res;
int pathbreak;

//Null out non-modal global data:
arcmode = ARC_NONE;
arci = 0.0;
arcj = 0.0;
arck = 0.0;
arcr = 0.0;
dgz = 0;     //change in z coord.
fripm = -1.0;
mcode = -1;
g0code = -1;
oldg1code = g1code;
g1code = -1;
g2code = -1;
g8code = -1;
oldgx = gx; oldgy = gy; oldgz = gz;

//Fill in the global data for the interpreter:
res = TokenScan(codestr);
if (res < 0) return(res);
pathbreak = res;

//If pathbreak encountered while building a path, start execution of the path
//and exit without further processing
if (pathbreak && pathappendmode)
  {
  InitPath();
  opmode = PATH;
  pathappendmode = 0;
  return(-1);
  }

//Process feedrate change
if (fripm>0.0)
  {
  feedrate = fripm/60.0;
  SetLimitedFeedrate(feedrate*froverride/100);
  }

//Process the G-Codes
//Process g8
if (g8code == 43)
  {
  if (ptoollennum<1 || ptoollennum>MAXTOOLS) return(-3);
  //Adjust the default Z coordinate by the difference in tool heights
  //only adjust the current gz if it has not changed from the prev. block
  //ie, gz is interpreted for the new tool length if set in the same block as G43
  if (!dgz) gz -= (toollen[ptoollennum-1] - toollen[toollennum]);
  oldgz -= (toollen[ptoollennum-1] - toollen[toollennum]);
  toollennum = ptoollennum-1;
  toolchanged = 1;
  }

//Process g2
if (g2code == 17)
  {
  arcnormx = 0.0;
  arcnormy = 0.0;
  arcnormz = 1.0;
  }
else if (g2code == 18)
  {
  arcnormx = 0.0;
  arcnormy = 1.0;
  arcnormz = 0.0;
  }
else if (g2code == 19)
  {
  arcnormx = 1.0;
  arcnormy = 0.0;
  arcnormz = 0.0;
  }

//Process g0
if (g0code == 4)     //process dwell as an M code
  {
  if (mcode != -1) return(-4);  //flag error if already an  M code
  mcode = 95;				    //95 = mcode for a HAAS dwell
  }

//Process g1 code
if (g1code == 0)      //process a rapid move
  {
  StartRapid(gx, gy, gz);   //tool length handled by StartRapid
  return(0);
  }
else if (g1code == 1 || g1code == 2 || g1code == 3)  //process a coordinated G01
  {
  if (feedrate == 0.0) return(-5);

  if (!pathappendmode)    //if path not initialized, start at prev. command pos.
    {
    cmdx = (float)(ServoGetPos(xaxis) + ServoGetPError(xaxis))/xscale - xorg;
    cmdy = (float)(ServoGetPos(yaxis) + ServoGetPError(yaxis))/yscale - yorg;
    cmdz = (float)(ServoGetPos(zaxis) + ServoGetPError(zaxis))/zscale - zorg;
    ClearSegList(cmdx, cmdy, cmdz);
    }

  if ( append == 0 )  //runs segments now
    {
    if (g1code == 1)
      res = AddLineSeg(gx, gy, gz + toollen[toollennum]);  //append line segment
    else
      res = ProcessArc();                                  //or append arc segment

    if ( res < 0 )    //punt on error
      	return(res);
	InitPath();
	opmode = PATH;
    pathappendmode = 0;
    return(0);
    }
  else     //appends seg to path
    {
    if (g1code == 1)
      res = AddLineSeg(gx, gy, gz + toollen[toollennum]);  //append line segment
    else
      res = ProcessArc();                                  //or append arc segment

    if (res < -1)       //on real error, punt
      	return(res);

    if (res ==-1 || pathbreak)   //if segment not tangent, execute the path
      {
	  InitPath();
	  opmode = PATH;
      pathappendmode = 0;
      if (res == -1)    //put gx, gy, gz back where they were
        {
        gx = oldgx;
        gy = oldgy;
        gz = oldgz;
        }
      return(res);         //returns 0 if a path break, -1 for not tangent
      }

    pathappendmode = 1;
    }
  }  //END G01

return(0);
} //END ExecuteGCode
//---------------------------------------------------------------------------
int ExecuteMCode(int mcode)
{
char msgstr[80];

switch (mcode)
  {
  case 0:	rungcode = 0;		//Halt execution of the program
  			break;
  case 3:	rungcode = 0;
  			SimpleMsgBox("Turn on spindle");
            break;
  case 5:   rungcode = 0;
  			SimpleMsgBox("Turn off spindle");
            break;
  case 6:   rungcode = 0;
    		if (ptoolnum<1 || ptoolnum>MAXTOOLS)
              {
              SimpleMsgBox("Invalid tool number");
              return(-3);
              }
  			sprintf(msgstr,"Change tool to T%d",ptoolnum);
  			SimpleMsgBox(msgstr);
			toolnum = ptoolnum-1;
  			toolchanged = 1;
            break;
  case 21:	MainForm->ContouringCB->Checked = true;	 //turn on contouring
  			break;
  case 22:	MainForm->ContouringCB->Checked = false; //turn off contouring
  			break;
  case 30:	rungcode = 0;		//Halt execution of the program
			curline = 0;
			DisplayGCode(0);
  			break;
  case 95:	dwellcounts = (dwelltime*1000)/MainForm->PathTimer->Interval;
  			break;
  default:	rungcode = 0;
  			sprintf(msgstr,"Unrecognized M code: %d", mcode);
            SimpleMsgBox(msgstr);
  }

return(0);
}
//---------------------------------------------------------------------------
void DisplayErrorMsg(int n)
{
switch (n)
  {
  case -2: SimpleMsgBox("Too many segments in a continuous path");
  		   break;
  case -3: SimpleMsgBox("Invalid arc data");
  		   break;
  case -4: SimpleMsgBox("Syntax error");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -