View Full Version : Need Help! How to Generate a CNC code from measured 3D tool path

02-08-2009, 03:53 PM
Generating a CNC from measured 3D tool path
I want to generate a CNC code from the measured tool path. The tool path is generated by taking the measure from three encoders mounted in 3-axes milling machine. Measurement is saved in a Desktop PC for an offline analysis.
The tool path looks like this:

Path_Matrix =

Time X1 Y1 Z1
0.01 0.001 0 0.001
0.02 0.002 0 0.004
0.03 0.003 0 0.008
0.04 0.004 0 0.012
0.05 0.005 0 0.018
0.06 0.006 0 0.018
1.2 10.00 6.00 0.018 (may be upto 100000 rows)

Now, I want to generate the reference CNC code (originally used for the process) from this measured tool path. This generated CNC code later can be then compared with original CNC code.

How I can do this ? (Generating a CNC from measured 3D tool path)
Any software functions, resources, web links, literature or personal help could be really helpful to me.
I use Matlab but functions in other languages could also be possible to use.


02-08-2009, 04:00 PM
It's not that hard to write a little program to convert those numbers to g-code that would produce the same path. However, I see a lot of problems comparing those with the original g-code.

For example, how do you know whether the numbers were the result of a lot of short line segments in the original program or an interpolated circle?

There are many alternate g-codes that can produce the same motion and it will be impossible to guess which one to choose from.

I don't think your problem is solvable for that reason.



02-08-2009, 06:50 PM
Thank for a very useful reply.
I could just come up with two ideas. But I am not sure if they are right or they work.
To make the problem simplify path can be considered consisting of only right angle turns (no circular motion)

From the measured tool path [time X Y Z]
If some how the “change in directions” (right angle turns) could be detected in all X, Y and Z vectors of measured tool path. Numbers at those points would be then used to generate new command lines of CNC code.
Could it be a way to generate a reliable and comparable CNC code?

If I do not go for comparing CNC code, rather I go for comparing two tool paths (Measures tool path and reference tool paths)
Now reference tool path would be generated from Orignal CNC code by interpolation and splines smoothing in my computer (Matlab).
Now both tool paths can be compared. But I see two problems here.

Reference tool path generated should be very good approximation of the one that Machine controller (simumerik 840D in my machine) do. Any idea how?

To compare both paths may only be possible if I could some how find the corresponding points. Any idea how?

I would be thankful for some further assistance.

Death Adder
02-09-2009, 02:10 PM
Hmm... It seems as if the measured data should proceed in the same manor as the original tool path. This could make the problem easier. They should essentially be the exact same path just two different ways of measuring it. As such you might be able to load up both at once and then step through the measured path. At each measured point try to see if it would fall coincident to the line or arc prescribed by the original tool path. This is easy for linear moves but a little more complicated for arc moves. For each measured point find the nearest location on the original path and then record the error. This seems to be roughly what you want to do. This approach could be pretty quick as you wouldn't need to compare the measured point to every move in the original file. Since both data should progress the same you can always start at the point in the original that was the closest match to the measured point and scan forward looking for the next closest. There shouldn't be a closer match behind the current point or something went wrong. You'll likely find multiple matches for each input line or arc but you should never find a match behind the current position. Make sense?

02-10-2009, 06:02 PM
This sounds like a mis-match of reverse engineering and inspection, each has it's own requirements / tools & software..

(Please answer the questions if you want any more help, I need to understand what's going on here)

Q) Was the original toolpath generated by CAD/CAM??

In which case it'll likely be thousands of lines of tiny linear movements, with no G02/G03 arc commands in it.

Q) Is this part cut with a ball nosed cutter??

Assuming your probe cancels it's own stylus tip radius, then the probed XYZ data is the surface geometry and not related to your cutter path. Imagine how a ball nose cutter would contact the geometry at different tangent angles as it passes over it. A CAM generated path for a 10mm ball nose would look very different to a path with a 20mm ball nose over the same geometry (assuming it's not simply flat!).

With those guesses out of the way, here's your options..

Take your XYZ data and create a surface mesh in a CAD program, and generate a toolpath on it with your CAM program. That gets the reverse engineering bit out of the way.

*IF* your original toolpath was CAM generated for a ball nose cutter, take your surface mesh and offset it by the radius of the ball, then lower it by the radius of the ball. This will give you a tangential height mesh for the cutter. Then you'd need to examine the original cutter path, and for every X/Y compare the Z with the Z in the tangent height mesh.

Not impossible, but certainly not trivial :/

02-11-2009, 04:21 PM
Thank you for very useful replies,
some details: The final goal is to analyse the dynamic errors of a high precise milling machine(research purposes).
The approach is to compare the measured tool path XYZ with original tool path (CNC program) XYZ.

Original tool path = G-Code= Reference path
Measured tool path = tool measured positions using a measurement system

Initially, to simplify the work, a hand written simple G-Code program consist of simple right angle movements is used. Machine is started with this code (non cutting mode, no work piece).

In next step, the original tool path generated by CAD/CAM and cutter details would be considered too. Then the surface mesh approach, I guess, might be applied…. But it would be the second step.

@ Death Adder:
Thanks for the idea. I have tried your approach of closest point search. Its works fine. But I am not sure it gives reliable results.
The corresponding points in both tool paths are found by searching the closest measured tool path points for every point of original tool path.

As an example, an original tool path consisted of 10 points (10 CNC commands lines). Machine is run. The tool path is measured which is now consist of about 10000 points. Closest point for all 10 original tool path points is search in selected portions of measured tool path. Now errors can be determined.

But I am not sure this approach provided correct corresponding points. Because values of measured tool path points also contain the effects of machine dynamic erros. Thus, I guess, it may be possible that wrong points in neighbourhood appear to be the closest.

I would be very thankful for further assistance and correction.

Thanks and Regards

02-11-2009, 05:16 PM
Ok, now I see what you're doing. At least with tracking the machine positions during execution of an nc program.

Here's my thoughts..

I don't see how a close wrong point could be mistaken for a correct point; It's down to how you analyze your data. Starting right from point #1 (the start point of program execution) compare it's position with the vector of the G-code movement, don't simply take an XYZ and look for it's closest match in the g-code. The data is timed so take advantage of that.

The simple commands; G00 X0 Y0 Z0; G01 X100. Y100. Z100. F2000; X200. Y0 Z0; will produce data that even shows how the machine accelerates/deccelerates and rounds corners as it changes direction.

As for comparing with a CAM program..

Use a CAD program to generate a wild and fanciful 3d curve, or many of them even. Ask your CAM program to engrave it - as if it's scribing it all over a complex shape. There will be no tangential offsets for cutter radius, you should end up with the same shape to execute as NC code. Now compare, over time, your machines position over the original curve(s).


02-13-2009, 08:16 AM
Thanks again for a useful reply.

Related to comparing data over time:
There could be one point:
Original tool path is fed to machine controller (Siumeric 840D in my case) which commands the machine axes. The measured tool path is then measured and saved in computer.
Now it seems possible to lock the Point #1 in both paths over time. But locking the further point over time could be reliable?
Because I guess, machine controller has a small delay (processing time) between input and output. Theoretically this delay is due to sample and time, calculation etc. The total delay may not be a constant value but a variable throughout the trajectory (not 100% sure). In the case this is a variable delay, it may not be reliable to lock further points of path based upon the time information.
I would be thankful for further assistance and correction.


02-16-2009, 02:50 PM
Hi again.

It's not an easy concept to explain so I'll try in bullet point :)

Using my example NC code from below.

Time = 0
Program start
Tracking deviation
For the first vector (X100, Y100, Z100, F2000) each probed coordinate *should* follow this vector, any deviation (position and/or vector dot product) is the error you're looking for.
Keep track of the distance travelled
Each probed coordinate provides an incremental distance travelled (vector length)
The vector length of each incremental movement should be equivalent to delta time (time increment) / feed rate. Devations will highlight how the machine accelerates and deccelerates, or simply doesn't achieve the programmed feedrate (this may not be important to you but it should be apparent in the data).
turning points
When approaching the end of the current vector (noticable by comparing vector length & distance travelled) you should see the deviation grow if it rounds the corners, or see it change rapidly. Once the dot product is closer to the Next Vector, than it is to the Current Vector, track relative to the Next Vector

I've never done this by the way, so don't take my methods as fact :) but I've done enough CAD, 3d graphics programming etc to poke my nose in. Hope it helps!

04-01-2009, 12:10 PM
I just read this and have two words

Following error

I encountered this on a high speed Cincy 5axis 25k rpm diamond cutter on graphite billet master mold. Since this part was symmetrical, and two parts were back-to-back. I programmed one quadrant, cut one quadrant, 100IPM. Walked away, let the experienced operator finish the cutting. The inspection report rejected the three other quadrants. So I went to query the operator. "yeah, it was cutting so good that I jacked up the feed rate to 400IPM and ran it" (rapid is 1200IPM)

Well, that mass of cast iron (Cincy bridge-type machine) could not accelerate to keep up with the commands and lagged (followed) the point to point data commands

The feedback servos reported no error. it showed up in the part. This was 15yrs ago, I suspect machine controls are better, faster now. Is there still that following error?

"can't inspect quality into an existing part"

04-02-2009, 01:51 PM
From the G-Code, create the lines in Mat-lab
Then, for each point, write a routine in Mat-lab that looks for the shortest distance between the point and the set of splines created from the G-code. This will be the maximum linear deviation from the path, with one value per point reported from the co-ordinate system.

To get fancier than this, you'd have to constrain your code (like you suggest with straight lines) so that it can be simulated vs time to do a time based comparison, but this will get messy fast (especially if propogation delays aren't constant).

I'm sure that you can find the acceleration profiles of your CNC machine/controller combo so you could even get your path simulator to take that into account when generating an estimated time path for the tool.

I'm not a CNC expert, but I am a PhD in Applied Physics.... Really, without more stuff you're somewhat constrained to do some sort of dynamic error estimate unless you intercept things like motor feeds and the like.

Also, what is your goal? Test positioning accuracy or some error that compounds over time? The output of the position system only measures where the machine thinks it is, not where it really may be. To really do this stuff well you'd want to use some sort of interferometer based system to get a very accurate absolute position information independent of the machine. It's possible that the machine position measurement system could be a error source as well (as tool diflection and the like would be if it were really cutting).

How accurate do you want to get? If it's really a high accuracy machining cell, this will be very hard to do well. The ruler has to have absolute accuracy and precision that is an order of magnatude better than the error that you're trying to measure, and this gets hard and expensive very fast.