Machsupport Forum

Mach Discussion => General Mach Discussion => Topic started by: CoreyCoop on April 24, 2020, 07:04:39 PM

Title: Mach3, G31 probe on Z axis, and variable 2002
Post by: CoreyCoop on April 24, 2020, 07:04:39 PM
I am new to Mach3, but a programmer in multiple languages, PLCs and motion control systems for many years.  I'm trying to help a friend set up a new (Chinese) CNC Router.  I'm working on a macro in Mach3 to do a simple probe to find the tool length.  When I run g31 with the current position minus one inch, it works fine, it stops (but does not retract any which the specification says it should), and variable 2002 (using GetVar(2002) ) always returns the part offset.  In other words, if G54 is active, with -12.4 in the Z offset, then GetVar(2002) returns 12.4 (positive value of the negative offset.  If I use no offset (offset of 0.0) GetVar(2002) returns 0.0.

What is going on here?
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: joeaverage on April 24, 2020, 11:40:28 PM
Hi,

Quote
When I run g31 with the current position minus one inch, it works fine, it stops (but does not retract any which the specification says it should),

Thats not my understanding.

Quote
The machine will move toward the specified end point, at the same time it is looking for the probe input to be activated. When the probe input is activated the current position is recorded to # variables according to the table below and motion is stopped. The recorded position can then be used to calculate tool offsets, work offsets, measure parts, etc.

It does not say anything about retracting. When you have a probing routine it takes two Gcode instructions, a G31 which
does the probe move per the description above followed by a G0 which retracts the Z axis.

Craig
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: CoreyCoop on April 25, 2020, 01:32:32 AM
Hi,

Quote
When I run g31 with the current position minus one inch, it works fine, it stops (but does not retract any which the specification says it should),

Thats not my understanding.

Quote
The machine will move toward the specified end point, at the same time it is looking for the probe input to be activated. When the probe input is activated the current position is recorded to # variables according to the table below and motion is stopped. The recorded position can then be used to calculate tool offsets, work offsets, measure parts, etc.

It does not say anything about retracting. When you have a probing routine it takes two Gcode instructions, a G31 which
does the probe move per the description above followed by a G0 which retracts the Z axis.

Craig

I think you are missing the point:  First off, I mentioned it only as a side note, but the documentation that came with Mach3, I believe quoted from the NIST standard but also notated by Artsoft, states:
"In response to this command, the machine moves the controlled point (which should be at the
end of the probe tip) in a straight line at the current feed rate toward the programmed point.
If the probe trips, then the probe is retracted slightly from the trip point at the end of command
execution."

My understanding from reading that was that it tries to compensate for overtravel, but again that is a side note.  I of course also do a move afterwards to reposition the probe out of the way when done probing.

The point that you seem to have missed is this, again from the documentation on G31:
"After successful probing, parameters 2000 to 2005 will be set to the coordinates of the location
of the controlled point at the time the probe tripped..."

And in fact several examples of other people's code for probing I have found online use something like:
Code "G90 F4 G31 Z-1.0"
somevar = GetVar(2002)

All the code I saw used the value in 2002, either as #2002 or GetVar(2002) to calculate the trip position, either to set the height of the work piece, or as in my case, to measure the tool length.  But all I get is the current Z work offset set by G54, G55 etc.

That is where I am confused.  I ended up writing a manual routine after the G31 to backoff the Z axis until it is de-asserted, then go in at a very slow feed until it is re-asserted.  For some reason, after backing off the probe, I could not get G31 to work at a very slow feed, (F0.5 or F1.0) so I did it myself in several loops of code. 

So I have working code, but I would like to know why 2002 is not the value I think it should be. 
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: ZASto on April 25, 2020, 04:43:24 AM
Analyze this code to figure out how touchoff is working.
It does NOT retract automatically after contact.

Code: [Select]
Sub Main() 'made it a sub, so you can return on "show stopper" errors
'Option Explicit 'Written by Big-Tex Dec 26 2010 Updated Jun 3 2014
'Mod pb 11dec10

Dim ZNew, Zplate, Zrestposition, ZMaterialmachcoord, ZPlatejobcoord, Zplatetomaterial
Dim xjobcoord, yjobcoord, xmachcoord, ymachcoord, zmachcoord
Dim xprobeloc, yprobeloc, xtoprobe, ytoprobe, PlateOffset
Dim CurrentFeed, Zretract
Dim CurrentAbsInc

xjobcoord = GetDRO(0) 'get current job coordinate for X
yjobcoord = GetDRO(1) 'get current job coordinate for Y
xmachcoord = GetOemDRO(83) 'get current machine coordinate for X
ymachcoord = GetOemDRO(84) 'get current machine coordinate for Y
zmachcoord = GetOemDRO(85) 'get current machine coordinate for Z
xprobeloc = GetUserDRO(1100) 'get X machine coordinate location of the touch plate
yprobeloc = GetUserDRO(1101) 'get Y machine coordinate location of the touch plate
xtoprobe = (xprobeloc - xmachcoord + xjobcoord) 'calculate the X move from the current location to the touch plate
ytoprobe = (yprobeloc - ymachcoord + yjobcoord) 'calculate the Y move from the current location to the touch plate
PlateOffset = GetUserDRO(1151) 'get plate offset DRO
CurrentFeed = GetOemDRO(818) 'get the current feedrate to return to later
Zretract = GetOemDRO(1202) 'get Z tool change location
CurrentAbsInc = GetOemLED(48) 'Get the current G90/G91 state

'//////// the block below will set all your reusable vars depending on Inch or mm.
'//////// this sets the vars so you only need ONE large block of probing code.

If GetOEMLED(801) Then 'ON = English Measure INCH
FirstProbeDist = 6.0 'first probe travel
FirstRetractDist = 0.1 'first probe retract travel
SecProbeDist = 0.25 'second probe travel
FirstProbeFeed = 10 'First Probe Feed Speed
SecondProbeFeed = 2 'Second Probe Feed Speed
Else 'OFF = Metric Measure MM
FirstProbeDist = 150.0 'first probe travel
FirstRetractDist = 3.0 'first probe retract travel
SecProbeDist = 6.0 'second probe travel
FirstProbeFeed = 300 'First Probe Feed Speed
SecondProbeFeed = 50 'Second Probe Feed Speed
End If

'//////// Error Condition checking code

If GetOemLED(16)<>0 Then 'Checks for machine coordinates
Code "(Please change to working coordinates)"
Exit Sub 'ERROR! exit the macro
End If

If GetOemLED(825)<>0 Then
Code "(Z-Plate Grounded Check connection and try again)"
Exit Sub 'ERROR! exit the macro
End If

'//////// Start Probing Code, Probe In -Z direction.MOVABLE PROBE PLATE
'//////// The vars will be Inch or Metric from above if/else statment

sleep(1000) 'pause 1 seconds to give time to position probe plate
Code "F" & FirstProbeFeed 'slow down feedrate to 10 ipm
ZNew = ( GetDro(2) - FirstProbeDist ) 'probe move to current z - 6 inches
Code "G90 G31Z" & ZNew
While IsMoving() 'wait for probe move to finish
Wend
ZNew = GetVar(2002) 'read the touch point
Code "G0 Z" & ( ZNew + FirstRetractDist ) 'move back to hit point incase there was overshoot +.1
While IsMoving ()
Wend
Code "F" & SecondProbeFeed 'slow down feedrate to 2 ipm
ZNew = ( GetDro(2) - SecProbeDist ) 'probe move to current z - .25 inches
Code "G90 G31Z" & ZNew
While IsMoving() 'wait for probe move to finish
Wend
ZNew = GetVar(2002) 'read the touch point
Code "G0 Z" & ZNew 'move back to hit point incase there was overshoot
While IsMoving ()
Wend
Call SetDro (2, PlateOffset) 'set the Z axis DRO to plate thickness
sleep(250) 'pause for Dro to update.
Code "G53 G0 Z" & Zretract
While IsMoving ()
Wend
Code "F" & CurrentFeed 'returns to prior feed rate

'//////// End Probing Code,

If GetOEMLED(801) Then 'ON = English Measure INCH
Code "(Z axis is now zeroed in English Units)" 'puts this message in the status bar
Else 'OFF = Metric Measure MM
Code "(Z axis is now zeroed in Metric Units)" 'puts this message in the status bar
End If

If CurrentAbsInc = 0 Then 'if G91 was in effect before then return to it
Code "G91"
End If

Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: Graham Waterworth on April 25, 2020, 07:24:45 AM
Do you have a length offset active, if so that will be included in the #2002 value you could do a G49 before you do the G31 G91 Z-1. move
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: CoreyCoop on April 25, 2020, 05:13:03 PM
Analyze this code to figure out how touchoff is working.
It does NOT retract automatically after contact.

Code: [Select]
Sub Main() 'made it a sub, so you can return on "show stopper" errors
'Option Explicit 'Written by Big-Tex Dec 26 2010 Updated Jun 3 2014
'Mod pb 11dec10

Dim ZNew, Zplate, Zrestposition, ZMaterialmachcoord, ZPlatejobcoord, Zplatetomaterial
Dim xjobcoord, yjobcoord, xmachcoord, ymachcoord, zmachcoord
Dim xprobeloc, yprobeloc, xtoprobe, ytoprobe, PlateOffset
Dim CurrentFeed, Zretract
Dim CurrentAbsInc

xjobcoord = GetDRO(0) 'get current job coordinate for X
yjobcoord = GetDRO(1) 'get current job coordinate for Y
xmachcoord = GetOemDRO(83) 'get current machine coordinate for X
ymachcoord = GetOemDRO(84) 'get current machine coordinate for Y
zmachcoord = GetOemDRO(85) 'get current machine coordinate for Z
xprobeloc = GetUserDRO(1100) 'get X machine coordinate location of the touch plate
yprobeloc = GetUserDRO(1101) 'get Y machine coordinate location of the touch plate
xtoprobe = (xprobeloc - xmachcoord + xjobcoord) 'calculate the X move from the current location to the touch plate
ytoprobe = (yprobeloc - ymachcoord + yjobcoord) 'calculate the Y move from the current location to the touch plate
PlateOffset = GetUserDRO(1151) 'get plate offset DRO
CurrentFeed = GetOemDRO(818) 'get the current feedrate to return to later
Zretract = GetOemDRO(1202) 'get Z tool change location
CurrentAbsInc = GetOemLED(48) 'Get the current G90/G91 state

'//////// the block below will set all your reusable vars depending on Inch or mm.
'//////// this sets the vars so you only need ONE large block of probing code.

If GetOEMLED(801) Then 'ON = English Measure INCH
FirstProbeDist = 6.0 'first probe travel
FirstRetractDist = 0.1 'first probe retract travel
SecProbeDist = 0.25 'second probe travel
FirstProbeFeed = 10 'First Probe Feed Speed
SecondProbeFeed = 2 'Second Probe Feed Speed
Else 'OFF = Metric Measure MM
FirstProbeDist = 150.0 'first probe travel
FirstRetractDist = 3.0 'first probe retract travel
SecProbeDist = 6.0 'second probe travel
FirstProbeFeed = 300 'First Probe Feed Speed
SecondProbeFeed = 50 'Second Probe Feed Speed
End If

'//////// Error Condition checking code

If GetOemLED(16)<>0 Then 'Checks for machine coordinates
Code "(Please change to working coordinates)"
Exit Sub 'ERROR! exit the macro
End If

If GetOemLED(825)<>0 Then
Code "(Z-Plate Grounded Check connection and try again)"
Exit Sub 'ERROR! exit the macro
End If

'//////// Start Probing Code, Probe In -Z direction.MOVABLE PROBE PLATE
'//////// The vars will be Inch or Metric from above if/else statment

sleep(1000) 'pause 1 seconds to give time to position probe plate
Code "F" & FirstProbeFeed 'slow down feedrate to 10 ipm
ZNew = ( GetDro(2) - FirstProbeDist ) 'probe move to current z - 6 inches
Code "G90 G31Z" & ZNew
While IsMoving() 'wait for probe move to finish
Wend
ZNew = GetVar(2002) 'read the touch point
Code "G0 Z" & ( ZNew + FirstRetractDist ) 'move back to hit point incase there was overshoot +.1
While IsMoving ()
Wend
Code "F" & SecondProbeFeed 'slow down feedrate to 2 ipm
ZNew = ( GetDro(2) - SecProbeDist ) 'probe move to current z - .25 inches
Code "G90 G31Z" & ZNew
While IsMoving() 'wait for probe move to finish
Wend
ZNew = GetVar(2002) 'read the touch point
Code "G0 Z" & ZNew 'move back to hit point incase there was overshoot
While IsMoving ()
Wend
Call SetDro (2, PlateOffset) 'set the Z axis DRO to plate thickness
sleep(250) 'pause for Dro to update.
Code "G53 G0 Z" & Zretract
While IsMoving ()
Wend
Code "F" & CurrentFeed 'returns to prior feed rate

'//////// End Probing Code,

If GetOEMLED(801) Then 'ON = English Measure INCH
Code "(Z axis is now zeroed in English Units)" 'puts this message in the status bar
Else 'OFF = Metric Measure MM
Code "(Z axis is now zeroed in Metric Units)" 'puts this message in the status bar
End If

If CurrentAbsInc = 0 Then 'if G91 was in effect before then return to it
Code "G91"
End If


Nicely written code, and I thought I was being careful about dim'ing everything and testing everything before and during the routine!  Good ideas here, I will be learning from this.

I see you using, as I have seen others do, the form
Code "(yada yada yada)"
which I found did not work I had to use
Message "(yada yada yada)"
although I see no mention of it in the documentation, is the parenthesis surrounding the text what makes it print to the message line?  I can't test for a few days until I'm back at the machine.

This is not unlike what I did for my first tries, but when I did the G31, GetVar(2002) always returned the current Z axis part offset, I never got that to work right.  Also, the docs say that it is an error to run G31 with a value less than 0.1 inch, but I was seeing it fail to move with a distance of 0.2 inches.  The guy I am writing the routine for wants it to run pretty fast, so backing up an inch or more to then run at F1 is a no-go.  I ran out of patience, and didn't test if it would work at 0.5, I just gave up and wrote my own code ignoring G31 after the first touch-off. 

My routine seems to be accurate to -0.001+0.0 or so to calculate tool length, so after fussing with g31 for a few hours I called that a win.  I still want to know why #2002 is holding the wrong value!
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: Graham Waterworth on April 25, 2020, 05:28:13 PM
Have you got an active tool offset?
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: CoreyCoop on April 25, 2020, 05:34:49 PM
Do you have a length offset active, if so that will be included in the #2002 value you could do a G49 before you do the G31 G91 Z-1. move
Since what I am doing is measuring the tool length, and putting it into the tool table, I first tried using "IncludeTLOinZFromG31()" to test and make the operator deselect that option before running.  However, even with it unchecked, G31 used the Tool offset.  So I concluded I don't understand what "TLO" is.  So now I just zero out the tool length at the start (setOEMDRO(42, 0.0)) and upon successful completion, load the new value in.

In any case that would not explain the behavior.  No matter what value the tool offset was set to, if a tool touched off from the sensor, or if I hit the sensor my self when the tool was 6 inches away, 2002 would always show the part Z offset.  Weird.
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: CoreyCoop on April 25, 2020, 05:37:15 PM
Analyze this code to figure out how touchoff is working.
It does NOT retract automatically after contact.

Yes, I know, my side question about that is why does Artsoft specifically mention that it does retract back to the 2002 position, when in fact it does not?
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: Graham Waterworth on April 25, 2020, 05:47:06 PM
The same reason why it says G73 retracts, the documentation is wrong.
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: CoreyCoop on April 25, 2020, 05:53:39 PM
The same reason why it says G73 retracts, the documentation is wrong.
Ugg, I'll watch for that!

Does Mach4 have either better documentation or stick better to ansi standards?
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: joeaverage on April 25, 2020, 06:33:27 PM
Hi,

Quote
Does Mach4 have either better documentation or stick better to ansi standards?

Mach4 documentation is much better than Mach3, having said that it tends to lag the latest developments. Probing
(there are four probe channels in Mach4) is of course very mature and so the documentation is pretty good. In particular
there is an excellent description of the sequence of signals between Mach4 and the motion controller with a g31 code.
And by the way if you read it you will discover that a g31 does not retract....contrary to your belief, the retract requires
a second gcode move.

Quote
Probing
Motion plugin probing procedure.
The probe moves are really just G01 moves in exeact stop mode with the addition of being marked as EX_PROBE in the execution_t struct from mcMotionCyclePlannerEx().

When executing a G31, the core considers the move as an exact stop move and therefore will request a motor stop report. It waits on one of two conditions:

A probe strike condition where the motion plugin calls mcMotionSetProbeComplete().
An end of move condition where the motion plugin reports the motors as being still.
Until either of these conditions are satisfied, the core will generate no more moves after the EX_PROBE marked moves. In other words, all you will get out of mcMotionCyclePlannerEx() will be EX_NONE type data. In the event of a probe strike, this makes it safe to clear the planner and be sure that future moves are not removed by accident.

A motion plugin should implement probing in the following way:

Upon seeing the first move marked as EX_PROBE, the plugin should arm position latches on the hardware.
Then consume the EX_PROBE marked moves as normal.
If the probe stikes, the plugin should:
Cancel any MSG_REPORT_MOTOR_STOP requests. (see g. VERY important!)
Report the latched positions via mcMotionSetProbePos() for each motor.
Clear the hardware of any additional moves.
Call mcMotionClearPlanner() to clear the Mach planner of any unretrieved EX_PROBE moves.
Update Mach with the hardware's current position via mcMotionSetPos().
Call mcMotionSetProbeComplete().
If a MSG_REPORT_MOTOR_STOP has been received, do not acknowledge it. mcMotionSetProbeComplete() does this for you and a position sync as well.
If no probe strike, then the move continues to it's end point as if it is a regular move.
The plugin should ack the MSG_REPORT_MOTOR_STOP messages with mcMotionSetStill().
The positions latches should be disarmed (if needed) when a MSG_PROBE_DONE is seen.
In the event that the probe move is aborted, the plugin should:
Call mcMotionClearPlanner() to clear the Mach planner of any unretrieved EX_PROBE moves.
Call mcMotorSetPos() for each controlled motor.
Call mcMotionSync.
Call mcMotionSetStill() for each controlled motor.
Probe Operation Overview.
Upon executing G31, the core will mark those moves as EX_PROBE in the data stream.
If the probe strikes before the end of the G31 move, the plugin aborts the rest of the probe moves as described above. If a probe data file has been opened via mcCntlProbeFileOpen(), then the positions recorded by the motion plugin are inserted into the probe data file in the specified format.
If the probe doesn't strike and the G31 move reaches its end point and a probe data file has been opened via mcCntlProbeFileOpen(), then the end point positions are inserted into the probe data file in the specified format.
Using the probe data file routines.
int mcCntlProbeFileOpen(MINSTANCE inst, const char *filename, const char *format);
The format argument is a printf style format with expanding macros for the axis values. AXIS_X, AXIS_Y, AXIS_Z, AXIS_A, AXIS_B, AXIS_C. It is used in the following manner:


mcCntlProbeFileOpen(inst, "myProbeFile.csv", "%.4AXIS_X, %.4AXIS_Y, %.4AXIS_Z");
This will produce a probe file in CSV format like so:



1.0000, 2.0000, -1.4356
1.0100, 2.0000, -1.4343
1.0200, 2.0000, -1.4324
...

A format of "X%.4AXIS_X, Y%.4AXIS_Y, Z%.4AXIS_Z" would yield:



X1.0000, Y2.0000, Z-1.4356
X1.0100, Y2.0000, Z-1.4343
X1.0200, Y2.0000, Z-1.4324
...

A format of "X%.4AXIS_X\tY%.4AXIS_Y\tZ%.4AXIS_Z" would yield a tab delimited file:



X1.0000 Y2.0000 Z-1.4356
X1.0100 Y2.0000 Z-1.4343
X1.0200 Y2.0000 Z-1.4324
...

int mcCntlProbeFileClose(MINSTANCE inst); Closes the probe data file.

Mach4 is generally ANSI compliant, but more importantly is made to be Fanuc 21B compliant, which is the template for ANSI.

I've been using Mach4 for five years and regularly probe parts and surfaces and have had no problems whatever.

Craig
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: CoreyCoop on April 27, 2020, 11:46:30 AM
And by the way if you read it you will discover that a g31 does not retract....contrary to your belief, the retract requires
a second gcode move.

I was just quoting the documentation that came from Artsofty with Mach3, and it was just a side note!  Why does everyone focus on this one thing?  I never thought it would retract back up to original position or anything, it simply said it retracted a little, which it does not!
Title: Re: Mach3, G31 probe on Z axis, and variable 2002
Post by: joeaverage on April 27, 2020, 03:44:30 PM
Hi,

Quote
Why does everyone focus on this one thing? 

Whatever.

Mach4 has a logging feature which offers a very granular diagnostic potential, for example the attached pic.
You wanted to know if Mach4 had better documentation, it does but more importantly it does have superb diagnostic
logging.

Craig