Hello Guest it is March 29, 2024, 05:47:30 AM

Author Topic: Where should I paste this script?  (Read 2593 times)

0 Members and 1 Guest are viewing this topic.

Offline sx3

*
  •  41 41
    • View Profile
Where should I paste this script?
« 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.
« Last Edit: January 10, 2019, 04:01:57 PM by sx3 »
Re: Where should I paste this script?
« Reply #1 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?
Chad Byrd

Offline sx3

*
  •  41 41
    • View Profile
Re: Where should I paste this script?
« Reply #2 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?

Offline sx3

*
  •  41 41
    • View Profile
Re: Where should I paste this script?
« Reply #3 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?
Re: Where should I paste this script?
« Reply #4 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline sx3

*
  •  41 41
    • View Profile
Re: Where should I paste this script?
« Reply #5 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.
« Last Edit: January 11, 2019, 03:33:58 AM by sx3 »

Offline sx3

*
  •  41 41
    • View Profile
Re: Where should I paste this script?
« Reply #6 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

« Last Edit: January 11, 2019, 04:34:09 AM by sx3 »
Re: Where should I paste this script?
« Reply #7 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline sx3

*
  •  41 41
    • View Profile
Re: Where should I paste this script?
« Reply #8 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]
« Last Edit: January 11, 2019, 10:21:46 AM by sx3 »
Re: Where should I paste this script?
« Reply #9 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'