Where are you running that code? In just a file? Mach won't ever run a random file. So yes, you will HAVE to put your code into the screen load and PLC script. I don't know how any of this worked with 4300, as it was no different than the current build in this regard. It didn't load random files either.
To implement this as a PLC script, then:
1. Put your ReadRegister() and SetSpindleSpeed() scripts in the screen load scrip.
2. You don't need a FireCount register. You just need a global variable defined in the screen load script. e.g. a line with "countVal = 0" in it.
3. Put your "main code" in the screen PLC script. But modify it to not look at the FireCount register. Take out your local countVal = 0 line and just reference the global countVal variable.
In the screen load script:
-- global countVal
countVal = 0
--Function to read value from Pokeys57CNC controller analog register (Pin41)
-----------------------------------------------------------------------------
function ReadRegister(i,device, analogPin)
--i = Instance
--device= the name of the PoKeys controller
--pin = the Pokeys pin to read
local hreg = mc.mcRegGetHandle(i, string.format("%s/Analog input %s",
device, analogPin))
local val = mc.mcRegGetValue(hreg)
return val
end
--Function to set active spindle speed
------------------------------------------
function SetSpindleSpeed(i, regValue, maxspeed, tweak)
--i = Instance
--regValue = number between 0-1 representing the
-- voltage read from controller pin41
-- where 0 represents 0 RPM, and 1.0 represents 24000 RPM
--maxspeed = maximum speed of the spindle (24000 RPM)
--tweak = fudge factor to correct RFI generated error on pin 41 reading
----------------------
local speed = regValue * maxspeed * tweak --RegValue 0-1.0V
if(regValue<=0.15) then -- If the register is under .15V then just set
-- the speed to 0 rpm
speed=0
end
local rc = mc.mcSpindleSetSensorRPM(i,speed)
--local rspeed=mc.mcSpindleGetSensorRPM(i) DEBUG to Verify SPINRPM "set"
end
And in the screen PLC script:
-- local inst = mc.mcGetInstance() -- this is probably already defined at the beginning of the PLC script.
local device = "PoKeys_*********X" -- The name of my controller
local analogPin = "41" -- Analog input pin number
local MAXSPEED=24000 -- Maximum spindle speed (RPM)
local TWEAK=.985144 -- Adjustment factor to correct for EMI on P41 reading
local mVal=countVal%10 -- get the modulo remainder
--mVal=0 -- Debug Code to fire every time (remove for operations)
if (mVal==0) then --Execution only runs every 10th execution of main
analogVal = ReadRegister(inst, device, analogPin) --get P41 Value
SetSpindleSpeed(inst, analogVal, MAXSPEED, TWEAK) -- Set SindleSpeed
end
countVal=countVal+1 -- pop the counter up 1
That will integrate your code into the Mach processes for your current screen set. However, I would implement it as a module and "require" the module in the screen load script and run a module function from the PLC script. It will make updating to a new build of Mach or a new screen set MUCH easier. So read up on LUA modules a bit.
Also, you can use the Mach spindle setup tab in the configuration dialog to implement ramp up and ramp down times. This is the standard way of implementing a spindle that has no RPM feedback. There is no need to modify your G code for a delay.
When you do get your feedback going, you can use the ISIG_SPINDLE_AT_SPEED and ISIG_SPINDLE_AT_ZERO to tell mach the status of the spindle and remove the ramp times.
If this help you, please close your support ticket. Because this is way beyond what our support people would be able to do for a hobby license as it is not a bug, but an implementation of a custom feature. Even Industrial license customers would have to pay for machine integration support.
Steve