Hello Guest it is March 29, 2024, 02:29:14 AM

Author Topic: Mach3 script editor bug?  (Read 7554 times)

0 Members and 1 Guest are viewing this topic.

Mach3 script editor bug?
« on: February 08, 2013, 09:40:43 AM »
I can't figure out why this is happening.
If I set BaseX = .020 it works fine.
If I set BaseX = .030 or higher it is a endless loop
Is this a bug in the mach3 vb? I have used VB5 and never seen anything like this.
I am using Demo Version R3.043.066


Dim BaseX As Double
BaseX = .030
Do While (BaseX > .000)

Print "Old BaseX = " & BaseX 
If BaseX = 0.001 Then BaseX = BaseX - .001
If BaseX = 0.002 Then BaseX = BaseX - .001
If BaseX = 0.01 Then BaseX = BaseX - .008
If BaseX > 0.01 Then BaseX = BaseX - .010
Print "new BaseX = " & BaseX
Loop

'Set BaseX = .020
'1st loop OldBaseX= 0.02, NewBaseX = 0.01
'2nd loop OldBaseX= 0.01, NewBaseX = 0.002
'3rd loop OldBaseX= 0.002, NewBaseX = 0.001     
'4th loop OldBaseX= 0, NewBaseX = 0
'Exits loop

'Set BaseX = .030
'1st loop OldBaseX= 0.03, NewBaseX = 0.02
'2nd loop OldBaseX= 0.02, NewBaseX = 0.01
'3rd loop OldBaseX= 0.01, NewBaseX = 0.01     
'4th loop OldBaseX= 0.01, NewBaseX = 0.01
'endless loop???
Re: Mach3 script editor bug?
« Reply #1 on: February 08, 2013, 09:41:46 AM »
I also tried it using Select Case with the same results

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: Mach3 script editor bug?
« Reply #2 on: February 08, 2013, 11:14:21 AM »
I have used VB5 and never seen anything like this.
Then you need to look again ;D because this WILL happen in VB5/6/.NET C, C++ and (sooner or later) on every other programming language on any DIGITAL computer this side of Alpha Centauri.

The problem is that REAL numbers will sometimres have an EXACT representation in BINARY (in whatever given format is being used to represent them on any given system) but SOMETIMES the binary format will be a CLOSE but not EXACT representation. It's physically IMPOSSIBLE to represent ALL real numbers in BINARY or any other numeric base for that matter - try writing down the EXACT result of 10/3 in DECIMAL!

You see your prinout saying BaseX = 0.01 BUT it's more likely something like 0.0099999999999999999999999999999999999.

You end up in an endless loop because you've fallen for the trap of NOT testing for the other possibility i.e. is it LESS than 0.01? Tap it into your code and see.

FWIW - many moons (sic) back - I heard the possibly apocryphal story that Apollo 13 actually went tits up because some NASA rookie programmer made the same mistake with real numbers and put a "burn" into an endless loop.  :o

Ian
Re: Mach3 script editor bug?
« Reply #3 on: February 08, 2013, 11:59:07 AM »
VB was printing it out just like I typed in my printout and I never got to far into binary.
I changed the code below and it seems to work. Is this a good fix to my problem or could you recommend a better solution?
Thanks for the help  :)

Dim BaseX As Double
BaseX = .030
Do While (BaseX > .000)
  If Round(BaseX,3) = 0.001 Then BaseX = BaseX - .001
  If Round(BaseX,3) = 0.002 Then BaseX = BaseX - .001
  If Round(BaseX,3) = 0.01 Then BaseX = BaseX - .008
  If Round(BaseX,3) > 0.01 Then BaseX = BaseX - .010
Loop

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: Mach3 script editor bug?
« Reply #4 on: February 08, 2013, 12:31:25 PM »
as I said - this is not JUST binary - this is the world of real numbers. Did you try the 10/3 thing? ;D

anyway... with ref to print and what I said above try this...

Print 0.099999999999999
Print 0.0999999999999999

There are many ways to deal with reals of which rounding is one - whether that's good enough for what you want your program to achieve can only be answered by you. Personally I would always handle > = and < so I KNOW what my prog will do and not assume anything.

Ian
Re: Mach3 script editor bug?
« Reply #5 on: February 08, 2013, 01:26:31 PM »
Print 0.0999999999999999 shows up as .1
print 10/3 displays 3.3333333333333
print 10/6 displays 1.6666666666667

I definitely want to know what my program will do. Crashing would be sad.
Does this look better?
Do I need the Round(var,4) or is it not needed now?
Thanks again for all the help :)

Dim BaseX As Double
BaseX = .030
Do While (BaseX > .000)
If Round(BaseX,4) <= 0.0021 Then BaseX = BaseX - .001
If Round(BaseX,4) < 0.0101 And Round(BaseX,4) >0.0099 Then BaseX = BaseX - .008
If Round(BaseX,4) > 0.01 Then BaseX = BaseX - .010
Loop

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: Mach3 script editor bug?
« Reply #6 on: February 09, 2013, 06:26:46 AM »
Yes, print 10/3 displays 3.3333333333333 but that does NOT mean 10/3 EQUALS 3.3333333333333. It means 10/3 equals 3.3333333333333 TO 13 DECIMAL PLACES. There's a world of difference - yes/no?.

Moving on, some questions you might want to ask yourself.

What initial values of BaseX are valid in YOUR program? For that set of valid numbers what does the round function do through your loop? as the numbers are processed, does it round up or down? What does it do if the number at some point ends up as EVEN with a decimal part that is exactly 0.5? what does it do when it's ODD with a 0.5 decimal part?. What does it do if the number goes negetive? Does it matter to YOUR overall program - not just that example fragment with those example numbers?

Sorry - I know you just want a one shot answer but hopefully you'll see why I think I would be wrong to give you one. Maybe someone will come along and explain it better.

and now a pause for thought... ta.twi.tudelft.nl/users/vuik/wi211/disasters.html

Ian

« Last Edit: February 09, 2013, 06:45:57 AM by stirling »
Re: Mach3 script editor bug?
« Reply #7 on: February 09, 2013, 04:19:12 PM »
Sorry - I know you just want a one shot answer but hopefully you'll see why I think I would be wrong to give you one. Maybe someone will come along and explain it better.
I am actually very grateful that you are teaching me, it is much more useful than just posting the code. This way next time I have to write something similar I will understand how it works :D

Yes there is a difference 10/3 in decimal is a infinite number and 3.3333333333333 is not infinite.
BaseX is used in a sub to control the X axis on a lathe to take the final roughing cuts and two finish cuts of a profile.
print round(0.5,0) Displays 0. I would have guessed it would have displayed 1. print round(0.6,0) displays 1.
print round(1.5,0) Displays 2 and so does print round(2.5,0).
Print Round(-1.5,0) and Print Round(-2.5,0) both display -2

I am not sure exactly how far off the rounding function makes my variable as it goes threw my loop. I now understand values produced by rounding are not accurate. Rounding a variable in a loop can cause the difference to grow based on the size of the loop. Possibly rounding difference * num of loops and is not good for accuracy.
In the future I will just use rounding for displaying information to the operator.

Currently my code looks like this. I tested it with n what do you think?
Code: [Select]
 Code "G0 X.4" 'Back away X
  Code "G0 Z.01" 'Back away Z
  Code "G0 X0" 'Zero X
  
  Dim BaseX As Double
  BaseX = .0004 'Make BaseX .01 larger than needed.
  Do Until BaseX = 0
    If BaseX <= 0.002 Then BaseX = BaseX - .001 'If down to last cuts take off .001 and subtract until 0
    If BaseX > 0.0021 And BaseX < .012 Then BaseX = .002 'If within .01 of .002 then set to .002
    If BaseX > 0.012 Then BaseX = BaseX - .010 'If far away rough .01
    If BaseX < 0 Then BaseX = 0 'Catch if BaseX starts <.001 and > 0
    Code "G1 X" & BaseX + 0.110 & " Z0.00 F10"
    Code "G2 X" & BaseX + 0.190 & " Z-0.075 R-0.066 F10"
    Code "G1 X" & BaseX + 0.130 & " Z-0.600 F10"

    Code "G0 X.4" 'Back away X
    Code "G0 Z0.01" 'Back away Y
    Code "G0 X" & BaseX 'Sets x to the base to save time
  Loop

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: Mach3 script editor bug?
« Reply #8 on: February 10, 2013, 01:13:14 PM »
OK - I think I see what you're trying to do but it's all getting a little bit messy. If I understand your requirement you want to rough down in rough steps until you're at some level or at a point where another rough step would be a step too far. From there you want to finish down in finer steps until you're done, but again mnaking sure you don't go beyond the finish level.

Here's the pseudo code - untested of course so the usual applies - if it breaks your machine or your heart you get to keep the pieces. :)

currentX is where you're at at any time
roughStepX is your roughing stepover
fineLevelX is where you want to fine cut from
fineStepX is your fine stepover
finishLevelX is where you want the whole shabang to stop

while ((currentX - roughStepX) > fineLevelX)
  currentX = currentX - roughStepX
  G1 currentX
wend

while ((currentX - fineStepX) > finishLevelX)
  currentX = currentX - fineStepX
  G1 currentX
wend

if (currentX > finishLevelX) then
  G1 finishLevelX
end if

Note that all tests are exclusively > so a) the = and < are handled implicitly i.e. by drop throu - so no possibility of an infinite loop (well unless you set the stepovers to zero! in which case you deserve all you get ;D) and b) we don't give a monkey's about decimal places and such. Also all numbers can be set at the top and therefore do not appear as "magic" numbers in the code proper.

« Last Edit: February 10, 2013, 01:15:59 PM by stirling »
Re: Mach3 script editor bug?
« Reply #9 on: February 10, 2013, 05:19:03 PM »
Much cleaner code than mine.
I will modify and test, then post up the results.

Thanks again for all of your help :D