Hi Zuxztah,
I have the same problem with less organic excuse.
The good news tho is that you only need four separate chunks of code each no more that half a dozen lines long. I don't claim to be
good enuf to write them and have them work straight off aside from which you would learn nothing in the process.
With the Program Run Tab on screen with Mach4 disabled, click Edit Screen on the Operator menu.
Click the Spindle CW/Stop button on the right hand side of the page. This should highlight the 'button code' in the screen elements tree on
the upper left and will show either the 'properties' or 'events' of the button at the lower left. With the 'properties' displayed you can change colours,
sizes, texts, enabled states and all that sort of stuff.
What I want you to have a look at tho is the 'events' associated with the button. You will note, assuming the default screen, that the left-up script
is the function 'SpinCW()'. The 'left-up' means the up-click event of the lefthand mouse button whereas the 'left-down' refers to the down-click event.
If you click on that and click on the rightmost extension button an editor window will open with the script for the button.
In this case it's pretty boring, it just the function SpinCW(), but notice the code underneath, all commented out but is in fact a copy of the code which
runs when SpinCW() is called. You may note that all it does is either set the spindle direction signal to off or set it to 1, ie CW. This won't work for you
because your using Modbus to control you spindle. You will have to substitute SpinCW() with code that turns your spindle CW at a given speed.
Just as a matter of interest the LUA editor display window is just a fraction of the LUA SCRIPT. All of the buttons, LEDs, DROs and so on all have their
own little bits of code associated with them. They are all gathered up into one script, the LUA SCRIPT, and it is that conglomerate which runs. You can view
the LUA script on the Oeratrator menu 'Lua Script'. Its quite a sizeable file, about 1000 lines on my machine. In there you will find the Screen load script, Screen
Unload script, Signal script and others besides. Over a period of time as you customise your machine, like your Modbus control, your code will end up
in this file as well. You can view it but you can't run or edit it. If you wish to make alterations you will have to open each button or what have you and edit
its script/code there. Note also that if you have say a button script open in the LUA editor then the conglomerate file can't be bought together because a
piece of it is still open in the editor. If you wish to run the code you need to save any edits and commonly close the editor window before the complete LUA
script can be compiled and run. Don't worry you will forget from time to time and Mach4 will hang as a result. You will have to close Mach4, sometimes
with the help of the 'Task Manager,' and reopen.
Open 'Lua Script' per the above and have a look at lines 136-165. These are the 'event scripts' for the two spindle buttons on screen.
With the left-up script in the Lua Editor rather than delete SpinCW() just comment it out, ie put '--' in front of it. Means that if you wish to unwind what you've
done at a later date its easy to do.
Type in this code (before or after the comments doesn't matter):
local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then
local hregcontrol=mc.mcRegGetHandle(inst,"modbus0/spinCntrl")
mc.mcRegSetValue(hregcontrol,01) -- substitute 01 with whatever bit pattern is required to turn the spindle clockwise
local hregfrequency=mc.mcRegGetHandle(inst,"modbus0/freq")
mc.mcRegSetValue(hregfrequency,2000) -- the value 2000 ie 200Hz will as this script grows and gets more sophisticated will be variable but for now....
else
local hregcontrol=mc.mcRegGetHandle(inst,"modbus0/spinCntrl")
mc.mcRegSetValue(hregcontrol,,00) - substitute 00 with whatever bit pattern is required to stop the spindle
end
Please note that I haven't tested this code, without a working 485 link I can't. Note also that some changes could be made to make it better but
try and read it thru and understand what is trying to do.
If sigState=1 (it must have been 0 before), then we want to turn the spindle on, ie update your two Modbus registers to turn the spindle CW and set
a frequency. If sigState~=1 (it must have been 1 before), then we want to turn the spindle off.
Craig