Machsupport Forum

Mach Discussion => General Mach Discussion => Topic started by: krsykes23 on February 27, 2016, 06:44:46 AM

Title: ATC code help
Post by: krsykes23 on February 27, 2016, 06:44:46 AM
I'm sure this subject has been discussed many times before, but I need help with setting up my ATC.

I have a Kress spindle with a Sorotec ATC, and a 5 position linear tool holder.  The M/C coordinates for each of the
5 tool holders positions are as follows...

         T1 - X6.83     Y61.61
         T2 - X6.83     Y96.45
         T3 - X6.83     Y131.23
         T4 - X6.83     Y166.05
         T5 - X6.83     Y200.25

The Z height is -50mm (M/C).

As I am fairly new to this, I have tried a VB script with the following code.  Having sourced some ideas from this
forum.  The script is just to select one tool.

If GetSelectedTool() = GetCurrentTool() Then
End
End If
If GetSelectedTool = 1 Then
Code "G53 G0 X40"      
Code "G53 G0 Y61.61"      
Code "G53 G0 X6.83"        
Code "G53 G1 Z-30 F500"
ActivateSignal(OutPut4)
Sleep 1000
Code "G53 G0 Z-50"
While IsMoving()
Wend
Sleep 1000
DeActivateSignal(OutPut4)
Code "G53 G0 Z-30"
Code "G53 G0 X40"
Code "G0 X0 Y0 Z0"
End If

If I omit the first four lines (and the last End If)...it sort of works!  But with the first four lines in, it doesn't do
anything!  I actually don't understand the first four lines....so if someone could advise, that would help.

Also, if I run just the code from line 5, it sort of works...BUT...the ATC opens the drawbar before moving, which
I'm puzzled!

I can't see what is wrong with the simpler code...

Code "G53 G0 X40"      
Code "G53 G0 Y61.61"      
Code "G53 G0 X6.83"        
Code "G53 G1 Z-30 F500"
ActivateSignal(OutPut4)
Sleep 1000
Code "G53 G0 Z-50"
While IsMoving()
Wend
Sleep 1000
DeActivateSignal(OutPut4)
Code "G53 G0 Z-30"
Code "G53 G0 X40"
Code "G0 X0 Y0 Z0"

What am I doing wrong?

Regards.
Title: Re: ATC code help
Post by: stirling on February 27, 2016, 07:38:55 AM
do you know what the while isMoving() statement does?
Title: Re: ATC code help
Post by: krsykes23 on February 27, 2016, 07:41:27 AM
I think so Stirling.  It tells Mach to wait until something stops....Correct?
Title: Re: ATC code help
Post by: stirling on February 27, 2016, 07:51:33 AM
In the context of what you're doing here - yes. So don't you want to wait for something to stop before you open the drawbar?
Title: Re: ATC code help
Post by: krsykes23 on February 27, 2016, 08:12:52 AM
Not with you!  I thought that part of the code was just about right...although I have made a couple of changes.

At this point the drawbar should be closed (OutPut4)

Code "G53 G0 X40"                Move to clear the tool holders
Code "G53 G0 Y61.61"           Move to Tool #1 position in Y
Code "G53 G0 X6.83"             Move to directly above the tool position
Code "G53 G1 Z-30 F500"       Move to a safe height just above the tool
ActivateSignal(OutPut4)          Open the drawbar
Sleep 1000                            Wait 1 second
Code "G53 G0 Z-50"                Move to the tool 'grab' location
DeActivateSignal(OutPut4)       Close drawbar
Sleep 1000                            Wait 1 second
Code "G53 G0 Z-30"               Move back up to the safe height      
Code "G53 G0 X40"                Move away from the tool holder
Code "G0 X0 Y0 Z0"               Move to Fixture Offset (0,0,0)
While IsMoving()
Wend

Could you make any amendments as required?

Keith
Title: Re: ATC code help
Post by: ger21 on February 27, 2016, 08:18:35 AM
You need a While IsMoving()
Wend

between every movement.

And if there's already a tool in the spindle, isn't it going to drop it?
Title: Re: ATC code help
Post by: stirling on February 27, 2016, 08:29:59 AM
OK - so you need to understand something about macros.

They run in a totally separate thread from Mach proper. i.e. they run CONCURRENTLY.

So when you issue (say)

Code "G53 G0 X40"  'Move to clear the tool holders

The macro is saying to Mach - please insert this into your execution queue.

Mach will do as it's asked but here's the thing. Mach will EXECUTE that line of code in it's own good time.

Meanwhile, the macro will move onto the next line and say to Mach - please insert this into your execution queue.

Again, Mach will do this BUT it's probably (most likely) still actually executing the first line.

What you're doing is asking Mach to queue all four lines and then immediately activating output 4.

HOWEVER, Mach is probaby STILL just actually executing the first gcode line.

So you end up activating your draw bar WELL before you should.

So when you ask Mach to queue requests (in this case for movement), you MUST then WAIT until Mach has finished before you carry on. This is called thread synchronisation. The method Mach provides us to do this is the isMoving() function.

So what you need is:

Code: [Select]
Code "G53 G0 X40"       'Move to clear the tool holders
Code "G53 G0 Y61.61"   
Code "G53 G0 X6.83"     'Move to directly above the tool position
Code "G53 G1 Z-30 F500" 'Move to a safe height just above the tool
while isMoving()        'wait for Mach to do everything in the queue
wend
ActivateSignal(OutPut4) 'THEN - Open the drawbar
Title: Re: ATC code help
Post by: krsykes23 on February 27, 2016, 09:24:05 AM
Hi all.  Thanks for the replies.  I seem to be getting somewhere now.  I'll adapt the code sample you sent and see
how I get on.

Might have a couple of extra related questions soon.
Title: Re: ATC code help
Post by: BR549 on February 27, 2016, 11:28:50 AM
Ian that is probably the best explaination of why you want to wait (;-)  That i have seen.

But there are times when Mach3 will not have full control over teh qued group of Gcode motion and it can get out of order as well.  This occurs in Mach4/Lua as well (;-)

They are still missing teh boat as a CNC MACRO language. They do NOT provide proper sync of the code line by line .

You will NEVER see that happen in  Macro B or on any control it runs on.


 (;-) TP
Title: Re: ATC code help
Post by: stirling on February 27, 2016, 11:33:32 AM
You old charmer - what do you want?  ;D

Dang - you went and edited whilst I posted...

Dang - you just dd it again...
Title: Re: ATC code help
Post by: BR549 on February 27, 2016, 11:40:48 AM
And yes I understand as a Powerfull scripting language teh programmer MUST have full control and be allowed to spawn threads at will as Sync and ASSync BUT we are talking a CNC macro language at this point not a full blown scripting language.

(;-) TP

Title: Re: ATC code help
Post by: BR549 on February 27, 2016, 11:45:00 AM
Also you well know that G04P0 can be your best friend EVEN in a macro at times. ONCE you understand what it actually does in teh script.

(;-) TP
Title: Re: ATC code help
Post by: stirling on February 27, 2016, 12:03:38 PM
But there are times when Mach3 will not have full control over teh qued group of Gcode motion and it can get out of order as well.

You'd have to give me a concrete example of that before I'll bite  ;)
Title: Re: ATC code help
Post by: BR549 on February 27, 2016, 12:12:12 PM
The next time I run into it I will send one your way.  I did run into it abut a month ago and I will see if I can find the example again. I used to think Grouping the Gcode together was a sure fired thing too but Mach3 proved me wrong again.

(;-) TP
Title: Re: ATC code help
Post by: stirling on February 27, 2016, 12:44:36 PM
Look forward to seeing it if you find it.

Debugging code is one thing but trying to debug anecdotes is like trying to herd cats.  ;D
Title: Re: ATC code help
Post by: BR549 on February 27, 2016, 12:56:56 PM
Herding Cats is not as hard as one would think , IF .

And yes that is a BIG IF .

(;-) TP
Title: Re: ATC code help
Post by: Davek0974 on February 28, 2016, 03:25:28 AM
Ian that is probably the best explaination of why you want to wait (;-)  That i have seen.


 (;-) TP

+1 Even I could understand that, explains a whole lot  :)