Code:
#include "KMotionDef.h"
#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"
#define Zaxis 2
#define Xaxis 0
#define Yaxis 1
#define XaxisCNTS_mm 3937.0078740157
#define ToolSensorPin 0
#define ToolSensorX 992000
#define ToolSensorY 400000
int DoPC(int cmd);
int DoPCFloat(int cmd, float f);
int DoPCInt(int cmd, int i);
int MsgBox(char *s, int Flags);
int SetVars(int poff, int varoff, int n);
int GetVars(int varoff, int n, int poff);
main()
{
int FixtureIndex,Units, TWORD, HWORD, DWORD;
double NewToolLength,Length,OriginOffsetZ,AxisOffsetZ,ToolOffsetX,ToolDiameter;
double Machinex,Machiney,Machinez,Machinea,Machineb,Machinec;
double P0,P1,A0,A1;
int GetToolOffsetX(int index, double *OffsetX);
int GetToolDiameter(int index, double *Diameter);
GetMiscSettings(&Units, &TWORD, &HWORD, &DWORD);
SetBitDirection(ToolSensorPin,0); // sensor input - вход датчика
// Stop the spindle and lift tool - Останавливаем шпиндель и поднимаем инструмент
ClearBit(153); // Stop spindle
ClearBit(154); // Stop spindle
Delay_sec(0.5);
MoveAtVel(Zaxis,1300000, 40000);
while (ch2->Dest < 1300000) ;
//Ansver "ToolChange"
GetToolDiameter(TWORD,&ToolDiameter);
printf("Original Tool #%d Diameter=%.17g\n",
TWORD,ToolDiameter);
int Answer;
//int tool = persist.UserData[9]; // value stored is actually a float
char s[100];
sprintf(s,"Change to Tool diameter D=%.17g\n",ToolDiameter); //sprintf(s,"Change to Tool diameter D=%d",ToolDiameter);
Answer = MsgBox(s,MB_ICONEXCLAMATION);
//if (Answer != IDOK)
//{
DoPC(PC_COMM_HALT);
//}
// Offset X - перемещаем инструмент к датчику и делаем смещение по Х
GetToolOffsetX(TWORD,&ToolOffsetX);
printf("Original Tool #%d OffsetX=%.17g\n",
TWORD,ToolOffsetX);
MoveAtVel(Xaxis,ToolSensorX-ToolOffsetX*XaxisCNTS_mm,40000);
MoveAtVel(Yaxis,ToolSensorY,40000);
while (ch0->Dest != ToolSensorX-ToolOffsetX*XaxisCNTS_mm) ; // ПРОВЕРИТЬ УСЛОВИЕ!!!!
while (ch1->Dest != ToolSensorY) ; // ПРОВЕРИТЬ УСЛОВИЕ!!!!
//omit the Z-axis of sensor - опускаем инструмент на датчик
Jog(Zaxis,-10000);
while (!ReadBit(ToolSensorPin)) ;
Jog(Zaxis,0);
P0 = chan[Zaxis].Dest;
P1=P0+100000;
MoveAtVel(Zaxis,P1,20000);
while (ch2->Dest < P1) ;
Jog(Zaxis,-1000);
while (!ReadBit(ToolSensorPin)) ;
Jog(Zaxis,0);
// calculate the length of the instrument and record in the table
// вычисляем длину инструмента и записываем в таблицу
GetFixtureIndex(&FixtureIndex);
GetOriginOffset(&OriginOffsetZ, FixtureIndex, Zaxis);
GetAxisOffset(&AxisOffsetZ, Zaxis);
GetMachine(&Machinex,&Machiney,&Machinez,&Machinea,&Machineb,&Machinec);
// Compute Tool Offset to make DRO zero when Tool Length selected and enabled
//
// Since Machine = DRO + OriginOffset + AxisOffset + ToolOffset
//
// Set DRO = 0 and solve for ToolOffset
//
NewToolLength = RoundToReasonable(Machinez - OriginOffsetZ - AxisOffsetZ,Units);
// Change Currently Selected Tool Length
SetToolLength(TWORD,NewToolLength);
// Lift tool - Останавливаем шпиндель и поднимаем инструмент
MoveAtVel(Zaxis,1300000, 40000);
while (ch2->Dest < 1300000) ;
printf("Units=%d T=%d H=%d D=%d\n",Units, TWORD, HWORD, DWORD);
printf("Current Tool Length is %g\n",Length);
printf("Fixture Index = %d\n",FixtureIndex);
printf("Origin Offset Z = %g\n",OriginOffsetZ);
printf("Axis Offset Z = %g\n",AxisOffsetZ);
printf("Machine Coordinates %.17g %.17g %.17g %.17g %.17g %.17g\n",Machinex,Machiney,Machinez,Machinea,Machineb,Machinec);
}
int SetVars(int varoff, int n, int poff)
{
persist.UserData[PC_COMM_PERSIST+2] = n; // number of elements
persist.UserData[PC_COMM_PERSIST+3] = poff; // persist offset (doubles)
return DoPCInt(PC_COMM_SET_VARS,varoff); // Var index and Cmd
}
int GetVars(int varoff, int n, int poff)
{
persist.UserData[PC_COMM_PERSIST+2] = n; // number of elements
persist.UserData[PC_COMM_PERSIST+3] = poff; // persist offset (doubles)
return DoPCInt(PC_COMM_GET_VARS,varoff); // Var index and Cmd
}
#define GATH_OFF 0 // define the offset into the Gather buffer where strings are passed
// Trigger a message box on the PC to be displayed
// defines for MS Windows message box styles and Operator
// response IDs are defined in the KMotionDef.h file
int MsgBox(char *s, int Flags)
{
char *p=(char *)gather_buffer+GATH_OFF*sizeof(int);
int i;
do // copy to gather buffer w offset 0
{
*p++ = *s++;
}while (s[-1]);
persist.UserData[PC_COMM_PERSIST+2] = Flags; // set options
DoPCInt(PC_COMM_MSG,GATH_OFF);
return persist.UserData[PC_COMM_PERSIST+3];
}
// put the MDI string (Manual Data Input - GCode) in the
// gather buffer and tell the App where it is
int MDI(char *s)
{
char *p=(char *)gather_buffer+GATH_OFF*sizeof(int);
int i;
do // copy to gather buffer w offset 0
{
*p++ = *s++;
}while (s[-1]);
// issue the command an wait till it is complete
// (or an error - such as busy)
return DoPCInt(PC_COMM_MDI,GATH_OFF);
}
// Put a Float as a parameter and pass the command to the App
int DoPCFloat(int cmd, float f)
{
int result;
persist.UserData[PC_COMM_PERSIST+1] = *(int*)&f;
return DoPC(cmd);
}
// Put an integer as a parameter and pass the command to the App
int DoPCInt(int cmd, int i)
{
int result;
persist.UserData[PC_COMM_PERSIST+1] = i;
return DoPC(cmd);
}
// Pass a command to the PC and wait for it to handshake
// that it was received by either clearing the command
// or changing it to a negative error code
int DoPC(int cmd)
{
int result;
persist.UserData[PC_COMM_PERSIST]=cmd;
do
{
WaitNextTimeSlice();
}while (result=persist.UserData[PC_COMM_PERSIST]>0);
//printf("Result = %d\n",result);
return result;
}
Sometimes the program KmotionCNC 4.33cc itself is paused, she can completely shut down, the axis sometimes not go to the right place. This version is not stable.