Hello Guest it is April 23, 2024, 05:50:18 AM

Author Topic: M6 Lua script help!  (Read 540 times)

0 Members and 1 Guest are viewing this topic.

M6 Lua script help!
« on: May 09, 2023, 11:29:31 PM »
Hello,

  I have been trying to get my tool changer to work again since changing over to mach 4. I have a hardinge chucker with an 8 station turret that I modified to be pneumatically lock into each station. It rotates with a 200w sanyo servo motor that I have setup as my A axis (motor 3). I had to upgrade to mach4 1/2 way through by build because the ethernet smoothstepper plugin would only register when negative moves were made. This is because the sanyo servos I am using use CW/CCW movement and that was fixed with the newer ESS plugins for mach 4..
 
Anyway, I just wanted to give that background info first.. Essentially I need the turret to activate output 1 to unlock the turret (air valve), move to a safe position, then rotate to the selected station, turret lock (turn off output 1), then continue with the g code program. I have been playing around with my m6 mcs file with pieces of other folks Lua scripts for m6 and have had no luck. When I call m6 t2 or even m6 t0202, or t2 m6 etc it flashes the m6 led, moves to offset 2 but does not change the tool.
  Here is the mach3 vb script that mostly worked when I was running mach3.. Please tell me how this would translate to lua script and work in mach4. I have read many people's scripts now and am quite confused. I will gladly pay  if someone can get me up and running here!

   Code"G53 X0.2"                              'Move to tool change location
While Ismoving()
Wend                         
Code"G53 Z0.2"
While Ismoving()
Wend

ActivateSignal(output1)              'unlock the turrent
While not Isactive(output2)         'wait for the switch to signal end of stroke
Sleep(1000)                                'Give it time for the motion to fully stop
Wend

If GetCurrentTool() = 1 then        ' Check for the Tool #
   Code"G0 A0.000"         ' Move to position
   While Ismoving()           'wait for the movement to stop
   Wend
   End If

If GetCurrentTool() = 2 then
   Code"G0 A45"
   While Ismoving()
   Wend
   End If

If GetCurrentTool() = 3 then
   Code"G0 A90"
   While Ismoving()
   Wend
   End If

If GetCurrentTool() = 4 then
   Code"G0 A135"
   While Ismoving()
   Wend
   End If

If GetCurrentTool() = 5 then
   Code"G0 A180"
   While Ismoving()
   Wend
   End If

If GetCurrentTool() = 6 then
   Code"G0 A225"
   While Ismoving()
   Wend
   End If

If GetCurrentTool() = 7 then
   Code"G0 A270"
   While Ismoving()
   Wend
   End If

If GetCurrentTool() = 8 then
   Code"G0 A315"
   While Ismoving()
   Wend
   End If

DeacctivateSignal(output1)              'Release air cylinder
While NOT Isactive(output3)
Wend
Sleep(1000)                                          'Give it time to complete end of stroke

End
« Last Edit: May 12, 2023, 07:39:10 PM by Graham Waterworth »

GOOMBA

*
Re: M6 Lua script help!
« Reply #1 on: May 10, 2023, 10:19:38 AM »
If you buy an industrial license NFS will do this for you.
Otherwise the provided M6 script really has most of everything you need.
If you don't know how to turn on an output there are examples of that in the macros folder(s).

Eventually we'll build a wizard to make m6 macros but that won't be finished for some time.
Re: M6 Lua script help!
« Reply #2 on: May 10, 2023, 06:13:50 PM »
Thank you much for the info. I purchased a license last night but i'm not sure if its an industrial one. It was $200. I put in a call to NFS to see what they can do for me as well. when I tried to run M6 using the original lua code as well as das the gas's code and all it does is change the offset and flash like it very quickly executes the program but no tool change. I was so close to being done with this build with mach3 (so I thought). A wizard would definitely be great for newbie's like me! haha
Re: M6 Lua script help!
« Reply #3 on: May 13, 2023, 02:19:16 AM »
I have been using Mach 3 on a lathe for years using a stepper motor to index the 8 station turret. I have wanted to move to Mach 4, but have not been able to convert my VBScript to Lua. Please post your results as it may be helpful to others when you get your Hardinge turret working!  Most code examples I have seen are for lathe turrets that are operated with the forward-to-turn and reverse-to-lock method. The stepper setup works great, but the code is different as far as I can tell.  Any help most welcome.
Re: M6 Lua script help!
« Reply #4 on: May 13, 2023, 10:54:14 PM »
Hi,

Quote
I purchased a license last night but i'm not sure if its an industrial one. It was $200.

A $200 license is Mach4Hobby. Mach4Industrial is $1400...quite a difference. There is very little that Industrial can do that Hobby cannot, don't be alarmed,
but as GOOMBA points out NFS 'roll out the red carpet' for Industrial customers, including direct assistance with m6 Lua code.

Coding in Lua has a distinct learning curve and there is quite a bit you'll want to read and digest before you become familiar with it.

You'll want this:

https://www.lua.org/docs.html

Mach4 uses version 5.3 if I'm not mistaken. I have  this open on the desktop when coding.

You'll read,  again and again.......this:

Mach4Hobby/Docs/Mach4CoreAPI.chm

There you'll find all the API's that are exposed for your use, both Lua and C.

If you want to use dialogs in your code, I'd recommend you don't to start with, or use those that you can copy and paste from Lua examples, other than that you'll need:

https://www.wxwidgets.org/docs/

Its written for geeks, by geeks, and in geekese, and remember you asked for it.

If you want to get into wxWidgets in a big way then you'll appreciate:

https://github.com/wxFormBuilder/wxFormBuilder

You might find a few useful things like ENUMS and pound variables here:

https://www.machsupport.com/forum/index.php?topic=40051.0

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: M6 Lua script help!
« Reply #5 on: May 15, 2023, 01:37:30 PM »
Jastein, I will absolutely share what I learn here.

Joeaverage:
   Thank you so much for all of the valuable info! I will be diving into this more this evening to see what I can learn. I'm all about geeking out on this stuff haha.
Re: M6 Lua script help!
« Reply #6 on: May 15, 2023, 10:12:17 PM »
Hi,
This is typical Mach3 code:
Code: [Select]
If GetCurrentTool() = 2 then
   Code"G0 A45"
   While Ismoving()
   Wend
   End If

The Lua equivalent is something like this:

Code: [Select]
function m303()
local inst = mc.mcGetInstance()
local selectedTool = mc.mcToolGetSelected(inst)
selectedTool = math.tointeger(selectedTool)
local currentTool = mc.mcToolGetCurrent(inst)
currentTool = math.tointeger(currentTool)
if(currentTool==2)then
mc.mcCntlGcodeExecuteWait(inst,"G0 A45\n")
end
end
if(mc.mcInEditor()==1)then
m303()
end

A few things to note here, firstly I have encapsulated the code in a function called m303(). This allows you to run the code in the ZeroBrane editor and test and debug as you go.
The little:
if(mc.mcInEditor()==1)then
      m303()
end

at the end is what allows the code to run......a function must be called and if Mach is 'InEditor' then it will call m303(). Then the function will run until it ends and then returns to the same place it
was called from.

mc.mcCntlGcodeExecuteWait(inst,"G0 A45\n")  this is the line that actually does the business, but note that a function is called 'mc.mcCntlGcodeExecuteWait(........)' and it will execute and Mach will wait
until it completes, there is no need of the While IsMoving() palava.

Translating VBasic into Lua is not especially productive, there are enough differences that writing Lua anew is likely the best result. I mentioned this only because the function:
mc.mcCntlGcodeExecuteWait(........) is a classic example of how Lua/Mach4 coding differs from VBasic and you do not want to replicate VBasic, you want to use what has been provided for in
Lua and mach's API.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: M6 Lua script help!
« Reply #7 on: May 16, 2023, 06:54:59 PM »
Wow, I cant thank you enough for your help!
I think I am starting to understand this a bit more. Lua definitely seems like a more sophisticated and robust language than VB.

I didn't have much time to play around last night but I tried running your script to see what it would do. For some reason weather its the stock M6 or any script that I have tried to run as a tool change, It only flashes the screen like it quickly ran the script but does nothing! No movement from the turret at all. I think I hear one of the small built in relays click on my mb2 board but that's about it for action. The macro folder should just be in with all of the other folders contained in the mach4hobby folder right? Also I am running windows 10 as well, not sure if that would effect anything.. I checked through all of my Ess plugin settings as well as the control settings to see if I was missing anything that would disable ATC. I cant see anything wrong (said the blind man).
   I did change the m303 to m6 and saved it as m6 in the macro folder after removing the previous m6 file. it is nice to see how a program is really supposed to successfully compile  8) . I'm pretty sure I know what else to add in order to move the turret to a safe index location etc, Just trying to get the meat and potatoes of it working first!
    Final thought.. maybe I just need to run another version of plugin for my Ethernet smooth stepper? IDK!
Re: M6 Lua script help!
« Reply #8 on: May 16, 2023, 07:30:48 PM »
Hi,
I did not intend that the function I wrote would actually work in a machine. I just wrote it to show what such code would look like.
The reason that I encapsulated it was so that you can single step through the code with Zero Brane to ensure it works as you expect.
Its an essential technique when coding in Lua within Mach. You need to 'put the code in a sandbox' so you can write it, fiddle with it and get it going
before you put it in the main code area. If you do so without having gotten the bugs out you'll crash Mach.


This is especially true for any code that you put in the PLC script. You put screwy code in there you'll drive yourself up the wall!

Craig
« Last Edit: May 16, 2023, 07:32:39 PM by joeaverage »
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'