Machsupport Forum

Mach Discussion => Mach4 General Discussion => Mach4 Toolbox => Topic started by: Screwie Louie on May 27, 2015, 03:34:11 AM

Title: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on May 27, 2015, 03:34:11 AM
I keep forgetting to set my soft limits to on! PITA when I crash. So, here is some little Lua code that you can add to the RefAllHome button to automatically set your soft limits to on. 1. Operator->Edit screen->highlight RefHomeAll button, 2. on the left middle side of your screen, click on the notepad with the lightning bolt, 3. highlight where you see the code starting and click on the three dots box (side note: left down action vs. left up action vs left down script vs. left up script vs. click script are actions that are in relation to your mouse whereas when you press your left mouse button down it can call a script you have written, or call a predefined action written by NFS that you can choose from the pop up menu, etc. and then when you release the mouse button the left up action/script can do another call; the clicked script only executes a written script once -thank you simpson36 for sharing!) Hope this may help someone.

function softLimitsOn ()
    local inst = mc.mcGetInstance()
    local rc = 0
    local i = 0
    local j = 0

    for i=0,11 do                                                 --because there are 12 axes that can be mapped
        local val, rc = mc.mcAxisIsEnabled(inst,i)     --find which axes are enabled
        if val == 1 then                                         --if they are enabled, then
            j=j+1                                                   --start a counter
        end
    end

    j=j-1                                                            -- j=j-1 because of the whole '0' is actually the first instance in an arrays/stack,etc.

    for i=0,j do                                                   -- for how many axes are enabled; set the softlimit state to on
        mc.mcSoftLimitSetState (inst, i, 1)               -- found that mcSoftLimitSetState is more reliable than mcAxisSoftLimitEnable call function;
    end
end
softLimitsOn()                                                   --[[ call your softLimitsOn function and execute, I found that if you put this code in screenLoadScript
                                                                            that it may cancel your jogging capabilities whereas they will become greyed out. But hey,
                                                                            you never know unless you try!--]]

--josh
Title: Autoenable softLimitsOn() on startup.
Post by: Screwie Louie on May 28, 2015, 03:43:08 AM
1. Operator -> Edit Screen -> highlight wxMach on screen tree editor (top left side of screen) -> goto Properties (middle left side of screen) -> click events (lightning bolt) -> highlight Screen Load Script -> click on Ellipsis box to enter the mcLua editor

2. Create a global variable to count ie, myCounter = 0

--Start my code.

myAxisCount = 0

function myTotalEnabledAxis ()
    local mInst = 0
    local rc = 0
    local inst = mc.mcGetInstance (mInst)

    for i=0, 11 do
        if mc.mcAxisIsEnabled (inst,i) == 1 then
            myAxisCount = myAxisCount + 1
        end
    end
end
myTotalEnabledAxis ()

function setMySoftLimitsOn(myAxisCount)
    local mInst = 0
    local rc = 0
    local inst = mc.mcGetInstance (mInst)

    for i=0,myAxisCount do
        mc.mcSoftLimitSetState (inst,i,1)
    end
end
setMySoftLimitsOn(myAxisCount)
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on May 28, 2015, 03:46:11 AM
--another way--


function autoSoftLimitsOn ()
    local mInst = 0
    local rc = 0
    local inst = mc.mcGetInstance (mInst)

    for i=0, 11 do
        if mc.mcAxisIsEnabled (inst,i) == 1 then mc.mcSoftLimitSetState (inst,i,1) end
    end
end
autoSoftLimitsOn()

Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on May 28, 2015, 06:36:14 AM
from the first post...j=j-1 -- because the whole '0' thing about being the first instance in a stack / array, etc., whereas '0' or Zero is a number!

but...Lua does not recognize the first key indexed as '0' but '1'.

So what? What does this mean to you? Well....if you are running a loop, you can start with i =1 and Lua will return the first instance. C++ and other languages like to start with '0'...just keep that in the back of your head when doing counters, loops, iterations, etc.

again, so what? tables, man...tables   indexing and pulling values like in milliseconds with Mach4 being off by '1'unit is not good. It is NOT mach4's fault but the macro programmer's fault (or micro programmer,lol!, it may be our own fault if we just copy and paste and not logically understand the sequence of events. BECAUSE we are manipulating one programming language as function calls and variable pointers to another (Lua -> Gcode...or C++ to machine code, or Lua -> C+++ -> machine code -> bits bytes and lions and tigers and bears oh my! barf.) Let me break it down...what's our standard? how about 0.0001? Now you see how much '1' unit, key or value, or instance can affect the outcome of your efforts and not achieve your desired endstate? not to mention the cost of time an money through trial and error...(but it can be fun, trial and error that is. I think that's called experience (not necessarily old age, ha!)))(I don't know how many parenthesis's I just used here?.?.?.)

idk, this is kinda fun. I actually get to program a sequence and watch physical objects move in space and time....oh wait, that's robotics...haha! yep. ok, one too many drinks for me tonite!

-josh
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on May 28, 2015, 06:47:42 AM
one more thing....yep, about the third way...

it may seem cleaner and neater but you need to know...

what if you had 1,000,000 things to look at?
what if you you needed the value of myCounter again for another function?
what if you needed to query the number of another value?...will you cycle through a whole table again to get your answer from number 1 in sequential order? I hope not. (think, web traffic, or lines of Gcode, esp. contour milling).

There is a post by TimGS about binary tree or hash table searching...and then it died. Guess what.?.?...He is right! It is called Discrete Mathematics. I applaud you Mr. Tim.
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: dude1 on May 28, 2015, 06:50:06 AM
your one hard out person in a good way
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: cd_edwards on June 07, 2015, 11:52:57 AM
--another way--


function autoSoftLimitsOn ()
    local mInst = 0
    local rc = 0
    local inst = mc.mcGetInstance (mInst)

    for i=0, 11 do
        if mc.mcAxisIsEnabled (inst,i) == 1 then mc.mcSoftLimitSetState (inst,i,1) end
    end
end
autoSoftLimitsOn()



I added this to my Ref All home button script.. Not sure if this is an ideal location as the function is completed and soft limits are set before the homing routine is finished. this probably needs to be done in the PLC while the machine is still.
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on June 07, 2015, 05:42:27 PM
Yep, I agree! Thanx for looking out. We could do this a couple ways like always...lol. Like using a global flag set = true in the RefAllAxis button script.

Screen load up script...
flagSoftLimits = false --define and initialize global flag

function autoSoftLimitSet () --define auto soft limits axis set for PLC script to call if global flag is true
local inst = mc.mcGetInstance ()
for i=0, 11 do; if mc.mcAxisIsEnabled(inst, i) == 1 and mc.mcAxisIsHomed (inst, i) == 1 then; mc.mcSoftLimitSetState (inst, i, 1) end; end
end

Button script...
   mc.AxisHomeAll(inst); flagSoftLimits = true

PLC....
if flagSoftLimits == true then autoSoftLimitsSet(); flagSoftLimits = false; end   --call function if global flag is true, execute, reset flag



....something like that I guess :) am I close? Better? I'm all about using the correct order of operations, process efficiencies, and safety. Thanx again!

--josh
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: dude1 on June 07, 2015, 06:29:19 PM
flag that`s good keep the safety stuff in your code is a good idea
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on June 07, 2015, 06:58:21 PM
...better yet!   if (call function and returns value = true) then set soft limits = on

rc = mc.mcAxisHomeCompleteWithStatus(
      number mInst,
      number axisId
      number success);
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: cd_edwards on June 08, 2015, 12:52:32 AM
after thinking about this, I don't think this is a good idea.. We'd need to loop and determine if an axis is enabled, if it is then call this function. Sounds great, until you think about your Z axis with a bit in it. With this function, I would be homing X, then Y/A and finally Z. Moving the X and Y before homing Z doesn't seem like it's in my best interest at all.
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on June 08, 2015, 01:35:36 AM
you set your homing order in mach config. the function mcAxisIsHomed is asking "is the axis homed?" before setting the soft limits on. I am not homing the axis. That is mc.mcAxisHome or mc.mcAxixHomeAll.

---oh, mc.mcAxisHomeCompleteWithStatus. Yes, you are right. That is a reporting function by the motion controller. Good catch. My mind is on something else on other functions to stop the motor, sync it's position, before moving onto the next chunk of lua code.

we want to use a conditional if statement with 'mc.mcAxisIsHomed' (after referencing is complete)---as the condition. If 'Is homed' = true then set soft limits on.....
 
Orginally I was just asking if the axis was enabled and setting the soft limits to on (assuming the softlimits are just any homing offsets) which would take effect after referencing machine coordinates (0,0,0). But it makes more sense to reference the machine first, then set the soft limits on 'after' finding (0,0,0). Yep.

gotta place it in the plc, but do it in such a way that when you toggle the SL button the plc call doesn't reactivate it; as so to give you the user the choice to turn off the soft lmits....hold on. give me fifeteen minutes and I'll figure it out. Only been doing this lua stuff for two weeks and I'm stuck on something simple. I have yet to get to screensets and stuff. Although I can toggle a full screen toolpath display. That was fun figuring out. I'm stuck on this motor stopping, position syncing, before going to next chunk of lua code bit. Hold on...I'll be back.
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on June 08, 2015, 06:23:58 AM
ok...two hours later but I gott'er done the way you suggested. I left all the messages for flag tracking purposes so anyone can see how the flags are changed from btn script / PLC scan & global function calls / and screen load up script.
Attached is the StatusList history report. You can see how the PLC, GUI, and global variables and functions interact and will not execute unless conditions are met (as if there are three different states with interlacing conditional statements that are nested to synchronize gCode, Lua, GUI, and signal events. Remember, this is only "a" way to do this. There are many more ways to accomplish the same endstate. Add to it, subtract whatever you like. I'm tired. forgot the following:" for i=0, 11 do if mc.mcAxisIsHoming(inst, i) then mc.mcSoftLimitSetState(inst, i, 0) end end" and mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_SOFTLIMITS_ON),0) signal is turned off while homing in case you deref your axes and need to re-reference. G'nite.

Task: auto set soft limits after homing for all enabled axes

Conditions: set soft limit state to on 'after' referencing all axes is complete, continue to use soft limit button toggle to turn on/off soft limits manually

Standard: use lua scripting in a emulated PLC environment

Assumptions: you must have soft enable checked for your axes in mach config, homing order: Zaxis = 1, Xaxis = 2, Yaxis = 2

CONOPS:

flagON     SignalOFF     =     SignalON
    1               0                         1      

flagOFF    SignalON      =     SignalOFF
    1               1                         0

flagOFF     SignalOFF     =     SignalON
    1               0                         1

Button script: "btnSoftLimits"
--take the SoftLimits Toggle option off; we will code our own toggle button
--Left Down Script

   flagSoftLimitsOff = 1

Button script: "btnRefAll"

    local inst = mc.mcGetInstance();
    mc.mcAxisHomeAll(inst);
    flagSoftLimitsOn = 1

Screen Load Up script:

--Define global variables.
    flagSoftLimitsOn = 0
    flagSoftLimitsOff = 0

--Soft limits set for all referenced axes after homing.
    function setMySoftLimits()
        local inst = mc.mcGetInstance()
        local a = 0
        local b = 0
        for i = 0, 11 do if mc.mcAxisIsEnabled(inst, i) == 1 then a = a + 1 end end
            mc.mcCntlSetLastError (inst, tostring(a) .." axes enabled.")
        for i = 0, a do if mc.mcAxisIsHomed (inst, i) == 1 then b = b + 1 end end
            mc.mcCntlSetLastError (inst, tostring(b) .." axes homed.")
        if flagSoftLimitsOn == 1 then
            if a == b and b ~= 0 then
                for i = 0, b-1 do
                    mc.mcSoftLimitSetState(inst, i, 1)  
                    mc.mcCntlSetLastError (inst, "Axis " ..tostring(i) .." soft limit value is " ..tostring(mc.mcSoftLimitGetState(inst, i)))
                end
                flagSoftLimitsOn = 0
            end
        elseif flagSoftLimitsOff == 1 then
            for i = 0, b-1 do
                mc.mcSoftLimitSetState(inst, i, 0)
                mc.mcCntlSetLastError (inst, "Axis " ..tostring(i) .." soft limit value is " ..tostring(mc.mcSoftLimitGetState(inst, i)))
            end
            flagSoftLimitsOff = 0
        end
    end

PLC script:
--SoftLimts scan    
if  machState == mc.MC_STATE_IDLE and mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.OSIG_SOFTLIMITS_ON)) == 0 and flagSoftLimitsOn == 1 then
    mc.mcCntlSetLastError (inst, "(PLC send, btnRefAllHome) flagSoftLimitsOn = " ..tostring(flagSoftLimitsOn) .."   flagSoftLimitsOff = " ..tostring(flagSoftLimitsOff))
    setMySoftLimits()
    mc.mcCntlSetLastError (inst, "(PLC return) flagSoftLimitsOn = " ..tostring(flagSoftLimitsOn) .."   flagSoftLimitsOff = " ..tostring(flagSoftLimitsOff))
elseif machState == mc.MC_STATE_IDLE and mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.OSIG_SOFTLIMITS_ON)) == 1 and flagSoftLimitsOff == 1 then
    mc.mcCntlSetLastError (inst, "(PLC send, btnSoftLimits toggle off) flagSoftLimitsOn = " ..tostring(flagSoftLimitsOn) .."   flagSoftLimitsOff = " ..tostring(flagSoftLimitsOff))
    setMySoftLimits()
    mc.mcCntlSetLastError (inst, "(PLC return) flagSoftLimitsOn = " ..tostring(flagSoftLimitsOn) .."   flagSoftLimitsOff = " ..tostring(flagSoftLimitsOff))  
elseif machState == mc.MC_STATE_IDLE and mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.OSIG_SOFTLIMITS_ON)) == 0 and flagSoftLimitsOff == 1 then
    flagSoftLimitsOn = 1
    flagSoftLimitsOff = 0
    mc.mcCntlSetLastError (inst, "(PLC send, btnSoftLimits toggle on) flagSoftLimitsON = " ..tostring(flagSoftLimitsOn) .."   flagSoftLimitsOff = " ..tostring(flagSoftLimitsOff))
    setMySoftLimits()
    mc.mcCntlSetLastError (inst, "(PLC return) flagSoftLimitsOn = " ..tostring(flagSoftLimitsOn) .."   flagSoftLimitsOff = " ..tostring(flagSoftLimitsOff))
end

--j

Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: dude1 on June 08, 2015, 06:49:27 AM
well done I will test for ya tomorrow if I can get the f in machine working
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: cd_edwards on June 08, 2015, 10:06:18 AM
so much for simple SoftLimitsOn() haha. Thanks for the hard work. I'll rip what I did out of my system and check this out.
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on June 09, 2015, 04:56:53 AM
No worries guys! I appreciate the feedback in doing the right thing the right way. No matter how much it takes or how many times I rewrite something, it doesn't mean anything to me unless it's #1 safe, #2 effective, #3 effecient (albeit calculated risks need to be accepted at times, if not assessed or mitigated -> your manager/supervisor is not looking out for you;  they are looking out for their own best interests). Especially dealing with automation. Hey, the Terminator is going to my b!t(h when I'm done learning from y'all!

On a side note....don't look at the code as a "SoftLimitsOn" function, but look at it in a way in which Mach 4 operates, communicates, and synchronizes events across multiple environments....that is the beauty and elegance of m4. Freakin pure genius;  them m4 programmers. They designed a core that is a system, or a core system of systems (multiple environments spread across multiple instances) I didn't even to start to think like this until I dug into m4.

--j

ps. gotta screenSet coming for you guys soon. (so a person posted  a "blue screen" thread. As in blue screen of death. I gotta a "black screen" of death coming!" ...it's cheesy and I'm sure has been done before...all black background, full screen toolpath with led's outputs/signal status, DRO's, gCode file ops, etc. The intent is to show what m4 can do WITHOUT any coding. That's the kicker. No coding! The built in functions and capabilities are great. Sorry, no naked ladies with boobies as MPG's...although...???...
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: dude1 on June 09, 2015, 05:46:52 AM
Sorry, no naked ladies with boobies as MPG's...although...???...

Bugger
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: dude1 on June 09, 2015, 05:48:35 AM
the way you are doing is good we all hate the health and safety BS but gotta have it
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Screwie Louie on June 09, 2015, 05:54:15 AM
thanx Dan!
Title: Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
Post by: Dbl_Tap on August 11, 2023, 09:37:44 AM
I know this is a VERY old post but it is exactly what I was looking for. Is it still functioning correctly in the latest builds of Mach4? I've learned the hard way to triple check these kinds of things before blindly copying and pasting.