angle calculation g68


 

Hi
 
I finish a small macro which calculates the angle of the stock on the table
travel is ok
the distance traveled between the 2 points is 20mm and the withdrawal is 3mm
I'm stuck, what code used to calculate the angle??

THANKS


 

Angle = Arcsin 3/20

 

From: MachCNC@groups.io <MachCNC@groups.io> On Behalf Of res.amazonite@...
Sent: Sunday, February 4, 2024 12:04 PM
To: MachCNC@groups.io
Subject: [MachCNC] angle calculation g68

 

Hi

 

I finish a small macro which calculates the angle of the stock on the table

travel is ok

the distance traveled between the 2 points is 20mm and the withdrawal is 3mm

I'm stuck, what code used to calculate the angle??

THANKS


 
Змінено

Surely the angle is given by arctan. For small angles arctan and arcsin will give similar answers but they will quickly diverge as the angle is increased. Imagine going up 20mm and along 20mm, 20/20=1,  arcsin(1) = 90° but arctan(1) = 45°

The Gcode used for this function in Mach3 is ATAN. Examples of using this in Gcode can be found in the user manual under "Unary Operation Value". For the given example using Mach3. Decide on a parameter for the angle eg #51 for the angle and the angle will be given by #51=ATAN[3/20] . You can also use parameters for the Z move and the X (or Y) move. So if you have #52=3 and #53=20 you then have:

#52=3
#53=20
g01 z #52
g01 x #53
#51 = atan[#52/#53]

What you do with that value is up to you. If you want to you can set up a DRO on the screen using something like the program Machscreen to edit the Mach3 screens to display it during running of the program.

:


 

I'm not a great programmer
thank you all, I'll test and let you know


 

Because he’s trying to calculate an angle, not the length of a side.

 

From: MachCNC@groups.io <MachCNC@groups.io> On Behalf Of Wesley Wilcox
Sent: Monday, February 5, 2024 3:26 PM
To: MachCNC@groups.io
Subject: Re: [MachCNC] angle calculation g68

 

If you are traveling along 20mm and going up 3mm, why not just use Pythagoras rule? a squared + b squared = c squared.


 

On Mon, Feb 5, 2024 at 12:52 PM, Andy Wander wrote:
If you are traveling along 20mm and going up 3mm, why not just use Pythagoras rule? a squared + b squared = c squared.
hi
simply because I'm terrible at math

c squared = my angle??


 

Sorry about the mix up, I thought you were chasing a length. 


 

Pythagoras will give you the distance between the start point and the end point. So if D is the distance between the start and end points up 3mm and across 20mm then D²=3²+20²    D²=9+400=409    so D is the square root of 409 which is 20.22mm and is not an angle. It is not what you asked for.

Are you using a probe to check stock angle? For example descend until the probe makes contact, set z axis to zero, rise something like 20mm, move across 20mm then descend until the probe touches, note reading on z axis. If so then ATAN is what you should use. The problem is MACH3 does not make it easy to then do something based on the result, there is no IF option in MACH3 but you can do it in a macro so you could have something that puts out the message "The angle is" or lets you know if the part is simply too steep or too shallow. So if you are writing a macro then that should make what you want to do possible.

Example macro, note I have not checked but I think 802 is the correct value for the Z axis. What you do in the macro after calculating the angle depends on what you propose to do with the information. I also have not checked what I have written it is just based on things I have done in the past for probing.

Angle = -89
Hfeed = 20
Feed1 = 30
Feed2 = 200
NegMove = -50
Posmove = 20

Code "G91 G31 F" & Feed1 & " Z" & NegMove
  While IsMoving()
  Wend

SetOEMDRO(802,0)

Code "G91 G31 F" & Feed2 & " Z" & PosMove
Code "G91 G31 F" & Feed2 & " X" & PosMove
 
 Code "G91 G31 F" & Feed1 & " Z" & NegMove
  While IsMoving()
  Wend
Zdro = GetOEMDRO(802)

Angle = ATAN(Zdro/20)

 






 

On Tue, Feb 6, 2024 at 01:18 AM, Martin Connelly wrote:


 

thank you martin for this approach

yes, I use a probe and GetOEMDRO(802) is indeed the Z

you start by probing the Z and then do the final calculation
Angle = ATAN(Zdro/20)
you declare a variable: Angle = -89, what is this variable used for??
for my part, I do a first lateral probing on the X- and move 20mm towards the X+ after a withdrawal of 3mm
 
I started a macro, can I put it in this thread??


 

I just like to declare variables to remind me what is in a macro. I also like to put a really unlikely value in something that is going to be calculated so I can tell when it has been changed in the macro. If you write a long macro you do not really want to search for the point at which you first set a variable to some value so it is better to list them all together at the beginning but it is not a necessity. You can also add comments to the line with the declaration in if you want that reminds you later, maybe a year later for example, of what that variable is for.

The order of probing is a personal choice and depends on what you are doing. As I don't have a full understanding of what you are doing I just chose a general probing order.


 

I have been thinking a little bit more on this. It occurred to me that you may be better just reading the Z axis DRO into two variables and using the difference between those two values to calculate the angle. That way you would not loose your Z axis setting. Like this:

Angle = -89
Hfeed = 20
Feed1 = 30
Feed2 = 200
NegMove = -50
Posmove = 20
Zdro1 = 0  
Zdro2 = 0
Zdifference = 50

Code "G91 G31 F" & Feed1 & " Z" & NegMove
  While IsMoving()
  Wend

Zdro1 = GetOEMDRO(802)

Code "G91 G31 F" & Feed2 & " Z" & PosMove
Code "G91 G31 F" & Feed2 & " X" & PosMove
 
Code "G91 G31 F" & Feed1 & " Z" & NegMove
  While IsMoving()
  Wend

Zdro2 = GetOEMDRO(802)

Zdifference = Zdro2-Zdro1

Angle = ATAN(Zdifference/20)


Note, if you get a negative value for the angle when you want it to be positive then use Zdifference = Zdro1-Zdro2
 
 


 

Much simpler is go to this Website and enter the dimensions you have and get the result immediately.
https://www.engineersedge.com/calculators/triangle_solution_3.htm

Emgee


 

Thank you for the link

but the goal is to obtain the result of the angle using a mach3 macro


 

On Wed, Feb 7, 2024 at 06:12 AM, Martin Connelly wrote:
I have been thinking a little bit more on this. It occurred to me that you may be better just reading the Z axis DRO into two variables and using the difference between those two values to calculate the angle. That way you would not loose your Z axis setting. Like this:
thank you Martin for this reflection,
I will test all this by replacing the Z with the Y
I will keep you informed of my tests

Note, if you get a negative value for the angle when you want it to be positive then use Zdifference = Zdro1-Zdro2
it all depends on how the piece is fixed on the table


 

Hi

after several attempts Atan is not taken into account in the GCode, it is possible that the function is not implemented in the controller card (Chinese)

I fell back on this formula

AB = 100' distance
BC = 5' distance Y
angle = Tan(BC/AB) 'angle in radian
MsgBox(angle/(2*3.14159/360) & " degrees") 'result converted to °
 
here is the macro as it stands which needs to be checked, all criticism is good to take
for the moment it is functional on the X
Dim angleX As Double 
 
Const rechercheMax = 10
Const degagement = 5
Const deplacementMaxPointB = 20 'un GETOEMDRO(1001) sera utilise plus tard pour choisir la deplacementMaxPointB
Const vitesseRapide = 1000
Const vitesseApproche = 100
Const vitesseLente = 50
YDro1 = 0  
YDro2 = 0
 
'deplacementMaxPointB = GETOEMDRO(1001) ' distance max à parcourir
 
SETOEMDRO(800,0) 'Mise à 0 de l'axe X
SETOEMDRO(801,0) 'Mise à 0 de l'axe Y
While isMoving()
Wend
 
'******************* 1er MESURE *******************
 
CODE "G31 Y"& rechercheMax &" F"& vitesseApproche
While isMoving()
Wend
 
YDro1 = GetOEMDRO(801)' Lit la valeur de Y
 
YDro1= YDro1 - degagement ' nouvelle valeur de Y
CODE "G1 Y" & YDro1 & "F"& vitesseRapide 
While isMoving()
Wend
 
CODE "G31 Y"& rechercheMax &" F"& vitesseLente
While isMoving()
Wend
 
YDro1= GETOEMDRO(801) ' Lit la valeur de Y
 
YDro1= YDro1 - degagement ' nouvelle valeur de Y
CODE "G1 Y" & YDro1 & "F" & vitesseRapide
While isMoving()
Wend
 
'******************* 2éme MESURE *******************
 
code "g1 x" & deplacementMaxPointB &" F"& vitesseRapide  'Deplacement pour la 2éme mesure
CODE "G31 Y"& rechercheMax &" F" & vitesseApproche
While isMoving()
Wend
 
YDro2 = GetOEMDRO(801)' Lit la valeur de Y
 
YDro2= YDro2 - degagement ' nouvelle valeur de Y
CODE "G1 Y" & YDro2 & " F" & vitesseRapide 
While isMoving()
Wend
 
CODE "G31 Y"& rechercheMax &" F"& vitesseLente
While isMoving()
Wend
 
YDro2= YDro2 - degagement ' nouvelle valeur de Y
CODE "G1 Y" & YDro2 & " F"& vitesseRapide 
While isMoving()
Wend
 
'******************* CACUL FINAL DE L'ANGLE ************************************  
 
YDifference = YDro2-YDro1
MsgBox ("La difference sur X est de: " & YDifference & "mm")
angleX = Tan(YDifference/deplacementMaxPointB) 'angle en radian
MsgBox("L'angle sur X est de: " & angleX/(2*3.14159265359/360) & " degres") 'resultat converti en degres  


 
Змінено

It never occurred to me that they did not implement Atan in Mach3 scripts since it is a standard VB function. There is a simple work around though using what are known as the Taylor Series for trig functions so the solution for finding the angle in degrees is:

slope=Zdifference/20
 
 Angle = 180*(slope-(slope^3)/3+(slope^5)/5-(slope^7)/7+(slope^9)/9)/3.14159
 
This is good to about 7 decimal places for angles below 20° so should be good enough for your needs. Once again I have not tried implementing it in Mach3 but it should work as it is using simple multiplication and division that I know works. If the exponent ^ does not work then just use slope*slope*slope for slope^3 etc. Based on what you did previously you should recognise the 180 and the 3.142589 for converting radians to degrees.

If you want better accuracy beyond 20° you just need to add more terms to the series increasing the exponent by 2 each time and alternating + and - . There is an issue of diminishing returns as you increase the angle, You need far more terms to get reasonable accuracy up to 35° for example. When you get as far as -(slope^15)/15 you are down to an accuracy of 2 decimal places for the angle 35°, one decimal place at about 38°.


 

hello Martin
 
I implemented the function calls the Taylor series
it worked well now, I don't think the measurements for angles less/greater than 20° are
justified for me the maximum could be less than 10°
 
now that the script is functional, I will look at how to integrate the result into the gcode
if you have an idea, I'm interested


 
Змінено

Glad I could help with the angle calculation. Here is something to consider. If you are not currently using the A axis you can make use of it. The A axis is rotation about the linear X axis and uses DRO 803. You can set the A axis to be active, write a value into the A axis DRO and then when you leave the script it will be there for you to see on screen.

There is also the option to do the angle calculation in Gcode using G31. When you probe using G31 the values of X Y and Z at the probing point are written to parameters #2000, #2001, #2002.

For example set up parameters

#50 = 0 (X axis change, you could set this to 20)
#51 = 0 (Y axis change, may not be needed)
#52 = 0 (Z axis change, you could set this to 3)
#53 = 0 (slope, may not be needed)
#54 = 0 (angle)
#1000 = 0 (temporary variable)
#1001 = 0 (temporary variable, may not be needed)
#1002 = 0 (temporary variable)

So go to your first probing point, set your own temporary variables to the values at the probing point.

#1000=#2000
#1001=#2001 (may not be needed)
#1002=#2002

Go to your second probing point then the axis changes will be given by:

#50 = #2000 - #1000
#51 = #2001 - #1001 (may not be needed)
#52 = #2002 - #1002

You may need to change the order of the variables in the subtraction if the angle comes out as negative when you want it to be positive.

You can then calculate the angle of the probed face using Atan in Gcode

#54 = Atan[#52]/[#50]

Once again I have not checked this out on my machine.

If I was setting this up I would probe using Z axis movement, not X axis movement. For a shallow slope a small variation in Z axis positioning will give a large variation in the X axis result, in this case at a ratio of 20:3. So if you move the Z axis and it is 0.01mm out of position then the X axis probe error will be in the ratio of 20:3, 0.0667mm. If you move the X axis and have an error in position of 0.01mm then probe in the Z axis then the error will be smaller in the ratio of 3:20 so 0.0015mm

If doing this in Gcode and not using the A axis you can also use G0 A #54 to set the A axis DRO to the calculated angle (make sure you are in G90 absolute mode for this or you will get odd results).


 

hello Martin
I thank you very much for the time you take to help me
I managed to turn the drawing on the screen with the following commands
Code “G68 A0 B0 R” & Final result
Sleep(150)
doOEMButton (160)
I finish this way of doing things and then I start again by trying your method with the G31
but as said above, the function (atan) does not work for me why is it the grand mistere
I think that the function is not implemented in the Chinese controller card that I use
fortunately, I have an AXBB-E ready to be assembled
I will soon start testing on my machine, I test with the F7 command of the Mach3 VB editor and it seems to work