Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: Pedio on March 09, 2016, 04:42:00 PM
-
Anyone know a way to map Lua code to a ShuttlePro button?
Thanks,
Peter
-
Hi Peter,
here are a short description
1. enable shuttle plugin
(https://dl.dropboxusercontent.com/u/60701703/foren/Shuttle2Signal/Shuttle2Signal2.png)
2. Map Input signals to shuttle keys (shuttle key numbers are 0 to 15 from upper left and ignore my change beetwen input2 and input0, description in the code sample)
(https://dl.dropboxusercontent.com/u/60701703/foren/Shuttle2Signal/Shuttle2Signal1.png)
3. edit screen (menu/Operator/edit Screen)
select first line in the screen tree manager (in my picture wx4)
in the lower tab select events
open "Screen load script"
(https://dl.dropboxusercontent.com/u/60701703/foren/Shuttle2Signal/Shuttle2Signal3.png)
here you can write handles for all input signals
must be in the SigLib Definition enclosed by { }
--------- code sample start ---------
---------------------------------------------------------------
-- Signal Library
---------------------------------------------------------------
SigLib = {
[mc.OSIG_MACHINE_ENABLED] = function (state)
machEnabled = state;
scr.SetProperty('btnRefAll', 'Enabled', tostring(state));
scr.SetProperty('btnRefAll2', 'Enabled', tostring(state));
scr.SetProperty('btnGotoZero', 'Enabled', tostring(state));
scr.SetProperty('tabJogging', 'Enabled', tostring(state));
if (state == 1) then
AxisEnable();
end
end,
-- here handle for input0
[mc.ISIG_INPUT0] = function (state)
--- your code here
end,
.....
.......
....
....
--------- code sample ende ---------
Here my code to select axis and steps for the jog
--------- code sample start ---------
-- if screen loaded all inputs are called multiple
-- to save x axis is first selection after screen start i swap INPUT2 and INPUT0
InputTrigger = 0 --suppress toggle for input7
[mc.ISIG_INPUT0] = function (state)
JogSetAxis(2)
end,
[mc.ISIG_INPUT1] = function (state)
JogSetAxis(1)
end,
[mc.ISIG_INPUT2] = function (state)
JogSetAxis(0)
end,
[mc.ISIG_INPUT4] = function (state)
local jogRate
jogRate=0.001
mc.mcJogSetInc(inst, 0,jogRate);
mc.mcJogSetInc(inst, 1,jogRate);
mc.mcJogSetInc(inst, 2,jogRate);
end,
[mc.ISIG_INPUT5] = function (state)
local jogRate
jogRate=0.01
mc.mcJogSetInc(inst, 0,jogRate);
mc.mcJogSetInc(inst, 1,jogRate);
mc.mcJogSetInc(inst, 2,jogRate);
end,
[mc.ISIG_INPUT6] = function (state)
local jogRate
jogRate=0.1
mc.mcJogSetInc(inst, 0,jogRate);
mc.mcJogSetInc(inst, 1,jogRate);
mc.mcJogSetInc(inst, 2,jogRate);
end,
[mc.ISIG_INPUT7] = function (state)
local jogRate
jogRate=1
mc.mcJogSetInc(inst, 0,jogRate);
mc.mcJogSetInc(inst, 1,jogRate);
mc.mcJogSetInc(inst, 2,jogRate);
end,
[mc.ISIG_INPUT8] = function (state)
if (state==1 and InputTrigger==0) then
InputTrigger = 1
ButtonJogModeToggle()
end
if (state==0 and InputTrigger==1) then
InputTrigger = 0
end
end,
....
.....
....
--------- code sample ende ---------
Hope i can help ;)
Tom
-
Here the forgoten function JOGSETAXIS
Insert it after the the SigLib Definition
This function select the axis.
ledX,ledY,ledZ are LEDs on the Screen to show me which axis is selected.
If you don't need comment out all lines with setproperty for led.
Or insert a LED on the Screen and name it ledX,........
function JogSetAxis(axis)
scr.SetProperty("ledX", "Value", "0")
scr.SetProperty("ledY", "Value", "0")
scr.SetProperty("ledZ", "Value", "0")
if (axis==0) then
scr.SetProperty("ledX", "Value", "1") ;
end
if (axis==1) then
scr.SetProperty("ledY", "Value", "1") ;
end
if (axis==2) then
scr.SetProperty("ledZ", "Value", "1") ;
end
mc.mcMpgSetAxis(0, 0, axis);
mc.mcCntlSetLastError(inst, "Axis " .. tostring(axis)); -- write to history
end
-
I will be at the CNC next week and will give it a try. Will let you know what happens.
-
tom|tom,
I am having problem with the } at the end of the code. I am getting "Unexpected character by }"
Here is the code I was using:
-- Turn on Laser at low power map to shuttle pro button 7
---------------------------------------------------------------
-- Signal Library
---------------------------------------------------------------
SigLib = {
[mc.OSIG_MACHINE_ENABLED] = function (state)
machEnabled = state;
scr.SetProperty('btnRefAll', 'Enabled', tostring(state));
scr.SetProperty('btnRefAll2', 'Enabled', tostring(state));
scr.SetProperty('btnGotoZero', 'Enabled', tostring(state));
scr.SetProperty('tabJogging', 'Enabled', tostring(state));
if (state == 1) then
AxisEnable();
end
end,
[mc.ISIG_INPUT7] = function (state)
local inst = mc.mcGetInstance();
local rpm = 1; -- Set this to the actual RPM you want as your starting point
mc.mcSpindleSetCommandRPM( inst, rpm );
mc.mcSpindleSetDirection( inst, 1 ); -- 1 == forward, 0 == off, -1 == reverse
}
-- End Turn on Laser at low power
-
I think there is a missing end at the end ;)
-- Turn on Laser at low power map to shuttle pro button 7
---------------------------------------------------------------
-- Signal Library
---------------------------------------------------------------
SigLib = {
[mc.OSIG_MACHINE_ENABLED] = function (state)
machEnabled = state;
scr.SetProperty('btnRefAll', 'Enabled', tostring(state));
scr.SetProperty('btnRefAll2', 'Enabled', tostring(state));
scr.SetProperty('btnGotoZero', 'Enabled', tostring(state));
scr.SetProperty('tabJogging', 'Enabled', tostring(state));
if (state == 1) then
AxisEnable();
end
end,
[mc.ISIG_INPUT7] = function (state)
local inst = mc.mcGetInstance();
local rpm = 1; -- Set this to the actual RPM you want as your starting point
mc.mcSpindleSetCommandRPM( inst, rpm );
mc.mcSpindleSetDirection( inst, 1 ); -- 1 == forward, 0 == off, -1 == reverse
end
}
-
Hmmmm - something does not seem to be working for me. I mapped button 7 on the ShuttlePro to input 6 and added the following code to the screen load.
-- Turn on Laser at low power map to shuttle pro button 7 (which is actually input 6 - I think)
---------------------------------------------------------------
-- Signal Library
---------------------------------------------------------------
SigLib = {
[mc.OSIG_MACHINE_ENABLED] = function (state)
machEnabled = state;
scr.SetProperty('btnRefAll', 'Enabled', tostring(state));
scr.SetProperty('btnRefAll2', 'Enabled', tostring(state));
scr.SetProperty('btnGotoZero', 'Enabled', tostring(state));
scr.SetProperty('tabJogging', 'Enabled', tostring(state));
if (state == 1) then
AxisEnable();
end
end,
[mc.ISIG_INPUT6] = function (state)
local inst = mc.mcGetInstance();
local rpm = 1; -- Set this to the actual RPM you want as your starting point
mc.mcSpindleSetCommandRPM( inst, rpm );
mc.mcSpindleSetDirection( inst, 1 ); -- 1 == forward, 0 == off, -1 == reverse
end
}
The ShuttlePro plugin is enabled. I then restarted M4. Am I doing anything wrong?
-
mc.ISIG_INPUT6 = function (state)
local inst = mc.mcGetInstance();
local rpm = 1; -- Set this to the actual RPM you want as your starting point
mc.mcSpindleSetCommandRPM( inst, rpm );
mc.mcSpindleSetDirection( inst, 1 ); -- 1 == forward, 0 == off, -1 == reverse
end
this compiles
this does not
[mc.ISIG_INPUT6] = function (state)
local inst = mc.mcGetInstance();
local rpm = 1; -- Set this to the actual RPM you want as your starting point
mc.mcSpindleSetCommandRPM( inst, rpm );
mc.mcSpindleSetDirection( inst, 1 ); -- 1 == forward, 0 == off, -1 == reverse
end
}
when you get to something that does not work test it by it's self if you go find what DAZ posted about how signals are done now that will help, the manual is wrong needs updated they should get Daz to do it
-
I am still trying to get the spindle button to work to turn the laser on my CNC on and off for positioning the work piece. I came back to this issue after I got a couple of other things fixed on the machine. Seems there is not enough time in the day.
Sorry if I am being remedial. The code after the siglib toggles the spindle (I use this to control the laser). The code after the second end statement works when used in a button.
Do I need to insert the following code in the 'screen load' load script on the top level of the tree? Is there any place special it should be inserted? I know nothing about Lua.
-- Map laser to shuttle pro button 8
SigLib = {
[mc.OSIG_MACHINE_ENABLED] = function (state)
machEnabled = state;
scr.SetProperty('btnRefAll', 'Enabled', tostring(state));
scr.SetProperty('btnRefAll2', 'Enabled', tostring(state));
scr.SetProperty('btnGotoZero', 'Enabled', tostring(state));
scr.SetProperty('tabJogging', 'Enabled', tostring(state));
if (state == 1) then
AxisEnable();
end
end,
mc.ISIG_INPUT6 = function (state)
local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then
mc.mcSpindleSetDirection(inst, 0);
else
mc.mcSpindleSetDirection(inst, 1);
local rpm = 1; -- Set this to the actual RPM you want as your starting point
mc.mcSpindleSetCommandRPM( inst, rpm );
end
-
Not sure I understand? but if you are referring to the 2 buttons on the right of the screen then you need to edit the functions for them in the screen load script as these turn the spindle on and off.
---------------------------------------------------------------
-- Spin CW function.
---------------------------------------------------------------
function SpinCW()
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then
mc.mcSpindleSetDirection(inst, 0);
else
mc.mcSpindleSetDirection(inst, 1);
end
end
---------------------------------------------------------------
-- Spin CCW function.
---------------------------------------------------------------
function SpinCCW()
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then
mc.mcSpindleSetDirection(inst, 0);
else
mc.mcSpindleSetDirection(inst, -1);
end
end
DazTheGas
-
I would like to get the same functionality on a button for the ShuttlePro. I would like to bring the spindle on CW at a power of 1 and then toggle off if the button on the ShuttlePro is pushed again. I don't seem to understand signal scripts. I posted what I was trying to insert into the screen load script for the main screen. It fails the compile.
The laser control script works on a button; however, the screen is far away from the CNC so I would like to put it on a ShuttlePro button.
-
You could do this by selecting User Gcode from the Shuttle config and then use a custom M code in that GCode.
DazTheGas
-
I found how to assign the User gcode to a button but don't know how to generate the file for the ShuttlePro to use or where to place the file. I looked at the scripting manual but did not get a lot of help. Is there any manual that talks about this?
I think I can use this code
function M3()
inst=mc.mcGetInstance()
mc.mcCntlSetLastError(inst, 'Spindle Clockwise')
mc.mcSpindleSetDirection (inst, 1)
end
if (mc.mcInEditor()==1) then
m3()
end
-
Create an m3 macro in your profile`s/macro directory to add your code from above.
load up your machine.ini in an editor and find the section ShuttlePro0, at the bottom of the section add "Gcode1=m3" (without the quotes) and this should then runthe m3 macro for you.
DazTheGas
-
Beware of mixing upper case and lower case in your function names. For example, when you define the M3() function you use upper case. But when it is called from the "in editor" if statement it is called with lower case m3().
Some people claim that the case of the function name and the upper/lower case used in the file name matter. I was not able to see any effect of upper/lower case in the file name, at least on my couple of PCs. But I did not test on Win8 or Win10, so I cannot guarantee there ISN'T as issue. To be safe, keep everything using the same case (I use lower case).
Bob
-
Still not doing something right? ???
I placed the attached macro in the macro directory under my machine's profile.
I then went to the .ini file and edited it to include the Gcode1=m3 (see attached)
It did not work. I notice the ShuttlePro plugin called it "User Gcode1" so I changed the "Gcode1" to "UserGcode1", "User Gcode 1", and "User Gcode1". None of them seemed to work.
Attached are screen shots.
Thoughts or suggestions?
Peter
-
Bob was right with the naming so I put this together so you can see the effects of upper and lower case problems.
http://www.machsupport.com/forum/index.php/topic,32620.0.html
DazTheGas
-
Daz,
I believe I have used the lower case for M3 in all instances. If I have done it wrong I am being remedial and can not see it.
I have tested the M3 macro with the MDI box as you show in the video and it works like it should. I suspect I am doing something wrong when assigning the Gcode1 name in the LUA screen load script. Any thoughts?
Thanks,
Peter
-
Just tested on 3 different versions of mach4
Gcode1=m3 at end of the ShuttlePro0 section works fine on all.
if you still have problems go into the help menu and use support/ package current profile, then upload the .m4prof file, I will see if theres anything causing the problem.
DazTheGas
-
Is there a way to get M4 to recompile the macros?
-
It seems the m3 code does not compile every time. I even shut down M4 and restart it and it does not work. Do I need to reboot the computer?
-
Delete the ones with the extension MCC and delete the mcLUA.MCC
DazTheGas
-
The program tells me that it can not find the m3.mcc file even when restart M4. I am removing the .mcc file and M4 seems to remake mcLUA but not the m3. Suggestions?
-
Check that theres no error in the script or it wont compile
DazTheGas
-
I hate being ignorant but that rarely stops me...
I got the script to work with the button on the ShuttlePro; however, it comes on with the last set spindle speed. I looked for LUA codes in the scripting manual but did not find them. Is it as easy as adding a line to my script that states:
mc.mcSpindleSetSpeed (1)
???? I would like to set the power level at 1 for the spindle speed.
-
This is driving me crazy - the good news is it is a very short trip!
I found the correct (I think) command for setting the laser power. I tried this:
function M3()
inst=mc.mcGetInstance()
mc.mcCntlSetLastError(inst, 'Spindle Clockwise')
mc.mcSpindleSetDirection (inst, 1)
mc.mcSpindleSetCommandRPM(inst, 1 )
end
if (mc.mcInEditor()==1) then
m3()
end
Did not work. Then I thought it may need the RPM before the command to turn on so I moved it - still does not work. Dang
I know I should be testing these before I use them, but not sure how? For example, I have this code on a button and it works well, but I don't seem to be able to get it to work elsewhere.
local inst = mc.mcGetInstance();
local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
local sigState = mc.mcSignalGetState(sigh);
if (sigState == 1) then
mc.mcSpindleSetDirection(inst, 0);
else
mc.mcSpindleSetDirection(inst, 1);
local rpm = 1; -- Set this to the actual RPM you want as your starting point
mc.mcSpindleSetCommandRPM( inst, rpm );
end
I would like to achieve the same function with a ShuttlePro button. I don't mind reading about how to do this, but I don't know where. Also, I would rather not learn LUA to set a ShuttlePro button.
Alms for the confused???
-
I cant see anywhere in your code where you have actually turned the spindle on??
try
mc.mcSpindleSetCommandRPM(inst, 1 )
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.OSIG_SPINDLEON),1)
mc.mcSpindleSetDirection(inst, mc.MC_SPINDLE_FWD);
DazTheGas