Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: sx3 on January 10, 2019, 03:55:26 PM

Title: Where should I paste this script?
Post by: sx3 on January 10, 2019, 03:55:26 PM
Hi guys,

I've written a small script for rotating a tool turret. But I'm not sure where I should put/paste it?

Code: [Select]
if (sig == mc.ISIG_INPUT1) and (state == 1) then
hSig, rc = mc.mcSignalGetHandle( inst, mc.OSIG_RUNNING_GCODE )
   if rc == 0 then
      GC_State = mc.mcSignalGetState(hSig)
      if GC_State == 1 then --GCode is running, better not let the turret to be turned!
         wx.wxMessageBox('GCode is running')
      else --Gcode is not running, it should e safe to rotate the turret!
            mcCntlGCodeExecuteWait(inst, "G91 B36") --Rotate the turret 36 degrees
         wx.wxMessageBox('Sucessfully rotated the turret! FTW!')
      end
   else
      wx.wxMessageBox('rc returned '..tostring(rc))
   end
end

I want it to rotate the turret when machine is enabled, but not when G-code is running.
Will the code run if the machine is running M6 tool change code or during a feed hold state you think? I rather want to block it during theese states as well.
Title: Re: Where should I paste this script?
Post by: Cbyrdtopper on January 10, 2019, 05:09:43 PM
What exactly are you doing?  As long as your code doesn't have a "B" axis move you shouldn't have to worry about it. 

Is this for a manual rotation?
Title: Re: Where should I paste this script?
Post by: sx3 on January 11, 2019, 01:12:59 AM
We have turret holding 10 tools that rotates. The turret is covered so that only one tool slot is availble.
The turret have a button on for manual operation (input1 in the code), so we want to to be able to rotate is by hand to take in/out tools from the turret.
Perhaps it's better to assign the step-motor to OB1 or something? how would the Gcode drive look like then? "G91 OB1 36" ?

But where should I put/paste this macro/sript?
Title: Re: Where should I paste this script?
Post by: sx3 on January 11, 2019, 02:52:31 AM
If I'm not mistaken, this should be a signal script?

When I open the signal script from screen editor there is by default this code:

Code: [Select]
if SigLib[sig] ~= nil then
    SigLib[sig](state);
end


Should I paste my code above, within or below this default code snippet?
Title: Re: Where should I paste this script?
Post by: joeaverage on January 11, 2019, 02:58:15 AM
Hi,

Quote
Perhaps it's better to assign the step-motor to OB1 or something? how would the Gcode drive look like then? "G91 OB1 36" ?

You cannot control out-of-band axes with Gcode as you can the regular coordinated axes. Out-of-band axes can and must be
jogged into position.

You will have to use Lua macros which use API's like:

LUA Syntax:
rc = mc.mcJogIncStart(
      number mInst,
      number axisId,
      number dist)
and:
LUA Syntax:
rc = mc.mcJogIncStop(
      number mInst,
      number axisId,
      number incr)
for incremental jogs........and for velocity jogs the APIs:
LUA Syntax:
rc = mc.mcJogVelocityStart(
      number mInst,
      number axisId,
      number dir);
and:
LUA Syntax:
mcJogVelocityStop(
      number mInst,
      number axisId)

The code that you posted sort of looks like it should be going in the PLC script. As it turns out Mach4 has introduced a much
more efficient way to handle inputs like this called the signal  table. Section 3.2.4 page 12 of
'Mach4 CNC Controller Lua Scripting Guide' in the docs folder will give you some ideas how it works.


Craig
Title: Re: Where should I paste this script?
Post by: sx3 on January 11, 2019, 03:29:09 AM
Thanks Craig,

Not sure how the jog works, but on the mc.mcJogIncStart, we have already given the dist (distance?) to 36.
But what number must we put in the mc.mcJogIncStop on the incr?

mc.mcJogIncStart(inst, mc.MOTOR7, 36)
mc.mcJogIncStop(inst, mc.MOTOR7)

Must I always start AND stop the jog? No possibility for a single command?


I want the script to add 36 degrees on the turretmotor for every push on the button.
Title: Re: Where should I paste this script?
Post by: sx3 on January 11, 2019, 04:28:55 AM
Hate it when I'm not able to edit my post, please apologize my spamming! :)
I think I start to understand this jogthing:

mc.mcJogIncStart(inst, 8, 0.1) --Axis 8
mc.mcJogIncStop(inst, 8, 360) --360/0.1=36 degrees

So the full code should be something like, if I'm not drunk. :)

Code: [Select]
if (sig == mc.ISIG_INPUT1) and (state == 1) then
hSig, rc = mc.mcSignalGetHandle( inst, mc.OSIG_RUNNING_GCODE )
   if rc == 0 then
      GC_State = mc.mcSignalGetState(hSig)
      if GC_State == 1 then --GCode is running, better not let the turret to be turned!
         wx.wxMessageBox('GCode is running')
      else --Gcode is not running, it should e safe to rotate the turret!
            mc.mcJogIncStart(inst, 8, 0.1) --Axis 8
            mc.mcJogIncStop(inst, 8, 360) --360*0.1=36 degrees
         wx.wxMessageBox('Sucessfully rotated the turret! FTW!')
      end
   else
      wx.wxMessageBox('rc returned '..tostring(rc))
   end
end

Title: Re: Where should I paste this script?
Post by: joeaverage on January 11, 2019, 09:07:00 AM
Hi,
I understand the intent of your code but it is not suitable as is. It has the wrong format for a signal table entry.
This is an example of a signal table entry:

Code: [Select]
[mc.ISIG_INPUT3]=function(state)
local hreg=mc.mcRegGetHandle(inst,'iRegs0/ToolIndex')
if state==1 then
mc.mcRegSetValue(hreg,1)
else
mc.mcRegSetValue(hreg,0)
end
end,

Note the square brackets [ mc.ISIG........ ] which denote an index into the table
and also the trailing comma:  ......end , which denotes the end of table entry definition.

Have you read and digested the section to which I referred in my previous post? The signal table idea is a very powerful
concept in Mach4 but it is far from obvious how it works.

Craig
Title: Re: Where should I paste this script?
Post by: sx3 on January 11, 2019, 10:05:59 AM
Hi,

Yes I started to read, but as you stated it wasn't quite obvious so I put focus on changing from G-code to JOG and while waiting for for input on that, start to investigate more in the signal table.
Now, I have done my best implementing it in the Signal table and this is my outcome, how do it look?
Must I put "RC =" before the mc.mcJog********* string or will it execute anyway?

I also added to check if the axis is homed aswell.

Code: [Select]
[mc.ISIG_INPUT1] = function (state)
if (state == 1) then
hSig, rc = mc.mcSignalGetHandle( inst, mc.OSIG_RUNNING_GCODE )
if rc == 0 then
GC_State = mc.mcSignalGetState(hSig)
if GC_State == 1 then --GCode is running, better not let the turret to be turned!
wx.wxMessageBox('GCode is running')
else --Gcode is not running, it should e safe to rotate the turret!
hSigHome, rc = mc.mcMotorIsHomed(inst, 8);
HomeState = mc.mcSignalGetState(hSigHome);
if HomeState == 1 then -- The turret motor seem to be homed, lest rotate the turret
mc.mcJogIncStart(inst, 8, 0.1) --Axis 8
mc.mcJogIncStop(inst, 8, 360) --360*0.1=36 degrees
wx.wxMessageBox('Sucessfully rotated the turret! FTW!')
else
wx.wxMessageBox('Turret motor is not homed, please home the machine!')
end
end
else
wx.wxMessageBox('rc returned '..tostring(rc))
end
end
end,/code]
Title: Re: Where should I paste this script?
Post by: joeaverage on January 11, 2019, 10:37:32 AM
Hi,
yes that structure is looking like the business.

Quote
Must I put "RC =" before the mc.mcJog********* string or will it execute anyway?

No, you don't have to include the return code, rc. The function will execute whether the return code is used or not.
It is good programming practice to use return codes at every juncture to catch errors but tends to be overlooked.

Craig
Title: Re: Where should I paste this script?
Post by: sx3 on January 11, 2019, 10:43:33 AM
Thanks Craig!

What do you think about the "check home part" ?
I'm not sure I've catched it correctly in above code.. I think it could be like this instead?
But don't know which one is correct, I'm not a programmer, did some HTML back in the 90's but that doesn't count :D

Code: [Select]
hSigHome, rc = mc.mcSignalGetHandle(inst, mc.mcMotorIsHomed(inst, 8));
HomeState = mc.mcSignalGetState(hSigHome);
if HomeState == 1 then -- The turret motor seem to be homed, lets rotate the turret
Title: Re: Where should I paste this script?
Post by: joeaverage on January 11, 2019, 01:12:50 PM
Hi,
it looks OK to me, I'm no expert however.

I think its time to put the code in the SigLib table (top of the screen load script) and try it out.

Craig
Title: Re: Where should I paste this script?
Post by: sx3 on January 11, 2019, 02:09:40 PM
Yes, will do that tomorrow!
Will post the outcome here when it's up & running.

Thanks Craig!
Title: Re: Where should I paste this script?
Post by: sx3 on January 12, 2019, 01:54:03 PM
I need some serious help guys!
I tried the script today, and it came out to nothing.

I  took a physical button and wired it to the ESS Port 2, pin 2 with a proper resistor.
I mapped port 2, pin 2 to Mach and ESS INPUT1.
I confirmed that the button is working as intended by looking under the operator tab, the input1 LED lit up when pushing the button.
Also under Diagnostic --> Log, I can see that Mach logs HIGH when pressing the button and LOW when releasing it.
So far so good!

But I can't seem to get the signal script to initiate at all. I tried my script and also simplyfied it alot.
I even made it so simple to just pop up a messagebox or setLastEroor to confirm it's alive, but no it's really dead!

Not sure I'm editing the script in the right way? I did it like this:
Operator --> Edit Screen: Marked WX in top left corner (screen tree manager), then I pressed on the lightning on the bottom left corner (properties) and opened the "screen load script" and simply pasted my script within the {} under siglib. Then just saved the script ans saved the screen.
Is this the correct way? or must I edit the script in other way?

I have also thought about if Input1 (assigned under Mach Configure) automatically is the same as mc.ISIG_INPUT1 ??
Or must they be assigned to eachother in some way so that mc.ISIG_INPUT1 listens to Mach physical input 1?

One thing I noticed but didn't got the time to test was, the Log said button had "SigID 2, INPUT#1", is it perhaps that Input1 actually is mc.ISIG_INPUT2 ?

Back to the Operator --> Edit Screen: There is a column named "Signal script" in the bottem left frame (properties).
That file/script whatever it is only contains:
Code: [Select]
if SigLib[sig] ~= nil then
    SigLib[sig](state);
end

Should I do something in here?


It's really frustrating when I'm not even able to receive a fault code, the code seem to not even be initiated. Running G-code or not, Enabled or not.
Title: Re: Where should I paste this script?
Post by: sx3 on January 12, 2019, 04:21:54 PM
I've tried some other basic scripts but none of return anything.

Code: [Select]
[mc.ISIG_INPUT1] = function(state)
if (state == 1 or state == 0) then
wx.wxMessageBox('ja')
else
wx.wxMessageBox('nej')
end
end,

Code: [Select]
[mc.ISIG_INPUT1] = function(state)
if (state < 3) then
wx.wxMessageBox('ja')
else
wx.wxMessageBox('nej')
end
end,

I've tried with "" and '' in the MessageBox, also tried ending it with a semicolon ;
From what I can tell, the script never initiate, it doesn't react to the designated input.

Must I enable something before signal scipts start to work?

It's really annoying not getting any error to help me forward. :D
Title: Re: Where should I paste this script?
Post by: joeaverage on January 12, 2019, 04:48:42 PM
Hi,
where did you put those code fragments? You do recognize that they are elements of a table....and must be in
the definition section of a table.....in this case the table is called SigLib?

These code fragments DO NOT BELONG in the Signal Script.

I assume that you have assigned the input (ISIG_INPUT1) to an input pin of  your controller?
Note also that the first few general inputs (inputs 0 thru 5) have attached LEDs on the machine diagnostics tab. Does the
appropriate LED light when the signal goes active?

Craig
Title: Re: Where should I paste this script?
Post by: sx3 on January 12, 2019, 04:53:57 PM
Hi Craig,

I've putted them in the Screen load Script, this is the total:

Code: [Select]
---------------------------------------------------------------
-- Signal Library
---------------------------------------------------------------
SigLib = {
[mc.OSIG_MACHINE_ENABLED] = function (state)
    machEnabled = state;
    ButtonEnable()
end,

[mc.ISIG_INPUT1] = function(state)
if (state == 1) then
wx.wxMessageBox("Ja")
else
wx.wxMessageBox("Nej")
end
end,
}

Correct, only thing I have in the Signal Script, is the default code that came with mach, that goes:
Code: [Select]
if SigLib[sig] ~= nil then
    SigLib[sig](state);
end

I have not assiged ISIG_INPUT1 inn particular, but I have in the Mach Config --> Inputs assigned Input #1 to my motion controller, ESS Port 2 pin 2. (which is mapped as input in the ESS plugin)
Yes, the apropriate LED light when I press the button, until I release it.
Title: Re: Where should I paste this script?
Post by: joeaverage on January 12, 2019, 05:06:01 PM
Hi,
well as far as I can tell I would expect your SigLib entry [mc.ISIG_INPUT1] to be executed at each change of signal.

Can you scan through the rest of the SigLib entries just to make sure that you don't have two function definitions
for the same signal?

Craig
Title: Re: Where should I paste this script?
Post by: sx3 on January 12, 2019, 05:13:09 PM
To see if ISIG_INPUT1 is present elsewhere in the load screen script? I'll check, but I have also tried to Map the button to Input20 and others just to outrule there is something going on with INPUT1.
Yes, I would have expected it to execute aswell. I haven't modyfied my profile at all, just assigned input/outputs and calibrated the machine so I don't think the profile is screwed up either but if I can't find the problem, then I'll maybe start from a fresh profile
Title: Re: Where should I paste this script?
Post by: sx3 on January 12, 2019, 05:24:01 PM
Hmm, could it be problems if I have two instances of Siglib in my Load Screen script?

Code: [Select]
SigLib = {}

SigLib = {}

I remember I've added a CNC4PC MPG I just copy & pasted the code and I beleive it had a SigLib = {} that I put in the bottom end of the Screen load Script.
Makes me think that the second SigLib will overwrite the first SigLib (where I'm elaborating at the moment) since they share the same name..

Unfortunatly, I don't have access to machine to test this.
Title: Re: Where should I paste this script?
Post by: joeaverage on January 12, 2019, 05:33:02 PM
Hi,
yes that sounds like the problem.....there should be only one SigLib table per instance.

Craig
Title: Re: Where should I paste this script?
Post by: sx3 on January 28, 2019, 06:52:04 AM
Just wanted to tell that the double SigLib was the problem. :)
Now we can continue testing and debugging the initial script.