Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: DAAD on April 13, 2020, 10:16:25 AM

Title: Passing variable for gcode to module
Post by: DAAD on April 13, 2020, 10:16:25 AM
At the moment i'm making a button to execute a function from in the module i have made.

but at the moment i'm stuck on howto pass the gcode placed into the button into the module function.

Here is what i did:

Button code:
Code: [Select]
mc.mcGetInstance()
local gcode = "G00 G90 G53 G0 Z-20 A-20\n G90 X0 Y0 \n"

package.path = wx.wxGetCwd() .. "\\Modules\\AvidCNC\\?.lua"
zc = require "MyzControl"

zc.CallGcode(gcode)
zc.CheckRouter() --functie in module MyzControl

Module function:
Code: [Select]
function zControl.CallGcode(gcode)
local rc = mc.mcCntlGcodeExecute (inst,gcode)
if rc~=0 then
mc.mcCntlEnable(inst, false)
mc.mcCntlSetLastError (inst, "Something whent wrong when executing gcode / machine disabled for safety. error state "..rc)
end

Other module functions called from the button are working fine indicating that the problem lies within the formulation of the gcode/passing.
Title: Re: Passing variable for gcode to module
Post by: Bill_O on April 13, 2020, 10:56:12 AM
I found that for me Registers are very useful.


•   How to turn on Registers.
Go to Configure, Control, Plugins tab then place a green check next to Regfile.

•   How to make a new Register.
Go to Configure, Plugins then Regfile.
Click on the green plus sign.
   Give the register a name. (no spaces)
   Give the register a starting value.
   Put in a longer description.
   Persistant
      A green check will keep value on exit from Mach4.
      A red x will start every new start of Mach4 with the starting value.

•   How to read a Register.
local variable = mc.mcRegGetHandle(inst, ‘path’)
local variable2 = mc.mcRegGetValue(variable)
   Examples:
local hreg = mc.mcRegGetHandle(inst, ‘Encoder_0’)
local EncRawVal = mc.mcRegGetValue(hreg)
or
local hreg = mc.mcRegGetHandle(inst, ‘ESS/EncRaw’)
local EncRawVal = mc.mcRegGetValue(hreg)
or
local hreg = mc.mcRegGetHandle(inst, ‘iRegs0/NotchTime’)
local EncRawVal = mc.mcRegGetValue(hreg)

•   How to write to a Register. (this might be wrong)
local variable = mc.mcRegGetHandle(inst, ‘path’)
local mc.mcRegSetValue(variable, value)
or
local variable = mc.mcRegGetHandle(inst, ‘path’)
local mc.mcRegSetValue(variable, tostring(value))
   Example:
local hreg = mc.mcRegGetHandle(inst, ‘ESS/EncRaw’)
local mc.mcRegSetValue(hreg, 23.35)
   or
local Num = 23.35
local hreg = mc.mcRegGetHandle(inst, ‘ESS/EncRaw’)
local mc.mcRegSetValue(hreg, tostring(Num))
Title: Re: Passing variable for gcode to module
Post by: DAAD on April 13, 2020, 01:59:32 PM
thanks for the reply

Think i will just put in the gcode directly.
Tought it was something simple i had overseen.

Adam