Hello Guest it is March 28, 2024, 11:48:04 AM

Author Topic: Need help with M6 Macro, translating from Mach3 to Mach4  (Read 6903 times)

0 Members and 1 Guest are viewing this topic.

Need help with M6 Macro, translating from Mach3 to Mach4
« on: September 20, 2018, 03:51:03 PM »
Ok, so my brain is not working like it used to. I haven't even heard of Lua before today. I have a macro from Mach3 that does exactly what I need it to, however since I'm runing a pokeys57u on my lathe, and want all of the features... I'm going to need to resort to Mach4.

Can someone please help me out?

Here is a Macro I found for Mach3 to control the PCturn Turret, using the factory internal encoder setup. I have everything wired up and mechanically ready to test.


The thread is short, but http://www.machsupport.com/forum/index.php/topic,16685.0.html is my source.

****************************************************************************************************************************************************

'M6Start.M1s
'Input1 is an optical sensor used to count tool positions
'Input2 is from a microswitch used to determine tool 0 position
'OUTPUT6 drives the tool changer forward, de activating it allows the tool changer to rotate back against the ratchet pawl at low power and is the default
'Un comment the Open statement, various write# statements and the close# function to write a logfile for diagnostic purposes

'Close #1
'Open "M6start_log.txt" For Output As #1 'open a file to write logs to
NewTool = GetSelectedTool()
'NewTool = 2
'Write #1, "NewTool ", NewTool
OldTool = GetCurrentTool()
'Write #1, "OldTool ", OldTool
MaxToolNum = 6 'Max number of tools for the changer
CurrPos = OldTool

While (NewTool > MaxToolNum)
    NewTool = Question ("Enter New Tool Number up to " & MaxToolNum)
Wend

    'there is nothing to do so we just exit
If (NewTool = OldTool) And (NewTool > 0) Then
   'Write #1, "New tool number is the same as old tool number. Nothing to do "
       Exit Sub
End If

'If OldTool is 0 that means we dont know where the tool changer is currently positioned and need to reference to the microswitch for tool 1 position.
    'need to establish tool 0 position from microswitch
If (OldTool = 0) Then
   'Write #1, "Old tool number is zero, looking for reference position"
   ActivateSignal(OUTPUT5)' Set toolchanger for full speed move
        'Write #1, "Activated Output 5 to enable toolchanger motor"
        ActivateSignal(OUTPUT6)' rotate tool changer forward
        'Write #1, "Activated Output 6 to set forward direction"
    While Not IsActive(INPUT2)
    Wend
    ' Input 2 must Now be active so the microswitch has been triggered at position 1
    CurrPos = 1
    'Write #1, "Reference position found "
    ' Decide whether to just stop for the normal toolchange process
    If ( CurrPos <> NewTool) Then
       DeActivateSignal(OUTPUT6)
       DeActivateSignal(OUTPUT5)
    Else
    ' Or if we actuallly wanted tool 1 then stop and lock
           DeActivateSignal(OUTPUT5) 'stop rotating forward, rotate backward now
      'Write #1, "DeActivateSignal Output 5 toolchanger reverse "
           SetCurrentTool(NewTool) 'sets up the current tool variable as the start point for a future tool change
           'Write #1, "Setting CurrentTool to NewTool value ", NewTool
           Sleep(2500) 'Allow a time interval for the toolchanger to settle back against the ratchet pawl
           'Write #1, "Waited 2.5 seconds for toolchanger to lock "
           DeActivateSignal(OUTPUT6) 'Disable toolchanger motor
           'Write #1, "DeActivateSignal Output 6 toolchanger stop "
    End If
End If

If (CurrPos <> NewTool) Then 'write the start condition to a variable
   If IsActive(INPUT1) Then
      PreviousSensorState = "Active"
   Else
      PreviousSensorState = "Inactive"
   End If
   'Write #1, "PreviousSensorState =", PreviousSensorState
   ActivateSignal(OUTPUT5)' Set toolchanger for full speed move
          'Write #1, "ActivateSignal Output 6 toolchanger go"
        ActivateSignal(OUTPUT6) 'start rotating forward
        'Write #1, "ActivateSignal Output 5 toolchanger forward "
       While CurrPos <>  NewTool
          'Some logic to ensure that we only count on the leading edge of a slot in the toolchanger optical disk
           If IsActive(INPUT1) Then
              SensorState = "Active"
           Else
              SensorState = "Inactive"
           End If
           'Write #1, "SensorState =", SensorState
           If SensorState <> PreviousSensorState Then
              If SensorState = "Inactive" Then
                 CurrPos = CurrPos +1
                 If CurrPos = 7 Then
                    CurrPos = 1
                    'Write #1, "CurrPos reached 7, reset to 1 "
                 End If
              End If
              'Write #1, "CurrPos =", CurrPos
              PreviousSensorState = SensorState
           End If
           If CurrPos = NewTool Then
                 DeActivateSignal(OUTPUT5) 'stop rotating forward, rotate backward now
                 'Write #1, "DeActivateSignal Output 5 toolchanger reverse "
                 SetCurrentTool(NewTool) 'sets up the current tool variable as the start point for a future tool change
                 'Write #1, "Setting CurrentTool to NewTool value ", NewTool
                 Sleep(1500) 'Allow a time interval for the toolchanger to settle back against the ratchet pawl
                 'Write #1, "Waited 1.5 seconds for toolchanger to lock "
                 DeActivateSignal(OUTPUT6) 'Disable toolchanger motor
                 'Write #1, "DeActivateSignal Output 6 toolchanger stop "
           End If
           sleep(200) ' just slows the iterations down a bit
           'Write #1, "Waited 0.2 second "
       Wend
       'Write #1, "should be job done "
       'Close #1
End If   


If you don't know what is going on above, but know Lua somehow... I have a 6 position turret. There are 2 "encoders" in it. One pulses each tool, and the other pulses on tool 1. A single output turned on advances the tool. When the output is turned off, it will automatically lock into place.

Any help would be greatly appreciated! Thanks!
-Bob

Offline thosj

*
  •  532 532
    • View Profile
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #1 on: September 20, 2018, 05:32:34 PM »
Bob,

If someone here can't help, that macro does seem pretty complicated. you can create a ticket at support.machsupport.com and ask them to write the code for you. You can even PAY them to do it!! They have a system where you can prepay with PayPal for some amount of time and they'll use it as required. It starts as a pending transaction with PayPal 'til they actually bill for it, otherwise you're out nothing.

Not that it wouldn't be cheaper if someone here can help, but it's an option.

Tom
--
Tom
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #2 on: September 20, 2018, 05:51:06 PM »
Hi Havoc340,
one of Mach4 strengths is its ability to be customized. That requires that you code in Lua.

That can be a steep learning curve, I could help but am not inclined to do it for you wthout
you helping yourself. Do you want to do so?

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #3 on: September 20, 2018, 07:30:45 PM »
Thanks for the replies!

Tom, that is an idea for sure!

Craig,  I can totally appreciate your process of helping me learn vs doing it for me. I am willing to learn however I'm a slow learner with the code.

If it were Arduino, I could hobble something together just fine. I know what needs to happen in the tool change process, I just do not know the Lua words. There are easily defined tutorials. With Lua everything I have come across is to create programs, and user interfaces, and way more technical stuff. None of the terminology makes any sense, and the tutorials go straight into randomization, UI, etc... and none of it really is pertaining to my issue. I have looked for other tool changer examples and haven't found any I can work from.
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #4 on: September 20, 2018, 07:59:41 PM »
Hi,
'its not that I'm a slow learner....but I am a quick forgetter!'

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #5 on: September 20, 2018, 08:38:36 PM »
Super quick haha.

So to break it down...

How do I define inputs and outputs in Lua?
I need to turn an output on "output 1" and then wait for encoder pulses.
How do I count? I would like to be able to index tool 1 and save it's position, then increment from there on each tool pulse.
Once the correct tool is reached, I need to turn off that output.

It would be nice to store the current tool location so the next tool change from T2 to T3 does not need to cycle all the way through to T1, and then count to 3... but honestly if it works I'll deal with it.

Then I need to be able to exit the macro telling MACH that the correct tool is loaded.

That is pretty much the jist of it.
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #6 on: September 20, 2018, 08:49:06 PM »
Hi,
inputs and outputs are handled in a different fashion in Mach4.

Inputs cause a lot of confusion when learning this from new.
In particular Mach makes use of the one and only data structure in Lua, the table.

I have this link open almost continuously when coding Lua:
https://www.lua.org/manual/5.2/
Note that it varies slightly from 5.3 Open the link and read about tables.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #7 on: September 21, 2018, 05:56:58 AM »
Hi,
in Mach3 the program would stop what it was doing every 10ms or so and read all the inputs. If one of the inputs was active it would
execute the code associated with that input. This behavior is called 'polling'. Its quite inefficient, it reads only every so often and then it has to read
ALL inputs when you may in fact only be interested in a few of them.

In Mach4 there are lots (hundreds) of signals, not just inputs but other signals internal to Mach. When any one of them changes Mach executes the signal script.
Mach only knows the signal handle and the new state of the signal with that handle. This happens very quickly indeed, a few usec at most.

The signal script as shipped with Mach4 is:
Code: [Select]
if SigLib[sig] ~= nil then
    SigLib[sig](state);
end

Thus when a signal changes Mach knows it only as the variable 'sig' and it has a state 'state'. When the signal script executes Mach is directed to look in the tabe
'SigLib' at the table index 'sig'. If there is an entry in the table for that signal it executes the function associated with that entry SigLib[sig] with the
parameter (state).

If the signal is of no interest to us then it wont have an entry in the signal library (SigLib) and so it will go back to what it was doing. If we are interested however,
for instance in ISIG_INPUT5, ie input #5 because its hooked to a switch, then we will have an entry in the table for it and a function to be executed if
the signal happens.

Note that there will be two instances when the function will be called, once when its state changes from off to on and again when it changes from on to off.
Thus your function must have a test to determine if some action is to be taken.

The upshot is that Mach interrogates the table anytime a signal changes but it only ever executes some extra code if it is a signal we are interested or waiting for.
It is very computationally efficient. At first glance it can appear confusing but its speed and reliability are miles ahead of Mach3.

Craig

'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #8 on: September 23, 2018, 02:45:15 AM »
Hi,
having probably confused you....you can now start to use what you have learnt.

You have at least one input, the index switch. You have at least one other, tool position encoder, can you tell us about how that works.

For the one input that we are sure of, we can make an entry in the signal library table for it.

When writing Lua code you will often find that you will write some parts in the form of macros which will end up in the macro folder
but there will be code fragments that end up in the screen load script and other places, this little addition to the signal library table is an example.
Its a good idea to set aside a temporary folder where you can write and maybe test these little fragments of code. Once you are happy with them
THEN you put them in their final location.

May I suggest that you open the main Mach4 folder and make a new folder within your current profile called 'tempfolder' or similar.

Now open the Script Editor under the Operator tab and you will have an untitled script page that you can save in the tempfolder you have created.
This tempfolder will hold all the little bits of code that you will put together.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Need help with M6 Macro, translating from Mach3 to Mach4
« Reply #9 on: September 23, 2018, 03:25:49 AM »
Hi,
now open the script editor and put this code in it:

Code: [Select]
[mc.ISIG_INPUT3]=function(state)
if state==1 then
toolindex=1
else
toolindex=0
end
end

Save it as 'siglib' or some other unique name.

Note that I have declared a variable 'toolindex', and I have not put 'local' in front of it. It is therefore a global variable and as it is declared,
or least will be when its finished and put in its permanent location, in the screen load script and is therefore visible throughout the GUI.
We may have to get a bit creative if we wish to pass it to a macro because a macro is not technically in the GUI. Anyway that's for the future.

You might ask why I chose input #3? If you look in the screen load script and the signal library table in particular you will see that Mach is already
using, or at least has table entries for inputs #1 and #2. I didn't want to confuse you, or me come to that so I left those out.

If you look on the machine diagnostic page, if the lathe screen set even has such a page you will see that there are LEDs for the first six inputs.
It is convenient to use those to start with because if you reach over and flick the index switch by hand you should see the LED on the diagnostic screen
light up. At a later date you might chose to reassign the switch to input #19 say, of course you wont have an onscreen LED to tell you whether Mach
has seen the switch or not, but by then you have already experimented with it enough to know it works.

If you can tell me how the tool position encoder works we can make a start on that code as well.

Craig
« Last Edit: September 23, 2018, 03:27:46 AM by joeaverage »
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'