📄 main.cpp
字号:
void __fastcall TMainForm::XOnCBClick(TObject *Sender)
{
if (XOnCB->Checked == true) //enable the servos
{
ServoStopMotor(xaxis, AMP_ENABLE | STOP_ABRUPT); //start servo
ServoClearBits(xaxis); //clear errors
XPosEdit->Color = clWindow;
if ( (YOnCB->Checked == true) && (ZOnCB->Checked == true) )
opmode = IDLE;
}
else //disable the servos
{
//leave amp enabled for SS-DRIVE (xpadv > 0)
if (xpadv>0) ServoStopMotor(xaxis, AMP_ENABLE | MOTOR_OFF);
else ServoStopMotor(xaxis, MOTOR_OFF);
XPosEdit->Color = clSilver;
opmode = SERVO_OFF;
rungcode = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::YOnCBClick(TObject *Sender)
{
if (YOnCB->Checked == true)
{
ServoStopMotor(yaxis, AMP_ENABLE | STOP_ABRUPT);
ServoClearBits(yaxis); //clear errors
YPosEdit->Color = clWindow;
if ( (XOnCB->Checked == true) && (ZOnCB->Checked == true) )
opmode = IDLE;
}
else
{
//leave amp enabled for SS-DRIVE (ypadv > 0)
if (ypadv>0) ServoStopMotor(yaxis, AMP_ENABLE | MOTOR_OFF);
else ServoStopMotor(yaxis, MOTOR_OFF);
YPosEdit->Color = clSilver;
opmode = SERVO_OFF;
rungcode = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ZOnCBClick(TObject *Sender)
{
if (ZOnCB->Checked == true)
{
ServoStopMotor(zaxis, AMP_ENABLE | STOP_ABRUPT);
ServoClearBits(zaxis); //clear errors
ZPosEdit->Color = clWindow;
if ( (XOnCB->Checked == true) && (YOnCB->Checked == true) )
opmode = IDLE;
}
else
{
//leave amp enabled for SS-DRIVE (zpadv > 0)
if (zpadv>0) ServoStopMotor(zaxis, AMP_ENABLE | MOTOR_OFF);
else ServoStopMotor(zaxis, MOTOR_OFF);
ZPosEdit->Color = clSilver;
opmode = SERVO_OFF;
rungcode = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::AllOnButtonClick(TObject *Sender)
{
XOnCB->Checked = true;
YOnCB->Checked = true;
ZOnCB->Checked = true;
MainForm->XPosEdit->Color = clWindow;
MainForm->YPosEdit->Color = clWindow;
MainForm->ZPosEdit->Color = clWindow;
opmode = IDLE;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::AllOffButtonClick(TObject *Sender)
{
//Disable servos
XOnCB->Checked = false;
YOnCB->Checked = false;
ZOnCB->Checked = false;
//set operting mode, turn off g-code interpreter, clear path being built
opmode = SERVO_OFF;
rungcode = 0;
pathappendmode = 0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
if (nummod != 0) NmcShutdown();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::XPlusButtonMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
long int vel;
byte mode;
//punt if we are past the maximum X position
if ( mill_homed && (float)ServoGetPos(xaxis)/xscale > xmax) return;
mode = LOAD_VEL | ENABLE_SERVO | VEL_MODE | START_NOW;
vel = (long int)(0x10000*0.5*xscale*xmaxvel*(JogTrackBar->Position+1)/(1953.12*JogTrackBar->Max));
if (vel<0)
{
vel = -vel;
mode |= REVERSE;
}
ServoLoadTraj(xaxis, mode, 0, vel, 0, 0);
opmode = JOGXP;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::XPlusButtonMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
ServoLoadTraj(xaxis, LOAD_VEL|ENABLE_SERVO|VEL_MODE|START_NOW, 0, 0, 0, 0);
opmode = JOG_STOP;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::XMinusButtonMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
long int vel;
byte mode;
//punt if we are past the minimum X position
if ( mill_homed && (float)ServoGetPos(xaxis)/xscale < xmin) return;
mode = LOAD_VEL | ENABLE_SERVO | VEL_MODE | START_NOW;
vel = -(long int)(0x10000*0.5*xscale*xmaxvel*(JogTrackBar->Position+1)/(1953.12*JogTrackBar->Max));
if (vel<0)
{
vel = -vel;
mode |= REVERSE;
}
ServoLoadTraj(xaxis, mode, 0, vel, 0, 0);
opmode = JOGXM;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::YPlusButtonMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
long int vel;
byte mode;
//punt if we are past the maximum Y position
if ( mill_homed && (float)ServoGetPos(yaxis)/yscale > ymax) return;
mode = LOAD_VEL | ENABLE_SERVO | VEL_MODE | START_NOW;
vel = (long int)(0x10000*0.5*yscale*ymaxvel*(JogTrackBar->Position+1)/(1953.12*JogTrackBar->Max));
if (vel<0)
{
vel = -vel;
mode |= REVERSE;
}
ServoLoadTraj(yaxis, mode, 0, vel, 0, 0);
opmode = JOGYP;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::YMinusButtonMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
long int vel;
byte mode;
//punt if we are past the minimum Y position
if ( mill_homed && (float)ServoGetPos(yaxis)/yscale < ymin) return;
mode = LOAD_VEL | ENABLE_SERVO | VEL_MODE | START_NOW;
vel = -(long int)(0x10000*0.5*yscale*ymaxvel*(JogTrackBar->Position+1)/(1953.12*JogTrackBar->Max));
if (vel<0)
{
vel = -vel;
mode |= REVERSE;
}
ServoLoadTraj(yaxis, mode, 0, vel, 0, 0);
opmode = JOGYM;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::YPlusButtonMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
ServoLoadTraj(yaxis, LOAD_VEL|ENABLE_SERVO|VEL_MODE|START_NOW, 0, 0, 0, 0);
opmode = JOG_STOP;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ZPlusButtonMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
long int vel;
byte mode;
//punt if we are past the maximum Z position
if ( mill_homed && (float)ServoGetPos(zaxis)/zscale > zmax) return;
mode = LOAD_VEL | ENABLE_SERVO | VEL_MODE | START_NOW;
vel = (long int)(0x10000*0.5*zscale*zmaxvel*(JogTrackBar->Position+1)/(1953.12*JogTrackBar->Max));
if (vel<0)
{
vel = -vel;
mode |= REVERSE;
}
ServoLoadTraj(zaxis, mode, 0, vel, 0, 0);
opmode = JOGZP;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ZMinusButtonMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
long int vel;
byte mode;
//punt if we are past the minimum Z position
if ( mill_homed && (float)ServoGetPos(zaxis)/zscale < zmin) return;
mode = LOAD_VEL | ENABLE_SERVO | VEL_MODE | START_NOW;
vel = -(long int)(0x10000*0.5*zscale*zmaxvel*(JogTrackBar->Position+1)/(1953.12*JogTrackBar->Max));
if (vel<0)
{
vel = -vel;
mode |= REVERSE;
}
ServoLoadTraj(zaxis, mode, 0, vel, 0, 0);
opmode = JOGZM;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ZPlusButtonMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
ServoLoadTraj(zaxis, LOAD_VEL|ENABLE_SERVO|VEL_MODE|START_NOW, 0, 0, 0, 0);
opmode = JOG_STOP;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::XOrgButtonClick(TObject *Sender)
{
//If servo is on, use commanded position, otherwise, use the actual
if (ServoGetAux(xaxis) & SERVO_ON )
xorg = (float)(ServoGetPos(xaxis) + ServoGetPError(xaxis))/xscale;
else xorg = (float)ServoGetPos(xaxis)/xscale;
SetOrigin(xorg, yorg, zorg);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::YOrgButtonClick(TObject *Sender)
{
//If servo is on, use commanded position, otherwise, use the actual
if (ServoGetAux(yaxis) & SERVO_ON )
yorg = (float)(ServoGetPos(yaxis) + ServoGetPError(yaxis))/yscale;
else yorg = (float)ServoGetPos(yaxis)/yscale;
SetOrigin(xorg, yorg, zorg);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ZOrgButtonClick(TObject *Sender)
{
//If servo is on, use commanded position, otherwise, use the actual
if (ServoGetAux(zaxis) & SERVO_ON )
zorg = (float)(ServoGetPos(zaxis) + ServoGetPError(zaxis))/zscale;
else zorg = (float)ServoGetPos(zaxis)/zscale;
zorg -= toollen[toollennum];
SetOrigin(xorg, yorg, zorg);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::SetOrgButtonClick(TObject *Sender)
{
XOrgButtonClick(Sender);
YOrgButtonClick(Sender);
ZOrgButtonClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FeedrateUDClick(TObject *Sender, TUDBtnType Button)
{
char prnstr[20];
if (Button == btNext)
froverride += 10;
else
froverride -=10;
sprintf(prnstr,"%d%",froverride);
FeedrateEdit->Text = prnstr;
if (!feedhold)
SetLimitedFeedrate(feedrate*froverride/100);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::RapidUDClick(TObject *Sender, TUDBtnType Button)
{
char prnstr[20];
if (Button == btNext)
rapidoverride += 10;
else
rapidoverride -=10;
sprintf(prnstr,"%d%",rapidoverride);
RapidEdit->Text = prnstr;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::BitBtn1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::OpenButtonClick(TObject *Sender)
{
int res;
int i;
if (OpenDialog->Execute() == false) return; //punt on cancel
res = ReadGCodeFile( OpenDialog->FileName.c_str() );
if ( res == -1 )
SimpleMsgBox("Could not open selected G-Code file");
else if (res == -2)
SimpleMsgBox("G-Code file too large");
else if (res == -3)
SimpleMsgBox("G-Code file has too many lines");
//Show the first n lines of code
GList->Items->Clear();
for (i=0; i<7; i++)
{
if (i == numlines) break;
GList->Items->Add( &(gcode[line[i]]) );
}
GList->ItemIndex = 0; //Select the first line
curline = 0;
feedrate = 0.0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ExecuteButtonClick(TObject *Sender)
{
int res;
//Initialize interpreter positions to the current command position
gx = (float)(ServoGetPos(xaxis) + ServoGetPError(xaxis))/xscale - xorg;
gy = (float)(ServoGetPos(yaxis) + ServoGetPError(yaxis))/yscale - yorg;
gz = (float)(ServoGetPos(zaxis) + ServoGetPError(zaxis))/zscale - zorg;
gz -= toollen[toollennum];
res = ExecuteGCode(ImmediateEdit->Text.c_str(), 0);
if (res != 0) SimpleMsgBox("Syntax or data error");
else
{
DisableMotionControls();
if (mcode>=0) ExecuteMCode(mcode);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ImmediateEditEnter(TObject *Sender)
{
ImmediateEdit->Tag = 1;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ImmediateEditClick(TObject *Sender)
{
if (ImmediateEdit->Tag == 1)
{
ImmediateEdit->SelStart = 0;
ImmediateEdit->SelLength = 100;
ExecuteButton->Default = true;
ImmediateEdit->Tag = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ResetButtonClick(TObject *Sender)
{
curline = 0;
DisplayGCode(0);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::StepButtonClick(TObject *Sender)
{
int res;
if (opmode != IDLE) return;
if (curline==numlines)
{
SimpleMsgBox("At end of G-Code program.");
return; //punt if at end of code
}
//Initialize interpreter positions to the current command position
gx = (float)(ServoGetPos(xaxis) + ServoGetPError(xaxis))/xscale - xorg;
gy = (float)(ServoGetPos(yaxis) + ServoGetPError(yaxis))/yscale - yorg;
gz = (float)(ServoGetPos(zaxis) + ServoGetPError(zaxis))/zscale - zorg;
gz -= toollen[toollennum]; //Adjust for tool length
res = ExecuteGCode( &(gcode[line[curline]]) , 0);
if (mcode >= 0) ExecuteMCode(mcode);
if (res == 0 || res == 1)
{
curline++;
if (curline<numlines) DisplayGCode(curline);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::StopButtonClick(TObject *Sender)
{
rungcode = 0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::StartButtonClick(TObject *Sender)
{
//Initialize interpreter positions to the current command position
gx = (float)(ServoGetPos(xaxis) + ServoGetPError(xaxis))/xscale - xorg;
gy = (float)(ServoGetPos(yaxis) + ServoGetPError(yaxis))/yscale - yorg;
gz = (float)(ServoGetPos(zaxis) + ServoGetPError(zaxis))/zscale - zorg;
gz -= toollen[toollennum]; //Adjust for tool length
rungcode = 1;
DisableMotionControls();
OpenButton->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FeedholdButtonClick(TObject *Sender)
{
if (feedhold) return; //punt if already holding
if (opmode == IDLE && !rungcode)
return; //punt if nothing is moving
SetFeedrate(0.0);
if (opmode == RAPID)
{
ServoLoadTraj(xaxis, LOAD_VEL|ENABLE_SERVO|VEL_MODE|START_NOW, 0, 0, 0, 0);
ServoLoadTraj(yaxis, LOAD_VEL|ENABLE_SERVO|VEL_MODE|START_NOW, 0, 0, 0, 0);
ServoLoadTraj(zaxis, LOAD_VEL|ENABLE_SERVO|VEL_MODE|START_NOW, 0, 0, 0, 0);
feedhold = RAPIDHOLD;
}
else feedhold = PATHHOLD;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ResumeButtonClick(TObject *Sender)
{
if (!feedhold) return; //punt if not holding
SetLimitedFeedrate(feedrate*froverride/100);
if (feedhold == RAPIDHOLD) StartRapid(prevrx, prevry, prevrz);
feedhold = NO_HOLD;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::AbortButtonClick(TObject *Sender)
{
if (!feedhold) //Execute a feedhold if not already holding
{
FeedholdButtonClick(Sender);
SimpleMsgBox("Click 'Abort' again to terminate motion");
}
else
{
opmode = IDLE; //force into IDLE mode, remove hold, stop execution
feedhold = NO_HOLD;
rungcode = 0;
SetLimitedFeedrate(feedrate*froverride/100); //restore old feedrate
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::HelpButtonClick(TObject *Sender)
{
int result;
result = (int)ShellExecute(NULL,NULL,"pscnc.pdf",NULL,"",SW_SHOWNORMAL);
if (result<32) SimpleMsgBox("Acrobat Reader or 'pscnc.pdf' not found");
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -