Hello Guest it is May 12, 2021, 07:50:26 PM

Author Topic: Button Script works in debug mode but not when clicked - 4612  (Read 274 times)

0 Members and 1 Guest are viewing this topic.

This is code that has worked prior to updating to 4612.  This homes my C axis (rotary) in the left up script.  When I step through the code in debugger it homes but when clicked no homing takes place.  Last message is output, 'Homing: 0.0'.

Any Ideas?

Code: [Select]
local function myHomeC()
  local inst=mc.mcGetInstance()
  local sState
  local pos=-1

mc.mcCntlEnable(inst,0)
mc.mcCntlConfigStart(inst)
  local hSigH=mc.mcSignalGetHandle(inst,mc.ISIG_MOTOR3_HOME)
  rc=mc.mcSignalEnable(hSigH,1)
  mc.mcProfileWriteInt(inst,'Signal68','Enabled',1)
  mc.mcProfileSave(inst)
  mc.mcProfileFlush(inst)
mc.mcCntlConfigStop(inst)
mc.mcCntlEnable(inst,1)

  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)
  if sState==1 then  -- sitting on the slot so move off it
    mc.mcCntlGcodeExecuteWait(inst,'g0 f20000 h15\n')--g1 h366 f5000')
  end
  rc=mc.mcAxisHome(inst,mc.C_AXIS)
 
--this never gets executed
 mc.mcCntlSetLastError(inst,'Homing')
 
  repeat
    wx.wxMilliSleep(1000)
    rc= mc.mcAxisIsHoming(inst,mc.C_AXIS)
    mc.mcCntlSetLastError(inst,'Homing: '..tostring(rc))
  until rc==0 
  rc=mc.mcSignalEnable(hSigH,0)
  mc.mcProfileFlush(inst)
end

if (mc.mcInEditor() == 1) then
  myHomeC() 
end

TIA

RT
Re: Button Script works in debug mode but not when clicked - 4612
« Reply #1 on: March 22, 2021, 01:37:28 PM »
RH,
Is the whole purpose of this script just to home the C axis?
Do you want it inside an M code?
I see a lot of stuff in there that may not be necessary like profile saves and flushes the enable signals etc.
Can you just give me a little description of use case and I may be able to help :)
 
Re: Button Script works in debug mode but not when clicked - 4612
« Reply #2 on: March 22, 2021, 02:06:13 PM »
Yes it is to Home the C axis.  My spindle has a timing gear that I have a belt for one motor to spin and another belt to change out and have it powered by a stepper so as to act as an indexer.  The other profile code was to try and disable the motor so it would stop sending Home C switch activated signals when not in use as a C axis.  The Home signal shares the slotted disc on the spindle for speed recognition.
The problem is that this worked for over a year with a couple of different builds but now with 4612 it does not.  I reverted to an earlier build and it works again. I submitted a ticket with my profile packaged Saturday.
Thanks for taking a look at this.

RT

Offline smurph

*
  • *
  •  1,402 1,402
  • "That there... that's an RV."
    • View Profile
Re: Button Script works in debug mode but not when clicked - 4612
« Reply #3 on: March 22, 2021, 03:36:47 PM »
Is this a M code macro script?  Or does something else call the myHomeC() function? 

If you have this code in M code, say m200.mcs, it will never get called.  Because there is no function stub called m200(). 

Steve
Re: Button Script works in debug mode but not when clicked - 4612
« Reply #4 on: March 22, 2021, 03:40:54 PM »
As I said in my initial.post this is in a left up button script

Offline smurph

*
  • *
  •  1,402 1,402
  • "That there... that's an RV."
    • View Profile
Re: Button Script works in debug mode but not when clicked - 4612
« Reply #5 on: March 22, 2021, 03:47:58 PM »
I was trying to find out where this script resides.  I assume that if some button script must call it that it is in your screen load script?  Or is that ALL in the left up script?

Also

    mc.mcCntlGcodeExecuteWait(inst,'g0 f20000 h15\n')--g1 h366 f5000')

goes where/does what?  You are waiting on G code that never moves.  Did you mean to move the C axis off the switch?  Maybe "G91 G0 C1"

Steve

Offline smurph

*
  • *
  •  1,402 1,402
  • "That there... that's an RV."
    • View Profile
Re: Button Script works in debug mode but not when clicked - 4612
« Reply #6 on: March 22, 2021, 03:50:40 PM »
If it is ALL in the left up script, what calls myHomeC()?  I see a function stub a the bottom of the script:

if (mc.mcInEditor() == 1) then
  myHomeC()
end

But that will ONLY call the myHomeC() function IF you are inside the code editor.  That is a M code macro script style function stub.  It isn't needed in a button script. 

Steve

Offline smurph

*
  • *
  •  1,402 1,402
  • "That there... that's an RV."
    • View Profile
Re: Button Script works in debug mode but not when clicked - 4612
« Reply #7 on: March 22, 2021, 04:10:02 PM »
Put this in your screen load script.  Notice I removed the local keyword from the function definition.  This way, you could have multiple buttons call this function, if you wish. 

Code: [Select]
function myHomeC()
local inst=mc.mcGetInstance('myHomeC()') -- this will log all API calls as coming from source "myHomeC()"
local sState
local pos=-1

mc.mcCntlEnable(inst,0)
mc.mcCntlConfigStart(inst)
local hSigH=mc.mcSignalGetHandle(inst,mc.ISIG_MOTOR3_HOME)
rc = mc.mcSignalEnable(hSigH,1)
mc.mcProfileWriteInt(inst, 'Signal68', 'Enabled', 1)
mc.mcProfileSave(inst)
mc.mcProfileFlush(inst)
mc.mcCntlConfigStop(inst)
mc.mcCntlEnable(inst,1)

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)
if (sState == 1) then  -- sitting on the slot so move off it
mc.mcCntlGcodeExecuteWait(inst, 'g0 f20000 h15\n')--g1 h366 f5000')
end
rc = mc.mcAxisHome(inst, mc.C_AXIS)
 
--this never gets executed
mc.mcCntlSetLastError(inst, 'Homing')
 
repeat
wx.wxMilliSleep(1000)
rc = mc.mcAxisIsHoming(inst, mc.C_AXIS)
mc.mcCntlSetLastError(inst,'Homing: '..tostring(rc))
until rc == mc.MERROR_NOERROR
rc = mc.mcSignalEnable(hSigH, 0)
mc.mcProfileFlush(inst)
end

And then simply put:

myHomeC()

in any button's left up script you want.

I know I harp on error checking, but...  it needs more error checking!  :) 

I also still don't like the G code execute wait that doesn't move.  It may or may not cause problems. 
Re: Button Script works in debug mode but not when clicked - 4612
« Reply #8 on: March 22, 2021, 04:29:29 PM »
Steve, thank you for taking time to look at this, it is much appreciated.
The G91 has no meaning in lathe mode, ergo the g0 h15 moves the spindle 15 degrees relative to where it is at which is enough to move it off of the optical slot.
I added the 'mcineditor' trying to find a way to get this to work, which it didn't, so originally just had the function definition and the call to it in the button script.  Took it out after my initial post.  I will try adding your example to the screen load script when I get some free time and am ready to go back to 6412. 

RT

Offline smurph

*
  • *
  •  1,402 1,402
  • "That there... that's an RV."
    • View Profile
Re: Button Script works in debug mode but not when clicked - 4612
« Reply #9 on: March 22, 2021, 04:34:17 PM »
Ahh...  lathe.  :) 

This is what I mean by error checking.  It is verbose and wordy with lots of IF statements.  But with good error checking, there will be no mysteries if something goes wrong.  It will help you 1, 2 or 3 years from now when you are troubleshooting something when a rodent eats through a switch wire, etc...  LOL 

Ideally, you should check the return code of every API call that is critical to the success of the script at hand. 

Code: [Select]
function myHomeC()
local inst=mc.mcGetInstance('myHomeC()') -- this will log all API calls as coming from source "myHomeC()"
local sState
local pos = -1
local hSig
local hSigH
local rc
rc = mc.mcCntlEnable(inst,0)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not disable the machine.')
return
end

rc = mc.mcCntlConfigStart(inst)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not command the configuration state.')
return
end

hSigH, rc = mc.mcSignalGetHandle(inst,mc.ISIG_MOTOR3_HOME)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not get the motor 3 home signal handle.')
return
end

rc = mc.mcSignalEnable(hSigH, 1)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not enable the motor 3 home signal.')
return
end

rc = mc.mcProfileWriteInt(inst, 'Signal68', 'Enabled', 1)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Write the motor 3 home signal state to the profile.')
return
end

rc = mc.mcProfileSave(inst)  -- Do we really need to save the mach config to the profile? 
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not save the Mach config to the profile.')
return
end

rc = mc.mcProfileFlush(inst)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not flush the profile to disk.')
return
end

rc = mc.mcCntlConfigStop(inst)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not exit the config state.')
return
end
rc = mc.mcCntlEnable(inst,1)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not enable the machine.')
return
end

hSig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INDEX)
if (rc == mc.MERROR_SIGNAL_NOT_FOUND) then
wx.wxMessageBox('myHomeC(): Wrong Signal')
return
end
sState, rc = mc.mcSignalGetState(hSig)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Error getting signal state');
return
end
if (sState == 1) then  -- sitting on the slot so move off it
rc = mc.mcCntlGcodeExecuteWait(inst, 'g0 f20000 h15\n')--g1 h366 f5000')
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Error executing G code.  rc = ' .. tostring(rc) .. ".");
return
end
end
rc = mc.mcAxisHome(inst, mc.C_AXIS)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Error homing the C axis.');
return
end
 
--this never gets executed
mc.mcCntlSetLastError(inst, 'Homing')
 
repeat
wx.wxMilliSleep(1000)
rc = mc.mcAxisIsHoming(inst, mc.C_AXIS)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Error getting the C axis home state.');
return
end

mc.mcCntlSetLastError(inst,'Homing: '..tostring(rc))
until rc == mc.MERROR_NOERROR

rc = mc.mcSignalEnable(hSigH, 0)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not disable the motor 3 home signal.')
return
end
rc = mc.mcProfileFlush(inst)
if (rc ~= mc.MERROR_NOERROR) then
wx.wxMessageBox('myHomeC(): Could not flush the profile to disk after home C is complete.')
return
end
end

Steve