Hello Guest it is March 28, 2024, 03:54:40 PM

Author Topic: Spindle Speed Lua Script..  (Read 1108 times)

0 Members and 1 Guest are viewing this topic.

Spindle Speed Lua Script..
« on: February 10, 2022, 03:45:04 PM »
Environment:

Mach4 Version: Hobby (latest version) Licensed
OS: Windows 10 Pro (664 bit).
Controller: Pokeys57CNC
VFD: Huanyang HY01D523B 1.5KW

My inverter, like many others, does not provide a digital stream that can be used to provide a real-time feedback signal that can be fed back to update Mach4's with the spindle's current speed (RPMs). The VFD, instead, provides a 0-10V output that represents 0(0 RPM) up to 10V (maximum spindle RPM).

I talked to Pokeys engineering about two years ago to see if I could find a solution since I was interested in configuring Mach4 to delay motion start until the Spindle had reach 90% of the G-Code programs Speed setting.  The solution was to feed this signal back to pin 41 on the controller via a voltage divider network that reduces the voltage range to 0-3.3VDC and write a LUA script to read the voltage and which will get converted to 0->1.0VDC and do a simple calculation to translate to RPM (V-reading * max Speed)=>RPM. Once this is done, I just had to invoke mc.mcSpindleSetSensorRPM(instance,speed).

All this worked when I was using V4300 of Mach4 however with the default milling profile  the screens field labled "SPIN RPM" is not updated. However if I run in debug mode All the values are fine, I set the ...SpindleSetSensorRPM then do a  val=SpindleGetSensorRPM(instance) and I can see that "set method" did its job and the "get method" returns the same set value.

There script is not complex it just has the main routine that calls a getRegister function to retrieve the value (Pin41) from the Pokeys57CNC controller, and then calls a routine that does the calculation to create the equivalent speed and does the ...SpindleSetSensorRPM function. the main program is also smart enough to throttle back to execute the update process every 10th cycle using a self-created register that is incremented +1 each time the "main" runs. This script should work with any inverter that provides an analog DC voltage that can be read by your converter.

When I initially implemented this code, about 2 years ago, I was unable to have the spindle speed test (90% before moving spindle) work but the actual updating and display of what I was sending to Mach4 (as per above) was working fine. The Mach4 support staff could not figure out how to resolve that issue, so I gave up and just inserted a 6 second delay in every G-Code script I generate.

Has anyone had any experience updating the SpindleSensor via provided "mc" methods?

I've been waiting four days from the support staff on the trouble ticket I wrote this week.

Reuben M. Prichard Jr.

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Spindle Speed Lua Script..
« Reply #1 on: February 15, 2022, 03:29:01 PM »
mc.mcSpindleSetSensorRPM(m_inst, currRpm) works for me.  It is what Sim uses to simulate a spindle as well.  Perhaps you can post the code that you are using?  Maybe something else is setting the spindle speed with mcSpindleSetSensorRPM(m_inst, currRpm) and there is a conflict. 

Steve
Re: Spindle Speed Lua Script..
« Reply #2 on: February 15, 2022, 09:20:02 PM »
I've included the script (below) and have removed some of the diagnostic code and added several comments. As previously mentioned I have no problem with the operations of the program and can see that the value that is used in the "set..RPM" method is indeed being set (via an immediate call to the "get" method)...It seems as though the source for the "Spin RPM" field may not be getting updated from the object that represents the "actual spindle speed".

The other thing may be how i'm using the script. For example, I modify the file..then compile it..run it in debug mode and runs as expected less updating anything on the Mach4 mill display. Is there something else I need to do to get this linked to the main execution stream??

Writing code is not a stumbling block for me as I've designed and written >2M lines of code prior to retirement.

====================(CODE FOLLOWS)============

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

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 inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, "iRegs0/FireCount") -- Get the handle
local countVal=0                         --initialize countVal to 0
 
if(hreg>0) then
 
                countVal= mc.mcRegGetValue(hreg) -- get the throttle count
                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
end
 
countVal=countVal+1                     -- pop the counter up 1
mc.mcRegSetValue(hreg,countVal)  --Update the "FireCount" register

=======================Code End=====================

Thanks in advance for any guidance!

Reuben

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Spindle Speed Lua Script..
« Reply #3 on: February 16, 2022, 03:38:07 PM »
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:
Code: [Select]
-- 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:
Code: [Select]

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

Re: Spindle Speed Lua Script..
« Reply #4 on: February 17, 2022, 07:44:02 AM »
Steve...

I'm sure you nailed the problem I'm having... the traditional (old school) UNIX development environment is quite a bit different as the process is programs and functions are developed to relocatable code, which is then linked together via a separate process that links libraries to create executable programs. Apparently when I original wrote the script 2+ years ago and the first and only time, I figured out this difference and with LUA and remembered it stick it in the screen script.

Thanks

Reuben
Re: Spindle Speed Lua Script..
« Reply #5 on: February 24, 2022, 07:45:21 AM »
Steve,

FYI: I did some additional digging in my original Mach4 V4300 PCL Script and after figuring out where everything is located and how to get to it realized that what I had implemented is a PCL script that executed a "dofie" method to run the script I imbedded in the previous post... Since i had relocated the entire install structure to my Desktop for backup...I just changed the PATH in the "dofile" and ran via the backup...still works fine.

I've trimmed down the functions, cleaned up the logic, and implemented your recommendations. It is not working yet however I'm still making some design changes.... I actually ordered the "Programming in LUA - 4th Ed." for a reference doc but the language syntax is so similar to "C," I probably don't need it.
Re: Spindle Speed Lua Script..
« Reply #6 on: February 24, 2022, 01:49:31 PM »
Just finished making all my changes and decided to move all my "Constants & two functions" to the Load_script. This left just a few lines to be included in the PCL_Script.. I've compiled and verified everything is running as expected with exception of the actual "Spin Field" being updated with the value being set by mc.mcSpindleSetSensorRPM(i,speed). Ive also checked the return code of the SpindleSetSensorRPM and it returns 0. I've also looked at the Spindle configuration and didn't see any options to set the source of the actual spindle speed. I continue to look through the manual, internet, etc...

I'm not expecting any response from Support since in the several weeks since I submitted a question regarding the API I haven't heard from them.  I do appreciate the guidance I have received to date!
Re: Spindle Speed Lua Script..
« Reply #7 on: February 24, 2022, 02:28:34 PM »
Try setting the 'DRO Code' property of the DRO you are using to display the spindle speed to 'Spindle True RPM'
Re: Spindle Speed Lua Script..
« Reply #8 on: February 25, 2022, 07:31:19 AM »
That was "the ticket"... Everything works now... I went back and checked the original "field" object (A.K.A DRO) I was using in the v4300 Mach4 that worked and...indeed it was set correctly as well.

Much thanks to All for the help! I'll add some additional config documentation so next time I have a reference.

I know there are many M3 & M4 users that have spindles that do not provide a digital pulse for spindle speed feedback (only a 0-10VDC pin for spindle speed available on the VFD).



Reuben