Page 1 of 1

PLC Programming Question - Not Using the Wizard - (Problem Solved)

Posted: Sat Jun 28, 2025 6:05 pm
by glbreil
Hello I have added a motorized tool probe arm and set it up so that INP16 is ProbeTrip, INP15 is ProbeDetect, and INP14 is ProbeArmMachineReady. The arm is raised and lowered with OUT19 & OUT20 using Auxillary buttons Aux3 & Aux4. That all works great, the arm goes up and down and activates the appropriate inputs. What I wanted to do was use INP14 to prevent the machine from executing a G-Code program when the arm was down in the tool measuring position. The problem is the code I have used not only keeps the machine from executing a G-Code program it keeps it from executing an auto tool measure command. It Seems SV_Program_Running is not the right command to use, but I am at a loss for what else would work. For now I have just commented it out and the rest works fine. I was hoping some of you could point me toward the right command to accomplish what I am trying to do. Here is the section of Code and the part in red is my problem. Thanks In Advance.

;---------------------------------------------------------------------
; NEW: Tool Setter Arm Control (Simplified for "Push and Hold" operation)
;---------------------------------------------------------------------

; Check if Tool Setter Machine Ready (INP14) is active
; This sets ToolSetterReady_M if INP14 is active, otherwise, it will be RST by default below
IF TRUE THEN RST ToolSetterReady_M ; Default to OFF
IF INP14 THEN SET ToolSetterReady_M

; Prevent G-code program from running if ToolSetterMachineReadyInput is not active
;IF SV_PROGRAM_RUNNING && !ToolSetterReady_M
; THEN ErrorMsg_W = TOOLSETTER_NOT_READY_MSG,
; SET ErrorFlag_M,
; SET SV_STOP


; Control ToolSetterArmDown (OUT19) with Aux3Key_M (push and hold)
; Default to OFF for ToolSetterArmDown and its active flag
IF TRUE THEN RST ToolSetterArmDown
IF TRUE THEN RST ToolSetterArmDownActive_M
IF TRUE THEN RST Aux3LED

; Condition to SET ToolSetterArmDown and its active flag
IF Aux3Key_M && !ToolSetterArmUpActive_M
THEN SET ToolSetterArmDown, SET ToolSetterArmDownActive_M, SET Aux3LED

; Control ToolSetterArmUp (OUT20) with Aux4Key_M (push and hold)
; Default to OFF for ToolSetterArmUp and its active flag
IF TRUE THEN RST ToolSetterArmUp
IF TRUE THEN RST ToolSetterArmUpActive_M
IF TRUE THEN RST Aux4LED

; Condition to SET ToolSetterArmUp and its active flag
IF Aux4Key_M && !ToolSetterArmDownActive_M
THEN SET ToolSetterArmUp, SET ToolSetterArmUpActive_M, SET Aux4LED

; Turn off both arms if E-Stop is active (Safety Interlock - highest priority)
; These will override any SET commands above if SV_STOP is true.
IF SV_STOP
THEN RST ToolSetterArmDown, RST ToolSetterArmUp,
RST ToolSetterArmDownActive_M, RST ToolSetterArmUpActive_M

;---------------------------------------------------------------------
; END NEW: Tool Setter Arm Control and G-code Program Interlock
;---------------------------------------------------------------------

Thanks Gary

Re: PLC Programming Question - Not Using the Wizard

Posted: Sat Jun 28, 2025 6:37 pm
by glbreil
I think I might have found a solution to my problem when studying other system variables in the PLC Programming Manual, but I haven't made it to the machine yet. Do you think this could work to all the machine to go ahead and auto measure when INP14 is not active? The key change is in red.

; Prevent G-code program from running if ToolSetterMachineReadyInput is not active
IF SV_PROGRAM_RUNNING && !ToolSetterReady_M && !SV_DOING_AUTO_TOOL_MEASURE
THEN ErrorMsg_W = TOOLSETTER_NOT_READY_MSG,
SET ErrorFlag_M,
SET SV_STOP

Re: PLC Programming Question - Not Using the Wizard

Posted: Sat Jun 28, 2025 7:52 pm
by glbreil
Problem Solved!

Re: PLC Programming Question - Not Using the Wizard - (Problem Solved)

Posted: Tue Jul 01, 2025 1:15 pm
by cncsnw
A couple of style and convention comments:

Code: Select all

; Check if Tool Setter Machine Ready (INP14) is active
; This sets ToolSetterReady_M if INP14 is active, otherwise, it will be RST by default below
IF TRUE THEN RST ToolSetterReady_M ; Default to OFF
IF INP14 THEN SET ToolSetterReady_M
Supposing that you have previously named INP14 as "ProbeArmMachineReady", it would be better to use that assigned name rather than the raw input name.

The above code (but using an assigned name) is exactly the same as:

Code: Select all

IF ProbeArmMachineReady THEN (ToolSetterReady_M)
... except that this version takes one line instead of two, probably runs a little faster, and is less prone to editing errors.

If there is no other condition that sets or resets ToolSetterReady_M, then you could just as well leave it out and reference ProbeArmMachineReady directly instead. The only reason to copy an input to a memory bit is so that M function macros and the cncxmsg.txt file can refer to a memory bit address (#70xxx / MEMxxx) which does not change even if the sensor gets moved to a different PLC input.

Regarding error reporting: fault conditions are not the same as error conditions.
See http://www.cncsnw.com/PLCApplicationsMPU11.htm

Code: Select all

IF SV_PROGRAM_RUNNING && !ToolSetterReady_M && !SV_DOING_AUTO_TOOL_MEASURE
THEN ErrorMsg_W = TOOLSETTER_NOT_READY_MSG,
SET ErrorFlag_M,
SET SV_STOP
Since you apparently intend this to be an error condition (one that stops the program cycle but does not remove power from the servos), you should not set SV_STOP.

If you wanted it to be a fault, you would assign the message code to FaultMsg_W instead of ErrorMsg_W, and you would not set ErrorFlag_M. Also, it would be better to set "OtherFault_M" rather than setting SV_STOP directly, so that the fault-detect and fault-reset code in MainStage can work properly.

Re: PLC Programming Question - Not Using the Wizard - (Problem Solved)

Posted: Tue Jul 01, 2025 11:29 pm
by glbreil
Thanks Marc, it took me a long time to get all that to work. I will have to study your comments some more to understand the error vs fault message. I am trying to get a little better understanding of what is going on in the PLC, it is slow going. Gary

Re: PLC Programming Question - Not Using the Wizard - (Problem Solved)

Posted: Wed Jul 02, 2025 10:29 pm
by glbreil
cncsnw wrote: Tue Jul 01, 2025 1:15 pm A couple of style and convention comments:

Code: Select all

; Check if Tool Setter Machine Ready (INP14) is active
; This sets ToolSetterReady_M if INP14 is active, otherwise, it will be RST by default below
IF TRUE THEN RST ToolSetterReady_M ; Default to OFF
IF INP14 THEN SET ToolSetterReady_M
Supposing that you have previously named INP14 as "ProbeArmMachineReady", it would be better to use that assigned name rather than the raw input name.

The above code (but using an assigned name) is exactly the same as:

Code: Select all

IF ProbeArmMachineReady THEN (ToolSetterReady_M)
... except that this version takes one line instead of two, probably runs a little faster, and is less prone to editing errors.

If there is no other condition that sets or resets ToolSetterReady_M, then you could just as well leave it out and reference ProbeArmMachineReady directly instead. The only reason to copy an input to a memory bit is so that M function macros and the cncxmsg.txt file can refer to a memory bit address (#70xxx / MEMxxx) which does not change even if the sensor gets moved to a different PLC input.

Regarding error reporting: fault conditions are not the same as error conditions.
See http://www.cncsnw.com/PLCApplicationsMPU11.htm

Code: Select all

IF SV_PROGRAM_RUNNING && !ToolSetterReady_M && !SV_DOING_AUTO_TOOL_MEASURE
THEN ErrorMsg_W = TOOLSETTER_NOT_READY_MSG,
SET ErrorFlag_M,
SET SV_STOP
Since you apparently intend this to be an error condition (one that stops the program cycle but does not remove power from the servos), you should not set SV_STOP.

If you wanted it to be a fault, you would assign the message code to FaultMsg_W instead of ErrorMsg_W, and you would not set ErrorFlag_M. Also, it would be better to set "OtherFault_M" rather than setting SV_STOP directly, so that the fault-detect and fault-reset code in MainStage can work properly.
Thanks for the information on the difference between errors and faults. I did indeed want to keep the machine from running a program if the arm is down and it is not specifically doing a tool measurement just to prevent being able to crash into the tool arm. So I set it up as a fault and set the OtherFault_M rather than the SV_Stop as you suggested. I also got rid of ToolSetterReady_M.

I am curious why it is better to set OtherFault_M rather than set SV_Stop directly? I also removed the ToolSetterReady_M. It does seem to work as intended, but is there anything in the rest of it that would be better done differently?

Thanks Gary

;---------------------------------------------------------------------
; NEW: Tool Setter Arm Control ("Push and Hold" operation)
;---------------------------------------------------------------------

; Prevent G-code program from running if ToolSetterMachineReadyInput is not active
IF SV_PROGRAM_RUNNING && !ToolSetterMachineReadyInput && !SV_DOING_AUTO_TOOL_MEASURE
THEN FaultMsg_W = TOOLSETTER_NOT_READY_MSG,
SET OtherFault_M

; Control ToolSetterArmDown (OUT19) with Aux3Key_M (push and hold)
; At the start of every check ensure ToolSetterArmDown is deactivated and reset.
IF TRUE THEN RST ToolSetterArmDown
IF TRUE THEN RST ToolSetterArmDownActive_M
IF TRUE THEN RST Aux3LED

; Make sure up and down can't be active at the same time.
IF Aux3Key_M && !ToolSetterArmUpActive_M
THEN SET ToolSetterArmDown, SET ToolSetterArmDownActive_M, SET Aux3LED

; Control ToolSetterArmUp (OUT20) with Aux4Key_M (push and hold)
; At the start of every check ensure ToolSetterArmUp is deactivated and reset.
IF TRUE THEN RST ToolSetterArmUp
IF TRUE THEN RST ToolSetterArmUpActive_M
IF TRUE THEN RST Aux4LED

; Make sure up and down can't be active at the same time.
IF Aux4Key_M && !ToolSetterArmDownActive_M
THEN SET ToolSetterArmUp, SET ToolSetterArmUpActive_M, SET Aux4LED

; If E-Stop is active then the tool arm won't move.
; Override any SET commands above if SV_STOP is active.
IF SV_STOP
THEN RST ToolSetterArmDown, RST ToolSetterArmUp,
RST ToolSetterArmDownActive_M, RST ToolSetterArmUpActive_M

;---------------------------------------------------------------------
; END NEW: Tool Setter Arm Control and G-code Program Interlock
;---------------------------------------------------------------------

Re: PLC Programming Question - Not Using the Wizard - (Problem Solved)

Posted: Fri Jul 04, 2025 2:02 am
by cncsnw
I am curious why it is better to set OtherFault_M rather than set SV_Stop directly?
Review the section in MainStage under the "Handle Faults" comment.

If none of the fault categories are set -- PLCFault_M, SV_STALL_ERROR, SpindleFault_M, LubeFault_M, AxisFault_M, ProbeFault_M, OtherFault_M or CriticalFault_M -- then SV_STOP will not persist, but will instead get reset in the same scan in which it was set.

If you set OtherFault_M (or any of the others, as appropriate) then the existing code will set SV_STOP in response, and will make sure it stays set until you press and release Emergency Stop.

Re: PLC Programming Question - Not Using the Wizard - (Problem Solved)

Posted: Fri Jul 04, 2025 2:10 am
by cncsnw

Code: Select all

; Control ToolSetterArmDown (OUT19) with Aux3Key_M (push and hold)
; At the start of every check ensure ToolSetterArmDown is deactivated and reset.
IF TRUE THEN RST ToolSetterArmDown
IF TRUE THEN RST ToolSetterArmDownActive_M
IF TRUE THEN RST Aux3LED

; Make sure up and down can't be active at the same time.
IF Aux3Key_M && !ToolSetterArmUpActive_M
THEN SET ToolSetterArmDown, SET ToolSetterArmDownActive_M, SET Aux3LED

; Control ToolSetterArmUp (OUT20) with Aux4Key_M (push and hold)
; At the start of every check ensure ToolSetterArmUp is deactivated and reset.
IF TRUE THEN RST ToolSetterArmUp
IF TRUE THEN RST ToolSetterArmUpActive_M
IF TRUE THEN RST Aux4LED

; Make sure up and down can't be active at the same time.
IF Aux4Key_M && !ToolSetterArmDownActive_M
THEN SET ToolSetterArmUp, SET ToolSetterArmUpActive_M, SET Aux4LED
That will work, but I am pretty sure it could be simplified to:

Code: Select all

; Control ToolSetterArmDown with Aux3 (push and hold)
IF Aux3Key_M && !(SV_STOP || Aux4Key_M)
  THEN (ToolSetterArmDown),
       (Aux3LED)

; Control ToolSetterArmUp with Aux4 (push and hold)
IF Aux4Key_M && !(SV_STOP || Aux3Key_M)
  THEN (ToolSetterArmUp),
       (Aux4LED)

Re: PLC Programming Question - Not Using the Wizard - (Problem Solved)

Posted: Fri Jul 04, 2025 1:34 pm
by glbreil
Thanks for the lessons, I do appreciate it and I see now that I see now that I went the extremely long way around making it even more difficult, because I was thinking I needed to look at the state of the tool arm instead of simply looking at the state of the buttons. From the PLC for dummies stand point you proved again what I already knew, you are a genius at this stuff.

Thanks Gary