584,879 active members*
5,408 visitors online*
Register for free
Login
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2017
    Posts
    591

    "equal to" operator not working in script

    Im working on a macro for fine tuning my A axis index points of 0, 90,180, 270. basically it accounts for pulley variation and encoder error. After the A axis is commanded to a position, the adjustment macro is called which starts with:

    A = GetOEMDRO(803)

    This part works fine and returns the value that is currently in A axis dro. Then i run through some if then statements to move the axis to corrected value. For example:

    if A = 90 Then
    code "G0 A89.9780" // calibrated position for exactly 90 degrees from 0
    elseIf A = 180 Then
    code "G0 A179.7803" // calibrated position for exactly 180 degrees from 0
    etc.

    Problem is, A = 90 and A = 180 etc isnt working. I also tried exactly as its shown in dro, like 90.0000, still doesnt work.

    I tried using a range of more than and less then, which seems to work. for example:

    If A < 90.01 And A > 89.99 Then
    code "G0 A89.9780"

    Why does it work this way but not when using "=" ?

  2. #2
    Join Date
    Dec 2013
    Posts
    5717

    Re: "equal to" operator not working in script

    I have run into this problem in programming. It may be that 90 is never = 90, but rather 90.0000001 due to floating point rounding errors. And like you I have solved the problem by using < and >, or by truncating the value to the needed accuracy.
    Jim Dawson
    Sandy, Oregon, USA

  3. #3
    Join Date
    Nov 2017
    Posts
    591

    Re: "equal to" operator not working in script

    i bet thats exactly what it is. I forgot mach3 is using something like 12 decimal places even though only 4 are displayed. since my steps per degree is 45.51111111111, 90 degrees is going to round to nearest step which puts dro at 90.000000002197. Not a big deal to use the <> range, at least now i know why. thanks!

  4. #4
    Join Date
    Mar 2003
    Posts
    35538

    Re: "equal to" operator not working in script

    Try something like this, to see what the actual value of A is.

    A2 = FormatNumber(A,15)
    MsgBox (A2)

    EDIT
    Looks like it might need to be:
    A2 = Format(A, ##0.000000000000000000)
    Gerry

    UCCNC 2017 Screenset
    http://www.thecncwoodworker.com/2017.html

    Mach3 2010 Screenset
    http://www.thecncwoodworker.com/2010.html

    JointCAM - CNC Dovetails & Box Joints
    http://www.g-forcecnc.com/jointcam.html

    (Note: The opinions expressed in this post are my own and are not necessarily those of CNCzone and its management)

  5. #5
    Join Date
    Nov 2017
    Posts
    591
    Quote Originally Posted by ger21 View Post
    Try something like this, to see what the actual value of A is.

    A2 = FormatNumber(A,15)
    MsgBox (A2)

    EDIT
    Looks like it might need to be:
    A2 = Format(A, ##0.000000000000000000)
    I'll try it out, good to know I can check the actual value like that.

    I wonder if I can actually use this to allow the use of equals. I can format A2 as only 2 or 3 decimal places, then use it in the rest of my script. At that point it should truly be equal to 90, 180, 270. I'll test it tonight. Probably what Jim meant by truncating the number

  6. #6
    Join Date
    Mar 2003
    Posts
    35538

    Re: "equal to" operator not working in script

    The scripting Mach3 uses, Cypress Enable, is a little different than VB, which is why I added the Format above. The Format returns a string, so you'd need a different function to get rid of the decimals, but yes, that's the best way to go about it.
    Gerry

    UCCNC 2017 Screenset
    http://www.thecncwoodworker.com/2017.html

    Mach3 2010 Screenset
    http://www.thecncwoodworker.com/2010.html

    JointCAM - CNC Dovetails & Box Joints
    http://www.g-forcecnc.com/jointcam.html

    (Note: The opinions expressed in this post are my own and are not necessarily those of CNCzone and its management)

  7. #7
    Join Date
    May 2015
    Posts
    1422

    Re: "equal to" operator not working in script

    This is a pretty common issue with floating point numbers in any software.

    Depending on the language I'll write an "isClose" utility function which takes the two numbers being compared, an optional precision eg 0.001, and contains:
    return ((a - precision <= b) && (a + precision >= b))

    or just use ABS(a-b)<0.001 instead of a==b

  8. #8
    Join Date
    Jun 2010
    Posts
    4252

    Re: "equal to" operator not working in script

    This is a pretty common issue with floating point numbers in any software.
    The bane of novice programmers in almost any language.

    Cheers
    Roger

  9. #9
    Join Date
    Jun 2015
    Posts
    4131

    Re: "equal to" operator not working in script

    Im working on a macro for fine tuning my A axis index points of 0, 90,180, 270
    hy quin if you wish to fine tune your code, then maybe this helps :
    ... unidirectional positioning : for 90*, always approach from 89*; same, for 180*, always approrach from 179*; same, for 270* approrach from 269*; for 0*, approach from 359*; thus, for x*, always approach from x*-angle_value_greater_then_backlash(avgtn)
    ... always execute avgtn, at a small feedrate

    if you will implement those 2, then you may achieve a higher consistency, but in reality, this extra-precision may not always be a must thus, even if you code those inside your itsi-bitsy macro, it won't boost your machine if there is too much wear; play with avgtn and small feeds

    Why does it work this way but not when using "=" ?
    in most cases, for general software, it may be a floating error, but cnc's may be coded differently, and the field that stores data may have noise inside it, that is not present when machine is delivered, but is build up in time because of math calculations; if you wish to find out the error, then try this :
    x=90
    x=x-90
    repeat x=x*10 until_something<>0_will_popp_up; this will tell something about the precision of your cnc

    if this value is aproxE10-3 then you may machine without worries, and also without using macros; if you wish to use macros, then you won't be able to reach a level of calculations that leads to an error sum > then the needed precision, and in many cases you may have to use a particular approach to handle the except yup, as you can see, you may replace 'a=b' with '|a-b|<eps'; this is also present on modern machines, but at a smaller scale, and it leads to misscalculations inside mathematic formulas; if you wish to live with it, you have to keep the numbers in a range that is not affected by noise / kindly
    Ladyhawke - My Delirium, https://www.youtube.com/watch?v=X_bFO1SNRZg

  10. #10
    Join Date
    Jun 2010
    Posts
    4252

    Re: "equal to" operator not working in script

    You do get used to the limitations of floating point calculations.

    Mach3 sometimes annoys me with the formatting of the DROs. I tell the CNC to go to X=40.0 and the DRO shows it has gone to X=39.9997. Why hasn't it gone to where I told it to?
    In reality it can not. The single step resolution of the ball screw is 0.8 microns; the position 'error' is 0.3 microns. That is s close as it can get.

    While this has actually nothing to do with the floating point resolution in the SW, it is in many ways the same problem. The system can only operate to within certain error bands, and hoping to get exact equality is futile. We must work within the abilities or limitations of the system. Yes, that means you have to think.

    Cheers
    Roger

Similar Threads

  1. Profile Rail Wood Frame Router, 25" x 37" x 6" working area
    By jpvonoy in forum CNC Wood Router Project Log
    Replies: 76
    Last Post: 03-01-2019, 03:47 AM
  2. "Probe" and "Verify" not working - Mach3 & ESS
    By Mogal in forum SmoothStepper Motion Control
    Replies: 3
    Last Post: 03-17-2017, 07:05 PM
  3. Need help working out feed rate for "C" Axis and "Z" axis simultaneously.
    By Jonaldhinio in forum Australia, New Zealand Club House
    Replies: 0
    Last Post: 08-12-2014, 09:27 AM
  4. Replies: 0
    Last Post: 10-11-2013, 06:50 AM
  5. X Axis "Goes Off Pattern", "Awry", "Skewed", "Travels"
    By DaDaDaddio in forum Laser Engraving / Cutting Machine General Topics
    Replies: 1
    Last Post: 05-06-2013, 09:59 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •