Quick Links to - Sep13 Oct13 Nov13 Dec13 Jan14 Feb14 Mar14 Apr14 May14 JunJul14
There are those who don't enjoy maths, and others who say they can't do maths, so happily there will be nothing in these articles that calls for use of the abacus, slide rule or calculator. Fortunately, the Mach3 dialect of the G code machine control language includes a sufficient range of mathematical functions to cover most of the machining moves we are likely to dream up. Rather than performing any tiresome number-crunching ourselves, a little time may be spent in the altogether more satisfying and enjoyable process of working out a method, passing the method over to the computer through our programme listings, and then standing well clear and watching the machine (or the simulation) do all the hard work for us.
In devising a method it is helpful, essential even, to have some appreciation of the basic geometry involved in plotting simple movements in a three dimensional space. However, the space available here on the printed page would not permit rigorous derivation 'from first principles', rather here is presented a quick revision of what most of us have seen before, even if it was a long time ago. For those who would like some greater detail, we have set up a web site to accompany these articles at :- www.jons-workshop.com There you will also find complete code listings for all the examples given as well as some further useful hints, tips and links.
The Essential Geometry – polar and Cartesian coordinates
Two frequently used ways of describing the position of some point in a two dimensional plane are illustrated in Fig 450. In one method the position is described as being at some known distance and angle from a reference point. Two values are needed to specify the relative position: the distance and the angle from the reference point. This expresses the point position in 'polar coordinates'.
Alternatively, the offsets along two perpendicular axes (most often the 'X' and 'Y' axes for our purposes) may be specified in terms of 'X' and 'Y'. This expresses the relative position of point 'P' in 'Cartesian coordinates'. Both methods tell us where point 'P' is in relation to the reference point, and both methods require two pieces of information. They are equivalent and no one is any better than the other, however, in our work with simple CNC machines, most of what we do uses Cartesian coordinates.
Sometimes the polar notation appears easier, for example when drilling holes around a flange or milling around the inside or outside of some regular polygon. Fortunately the conversion from polar to Cartesian coordinates can be performed very easily using only two lines of Mach3 G code, and this will prove useful time and again. The conversion the other way from Cartesian to polar, while simple enough in mathematical terms, is not so easy using the limited resources Mach3 makes available to us, more of which anon.
Fig 451 shows the classic introduction to the trigonometric functions sine, cosine and tangent, being simply ratios of side lengths of a right angled triangle. While useful, Fig 451 may give the misleading impression that sines, cosines and tangents only relate to angles less than 90° (degrees) or a quarter of a circles-worth. Fig 452, 'The Unit Circle', gives the bigger picture. The point 'P' may be moved to any point around the unit circle in either direction so that the angle 'Ө' (the Greek letter 'theta') may take any value from -360° to +360°.
Moving point 'P' anti clockwise from the position shown, as 'P' approaches the highest point where X is zero and Y is one, note the tangent line rapidly gets much longer as it becomes more nearly horizontal. As 'P' touches point (0,1), the tangent line becomes horizontal, parallel to the X axis, therefore not meeting the X axis in either direction and the value of the tangent function is indeterminate at this point. The value then increases from minus infinity as the point (0,1) is crossed, and the tangent line flips to the left of the circle.
The value has increased (become less negative) to -1.0 by the time 'P' is three eighths of the way around and has increased to zero again once half way around. The tangent function is described as 'discontinuous' because of the sudden switches between positive and negative infinite values. The sine and cosine values in contrast are much better behaved, moving smoothly and continuously between maximum values of +1.0 and minimum values of -1.0. Computers are not so good at coping with infinity, and so the sine and cosine functions are likely to be more useful to us than the tangent. The sine, cosine and tangent functions are plotted for one complete revolution in Fig 454.
From Fig 452 we can see that the value of the sine function is positive for all placings of point 'P' around the top half of the unit circle, and negative everywhere around the lower half. Similarly, the cosine function is positive for all points around the right half of the circle and negative for all points around the left half.
The sine, cosine and tangent functions all take on positive values for two quarters, or quadrants, of the circle, and negative values for the other two. Fig 453 reminds us which functions have positive values in which quadrants. Maths texts often name this the 'All Silly Tom Cats' diagram as a way of remembering the order. As engineers we may prefer to think 'CAST'. Either way you still need to remember where you are starting from! To help, remember that in the first quadrant turning anticlockwise from from zero degrees at three o'clock, sine, cosine and tangent are all positive. In the other three quadrants only one is positive while the other two are negative.
Fig 453 also reminds us of the convention used to describe angles. By convention the zero degrees direction is defined as horizontal to the right, at three o'clock, or east, as you prefer. Angle increases becoming more positive (or less negative) in an anticlockwise rotation, and less positive (or more negative) clockwise. It might seem odd choosing 'east' for zero angle but this is the convention upon which all the definitions are based, and we deviate from this at our peril.
There are two systems in use for measurement of angle. The more familiar is probably the 360 degrees (360°) in a circle. The alternative measure is the radian. There are approximately 6.284 or more precisely two times 'pi' (2 * π) radians to make a circle – a radian is the angle you get by bending a radius around the outside, the circumference, of the circle (about 57.3°). Mach3 accepts angles in degrees, however most other software used in maths and engineering (spreadsheets for example) are more likely to use radians (in fact Mach3 uses radians internally but hides that from the user by doing conversions unseen).
In working with angles it makes sense to try to work within a range. You may choose 0° to +360°, or -180° to +180°, or even -360° to +360° it makes no difference. The functions within Mach3 (and most other software) are smart enough to add or subtract any number of whole circles-worth of angle to make sure they are in a sensible range before use.
More about Pythagoras Theorem
“For a right angled triangle, the square on the hypotenuse is equal to the sum of the squares on the other two sides.”
In trying to work out formulae to put into programme listings, finding the length of a side of a triangle is a common requirement, and often one of the 'known' sides will be some factor of the sine or cosine function which may have a positive or negative value. This is of no consequence when using Pythagoras because the square of any number, positive or negative, is positive. As well as the familiar formula to find the hypotenuse, or longest side, it can be rearranged to find any side if the other two are known, the various uses are listed in Fig 455.
Interestingly, this theorem can be extended into three dimensions and the distance between two points anywhere in a three dimensional space may be found using the form:
D2 = X2 + Y2 + Z2
∴ D = √ (X2 + Y2 + Z2)
A final thought about squares and square roots – any positive number squared produces the same positive result as the corresponding negative number squared. It follows that the square root function ought to return two values, one positive the other negative. Often the negative root has no useful meaning, when using Pythagoras to find the length of the side of a triangle for example.
Cartesian to polar conversion
Returning to the difficulties involved in Cartesian to polar conversion, calculating the 'Distance' is a straightforward application of Pythagoras theorem, but finding the angle requires using one of the 'Inverse Trig functions'. The inverse of the 'sin', 'cos' and 'tan' functions are written 'arc sin', 'arc cos' and 'arc tan', usually implemented as computer functions named 'ASIN', 'ACOS' and 'ATAN'.
The meaning of 'arc sin' is “the angle whose sine is”, 'arc cos' means “the angle whose cosine is”, and 'arc tan', “the angle whose tangent is”. But there are problems. As shown in Fig 454, the value of each of the functions sin, cos and tan all pass through the same value at two different angles around the circle. It follows that as the inverse functions ASIN, ACOS and ATAN are capable of returning only one value for angle, it may well be the 'wrong' angle. There is a further problem using ATAN because there are two angles around the circle where the value of tangent is indeterminate and as luck would have it, these are two of the angles we will encounter regularly. In a code equation we may write something of the form:
Angle = ATAN['O' / 'A']
In trying to execute this line, the computer would first attempt the division ['O'/'A'] to calculate the single parameter value required by the ATAN function. The tangents are indeterminate in the case where 'A' = zero and an attempt to divide by zero (if not detected and trapped earlier) is an “error”, or to put it another way, something computers do not easily handle. However both of these problems have been addressed in many modern applications by providing a modified version of ATAN, usually named ATAN2. Whereas ASIN, ACOS and ATAN all operate on a single parameter, e.g.:
Angle = ASIN (Number)
the ATAN2 function takes two parameters, e.g.:
Angle = ATAN2 ('A' ; 'O')
Because ATAN2 takes both X and Y parameters, and the polarities of this pair indicate the quadrant, ATAN2 is capable of unambiguously determining the correct angle. It is also clever enough to handle cases where either parameter is zero, and the only way of tripping it up is to give both as zero.
However, Mach3 provides no access to 'ATAN2' – one of many minor irritations – but we'll meet ASIN and ACOS again.
Polar to Cartesian Coordinates Conversion
This conversion is straightforward and very useful. From Fig 452, the X and Y offsets of a point 'P' at a fixed distance of one unit at any angle Ө are seen to be :
x = cos Ө, and
y = sin Ө
If the circle is now scaled by some factor 'R', such that the new radius = 'R', then the X and Y offsets of a point 'P' at a distance of 'R' units at any angle Ө must be similarly scaled by the factor 'R':
x = R * cos Ө, and
y = R * sin Ө
Therefore the absolute coordinates are:
Vertex X = Centre X + (R * cos Ө)
Vertex Y = Centre Y + (R * sin Ө)
In Mach3 G code, with a couple of lines of supporting comment, this might look like:
; Next X = Centre X + [Radius * Cos[Next Angle]] ; Next Y = Centre Y + [Radius * Sin[Next Angle]] #162 = [#154 + [#160 * cos[#161]]] #163 = [#155 + [#160 * sin[#161]]]
Where X and Y co-ordinates of the polar centre are, for this example, held in parameter memory locations #154 and #155, the radius in #160 and the angle in #161.
Polar to Cartesian conversion may suggest itself as being worthy of a subroutine for use time and again, however the two lines of code involved hardly justify the effort of coding a subroutine which would also involve including some ties on what data was expected to be stored where, and so on.
Continued in Part Seven…