191
Mach4 General Discussion / Re: Read encoder
« on: August 05, 2021, 08:46:34 AM »
This is my code in the macro
local inst = mc.mcGetInstance()
--get starting positon and encoder postion
local hreg = mc.mcRegGetHandle(inst, 'iRegs0/StartPos')
local hreg2 = mc.mcRegGetHandle(inst, 'iRegs0/StartEncPos')
local StartEncPos = mc.mcRegGetValue(hreg2)
--read rough position from position dro and set temp postion dro
local TempPos = mc.mcAxisGetPos(inst, 0)
--determine encoder counts moved
local hreg3 = mc.mcRegGetHandle(inst, 'ESS/Encoder_0')
local EncRawVal = mc.mcRegGetValue(hreg3)
local EncMove = EncRawVal - StartEncPos
--determine distace encoder moved
local hreg4 = mc.mcRegGetHandle(inst, 'iRegs0/EncScale')
local EncScale = mc.mcRegGetValue(hreg4)
local EncPos = EncMove / EncScale
--set position to encoder postion
local GCode = string.format("G92 X%.4f\n", EncPos)
mc.mcCntlGcodeExecute(inst, GCode)
--wx.wxMilliSleep(2000)
--calculate distance off
local hreg5 = mc.mcRegGetHandle(inst, 'iRegs0/DistOff')
local DistOff = EncPos - TempPos
local rc = mc.mcRegSetValue(hreg5, DistOff)
--wx.wxMilliSleep(2000)
--if distance off too great move to position
if (DistOff >= 0.010) then
--move to position after actual position determined
GCode = string.format("G1 F100 X%.4f\n", TempPos)
mc.mcCntlGcodeExecute(inst, GCode)
end
if (DistOff <= -0.010) then
--move to position after actual position determined
GCode = string.format("G1 F100 X%.4f\n", TempPos)
mc.mcCntlGcodeExecute(inst, GCode)
end
local inst = mc.mcGetInstance()
--get starting positon and encoder postion
local hreg = mc.mcRegGetHandle(inst, 'iRegs0/StartPos')
local hreg2 = mc.mcRegGetHandle(inst, 'iRegs0/StartEncPos')
local StartEncPos = mc.mcRegGetValue(hreg2)
--read rough position from position dro and set temp postion dro
local TempPos = mc.mcAxisGetPos(inst, 0)
--determine encoder counts moved
local hreg3 = mc.mcRegGetHandle(inst, 'ESS/Encoder_0')
local EncRawVal = mc.mcRegGetValue(hreg3)
local EncMove = EncRawVal - StartEncPos
--determine distace encoder moved
local hreg4 = mc.mcRegGetHandle(inst, 'iRegs0/EncScale')
local EncScale = mc.mcRegGetValue(hreg4)
local EncPos = EncMove / EncScale
--set position to encoder postion
local GCode = string.format("G92 X%.4f\n", EncPos)
mc.mcCntlGcodeExecute(inst, GCode)
--wx.wxMilliSleep(2000)
--calculate distance off
local hreg5 = mc.mcRegGetHandle(inst, 'iRegs0/DistOff')
local DistOff = EncPos - TempPos
local rc = mc.mcRegSetValue(hreg5, DistOff)
--wx.wxMilliSleep(2000)
--if distance off too great move to position
if (DistOff >= 0.010) then
--move to position after actual position determined
GCode = string.format("G1 F100 X%.4f\n", TempPos)
mc.mcCntlGcodeExecute(inst, GCode)
end
if (DistOff <= -0.010) then
--move to position after actual position determined
GCode = string.format("G1 F100 X%.4f\n", TempPos)
mc.mcCntlGcodeExecute(inst, GCode)
end