Hello Guest it is March 28, 2024, 09:19:36 AM

Author Topic: Creating your own homing script  (Read 3657 times)

0 Members and 1 Guest are viewing this topic.

Creating your own homing script
« on: December 02, 2017, 02:44:25 PM »
build 3841, pmdx411, gecko g540, lathe

I just finished installing a stepper to use my lathe spindle as an indexer and it works great.  I would like to be able to home this.  I have a single slot timing signal on the slot for rpm when using the spindle normally but thought I should be able to use it to home when the stepper is in use.  The following code actually stops at the same place each time but the pmdx411 sometimes throws off an error message 'can't transition from MDI RUNNIG to STOP'.  When I don't get that message the C axis dro gets set to zero but the machine position doesn't get set to zero and the led doesn't turn green.

First, is my approach logical/correct?
Second, can you spot any errors in the calls or signals
Third, is there another approach that doesn't include adding more sensors or wiring?

Code: [Select]
local function myhome()
  local inst=mc.mcGetInstance()
  local rc,sState
  local hSig,rc=mc.mcSignalGetHandle(inst,mc.ISIG_INDEX)
  if rc==mc.MERROR_SIGNAL_NOT_FOUND  then
    wx.wxMessageBox('wrong Signal')
  end
  sState=mc.mcSignalGetState(hSig)
  mc.mcCntlMdiExecute(inst,'g1 h366 f5000')
  while mc.mcSignalGetState(hSig)==1 do
    wx.wxMilliSleep(25)
  end
  mc.mcCntlCycleStop(inst)
wx.wxMilliSleep(500)
  mc.mcAxisSetPos(inst,mc.C_AXIS,0.0)
  mc.mcAxisSetMachinePos(inst,mc.C_AXIS,0.0)
  hSig,rc=mc.mcSignalGetHandle(inst,mc.ISIG_MOTOR3_HOME)
  mc.mcSignalSetState(hSig,1)
end


myhome()

TIA

RT
Re: Creating your own homing script
« Reply #1 on: December 03, 2017, 04:11:34 AM »
Hi RT,
index homing is usually enacted by the motion controller. The ESS certainly is and that's what I'm familiar with.

The reason is that if Mach were monitoring the index signal there will be a delay of milliseconds at the least before it could make some sort of decision
and stop the motor. If enacted within the FPGA or DSP chip of the external controller it could respond within microseconds, this is exactly what the ESS does
and so it can very quickly detect and act on an index pulse.

Within the limitation that Mach is going to respond slowly with consequences re the accuracy of the homed position I can see several ideas that would allow
you to monitor the index pulse.

One I've had some success with is:
Code: [Select]
LUA Syntax:
rc = mc.mcSignalWait(
number mInst,
number sigId,
number waitMode,
number timeoutSecs);
This responds at the rate of the signal script, ie pretty damn quick. If I understand Machs treatment of signals then within a millsecond or so.

Another possibility is to have code within the PLC script which reads the index input. The PLC script in Mach4 runs a lot faster than the macro pump in Mach3
but would still represent a potential delay in reading an index pulse of 10milliseconds or so.

Your code does something similar, you read the index pin and if not active then sleep for 25ms and read it again. In the mean time Mach can do nothing
else. You've given it a job to do so it tries to do it...but it can only do one thing at a time. If for whatever reason it can't do the job you've given it
then Mach is said to block, and you've seen messages to that effect.

I am no Lua expert but I believe that either of the two strategies I've mentioned allow Machs GUI to run while its waiting for an index pulse. In the case of the
first API call approach if an index pulse does not ocurr within a given time it will timeout rather than block.

The broad strategy I'm proposing is:
1) Issue a motion command that allows the spindle to rotate at slow speed for at least one complete revolution. This would require a
    mc.mcCntlGcodeExecute() API call so that the function returns immediately.
2)Have a small block of code to store and manipulate the machine co-ords that would execute when the index signal came active with the mc.mcSignalWait()
    API above.
3) Abort the remainder of the move. Note I'm not sure that a clearing of the motion planner is possible in Lua, it may be restricted to C++ in a plugin.
    Should that prove to be the case then it could be worked around...just let the motion command run its course and then back up to the co-ords stored
     as a result of step 2.

Does any of that sound like it might work to you?

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Creating your own homing script
« Reply #2 on: December 03, 2017, 06:25:23 PM »
Thanks for that Craig.

I'm getting MERROR_INVALID_ARGUMENT returned from this call:

local inst=mc.mcGetInstance()
rc=mc.mcSignalWait(inst,mc.ISIG_INDEX,mc.WAIT_MODE_LOW,10.0)

Same return value with mc.WAIT_MODE_HIGH

Syntax error?

TIA

RT
Re: Creating your own homing script
« Reply #3 on: December 03, 2017, 06:30:51 PM »
Hi,
yes I encountered the same problem when I first used the call. It turns out the call is valid for the first 64 Mach input
signals, mc.ISIG_INPUT0 through to mc.ISIG_INPUT63.

You will have to copy or double assign the index pin.
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Creating your own homing script
« Reply #4 on: December 03, 2017, 07:35:17 PM »
Okay, that works.  It would be nice if they would include that in the api manual!

Last two items, the following code does not set the machine position to zero and does not set the homed flag.  Am I not allowd to or is there syntax error here:

  mc.mcAxisSetMachinePos(inst,mc.C_AXIS,0.0)
  hSig,rc=mc.mcSignalGetHandle(inst,mc.ISIG_MOTOR3_HOME)
  mc.mcSignalSetState(hSig,1)

Going out of town on real business tomorrow for the week so I'll let you guys alone for a bit.
Thanks Craig for your help on this,

RT

BTW, always get a chuckle out of your signature line 8)
Re: Creating your own homing script
« Reply #5 on: December 03, 2017, 08:11:28 PM »
Hi,
ISIG_MOTOR3_HOME is an input signal, how can you set it? You can set an output signal but not an input.
There is a specific API for setting the homed flag, cant remember what it is at the momoent

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Creating your own homing script
« Reply #6 on: December 04, 2017, 12:47:34 AM »
Hi RT,
the APIs you're looking for are:
Code: [Select]
LUA Syntax:
rc = mc.mcAxisHome(
number mInst,
number axisId)
and:
Code: [Select]
LUA Syntax:
rc = mc.mcAxisHomeAll(
number mInst)


Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Creating your own homing script
« Reply #7 on: December 04, 2017, 07:04:27 AM »
Here is a button script I use to home the Z axis. You should be able to adapt this to suit your needs:

--Move Z axis to home position
local inst = mc.mcGetInstance()
-- Check  homed indicator
local hSig = mc.mcSignalGetHandle(inst, mc.OSIG_HOMED_Z)
local zHomed = mc.mcSignalGetState(hSig)
if (zHomed == 1) then
    mc.mcCntlMdiExecute(inst, "G00 G90 G53 Z0")
else
    local rc = mc.mcAxisHome(inst, mc.Z_AXIS)
    if (rc == mc.MERROR_NOERROR) then
        -- Set homed indicator
        local rc = mc.mcSignalSetState(hSig, 1)
    end
end

Allan
« Last Edit: December 04, 2017, 07:09:13 AM by Fledermaus »