Hello Guest it is March 29, 2024, 04:16:49 AM

Author Topic: Need some minor LUA adjustments...  (Read 6118 times)

0 Members and 1 Guest are viewing this topic.

Need some minor LUA adjustments...
« on: May 15, 2017, 03:15:41 PM »
Hi. I got a LUA script for an Pedant, but I need help to adjust it a bit for my needs.
This Lua is written for pedant with 2 selector knobs to set MPG Axis selection and STEP rate.
I will use momentary pushbuttons for this if possible. So I will use 4 buttons for X Y Z A axis, and 3 buttons for STEP rate 0.1 - 0.01 and 0.001.
Can someone please help me?  I should mention that I dont know much about LUA... ???


--Go to the closing } of the SigLib table in the screen load script and
--delete it. Then paste all of this code in its place.
--The new closing } for the SigLib table is on line 48 of this script.
--------------------------------------
--          CNC4PC Pendant         --
--------------------------------------
-- These simply run the CNC4PCPendant function if their state changes.
[mc.ISIG_INPUT10] = function (state)
    CNC4PCPendant()
end,

[mc.ISIG_INPUT11] = function (state)
    CNC4PCPendant()
end,

[mc.ISIG_INPUT12] = function (state)
    CNC4PCPendant()
end,

[mc.ISIG_INPUT13] = function (state)
    CNC4PCPendant()
end,

[mc.ISIG_INPUT14] = function (state)
    CNC4PCPendant()
end,

[mc.ISIG_INPUT15] = function (state)
    CNC4PCPendant()
end,

[mc.ISIG_INPUT16] = function (state)
    CNC4PCPendant()
end,

[mc.ISIG_INPUT17] = function (state)
    CNC4PCPendant()
end,

[mc.ISIG_INPUT18] = function (state)
    CNC4PCPendant()
end,

[mc.ISIG_INPUT19] = function (state)
    CNC4PCPendant()
end

}

---------------------------------------------------------------
-- CNC4PC Pendant function.
---------------------------------------------------------------
function CNC4PCPendant()
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT10)    -- Is mapped to Port 2 Pin 4 *X Selection
   local XSelection, rc = mc.mcSignalGetState(hSig)
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT11)    -- Is mapped to Port 2 Pin 5 *Y Selection
   local YSelection, rc = mc.mcSignalGetState(hSig)
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT12)    -- Is mapped to Port 2 Pin 6 *Z Selection
   local ZSelection, rc = mc.mcSignalGetState(hSig)
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT13)    -- Is mapped to Port 2 Pin 7 *A Selection
   local ASelection, rc = mc.mcSignalGetState(hSig)
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT14)    -- Is mapped to Port 2 Pin 8 *.001 Selection
   local Step001, rc = mc.mcSignalGetState(hSig)
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT15)    -- Is mapped to Port 2 Pin 9 *.010 Selection
   local Step010, rc = mc.mcSignalGetState(hSig)
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT16)    -- Is mapped to Port 2 Pin 10 *.100 Selection
   local Step100, rc = mc.mcSignalGetState(hSig)
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT17)    -- Is mapped to Port 2 Pin 15 *Estop
   local PenStop, rc = mc.mcSignalGetState(hSig)
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT18)    -- Is mapped to Port 2 Pin 12 *B Selection
   local BSelection, rc = mc.mcSignalGetState(hSig)
   local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT19)    -- Is mapped to Port 2 Pin 13 *C Selection
   local CSelection, rc = mc.mcSignalGetState(hSig)
   local PenJogOn, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)-- Is mapped to Port 2 Pin 1 *Jog on LED
   
   if XSelection == 1 then
      mc.mcMpgSetAxis(inst, 0, 0) --X Axis
      mc.mcCntlSetLastError(inst, "X Selected")
      mc.mcSignalSetState(PenJogOn, 1)
   elseif YSelection == 1 then
      mc.mcMpgSetAxis(inst, 0, 1) --Y Axis
      mc.mcCntlSetLastError(inst, "Y Selected")
      mc.mcSignalSetState(PenJogOn, 1)
   elseif ZSelection == 1 then
      mc.mcMpgSetAxis(inst, 0, 2) --Z Axis
      mc.mcCntlSetLastError(inst, "Z Selected")
      mc.mcSignalSetState(PenJogOn, 1)
   elseif ASelection == 1 then
      mc.mcMpgSetAxis(inst, 0, 3) --A Axis
      mc.mcCntlSetLastError(inst, "A Selected")
      mc.mcSignalSetState(PenJogOn, 1)
   elseif BSelection == 1 then
      mc.mcMpgSetAxis(inst, 0, 4) --B Axis
      mc.mcCntlSetLastError(inst, "B Selected")
      mc.mcSignalSetState(PenJogOn, 1)
   elseif CSelection == 1 then
      mc.mcMpgSetAxis(inst, 0, 5) --C Axis
      mc.mcCntlSetLastError(inst, "C Selected")
      mc.mcSignalSetState(PenJogOn, 1)
   else
      mc.mcMpgSetAxis(inst, 0, -1) --No Axis
      mc.mcCntlSetLastError(inst, "No Axis Selected")
      mc.mcSignalSetState(PenJogOn, 0)
   end
   
   if Step001 == 1 then
      mc.mcMpgSetInc(inst, 0, .001)
   elseif Step010 == 1 then
      mc.mcMpgSetInc(inst, 0, .010)
   elseif Step100 == 1 then
      mc.mcMpgSetInc(inst, 0, .100)
   end
   
   if PenStop == 1 then
      mc.mcCntlEStop(inst)
   end
end

Offline CRS

*
  •  61 61
    • View Profile
Re: Need some minor LUA adjustments...
« Reply #1 on: May 25, 2017, 04:21:00 PM »
Hi Soruud,

Not sure this will be of much help, but I have just been using the same script for my external MPG wheel with really good success.  I am also currently helping a friend with his Bridgeport conversion to Mach4 and he needs to emulate on-screen momentary buttons for where the existing toggle style buttons operate the Spindle FWD, REV and STOP.

Like you, I find Lua very difficult, getting success only by noticing some kind of repeating pattern, then copy and paste and try stuff until it works.  So I sympathise.

Your external momentary buttons will most likely be configured in a similar way to what we will have to do on screen.  I have asked the question about how to emulate a momentary button direct to Rob Gaudette at Mach Support, you may have seen or heard Rob on some of the Mach4 videos.  In the past he has been diligent at getting back to me, so when he does, I will let you know what he says.

There is a toggle option for the on screen buttons which Rob has said I can configure to suit external buttons as well, but that is not what you need.  If you look in your Mach4 Hobby Program folder and then in the Lua Examples, there is a file called Activate Output.mcs.  I think we may be able to use this.  Might be wrong, but I think perhaps the only way to emulate a true momentary action is to have the signal activate for a period of time, say 0.5 seconds, and then auto deactivate.  The Activate Output.mcs script does exactly that.  I don’t know yet how to implement it, but I will get there somehow.  Maybe Rob will steer me that way.

Our requirements a slightly different.  With the Bridgeport conversion to Mach, that machine has internal latching relays for the Spindle, and so the signal from Mach needs to only be active to latch which ever relay.  It would actually still work to use the existing toggle function leaving constant 24V power to the relay coil , but then the momentary STOP button at the machine which breaks the latching circuit would not be effective.  As soon as your figure comes off the button, the machine starts again. NOT very desirable hey!

In your case, unless you rig up some little latching relays between your pendant and PC, I guess you might have to somehow emulate that latching function in Mach4.  Not sure if that’s even possible.  I know it would just be easier for you to use something like a 5 position rotary switch for axis select.  You need an OFF position, plus the 4 axis, and one for the speed select.  Or perhaps like the PoLabs pendant, a separate dead man switch for the OFF.

Anyway Soruud, as soon as I find anything out, I’ll let you know.

Craig.
Re: Need some minor LUA adjustments...
« Reply #2 on: May 26, 2017, 06:39:28 AM »
Hi Craig. Thanx for your reply.
As time has past and no one had a awnser I had to try myself. And I got it to work :)
Take a look at the LUA below. I only got the X,Y,Z axis active for now.
I allso added a line ( scr.SetProperty('mpgaxisdisplay', 'Label', 'X axis'); under each Axis selection to post on screen what axis are currently active.
To display the current Axis just make an Toggle button in screen edit, and name it "mpgaxisdisplay" This name can be what you desire, but MUST be the same as in the
( scr.SetProperty('mpgaxisdisplay', 'Label', 'X axis'); line. The name is allso Case Sensitive !!!. And ofcourse you will need to change the X letter with Y, Z,A.. and so on in this line to show correct axis.
Actually this line can be used to display anything you want at anny time :)
The momentary buttons on my Control Panel are connected to a POKEYS57U board, and can easly be mapped to your desired Signal Inputs in MACH Config.
In my case as you see in the script I use Input 50, 51 and 52 for my X Y and Z axis.
Thats it. Now Axis selection will work with momentary buttons, and the current axis will be displayed on screen.



--------------------------------------
--       CNC4PC Pendant             --
--------------------------------------
-- These simply run the CNC4PCPendant function if their state changes.
[mc.ISIG_INPUT50] = function (state)    CNC4PCPendant() end,
[mc.ISIG_INPUT51] = function (state)    CNC4PCPendant() end,
[mc.ISIG_INPUT52] = function (state)    CNC4PCPendant() end,
--[mc.ISIG_INPUT13] = function (state)    CNC4PCPendant() end,
--[mc.ISIG_INPUT14] = function (state)    CNC4PCPendant() end,
--[mc.ISIG_INPUT15] = function (state)    CNC4PCPendant() end,
--[mc.ISIG_INPUT16] = function (state)    CNC4PCPendant() end,
--[mc.ISIG_INPUT17] = function (state)    CNC4PCPendant() end,
--[mc.ISIG_INPUT18] = function (state)    CNC4PCPendant() end,
--[mc.ISIG_INPUT19] = function (state)    CNC4PCPendant() end,
}
-------------------------------------------------------------
-- CNC4PC Pendant function. --
-------------------------------------------------------------
function CNC4PCPendant()
local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT50)    -- Is mapped to Pokeys pin50 *X Selection
local XSelection, rc = mc.mcSignalGetState(hSig)
local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT51)    -- Is mapped to Pokeys pin51 *Y Selection
local YSelection, rc = mc.mcSignalGetState(hSig)
local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT52)    -- Is mapped to Pokeys pin52 *Z Selection
local ZSelection, rc = mc.mcSignalGetState(hSig)
--local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT13)    -- Is mapped to Port 2 Pin 7 *A Selection
--local ASelection, rc = mc.mcSignalGetState(hSig)
--local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT14)    -- Is mapped to Port 2 Pin 8 *.001 Selection
--local Step001, rc = mc.mcSignalGetState(hSig)
--local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT15)    -- Is mapped to Port 2 Pin 9 *.010 Selection
--local Step010, rc = mc.mcSignalGetState(hSig)
--local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT16)    -- Is mapped to Port 2 Pin 10 *.100 Selection
--local Step100, rc = mc.mcSignalGetState(hSig)
--local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT17)    -- Is mapped to Port 2 Pin 15 *Estop
--local PenStop, rc = mc.mcSignalGetState(hSig)
--local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT18)    -- Is mapped to Port 2 Pin 12 *B Selection
--local BSelection, rc = mc.mcSignalGetState(hSig)
--local hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT19)    -- Is mapped to Port 2 Pin 13 *C Selection
--local CSelection, rc = mc.mcSignalGetState(hSig)
local PenJogOn, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)-- Is mapped to Port 2 Pin 1 *Jog on LED

if XSelection == 1 then
    mc.mcMpgSetAxis(inst, 0, 0) --X Axis
    mc.mcCntlSetLastError(inst, "X Selected")
    mc.mcSignalSetState(PenJogOn, 1)
    scr.SetProperty('mpgaxisdisplay', 'Label', 'X axis');

elseif YSelection == 1 then
    mc.mcMpgSetAxis(inst, 0, 1) --Y Axis
    mc.mcCntlSetLastError(inst, "Y Selected")
    mc.mcSignalSetState(PenJogOn, 1)
    scr.SetProperty('mpgaxisdisplay', 'Label', 'Y axis');

elseif ZSelection == 1 then
    mc.mcMpgSetAxis(inst, 0, 2) --Z Axis
    mc.mcCntlSetLastError(inst, "Z Selected")
    mc.mcSignalSetState(PenJogOn, 1)
    scr.SetProperty('mpgaxisdisplay', 'Label', 'Z axis');

--elseif ASelection == 1 then
--    mc.mcMpgSetAxis(inst, 0, 3) --A Axis
--    mc.mcCntlSetLastError(inst, "A Selected")
--    mc.mcSignalSetState(PenJogOn, 1)  

--elseif BSelection == 1 then
--    mc.mcMpgSetAxis(inst, 0, 4) --B Axis
--    mc.mcCntlSetLastError(inst, "B Selected")
--    mc.mcSignalSetState(PenJogOn, 1)

--elseif CSelection == 1 then
--    mc.mcMpgSetAxis(inst, 0, 5) --C Axis
--    mc.mcCntlSetLastError(inst, "C Selected")
--    mc.mcSignalSetState(PenJogOn, 1)

--else mc.mcMpgSetAxis(inst, 0, -1) --No Axis
--    mc.mcCntlSetLastError(inst, "No Axis Selected")
--    mc.mcSignalSetState(PenJogOn, 0)

 end

    if Step001 == 1 then
        mc.mcMpgSetInc(inst, 0, .001)
    elseif Step010 == 1 then
        mc.mcMpgSetInc(inst, 0, .010)
    elseif Step100 == 1 then
        mc.mcMpgSetInc(inst, 0, .100)
    end

--if PenStop == 1 then mc.mcCntlEStop(inst) end
end


To make it work with momentary buttons I had to disable the:

--else mc.mcMpgSetAxis(inst, 0, -1) --No Axis
--    mc.mcCntlSetLastError(inst, "No Axis Selected")
--    mc.mcSignalSetState(PenJogOn, 0)

Offline CRS

*
  •  61 61
    • View Profile
Re: Need some minor LUA adjustments...
« Reply #3 on: May 26, 2017, 07:27:20 PM »
Hi Soruud, that is well done.  Thanks so much for sharing this with me and all interested.
Hey, I thought you said you didn't know much about Lua.  Maybe not, but in my present state of ignorance, I couldn't have done that.

I have studied what you did and can see how you did it.  I only just learned about “scr.SetProperty" by watching a video that DazTheGas has just made to show me (and anyone interested) how to set up a momentary screen button.

Check this;  https://www.youtube.com/watch?v=1Q5YxjsBfT4&feature=youtu.be

I like what you have done with the screen message for the selected axis.  The way it was, the selected axis comes up in the message line, but then disappears out of site when the next thing happens.  Well done!  I have learnt another thing.

I also have the Pokeys, but the 57E.  I have a slightly elaborate front panel and needed more inputs than the MB2 breakout board could give me.  I'll attach my front panel for your horror.

Smart what you did disabling the "No Axis Selected".  Now I get it,…. once an axis is selected, it just stays there until the state is altered by another selection.  It doesn't require an on-going ON state.  I guess this is how one learns this stuff.  One penny drop at a time.  No special momentary function needed.

Rob from Mach Support is also getting back to me with his idea for the Momentary switch.  So if you have an interest, I will post what he says in this thread.

Have a great day Soruud

Craig
Re: Need some minor LUA adjustments...
« Reply #4 on: May 28, 2017, 10:58:01 AM »
Hi again. So nice that you found my solution useful. It takes some effort to get things as I want them in Mach4, and most because of LUA. But things are getting bether evry day.
I am waiting for my panel buttons to arrive from a fabric in China, so I am just testing some things, and playing with the Screen editor. I allso made an screen background in SolidWorks just to
see if I could use it, and that worked out great. Now I just *********ed upp a lot of other things ;D

Take a look at the attached screen file. You may find a few features there you may like. The screen are just a mess, it works, but misses a lot.
I wrecked the MDI input, so it wont work, so I got a unique chanse to learn how to fix it. haha.

Allso take a look at this video from my Youtube channel. Thats my machine and control panel with 22" Touchscreen  8)

https://www.youtube.com/watch?v=hke3yk6u3_o

Offline CRS

*
  •  61 61
    • View Profile
Re: Need some minor LUA adjustments...
« Reply #5 on: May 29, 2017, 06:37:20 AM »
Hi Soruud or Stig.

Hey great job on your control cabinet and panel.  I watched your videos, very entertaining.  I also use Solidworks a lot and I was impressed by how fast you were mirroring things.  I couldn’t see you using your cursor, do you have a hotkey for mirror?

I am just about done, long day of Lua and switches.  Noting does me in faster than being ignorant and staring at the screen partly from frustration and partly just plain stuffed.  I would like to find out more about your on-going project.  It looks great.  Mine is just a cover to go over the existing control panel.  Yours is a creation.
I’ll get back when I am more awake.

Craig.
Re: Need some minor LUA adjustments...
« Reply #6 on: May 29, 2017, 08:26:12 AM »
Hi Craig. Well I got hotkeys for many things in SolidWorks. M for mirror, L for line, K for center line, C for circle, N for normal to and many more.. I draw way faster that way combining mouse and keyboard.
SolidWorks is mainly what I am doing at work when prototyping and product designing things. So when drawing skills are taken care of it is a lot easyer to have cnc as hobby :)
I allso use SolidCam for programming. Maybe some1 here in forum got a good post prosessor for me? SolidCam to Mach4.
Did you look at the screen file ? Tell me what you think? I can see the file has been downloaded a few times already, so maybe some1 else got some tips for me? Did I do anything wrong?

Sor.

Offline CRS

*
  •  61 61
    • View Profile
Re: Need some minor LUA adjustments...
« Reply #7 on: May 30, 2017, 07:05:24 PM »
Hi Soruud,

Sorry for the late reply, you know how life gets in the way.  Yes I opened your screen, you have been a busy guy haven’t you.  I still have to play around with it as my screen in my workshop is only a 17” (3:4), so your screen has its fair share of problems trying to fit properly.  It’s all squashed up like a can of sardines.

I am sure there is an easy and obvious way to adjust the fit, I will go and try again.  Lots of thought has gone into your screen hey.  You are a bit ahead of me.  I am still trying to get my head around not electrocuting myself and not blowing up the Pokeys, MB2 breakout board or the ESS.

I want to poke around a lot more and see how you have done things.  The whole screen set up thing is something that isn’t too hard to get your head around.  BUT LUA!  For someone with absolutely no experience in programming, that right there is enough to turn people of Mach 4.  It is the biggest hurdle and the thing I have spent and wasted the most amount of time on.  The rest is relatively intuitive and easy.  But Lua is a pain.  And without an understanding you can’t do a lot of things.

Anyway, stop my whinging. I watched a couple more of your videos.  You putting together your cabinet and also the SW videos.  It is so interesting to see someone else’s take on how to do things.  I use Solidworks a lot, but not really for my work.  Just to design stuff.  I have been building radio control racing yachts as my income for about 24 years now and after designing the hull in a program called Maxsurf, I bring the hull surface into Solidworks to construct all the deck and other detail.

Then it’s off to the Pattern Making guy (my mate) to get the patterns CNCed on one of his much bigger machines.  So it is with much interest that I watch how you work with SW.  Hey can I ask you a question?  By the way, that wasn’t the question.

Do you have external buttons for jogging? I don’t think I saw you do that on the video.  If you do, or don’t, do you have external control for the Incremental of continuous jogging?

Really like watching the videos Soruud.  What software do you use to capture your work on Solidworks?

Craig.
Re: Need some minor LUA adjustments...
« Reply #8 on: May 31, 2017, 01:58:12 AM »
Hi Craig. The screen file are looking the same on my screen I think. Main tab window down to the right are allmost folded up. Its because I only use this screen for testing,
and I needed the space. If youn are watching at 2.35min into the video you will see that I use an digital joystick for jogging. That is all I got, but an digital joystick
works exactly the same way as an momentary pushbutton, only 4 of them :) And it only works in continious mode. >:( This is a problem because I want it to work in INC mode as well. I really dont know how to do that... But some1 here in the formy may know? I am afraid we need to have dedicated buttons for INC jog, but it SHOUD be able to change the mapping of a button ?
I actually posted a tread about this problen in this forum, but there is no replays.  ???

Offline CRS

*
  •  61 61
    • View Profile
Re: Need some minor LUA adjustments...
« Reply #9 on: May 31, 2017, 02:55:36 AM »
Okay so the screen is still a work in progress.  I kind of got it to fit just by resizing, but thought there was a way of making the existing full screen re adjust, like one does with the TV screen.

Anyway, regarding the incremental and continuous jog from an external button.  Funny about that, it is exactly one of the things I still have to get sorted.  I'm helping a friend, who has even less idea about any of this, convert his old Bridgeport mill to Mach4 and then there is my own little Hercus mill conversion.  The external jog with inc and cont is for my machine.

I thought to have this one as a latching switch on my control panel.  I have many of the same kind of little push button switches as I saw on your video, I assume yours all come from China as well.
I also thought to make the default “off or un-latched” position incremental for safety and latched in for continuous.  Only need to use one input and now I just need to figure out how.  Should never use the work "JUST" with all this.

So maybe we can let each other know when we find out new stuff.  I think you have a better grasp.  I am still back at WHY?  All the Lua material seems to assume you know what the heck they are talking about.  I need to go back to, “why is it so?”

Here’s a basic question Soruud.  Do you know how to make a screen button activate an output?  I just want to know that right now.  The hours I have wasted.  They all seem to activate a pre-defined output like mc.OSIG_SPINDLEON.  I just want to active an output that I chose, for example OUTPUT_0.

Any ideas?

Craig.