M-Codes & PLC Calling Interactions
M Codes
Rules:
- M-Codes are executed in PLC, unless a G-code replacement is specified.
- Can be registered (replaced) to a G-Code program like a custom G-Code
- For example, a PLC program performing a tool change routine executed with M06 M-code logic, can be replaced with a G-Code Macro program controlling all motion to and from the tool changer with additional custom M-codes for I/O activation and check.
- If an M code is not registered, it is delegated to the PLC for standard ack behaviors.
- The logic of the M-Code has to be created in the PLC and will be executed in the PLC
- For example, a router tool changer logic could be written as a PLC function block and executed with M06 M-code logic.
- After the M-code execution it has to be acknowledged (completed).
- Some M-codes can be acknowledged right away.
- For example: M104 - Set Printer nozzle temperature. Set and forget....it doesn't wait for the temperature to reach the setpoint.
- Some M-codes are blocking - they are acknowledged only when the conditions are met
- For example: M109 - Set Printer nozzle temperature and wait for it to reach the setpoint. The M-Code will be acknowledged only when the temperature setpoint is reached.
- If an M-Code is registered, then there is no fall through to the PLC.
- The M-Code logic has to be created in a G-Code file and will be executed as a G-Code replacement sub-program (see Tool changer G-Code program above)
- Multiple M-Codes are allowed in one file line if their execution order is not important and S, M, T, P arguments do not interfere or overwrite each other.
- G-code processing pauses and waits for all active M-Codes logic to be performed, complete or acknowledged before continuing to the next G-code file line.
- M-Codes can be used to set, reset and check IO of the machine or execute various logic.
-
PLC receives an M-command and uses the
M_Arrayvariable to trigger the execution of appropriate logic.M_Arrayindex number matches the M-code number.- The
S, T, H, and Pargument values are passed from the G-Code line to the PLC to be used in the M-Code logic. - Once the M-code logic is executed or completed, it should be acknowledged by resetting the same
M_Arraymember (e.g.,M_Array[55] := 0 or FALSE). - After processing the M-command, if this was not the last M-code of the line (
EOL=FALSE), we are requesting the next M-command and repeating the above steps. - Multiple M-codes can be active at the same time, but before continuing to the next G-code line all the M-codes have to be acknowledged.
- If this was the last M-Code of the line (
EOL=TRUE), after executing the M-Code logic, the M_Array values are constantly checked before proceeding to the next command (only if all the values areFALSE...all M-codes were acknowledged).
- The
-
M30is immediately acknowledged. -
M03triggers the command to start to turn the spindle clockwise and inserts a delay based on the P value. -
M03 S2000 P5000would spin at 2000 RPM, clockwise, followed by a 5-second delay, and then acknowledge the command. - Sample M30 will set M-Code[30].Active bit to TRUE, execute M5 to trigger Spindle Stop and will be acknowledged immediately by resetting M-Code[30].Active bit.M-Code[5].Active will be true and the G-code processing will wait until the M5 is acknowledged before processing to the next G-code line line.
- Sample M3 S2000 P5000 line will set M-Code[3].Active bit to TRUE, set spindleSpeed variable to 2000.0 and wait 5 seconds for the spindle to reach the new velocity before acknowledging this M-code and resetting M-Code[3].Active bit.
- Sample custom M213 M-code activates a Dust Shroud valve. Since there are no sensors present and the timing is not critical, it can be acknowledged right away in the same scan:
- Sample custom M211 M-code checks the tool changer slot sensor if the tool is present or not. If the slot is empty, the M-code is acknowledged right away, otherwise an error is generated.
PLC Handling Example
Sample M-Codes are in M_CodeManager POU.
M-Code logic is executed by the G-Code Processor setting the M_Code[number].Active bit and acknowledged after the execution by resetting the same M_Code[number].Active bit.