Hello Guest it is March 28, 2024, 09:14:34 PM

Author Topic: PLC Script troubleshooting  (Read 3592 times)

0 Members and 1 Guest are viewing this topic.

PLC Script troubleshooting
« on: September 10, 2016, 09:37:26 AM »
I have a Moore Jig grinding machine that has a reciprocating pneumatic Z-axis.  It has a glass scale on the axis that is hooked to my motion controler (PMDX-424).  I have a PLC script set up so the user can specify an upper and lower limit on the stroke and the solenoid will change direction once one of the limits are reached.  My problem is that sometimes the machine will only perform a partial stroke on the way down changing direction well before it reaches the lower limit.  It never does this on the way up.  Perhaps there is an issue with my code?

Here is a link to a quick video showing a half stroke on about the 3rd or 4th stroke:

https://youtu.be/L82Gi7fmf28

Here is the code I am using in the PLC script:

Code: [Select]

--My Z axis DRO to inches script starts here:

local inst = mc.mcGetInstance()
local hreg = mc.mcRegGetHandle(inst, "SmartBOBUSB/EncoderDRO") --get encoder input handle
local zEncoderVal = mc.mcRegGetValueString(hreg)
local zEncoderVal = tonumber(zEncoderVal) --convert input to number for math
local zEncoderVal = (zEncoderVal * 0.0004) --convert input from encoder counts to inches
WriteRegister("Z_DRO_INCH", tostring(zEncoderVal)) --write the result to the Z axis DRO

--My Z axis DRO to inches script ENDS here:

--My Z axis stroke monitor script Starts here:

local zStrokeUp = GetRegister("Z_STROKE_UP")
local zStrokeDown = GetRegister("Z_STROKE_DOWN")
local zStrokeUp = tonumber(zStrokeUp)
local zStrokeDown = tonumber(zStrokeDown)
local output_0 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0) -- Get handle for output 0
local output_1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1) -- Get handle for output 1
local output_0_state = mc.mcSignalGetState(output_0) --Get state of  signal
local output_1_state = mc.mcSignalGetState(output_1) --Get state of  signal

if  zEncoderVal >= zStrokeUp then
    mc.mcSignalSetState(output_0, 1)
elseif zEncoderVal > zStrokeDown and zEncoderVal < zStrokeUp and output_0_state == 1 then
    mc.mcSignalSetState(output_0, 0)
elseif zEncoderVal > zStrokeDown and zEncoderVal < zStrokeUp and output_1_state == 1 then
    mc.mcSignalSetState(output_1, 0)
elseif zEncoderVal <= zStrokeDown then
    mc.mcSignalSetState(output_1, 1)
end

--My Z axis stroke monitor script Ends here:




« Last Edit: September 10, 2016, 09:43:08 AM by beezerlm »
Re: PLC Script troubleshooting
« Reply #1 on: September 11, 2016, 11:52:40 PM »
I suggest adding some debug output to your script.  The easiest way is to use mcCntlSetLastError(), which outputs to the status/error line at the bottom of the Mach4 screen (and the "history" button gets you all the messages).  In each of your "if" and "elseif" cases where you change one of the solenoid control outputs, log the current encoder position and what you are commanding the solenoids to do.

For example,
Code: [Select]
if  zEncoderVal >= zStrokeUp then
    mcCntlSetLastError(inst, string.format( "pos %.4f turn output0 on",zEncoderVal) )
    mc.mcSignalSetState(output_0, 1)
elseif zEncoderVal > zStrokeDown and zEncoderVal < zStrokeUp and output_0_state == 1 then
    etc...

Not tested and you may need to tweak the string.format() to get it to work.  Then see what the encoder values are when you make the decisions to change directions.

Even better would be to save the raw encoder value in a separate variable from the one you use to store the value in inches.  The output both values in the mcCntlSetLastError() calls, just on the off chance that there is something happening in the conversion to inches (I don't know WHAT might go wrong there, but I do know that when debugging something, collecting more data is almost always the right thing to do).

Bob
Re: PLC Script troubleshooting
« Reply #2 on: September 12, 2016, 09:56:13 AM »
I added the debug script and set the upper limit to 0.0000" and the lower limit to -1.0000" and here is what I found:

Code: [Select]

pos -0.0112 turn output0 off
enc -28.0 turn output0 off

pos -6.6916 turn output1 on   ← bad number here, axis is definitely not at this position.
enc -16729.0 turn output1 on  ← bad encoder count here, axis is definitely not at this position.

pos -0.2652 turn output1 off
enc -663.0 turn output1 off

pos 0.0784 turn output0 on
enc 196.0 turn output0 on

pos -0.0628 turn output0 off
enc -157.0 turn output0 off


Any Idea why I get random bad encoder numbers?  I can actually see the bad number on my screen for a split second while watching the encoder count readout "dro" window i have.  It seems to self correct (carries on with correct readings after the bad one) and the machine does not appear to lose position after the bad encoder number?
Re: PLC Script troubleshooting
« Reply #3 on: September 12, 2016, 11:34:17 AM »
I just did some more testing,  this time the upper stroke limit was 3.000" and the lower limit was 1.000" so all numbers were positive.  I also set the feed rate to a very slow speed.  I still had a bad encoder number of about -12,000.  For some reason it always seems to be an encoder reading between -10,000 and -20,000 even if all numbers are in the positive range .  That would also explain why it only happens on the downstroke.
Re: PLC Script troubleshooting
« Reply #4 on: September 12, 2016, 01:43:53 PM »
Thanks for that last clue.  There looks to be a bug in the firmware that only shows up when the encoder is moving in a "negative" direction, and only under certain circumstances.  Let me know which version of our plug-in you are running (go to the "Diagnostics" menu and select the "PMDX SmartBOB").  I will send you a TEMPORARY patched version to see if this is indeed your problem.

Bob
Re: PLC Script troubleshooting
« Reply #5 on: September 13, 2016, 09:21:48 AM »
Screenshot is attached.
Re: PLC Script troubleshooting
« Reply #6 on: September 13, 2016, 12:05:32 PM »
Thanks.  I've just sent you a personal message with instructions on where to get the new plug-in and how to test it.

Bob
Re: PLC Script troubleshooting
« Reply #7 on: September 13, 2016, 04:06:55 PM »
Update: the patched version of the plug-in appears to have solved the problem.  This fix will be included in the next public SmartBOB plug-in release.

Bob