Hello Guest it is March 28, 2024, 11:39:28 AM

Author Topic: Simple Lua code for softLimitsOn() after RefAllHome button execution  (Read 12592 times)

0 Members and 1 Guest are viewing this topic.

Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #10 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.
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #11 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.
« Last Edit: June 08, 2015, 01:37:36 AM by Screwie Louie »
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #12 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

« Last Edit: June 08, 2015, 06:37:35 AM by Screwie Louie »

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #13 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
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #14 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.
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #15 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...???...
« Last Edit: June 09, 2015, 05:07:44 AM by Screwie Louie »

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #16 on: June 09, 2015, 05:46:52 AM »
Sorry, no naked ladies with boobies as MPG's...although...???...

Bugger

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #17 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
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #18 on: June 09, 2015, 05:54:15 AM »
thanx Dan!
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #19 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.