Hand-Coding G Code for a Curved Handrail

Lacking software to automate the job, a CNC operator wrote the code for a complex handrail job on a 5-axis CNC router himself. Here's an extended discussion, with photos, including input from people who prefer the automated route. July 29, 2012

I thought that maybe someone here might be interested in a process I worked out for drawing tool paths for curved handrails. I looked at some 5 axis CNC programs and observed that the movements were a whole lot of little straight line moves, each being around 0.3125 inches long. The following is a process to draw such a path. This is a really simple concept. It gets a little more complicated to make it practical, but this shows the basic idea.

Pick start point of line in elevation view (PT1).

Pick start point of arc in plan view (PT2) and (PT2A). (These two points are the same.)

Start of process: Move along arc for an arc length of 0.001 (PT2B). (0.001 = arbitrarily tinny distance.)

Move horizontally under line in elevation view from PT1 for a distance of 0.001 (PT1A).

Move upward from PT1A until you intersect the elevation line (PT1B).

Use the X and Y values of PT2B for the X and Y values of point PT2C and use the Y value of PT1B as the Z value of PT2C. (In the illustration PT2C would actually be coming out of the screen toward you directly above PT2B rather than as shown.)

If the distance from PT2A to PT2C is less than 0.3125 then:

Move PT1A and PT2B an additional 0.001.

Recalculate points PT1B and PT2C.

Repeat this until the distance from PT2A to PT2C is equal to or
greater than 0.3125.

Draw a line from PT2A to PT2C.

Change the X, Y, and Z values of PT2A to the values of PT2C.

Repeat the above process for the entire length of the arc in the plan view.

This process can be made to work for any combination of lines and arcs in the elevation and plan views. I had the elevation and plan views drawn for the center line of the bottom of the rail and then had the actual tool paths drawn according to where the tool center line path should be in relation to that.

Click here for higher quality, full size image

Forum Responses
(CAD Forum)
From contributor L:
As I explained earlier, point PT2C is on the center line of the bottom of the rail. To get the point needed for the center line path of the tool, I need the distance from the center line of the profile to the center line of the tool (offset) and the distance from the bottom of the profile to the point being programmed for on the tool, usually the bottom of the tool (depth).

I then come straight out from PT2C for the distance offset and straight up for the distance offset to get a temporary point. I then rotate that temporary point so that it is lined up with the center of the arc in the plan view. This will be the angle of C axis of the router. Next, I rotate the temporary point to an angle that is perpendicular to the line in the elevation view. This will be the angle of the B axis of the router. The temporary point is now where it needs to be for the tool path (new PT2C). Note that the rail is routed with the profile up-side-down, but the paths are drawn with the profile up-side-right. The path is then rotated into the position it would be in on the router.

From contributor B:
I am assuming you have a 5 axis machine and you're just trying to write your own programming? I am unfamiliar with this but isn't the software already available?

From the original questioner:
Yes I was just writing my own programming for a 5 axis router. Iím sure that there is software out there now, but I couldnít find anything back when I was trying to figure this out. At that time the company was cutting rail on the router, but the software they were using was a DOS exe and was basically a black box. There were a few things that they didnít like about it and the IT department wanted to find a Windows program to replace it. The company manager was looking into it and a few other people were also. I was just the AutoCAD guy, but I thought that I would see if I could find something too.

In the meantime it drove me nuts that I had no idea how this 5 axis router worked and so I was looking at the programs and trying to back-engineer between the G-code and the part. After I figured it out I told my supervisor that I thought I could write my own software using AutoCAD and he said to go for it. I donít know if this was the best way to go, but it was a tremendous amount of fun.

From contributor C:
Graphing in the other direction: First, our methods are approximations of the bounded region. The area we're defining by an infinitesimal amount of minutely short line segments will only approximately bound the region. In other words, it is not exact (rough).

Secondly, functions can be written arcos any domain from "A to B", "B to C", C to D", etc. with exactness.

Thirdly, function programming is continuous across the profile, not tangent (as your describing).

Function programming can be shifted and rotated in any direction. This is a versatile programming method whose endpoint is a smoother operation, can be integrated, and written parametricly (time). If you want to program complex plane curves, your application must have a math library with algebra, trig, and log functions.

From contributor B:
That's interesting. As far as it appears to me, you have worked out how to plot a series of 3D position-points along a regular spiral line or "true helix" and then angled the cutter perpendicular to its' pitch? Is there an easier way?

For a typical solid handrail length of 5', you would need at least 192 individual reference points (60" / .3125) for the inside edge alone (is that correct)? Of course every stair is different and would therefore have its own required geometry (that seems like an awful lot of code to manually compute every time). There are also helical curves which are not generated from regular (straight) spiral lines but from curved lines required for pitch changes within the cylindrical plan. What about handrails which are taller than a typical single cutters' capacity? Would you then have to stack a series of perfectly parallel position points? There are a lot of curiosities here.

I am wondering if a true helical handrail might be computed quicker by using "helix-radius and torsion calculations" instead of your "quantum-calculus" approach? Since we're really trying to re-invent the (helical) wheel here, I wonder how it's already being done? What are some of the objections (or limitations) to the programming that you are familiar with?

I know of one company who claims to write all their own tool paths (claiming that commercially available software programs are inadequate). I thought "tangent handrail" was complicated!

From the original questioner:
Contributor C - I didnít seem to have any problem with accuracy. The rails looked great. I think that the radius of the tool and the action of the B and C axis rotating from one point to the next helped smooth things out. As far as I could tell, these little straight line moves are what the router required for 5 axis application. The controller was an older Fanuc. I donít know how old, but there was no 5 axis tool compensation, if that means anything.

As for the math used, I did have to incorporate some algebra and trig, but a great deal of the math was done for me by AutoCAD. One thing to consider is that I wasnít trying to program complex plane curves as in a graphical representation of the rail, but rather to come up with what the router required.

Contributor B - plotting a series of 3D position points is pretty much it (with lines drawn between those points). Perhaps someone knows if there is an easier way. You are right about the 192 points. Not only is that just for one edge, but also just for one pass of one tool. It might take a lot of passes of a rough cutter going up one side and down the other, and it also has to plane the bottom of the rail. The profile tool made a rough pass and a finish pass. There were more operations in addition to that, so the CNC files were huge and were drip fed to the router.

The points didnít have to be manually computed for each stair. This was an AutoCAD automation and its only purpose was to draw paths of those little line segments. It was only necessary to draw an elevation view (un-curved that is) and a plan view of the rail. That was also later automated, but that is another story.

Rail that was not a regular spiral was no problem, as in the case of pitch transitions like you would have with straight treads at the top or bottom of a circular stair. I wrote the program to work for any combination of lines and arcs in the elevation and plan drawing. For example, you could replace the line in the elevation drawing with an arc. The principle still works the same. You get your Z value by going straight up until you intersect the arc just as you would go straight up until you intersect the line. You could also replace the arc in the plan drawing with a line.

It was also possible to have more than one segment in each drawing (an arc and a line in the plan view for example) as long as the elevation and plan view matched. There was no problem doing tall rail that required two profile cutters. I donít know if a stack of perfectly parallel position points were produced or not. If the distance from the center of the profile to the center of the tools was the same, it probably did. I donít think that it would matter as everything would tend to smooth out and blend at that scale. Anyway, they liked the rail that they got.

"Helix-radius and torsion calculations" might be faster than my "quantum-calculus", but since the computer was doing the calculations it was fast enough. I probably am trying to re-invent the wheel here. I would love to know how it is being done elsewhere.

You asked about what are some of the objections (or limitations) to the programming that I am familiar with. Well, I guess the problem is that I am not actually familiar with any other software. It was a very long time ago that I looked for something and couldnít find anything. I do remember a few things that they didnít like about the DOS program that they were using. One was that it didnít drill balusters holes at exactly the right angle and that made it a bit difficult to install them. Another was that it didnít cut the rail to exact lengths. When the stair was built they would have to overlap the ends of the different rail sections, line up the baluster holes, then mark and cut the ends manually. I think that the biggest thing they didnít like was that the software could not be changed in any way.

I am quite pleased to know that there is a company out there that likes to ďroll their ownĒ. I wonder what it is that they donít like about the commercially available software. At least if you create your own software you are in full control. You donít have to change how you do anything to fit the software and you can alter the software anytime you want to.

From contributor C:
Programs written as parametric equations incorporate time (T) and thus become continuous (smooth). Spot this into a graphing calculator in parametric mode: x=cos (T), y=sin (T) (0). You will see what I'm talking about, it graphs a unit circle continuously, in one direction, without stopping until it sweeps 360 degrees(2pi).

An equation similar to the one I stated could be written using this moving particle algebra over infinitesimal 2pi intervals while incorporating "z" shifts and increasing and deceasing amplitudes to define, say "a finial". Much, much, much more complex graphing (programs) solving difficult complex curves can be written and run.

From the original questioner:
Contributor C - I see what you mean. What I am doing is actually a much more crude form of that principle. AutoCAD, behind the scene, provides the equations for the plan view arc and the elevation view line. The elevation drawing defines the ďZĒ shifts. My increment of 0.001 would be the equivalent of your time function. To advance along the arc, I end up having to change that to an angular distance in radians. So, it looks to me that what you are doing directly I am accomplishing by going around the barn, over the hill, and down the road.

From contributor G:
I have a file of code and the handrail was wreathed over seven steps with a rise of 194 and there were 3,760 lines of code. The handrails are roughed out of a square block carved, grooved for spindles, and then moulded. Drillings can be done for connections as well.

Click here for higher quality, full size image

From contributor B:
Here's Contributor G's floor plan (left hand version) with volutes, cut and shaped from solid plank (no "lines of code", no CNC, and no required software). Required hardware: bandsaw, spokeshave, and shaper. Contributor G - how do you get a rise of 194 over seven steps?

Click here for higher quality, full size image

From contributor G:
It was seven at 194, I can do it by hand too.

From contributor C:
Contributor B - that's beautiful, I totally get it! The time we spend programming, the full scale drawing can be converted into a one of a kind by hand, seriously accurate, materials hand selected from the lumber yard, scraping tools hand ground, and etc. I'm a proponent of this procedure for custom one-off but then I'm not afraid to get out of my engineering work station, lose a few pounds, be productive-carving tool in hand, and sleep well at night.

From the original questioner:
There is one final piece to the puzzle. Any one tool path segment alone does not contain enough information to know how to angle the router head. A line perpendicular to a path segment can rotate 360 degrees around the axis of that segment and still be perpendicular to it. As it turns out, the imaginary line that represents the depth of cut is what the axis of the router tool needs to be lined up with, so I added tiny line segments that I call normals to my path that match those imaginary depth lines. When I extract the G-code information from the tool path, The X,Y, and Z values are extracted from the tool path segments and the B and C axis information is extracted from the normals.

From the original questioner:
In my last post I said that drawing the normal was the last piece of the puzzle. That is true for drawing the tool path. However, CNC code cannot be generated from that path yet. The tool path is drawn alongside the rail with the rail in the orientation that it would be in on the stair in 3D space. That is not the orientation that the tool path needs to be in on the router.

The rail is not actually drawn, only the tool path is drawn. The rail is imaginary. For the section of rail to be cut out, I draw an imaginary line from one end of the imaginary rail section to the other end. I then rotate the imaginary line, the imaginary rail, and the real tool path around the Z axis so that the imaginary line is parallel with the X axis. Next I rotate everything around the Y axis so that my imaginary line is flat in the Z. This makes my line parallel with the router table. I then rotate everything around the Y axis 180 degrees. This makes the rail bottom-side-up as this is how it is cut out. Last, I move everything to a particular point so that it is in relation to the origin as it needs to be in relation to the routerís origin. I am now ready to generate code from the tool path.

Generating the G-code for the 5-axis router from the tool path is an interesting process. To draw all of the tool paths for a section of rail and generate a CNC program with 3600 lines of code takes about two minutes. I think that is a subject for CNC rather than CAD.