Hello Guest it is May 13, 2021, 03:08:53 AM

Author Topic: Screwie Louie mcLua scripting for screwie lua learning  (Read 2095 times)

0 Members and 1 Guest are viewing this topic.

Screwie Louie mcLua scripting for screwie lua learning
« on: May 30, 2015, 01:39:19 PM »
Just copy & paste in the mcLua editor and Run or use it in a button script. Couldn't get the .mcs file attached.

Demonstrates variable scoping, using 'for' loops, return codes, and 'goto' statement.

Run it first. Then deref your axis and run it again to see if the script picks up that your axes are not homed. Then read the code remarks.

The functions are only examples of what we can do. I just did this as a learning process.

--josh


--comments on--
Code: [Select]
--local variables are associated within this script only
--you can define global variables in the Screen Load Up script
--variables pass through all functions in this script
--Define local variables and initialize

local inst = mc.mcGetInstance ()                                                           
local rc = 0
local remark = ""

--using a 'for' loop to find what axes are enables
--this example is just written a different way, it is harder to read but faster to write

function countEnabledAxis ()
    local j = 0; for i=0, 11 do; if mc.mcAxisIsEnabled (inst, i) == 1 then j = j + 1; end; end
    wx.wxMessageBox ("You have " ..j.." axes enabled."); return j
end

--using a 'for' loop to make sure the axes are homed before executing movement
--the number of iterations or loops is equal to the number of enabled axes only
--we then check to see if the number of axes is homed is equal to number of axis enabled, kinda like a signal verification check

function safetyCheck1 ()
    local var = 0
    var = countEnabledAxis ()
    local j = 0
    for i=0, var do
        if mc.mcAxisIsHomed(inst,i) == 1 then
            j = j + 1
            wx.wxMessageBox ("Axis " ..j-1 .." is homed")
        end
    end
    wx.wxMessageBox (var .." axes enabled\n" ..j .." axes homed")
    if j ~= var then
        return "Not all Axes are homed.", false
    end 
end

--if applicable above functions can be loaded into the Screen Load Up script to return a value to your button script
--functions in the Screen Load Up script are accessible to all button scripts
--visualize function main(), call for your defined global safetyCheck function and then execute main function in your button

function main() 
    wx.wxMessageBox ("All is good to execute some screwie louie custom button script.")
end

--this is the point at which the script actuall starts
--the first thing is to start a safety check, this check can be anything you want it to be
--if the machine is not where you want it to be the safety check will return false

wx.wxMessageBox ("Being safety check.")
remark, rc = safetyCheck1 ()
local var = rc

--safety check returns a message 'remark' and a return code value
--just want to demonstrate what we can use return codes for and the 'goto' statement
--if the machine is not where we want it, skip executing the main function of our button script and goto finish line

if rc == false then wx.wxMessageBox (tostring(remark)) mc.mcCntlEnable (inst, 0) end goto finish
main ()


::finish::

--this let's use execute our current script in the editor enviroment for debugging puposes
if mc.mcInEditor () == 1 then main() end

--comments off--
Code: [Select]
local inst = mc.mcGetInstance ()                                                           
local rc = 0
local remark = ""

function countEnabledAxis ()
    local j = 0; for i=0, 11 do; if mc.mcAxisIsEnabled (inst, i) == 1 then j = j + 1; end; end
    wx.wxMessageBox ("You have " ..j.." axes enabled."); return j
end

function safetyCheck1 ()
    local var = 0
    var = countEnabledAxis ()
    local j = 0
    for i=0, var do
        if mc.mcAxisIsHomed(inst,i) == 1 then
            j = j + 1
            wx.wxMessageBox ("Axis " ..j-1 .." is homed")
        end
    end
    wx.wxMessageBox (var .." axes enabled\n" ..j .." axes homed")
    if j ~= var then
        return "Not all Axes are homed.", false
    end 
end

function main() 
    wx.wxMessageBox ("All is good to execute some screwie louie custom button script.")
end

wx.wxMessageBox ("Being safety check.")
remark, rc = safetyCheck1 ()
local var = rc
if rc == false then wx.wxMessageBox (tostring(remark)) mc.mcCntlEnable (inst, 0) end goto finish
main ()
::finish::

if mc.mcInEditor () == 1 then main() end

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Screwie Louie mcLua scripting for screwie lua learning
« Reply #1 on: May 30, 2015, 08:05:52 PM »
works good just tried it
Re: Screwie Louie mcLua scripting for screwie lua learning
« Reply #2 on: June 02, 2015, 12:53:06 AM »
Heck yah! Thanx Daniel.

--josh

Offline Chaoticone

*
  • *
  •  5,626 5,626
  • Precision Chaos
    • View Profile
Re: Screwie Louie mcLua scripting for screwie lua learning
« Reply #3 on: June 02, 2015, 01:41:20 AM »
mcs attachment test
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!
Re: Screwie Louie mcLua scripting for screwie lua learning
« Reply #4 on: June 02, 2015, 03:08:07 AM »
My computer just wanted to upload sample.mcs.  (with a period after the mcs extension, I'm sure it is something on my part) Thanx tho!