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

📄 main.cpp

📁 一个雕刻机的源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
  		   break;
  case -5: SimpleMsgBox("No feedrate specified");
  		   break;
  }
}
//---------------------------------------------------------------------------
void ComError()
{
MainForm->PathTimer->Enabled = false;
SimpleMsgBox("NMC Communications Error!");
MainForm->Close();
}
//---------------------------------------------------------------------------
int AnyKey()
{
int i, retval;

retval = 0;
for (i=0; i<256; i++)
 if (GetAsyncKeyState(i) & 1) retval = 1;

return(retval);
}
//---------------------------------------------------------------------------
int HomeMill()
{
TMouseButton Button = mbLeft;
TShiftState Shift;

Screen->Cursor = crAppStart;
HomeForm->Show();
HomeForm->Repaint();
AnyKey();

MainForm->AllOnButtonClick(NULL);  //First enable all servos

NmcNoOp(xaxis);  	//get the current switch states
NmcNoOp(yaxis);
NmcNoOp(zaxis);

//
//  First do Z axis homing:
//
//first home in the plus direction if not on limit switch
if ( !(NmcGetStat(zaxis) & LIMIT1) )
  {
  ServoSetHoming(zaxis, ON_LIMIT1 | HOME_STOP_ABRUPT);
  MainForm->ZPlusButtonMouseDown(NULL, Button, Shift, 0, 0);  //move in plus Z direction
  do  //Wait for homing to complete
    {
    if (!NmcNoOp(zaxis)) ComError();
    if (AnyKey()) return(-1);
    }
  while ( (NmcGetStat(zaxis)&HOME_IN_PROG) && (ServoGetAux(zaxis)&SERVO_ON) );
  }

if ( !(ServoGetAux(zaxis) & SERVO_ON) )   //make sure we didn't hit a stop
  {
  SimpleMsgBox("Homing procedure failed!");
  MainForm->Close();
  }

if ( NmcGetStat(zaxis) & LIMIT1 )     //home in the minus direction if on limit
  {
  ServoSetHoming(zaxis, ON_LIMIT1 | HOME_STOP_SMOOTH);
  MainForm->ZMinusButtonMouseDown(NULL, Button, Shift, 0, 0);  //move in minus Z direction
  do  //Wait for homing to complete
    {
    if (!NmcNoOp(zaxis)) ComError();
    if (AnyKey()) return(-1);
    }
  while ( (NmcGetStat(zaxis)&HOME_IN_PROG) && (ServoGetAux(zaxis)&SERVO_ON) );
  }
ServoResetRelHome(zaxis);

//
//  X axis homing:
//
//first home in the plus direction if not on limit switch
if ( !(NmcGetStat(xaxis) & LIMIT1) )
  {
  ServoSetHoming(xaxis, ON_LIMIT1 | HOME_STOP_ABRUPT);
  MainForm->XPlusButtonMouseDown(NULL, Button, Shift, 0, 0);  //move in plus X direction
  do  //Wait for homing to complete
    {
    if (!NmcNoOp(xaxis)) ComError();
  	if (AnyKey()) return(-1);
    }
  while ( (NmcGetStat(xaxis)&HOME_IN_PROG) && (ServoGetAux(xaxis)&SERVO_ON) );
  }

if ( !(ServoGetAux(xaxis) & SERVO_ON) )   //make sure we didn't hit a stop
  {
  SimpleMsgBox("Homing procedure failed!");
  MainForm->Close();
  }

if ( NmcGetStat(xaxis) & LIMIT1 )     //home in the minus direction if on limit
  {
  ServoSetHoming(xaxis, ON_LIMIT1 | HOME_STOP_SMOOTH);
  MainForm->XMinusButtonMouseDown(NULL, Button, Shift, 0, 0);  //move in minus X direction
  do  //Wait for homing to complete
    {
    if (!NmcNoOp(xaxis)) ComError();
  	if (AnyKey()) return(-1);
    }
  while ( (NmcGetStat(xaxis)&HOME_IN_PROG) && (ServoGetAux(xaxis)&SERVO_ON) );
  }
ServoResetRelHome(xaxis);

//
//  Y axis homing:
//
//first home in the plus direction if not on limit switch
if ( !(NmcGetStat(yaxis) & LIMIT1) )
  {
  ServoSetHoming(yaxis, ON_LIMIT1 | HOME_STOP_ABRUPT);
  MainForm->YPlusButtonMouseDown(NULL, Button, Shift, 0, 0);  //move in plus y direction
  do  //Wait for homing to complete
    {
    if (!NmcNoOp(yaxis)) ComError();
  	if (AnyKey()) return(-1);
    }
  while ( (NmcGetStat(yaxis)&HOME_IN_PROG) && (ServoGetAux(yaxis)&SERVO_ON) );
  }

if ( !(ServoGetAux(yaxis) & SERVO_ON) )   //make sure we didn't hit a stop
  {
  SimpleMsgBox("Homing procedure failed!");
  MainForm->Close();
  }

if ( NmcGetStat(yaxis) & LIMIT1 )     //home in the minus direction if on limit
  {
  ServoSetHoming(yaxis, ON_LIMIT1 | HOME_STOP_SMOOTH);
  MainForm->YMinusButtonMouseDown(NULL, Button, Shift, 0, 0);  //move in minus y direction
  do  //Wait for homing to complete
    {
    if (!NmcNoOp(yaxis)) ComError();
  	if (AnyKey()) return(-1);
    }
  while ( (NmcGetStat(yaxis)&HOME_IN_PROG) && (ServoGetAux(yaxis)&SERVO_ON) );
  }
ServoResetRelHome(yaxis);

StartRapid(xmax-0.1, ymax-0.1, zmax-0.1);  //move to the edge of the range of motion
do
  {
  if (!NmcNoOp(xaxis)) ComError();
  if (!NmcNoOp(yaxis)) ComError();
  if (!NmcNoOp(zaxis)) ComError();
  if (AnyKey()) return(-1);
  }
while ( !(NmcGetStat(xaxis) & MOVE_DONE) ||
	    !(NmcGetStat(yaxis) & MOVE_DONE) ||
        !(NmcGetStat(zaxis) & MOVE_DONE) );

mill_homed = 1;

Screen->Cursor = crDefault;
opmode = IDLE;
HomeForm->Close();
return(0);
}
//---------------------------------------------------------------------------
//
// Event Functions
//
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::PathTimerTimer(TObject *Sender)
{
int res;
float posx, posy, posz;
char txtstr[80];

if (!controlsenabled)
  {
  if (opmode == IDLE && !rungcode && !feedhold)
    EnableMotionControls();
  }
else
  {
  if (opmode == SERVO_OFF)
    DisableMotionControls();
  }

//Execute path mode or Nop as required to get current data
//
if (opmode == PATH)
  {
  res = AddPathPoints();
  if ( res ==-1 )
    {
    opmode = PATH_END;
    NmcNoOp(xaxis);
    NmcNoOp(yaxis);
    NmcNoOp(zaxis);
    }
  else if (res == -2) ComError();
  }
else
  {
  if (!NmcNoOp(xaxis)) ComError();
  if (!NmcNoOp(yaxis)) ComError();
  if (!NmcNoOp(zaxis)) ComError();
  }

//Check for path end complete
//
if (opmode == PATH_END)
  if ( !(ServoGetAux(xaxis)& PATH_MODE) && !(ServoGetAux(yaxis)& PATH_MODE) &&
       !(ServoGetAux(zaxis)& PATH_MODE) )
  {
  opmode = IDLE;
  }

//Check for completion of jog mode or rapid move
//
if (opmode == JOG_STOP || opmode == RAPID)
  {
  if (  (NmcGetStat(xaxis)&MOVE_DONE) && (NmcGetStat(yaxis)&MOVE_DONE) &&
        (NmcGetStat(zaxis)&MOVE_DONE) )
    {
    opmode = IDLE;
    }
  }

//Check for servo off
//
if ( !(ServoGetAux(xaxis)& SERVO_ON)  )
  {
  XOnCB->Checked = false;
  //shutdown all axes if not idle or off
  if (opmode != IDLE && opmode != SERVO_OFF) AllOffButtonClick(Sender);
  }
if ( !(ServoGetAux(yaxis)& SERVO_ON)  )
  {
  YOnCB->Checked = false;
  //shutdown all axes if not idle or off
  if (opmode != IDLE && opmode != SERVO_OFF) AllOffButtonClick(Sender);
  }
if ( !(ServoGetAux(zaxis)& SERVO_ON)  )
  {
  ZOnCB->Checked = false;
  //shutdown all axes if not idle or off
  if (opmode != IDLE && opmode != SERVO_OFF) AllOffButtonClick(Sender);
  }

//get machine position
posx = (float)ServoGetPos(xaxis)/xscale;
posy = (float)ServoGetPos(yaxis)/yscale;
posz = (float)ServoGetPos(zaxis)/zscale;

//Check if outside range of motion
if (mill_homed)
  {
  if ( (posx>xmax && opmode==JOGXP) || (posx<xmin && opmode==JOGXM) )
    {
    ServoLoadTraj(xaxis, LOAD_VEL|ENABLE_SERVO|VEL_MODE|START_NOW, 0, 0, 0, 0);
    opmode = JOG_STOP;
    }
  else if ( (posy>ymax && opmode==JOGYP) || (posy<ymin && opmode==JOGYM) )
    {
    ServoLoadTraj(yaxis, LOAD_VEL|ENABLE_SERVO|VEL_MODE|START_NOW, 0, 0, 0, 0);
    opmode = JOG_STOP;
    }
  else if ( (posz>zmax && opmode==JOGZP) || (posz<zmin && opmode==JOGZM) )
    {
    ServoLoadTraj(zaxis, LOAD_VEL|ENABLE_SERVO|VEL_MODE|START_NOW, 0, 0, 0, 0);
    opmode = JOG_STOP;
    }
  else if (opmode==PATH || opmode==PATH_END || opmode==RAPID) //check for programmed modes
    {
    if ( posx>xmax || posx<xmin ||
         posy>ymax || posy<ymin ||
         posz>zmax || posz<zmin )
      {
      ServoStopMotor(xaxis, AMP_ENABLE | STOP_ABRUPT);
      ServoStopMotor(yaxis, AMP_ENABLE | STOP_ABRUPT);
      ServoStopMotor(zaxis, AMP_ENABLE | STOP_ABRUPT);
      opmode = IDLE;
      rungcode = 0;
      pathappendmode = 0;
      SimpleMsgBox("Programmed move exceeds the range of motion");
      }
    }
  }

//Update position display
//
if (ProgramRB->Checked == true) posx -= xorg;
sprintf(txtstr,formatstr, posx);
XPosEdit->Text = txtstr;

if (ProgramRB->Checked == true) posy -= yorg;
sprintf(txtstr,formatstr, posy);
YPosEdit->Text = txtstr;

if (ProgramRB->Checked == true) posz -= (zorg + toollen[toollennum]);
sprintf(txtstr,formatstr, posz);
ZPosEdit->Text = txtstr;

//Update tool info if changed
//
if (toolchanged)
  {
  sprintf(txtstr,"%d", toolnum+1);
  ToolNumEdit->Text = txtstr;

  sprintf(txtstr,"%.4f", toollen[toollennum]);
  ToolLenEdit->Text = txtstr;
  toolchanged = 0;
  }

//Check for dwell
if (dwellcounts)
  dwellcounts--;  //decrement dwell counter if not already zero

//Run G-Code if enabled
//
if ( (opmode == IDLE) && (rungcode==1) && (dwellcounts==0) )
  {
  if (ContouringCB->Checked == true) contouring = 1; //get current contouring state
  else contouring = 0;

  while (1)  //run lines of G-Code until not IDLE (or until an error)
    {
    if (curline>=numlines)  //punt if at end of code
      {
      rungcode = 0;
      pathappendmode = 0;
      SimpleMsgBox("At end of G-Code program.");
      break;
      }

    res = ExecuteGCode(&(gcode[line[curline]]), contouring);

    if (res != 0 && res != -1)
      {
      rungcode = 0;
      pathappendmode = 0;
      DisplayErrorMsg(res);
      break;
      }

    if (res == -1) break;  //break out of while loop without advancing curline

    if (res == 0)    //line of code executed successfully
      {
      curline++;

      if (mcode>=0)
      		ExecuteMCode(mcode);

      if (curline == numlines)     //require an M00 at end of program
        {
        rungcode = 0;
        pathappendmode = 0;
        break;
        }

      DisplayGCode(curline);

      //break if not idle or if an M code was executed - otherwise g-codes will
      // continue to execute until an action is started
      if ( (opmode != IDLE) || (mcode>=0) ) break;
      }

    } //END while 1
  } //if IDLE and rungcode
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ExitButtonClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::StartupTimerTimer(TObject *Sender)
{
long int jogacc;
int res;

DisableMotionControls();

ErrorPrinting(0);  //Turn off error printing

StartupTimer->Enabled = false;
SimpleMsgBox("Please turn on Servo Controller Power");

if (ReadIniFile() != 0)
  {
  SimpleMsgBox("Could not read initialization data in PSCNC.INI");
  Close();
  }
if (ReadToolFile() != 0)
  {
  SimpleMsgBox("Could not read tool length data in PSCNC.TLL");
  Close();
  }

if (nummod>0)
  {
  NmcShutdown();
  }

nummod = NmcInit(comport, 115200);  //Controllers on comport, use 115200 baud

if (nummod != 3)
  {
  SimpleMsgBox("3 servos not found");
  Close();
  }

ServoStopMotor(xaxis, MOTOR_OFF | ADV_FEATURE);     //reset amp enable
ServoStopMotor(xaxis, AMP_ENABLE | MOTOR_OFF);
ServoStopMotor(yaxis, MOTOR_OFF | ADV_FEATURE);
ServoStopMotor(yaxis, AMP_ENABLE | MOTOR_OFF);
ServoStopMotor(zaxis, MOTOR_OFF | ADV_FEATURE);
ServoStopMotor(zaxis, AMP_ENABLE | MOTOR_OFF);

Screen->Cursor = crHourGlass;
if (xpadv>0) ServoSetPhase(xaxis, xpadv, xpoff, PH_PWM);
if (ypadv>0) ServoSetPhase(yaxis, ypadv, ypoff, PH_PWM);
if (zpadv>0) ServoSetPhase(zaxis, zpadv, zpoff, PH_PWM);
Screen->Cursor = crDefault;

ServoSetGain(xaxis, xgain.kp, xgain.kd, xgain.ki, xgain.il,
             xgain.ol, xgain.cl, xgain.el, xgain.sr, xgain.dc);
ServoSetGain(yaxis, ygain.kp, ygain.kd, ygain.ki, ygain.il,
             ygain.ol, ygain.cl, ygain.el, ygain.sr, ygain.dc);
ServoSetGain(zaxis, zgain.kp, zgain.kd, zgain.ki, zgain.il,
             zgain.ol, zgain.cl, zgain.el, zgain.sr, zgain.dc);

jogacc = 0x10000*(xacc*xscale/(1953.12*1953.12));
if (jogacc<0) jogacc = -jogacc;
ServoLoadTraj(xaxis, LOAD_ACC | START_NOW, 0, 0, jogacc, 0);

jogacc = 0x10000*(yacc*yscale/(1953.12*1953.12));
if (jogacc<0) jogacc = -jogacc;
ServoLoadTraj(yaxis, LOAD_ACC | START_NOW, 0, 0, jogacc, 0);

jogacc = 0x10000*(zacc*zscale/(1953.12*1953.12));
if (jogacc<0) jogacc = -jogacc;
ServoLoadTraj(zaxis, LOAD_ACC | START_NOW, 0, 0, jogacc, 0);

//Set the required status items the path control module
NmcDefineStatus(xaxis, SEND_POS | SEND_NPOINTS | SEND_PERROR | SEND_AUX);
NmcDefineStatus(yaxis, SEND_POS | SEND_NPOINTS | SEND_PERROR | SEND_AUX);
NmcDefineStatus(zaxis, SEND_POS | SEND_NPOINTS | SEND_PERROR | SEND_AUX);

//Establish max feed rate as the maximum of the X, Y and Z feed rates
maxfr = xmaxvel;
if (ymaxvel>maxfr) maxfr = ymaxvel;
if (zmaxvel>maxfr) maxfr = zmaxvel;

//Initialize path control module
accel = xacc;
if (accel>yacc) accel = yacc;
if (accel>zacc) accel = zacc;
if (SetPathParams(pfreq, nbuf, xaxis, yaxis, zaxis, 255, 0, xscale, yscale, zscale, accel) != 0)
  Close();
xorg = 0.0; yorg = 0.0; zorg = 0.0;
SetOrigin(0.0, 0.0, 0.0);
SetFeedrate(0.10*maxfr);     //use 10% of max feed rate as default
SetTangentTolerance(maxang);

//Set the format for displaying positions
if ( (xmax - xmin)>=90.0 || (ymax - ymin)>=90.0 || (zmax - zmin)>=90.0 )
  strcpy(formatstr,"%8.3f");
else strcpy(formatstr,"%8.4f");

res = MessageBox(NULL, "Do you want to execute the homing procedure?", "", MB_YESNOCANCEL);
if (res == IDYES)
  if (HomeMill() != 0) Close();
else if (res == IDCANCEL) Close();

//Set path timer tick rate to 1/3th the path buffer time
PathTimer->Interval = (nbuf*1000)/pfreq/3;
PathTimer->Enabled = 1;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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