Below is an edited version of mmurray's code, which iam sure i have missed something. Also,what all do i need to configure in KMCNC to use this code? And is there more code that needs to be added to my main INIT code?
Troy
Code:
#include "KMotionDef.h"
#define TMP 10 // which spare persist to use to transfer data
#include "KflopToKMotionCNCFunctions.c"
// Current tool = persist.UserData[157]
// Requested tool = persist.UserData[6]
main()
{
if (!ReadBit(137)) return 0; // if in Estop, Exit program
// Option to move table to front. Uncomment to move in Y.
//MoveAtVel(1, 1100000, 800000);
//while (!CheckDone(1)) WaitNextTimeSlice(); // wait until we are stopped
// Wait until spindle is orientated, if started already by M100 in G code
while (persist.UserData[164] == 1) WaitNextTimeSlice(); // wait until orientated
// 1. Determine how many pockets to rotate
int pockets = persist.UserData[6] - persist.UserData[157]; // pockets = requested tool - current tool
if (pockets == 0) return 0; // No toolchange needed, End
if (pockets >= 8) pockets = -16 + pockets; // If more then halfway CW, reverse
if (pockets <= -8) pockets = 16 + pockets; // If more then halfway CCW, reverse
persist.UserData[161] = 1; // Toolchanger active disable feedhold button
ResumeCoordinatedMotion(); // Clear feedhold
// 2. Turn off Flood Coolant if left on
int coolanton = 0;
if (ReadBit(157)) // if flood coolant on
{
coolanton = 1; // set variable to 1
ClearBit(157); // Flood coolant off
Delay_sec(.2); // delay
}
// 3. Turn off spindle
if (ReadBit(147) || ReadBit(148)) //check if spindle is running forward or reverse
{
SetBit(147); // turn off spindle clockwise
SetBit(148); // turn off spindle counterclockwise
Delay_sec(1.6);
}
// 4. Move to toolchange position Z0.000
{
MDI("G0 G53 Z-1.025"); // Move Z to -1.025inches from Reference Position
//MoveAtVel(2, 0, 800000);
//while (!CheckDone(2)) WaitNextTimeSlice(); // wait until we are stopped
}
// 5. Orientate
//if (!ReadBit(1045) || !ReadBit(63)) // if not orientated
//{
//if (ReadBit(63)) //check if locked and not orientated
//{
//ClearBit(63); // unlock
//Delay_sec(0.5);
//}
if (!ReadBit(1035)) // if toolchanger slide is not home estop
{
ClearBit(137); // E-stop
printf("E-stop Toolchanger not Home");
}
//if (ReadBit(156)) // If in high gear
{
persist.UserData[155] = 1; // Enable orientate speed override
SetBit(156); // Kanalog Switch4 to turn on SpindleOrientateProxSensor
Delay_sec(0.1); // Delay 0.1s
MDI("M03 S25"); // set motor rpm 25
//DAC(7,-43); // set motor rpm approx 100
SetBit(147); // spindle on
Delay_sec(0.2); // wait for spindle to reach speed
while (!ReadBit(1038)) WaitNextTimeSlice(); // wait for SpindleOrientateProx signal
SetBit(48); // Signal to VFD, set VFD to ZeroServoCommand
Delay_sec(0.50); // Delay for spindle to stop at orientation that is set in VFD parameter
}
//if (ReadBit(157)) // If in low gear
{
//persist.UserData[155] = 1; // Enable orientate speed override
//Delay_sec(0.1); // Delay 0.1s
//DAC(3,-76); // set motor rpm approx 100
//SetBit(154); // spindle on
//Delay_sec(0.5); // wait for spindle to reach speed
//while (!ReadBit(1045)) WaitNextTimeSlice(); // wait for orientation signal
//Delay_sec(0.3); // Wait until past notch
//SetBit(63); // turn on orientation air cylinder
//Delay_sec(0.5); // Delay for spindle to stop at orientation notch
//ClearBit(154); // turn spindle off
persist.UserData[155] = 0; // Spindle speed normal
Delay_sec(0.75); // wait before checking if successfull
}
while (!ReadBit(1038) || !ReadBit(63)) WaitNextTimeSlice(); // Wait if not Orientated
// 6. Tool Chnager OUT
SetBit(63); // TC_OUT solenoid
Delay_sec(0.9);
ClearBit(63); //TC_OUT solenoid
double TF = Time_sec() + 2; // timeout in 2 seconds
while (!ReadBit(1034)) // Wait for TC_OUT limit switch
{
WaitNextTimeSlice();
if (Time_sec() > TF) // Timed out?
{
if (MsgBox("TC OUT Not Extended. Halt?", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
DoPC(PC_COMM_HALT);
persist.UserData[161] = 0; // toolchanger finished
return 0; //exit program
}
else
{
TF = Time_sec() + 3; // continue and timeout after 3 seconds
}
}
}
// 7. Unclamp Tool
SetBit(59); //Tool Release Solenoid
double TF = Time_sec() + 2; // timeout in 2 seconds
while (!ReadBit(1039)) // Wait for ToolReleaseProxSensor
{
WaitNextTimeSlice();
if (Time_sec() > TF) // Timed out?
{
if (MsgBox("Tool Release Failed. Halt?", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
DoPC(PC_COMM_HALT);
persist.UserData[161] = 0; // toolchanger finished
return 0; //exit program
}
else
{
TF = Time_sec() + 3; // continue and timeout after 3 seconds
}
}
}
// 8. Move Tool Changer DOWN
SetBit(61); // TC_DOWN Solenoid
Delay_sec(0.9);
ClearBit(61); //TC_DOWN solenoid
double TF = Time_sec() + 2; // timeout in 2 seconds
while (!ReadBit(1036)) // Wait for TC_DOWN Limit Switch
{
WaitNextTimeSlice();
if (Time_sec() > TF) // Timed out?
{
if (MsgBox("TC DOWN Failed. Halt?", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
DoPC(PC_COMM_HALT);
persist.UserData[161] = 0; // toolchanger finished
return 0; //exit program
}
else
{
TF = Time_sec() + 3; // continue and timeout after 3 seconds
}
}
}
// 9. Rotate Carousel
while (pockets >= 1) // Rotate Turret CW whatever number of pockets
{
SetBit(49); // TC Carousel Motor On CW
while (!ReadBit(1033)) WaitNextTimeSlice(); // Wait for CarouselMotorProxSensor
Delay_sec(0.5);
ClearBit(49); // TC_Carousel CW Motor Off
Delay_sec(0.5);
pockets = pockets-1;
}
while (pockets <= -1) // Rotate Turret CCW whatever number of pockets
{
SetBit(50); // TC Carousel Motor On CCW
while (!ReadBit(1033)) WaitNextTimeSlice(); // Wait for CarouselMotorProxSensor
Delay_sec(0.5);
ClearBit(50); // TC_Carousel CCW Motor Off
Delay_sec(0.5);
pockets = pockets+1;
}
// 10. Move Tool Changer UP
SetBit(60); // TC_UP Solenoid
Delay_sec(0.9);
ClearBit(60); //TC_UP solenoid
double TF = Time_sec() + 2; // timeout in 2 seconds
while (!ReadBit(1037)) // Wait for TC_UP Limit Switch
{
WaitNextTimeSlice();
if (Time_sec() > TF) // Timed out?
{
if (MsgBox("TC UP Failed. Halt?", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
DoPC(PC_COMM_HALT);
persist.UserData[161] = 0; // toolchanger finished
return 0; //exit program
}
else
{
TF = Time_sec() + 3; // continue and timeout after 3 seconds
}
}
}
// 11. Clamp tool
ClearBit(59); //Tool Release Solenoid
double TF = Time_sec() + 2; // timeout in 2 seconds
while (ReadBit(1039)) // Wait for Tool Release Proximity Sensor
{
WaitNextTimeSlice();
if (Time_sec() > TF) // Timed out?
{
if (MsgBox("Tool Clamp Failed. Halt?", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
DoPC(PC_COMM_HALT);
persist.UserData[161] = 0; // toolchanger finished
return 0; //exit program
}
else
{
TF = Time_sec() + 3; // continue and timeout after 3 seconds
}
}
}
// 12. Tool Changer IN
SetBit(62); // TC_IN solenoid
Delay_sec(0.7);
ClearBit(62); // TC_IN solenoid
double TF = Time_sec() + 2; // timeout in 2 seconds
while (!ReadBit(1035)) // Check TC_IN limit switch
{
WaitNextTimeSlice();
if (Time_sec() > TF) // Timed out?
{
if (MsgBox("TC Not Home. Halt?", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
DoPC(PC_COMM_HALT);
persist.UserData[161] = 0; // toolchanger finished
return 0; //exit program
}
else
{
TF = Time_sec() + 3; // continue and timeout after 3 seconds
}
}
}
// 13. Set Variables
persist.UserData[157] = persist.UserData[6]; // Update current tool number
persist.UserData[161] = 0; // toolchanger finished
printf("Tool changed to T%d\n", persist.UserData[157]); // send message to console
// 16. Write current tool # disk
FILE *f;
char s[256];
float CTool = persist.UserData[157]; // s value from g code
f=fopen("c:\\KMotion Files\\KFlopData.txt","wt");
fprintf(s,"%f\n",CTool);
fclose(f);
// 15. Wait if feedhold pushed
//while (persist.UserData[160] == 1) // Wait if feedhold is on
//{
//WaitNextTimeSlice();
//if (ReadBit(1039)) return 0; // If Reset pushed while waiting in feedhold, End
//}
// 16. Turn coolant back on
if (coolanton == 1) SetBit(157); // if flood coolant was on, turn on again
}