Toolsetter program

Moderators: TomKerekes, dynomotion

Post Reply
nmorong
Posts: 6
Joined: Sun Jun 07, 2026 12:48 am

Toolsetter program

Post by nmorong » Fri Jun 19, 2026 2:59 am

I thought I would share the program I made to use a Metrol T24D-16-01 toolsetter on my Kogna controlled Milltronics VMC. I've posted a lot about this machine over at the CNCzone forums, but this forum seems more active for Dynomotion users.

I looked for an example of a toolsetter program that worked more like the industrial stuff I'm used to, where all tool offsets are the distance between the tool tip and the machine table, and didn't find one. I don't claim to really know what I'm doing with C, so this could probably be done much more elegantly, but I have this working to my satisfaction on my machine. I set it as a user button, and also as an M code. The idea is to have a Gcode program consisting of only tool changes and the Mcode following each to automate setting tool lengths for every tool in the ATC carousel.

This program just saves the position of the Z axis at the moment of switch activation, converts it to user units, applies a user defined offset, and sets the tool length to this value. This allows you to set each tool such that Z0 is the machine table, or some other such stationary reference height.

It moves Z to a safe height, moves the spindle centerline over the toolsetter, moves Z down and takes a first measurement at a fast hit speed. Then it takes 3 more precise measurements at a slower speed, averages them, and compares each individual measurement to the average. If all 3 precise measurements are less than a user defined maximum error, it sets the tool length. If not, it repeats the measurement cycle one more time before failing with an error message.

Note: this toolsetter is from a Japanese higher end industrial machine, and is specced to have better than one micron repeatability moving in both directions. Other setters may need to make measurements only on downward motions.
Attachments
ToolSetter-Working 6-18-26B.c
(9.72 KiB) Downloaded 18 times

User avatar
TomKerekes
Posts: 2942
Joined: Mon Dec 04, 2017 1:49 am

Re: Toolsetter program

Post by TomKerekes » Sun Jun 21, 2026 12:12 am

Hi,

Thanks for sharing.

btw Have you tested in mm mode? ChatGPT Code thinks it has a bug:
Bug: mm conversion divides by 25.4 instead of multiplying
else if (Units == 2)
{
NewToolLength = -1*(NewToolLength/(ZCounts*25.4))+Offset; // wrong
}
ZCounts is counts per inch, so counts → mm is counts × 25.4 / ZCounts. You're dividing by 25.4 where you should multiply, so the result is off by 25.4² ≈ 645× (a 700000-count reading lands at ~6.8 mm instead of ~356 mm). Fix:

NewToolLength = -1*(NewToolLength*25.4/ZCounts)+Offset; // or /(ZCounts/25.4)
The inch branch right above it is correct, which tells you this is an isolated typo — and that the mm path has almost certainly never been run.

Related, same branch: Offset (7.349067) is applied after conversion, so it's in the active units. One compile-time constant can't be correct for both modes — in mm you'd need Offset*25.4 if the constant is the inch value. Another sign the mm branch is untested.
Thanks
Regards,

Tom Kerekes
Dynomotion, Inc.

nmorong
Posts: 6
Joined: Sun Jun 07, 2026 12:48 am

Re: Toolsetter program

Post by nmorong » Sun Jun 21, 2026 2:16 am

You're right, that is a mistake. I didn't test it in mm, it seemed simple enough but I just had a stupid moment and divided when I should have multiplied.

User avatar
TomKerekes
Posts: 2942
Joined: Mon Dec 04, 2017 1:49 am

Re: Toolsetter program

Post by TomKerekes » Sun Jun 21, 2026 6:27 pm

I've made that mistake countless times :)
Regards,

Tom Kerekes
Dynomotion, Inc.

nmorong
Posts: 6
Joined: Sun Jun 07, 2026 12:48 am

Re: Toolsetter program

Post by nmorong » Mon Jun 22, 2026 10:54 pm

I'm trying to change one thing with this program, and I can't seem to get it to work. I want to automatically apply tool length compensation using the new length value, which it does when I run it as a user button or from the C programs screen. I have tool length/offset immediately selected.

But when I try to use it as an M code from within a Gcode program, the tool length is set correctly but the tool length offset is not automatically applied. I have to use G43Hxx in Gcode before it's applied. Sooner or later I'm going to forget the G43 and cause a crash.

I've tried using the following code, but I can't get it to apply the new tool length offset. I don't think the MDI command is working while Gcode is executing:

Code: Select all

char s[80];
sprintf(s,"G43H%d",persist.UserData[7]); //Current tool number is stored in persist.UserData[7]
MDI(s); 
Is there any way to apply a new tool length offset from a C program while Gcode is running? Or a way to stop running the Gcode program while the C program runs, and pick back up where it left off?

User avatar
TomKerekes
Posts: 2942
Joined: Mon Dec 04, 2017 1:49 am

Re: Toolsetter program

Post by TomKerekes » Tue Jun 23, 2026 8:27 pm

It’s supposed to be applied. KMotionCNC checks if length compensation is on and the tool that is being changed is currently in use, if so then the compensation is updated.

What Version are you running?

Do you have Exec/wait/sync configured for the MCode?

How you you determine it isn’t being applied?

MDI commands aren’t allowed while GCode is running. It’s technically another GCode program.
Regards,

Tom Kerekes
Dynomotion, Inc.

nmorong
Posts: 6
Joined: Sun Jun 07, 2026 12:48 am

Re: Toolsetter program

Post by nmorong » Tue Jun 23, 2026 11:10 pm

I'm running 5.3.8

I do have exec/wait/synch configured

Behavior is as follows:

If I have tool 40 active, and press the tool set button that runs the program, tool 40 cycles through the setting process, length is set in the tool table, and new tool 40 offset is applied.

If I have tool 39 active, and run the following code:

Code: Select all

%
T40 M6
M104
M30
%
The machine changes to tool 40, and cycles through the setting process. Length for tool 40 is correctly set in the tool table, but the tool length offset for tool 39 is still the active tool length offset until I either open/close the tool data page or issue G43HXX.

When I use the dropdown menu to change a tool, it updates to the correct tool offset. But when I command a tool change via MDI, the length offset of the previous tool remains active. I never previously had a reason to command tool changes via MDI, so I never realized that the machine was behaving differently depending on HOW a tool change was selected.

So I guess my issue is actually with the way my machine is handling tool changes when initiated by M6. What can I do in my tool change program to get the tool length offset to switch to the current tool?

nmorong
Posts: 6
Joined: Sun Jun 07, 2026 12:48 am

Re: Toolsetter program

Post by nmorong » Thu Jun 25, 2026 9:24 pm

I did figure out how to, in effect, apply a tool length offset from within a C program with an active Gcode program running. But it's kludgy and not really useable as is. There's got to be a better way to do this.

I used PC_COMM_HALT_NEXT_LINE to stop the Gcode, then set a persist variable to something I would never accidentally write to it. Then my forever loop looks for the persist value, uses MDI to issue G43HXX, and cycle starts.

Seems to work fine when M6 is embedded in Gcode that I want to continue running. Not so great if I use MDI and get a cycle start I didn't want after the tool change. Maybe there's a way to avoid this?

Another kludgy solution I thought of was to store my actual tool lengths somewhere else (maybe the X or Y offset fields, because I don't use those), and write the tool length for the actual current tool to the tool length Var for EVERY tool.

User avatar
TomKerekes
Posts: 2942
Joined: Mon Dec 04, 2017 1:49 am

Re: Toolsetter program

Post by TomKerekes » Fri Jun 26, 2026 1:27 am

I think we will need to add a new command to do the equivalent of G43Hxx. I couldn’t find a reasonable way to do it currently. Give us some time to look into it.
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply