Hello Guest it is April 24, 2024, 11:44:58 PM

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

0 Members and 1 Guest are viewing this topic.

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
Autoenable softLimitsOn() on startup.
« Reply #1 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)
--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()

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
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.

Offline dude1

*
  •  1,253 1,253
    • View Profile
your one hard out person in a good way
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #6 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.
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #7 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
« Last Edit: June 07, 2015, 05:44:47 PM by Screwie Louie »

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #8 on: June 07, 2015, 06:29:19 PM »
flag that`s good keep the safety stuff in your code is a good idea
Re: Simple Lua code for softLimitsOn() after RefAllHome button execution
« Reply #9 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);