PLC stands for “Programmable Logic Controller”.
PLC controller can load and run small binary programs (PLC procedures). PLC procedure should be written in a simplified C-like language, compiled and stored in PLC controller memory to be ready to run. myCNC software includes PLC Builder - tiny IDE (Integrated Development Environment) to create and modify PLC procedure source files, compile them to binary code and upload it as RomFS iso image disk to PLC controller memory.
myCNC control has 2 types of built-in PLC systems that named Hardware PLC and Software PLC.
Note that hardware PLC can be launched from software PLC if necessary, through the use of gvarset(100040,HARDWAREPLC); command (replace HARDWAREPLC with your M-command of choice). You can read more on the process in the Software PLC section down below.
“Hardware PLC” means PLC system runs inside the CNC control board and able to access directly to CNC controller peripherals (inputs, outputs, PWMs, DACs, ADCs etc). PLC has also API to access to Motion Controller of a myCNC control board, so positioning commands are possible from Hardware PLC.
Hardware PLC is a tiny virtual machine that runs pre-compiled PLC procedures. PLC procedure can be started from
Hardware PLC loop cycle time is 1ms. PLC core runs PLC procedure until the end of next loop. At the end of each loop PLC sleeps for 1ms, then continue running PLC. N-times repeat loop (even empty) will be executed N milliseconds. For example, delay for 10ms can be programmes as
timer=10; do{timer--;}while(timer>0);
IMPORTANT. Hardware PLC is single-tasking. Only one PLC process can be executed at the moment. If new PLC procedure loaded, previous PLC process will be terminated immediately and replaced by the new one.
PLC is a tiny virtual machine with a very limited register and memory space. PLC was created for very simple Inputs/Outputs manipulation. Heavy algorithms cannot be handled by PLC. Please keep in mind this while creating Hardware PLC procedure.
Operator | Description | Example |
---|---|---|
+ | sum | c=a+200; |
- | subtract | c=a-150; |
* | multiply | c=a*b; |
/ | divide | c=b/7; |
& | logical AND | c=a&7; |
| | logical OR | c=b|1; |
>> | binary right shift | c=a>>16; |
<< | binary left shift | c=1<<n; |
== | equal to | if (a==20) ...... |
!= | not equal to | if (a!=0) ...... |
> | more than | if (b>0) ...... |
>= | more or equal than | if (a>=0xf) ...... |
< | less than | if (c<10) ...... |
<= | less or equal than | if (a<=b) ...... |
++ | variable post increment | x++; |
-- | variable post decrement | a--; |
It is possible to access the state of the output via gvarget commands from within the PLC process:
a=gvarget(0x400); //OUT0 b=gvarget(0x407); //OUT7
Starting from 0x400 to represent OUT0, this is a hexadecimal system that is simple to convert to dotted decimals (through the likes of a simple reference site here). Thus, for example, gvarget(0x40d); will return the state of Output #13.
Name | Address | Description |
---|---|---|
GVAR_HW_INPUTS0 | 7180 | |
GVAR_HW_INPUTS1 | 7181 | |
GVAR_HW_INPUTS2 | 7182 | |
GVAR_HW_INPUTS3 | 7183 | |
GVAR_HW_OUTPUTS0 | 7184 | |
GVAR_HW_OUTPUTS1 | 7185 | |
GVAR_HW_OUTPUTS2 | 7186 | |
GVAR_HW_OUTPUTS3 | 7187 | |
GVAR_HW_INPUTS4 | 7188 | |
GVAR_HW_INPUTS5 | 7189 | |
GVAR_HW_INPUTS6 | 7190 | |
GVAR_HW_INPUTS7 | 7191 | |
GVAR_HW_OUTPUTS4 | 7192 | |
GVAR_HW_OUTPUTS5 | 7193 | |
GVAR_HW_OUTPUTS6 | 7194 | |
GVAR_HW_OUTPUTS7 | 7195 | |
GVAR_HW_ADC0 | 7196 | |
GVAR_HW_ADC1 | 7197 | |
GVAR_HW_ADC2 | 7198 | |
GVAR_HW_ADC3 | 7199 | |
GVAR_HW_ADC4 | 7200 | |
GVAR_HW_ADC5 | 7201 | |
GVAR_HW_ADC6 | 7202 | |
GVAR_HW_ADC7 | 7203 | |
GVAR_HW_DAC0 | 7270 | |
GVAR_HW_DAC1 | 7271 | |
GVAR_HW_DAC2 | 7272 | |
GVAR_HW_DAC3 | 7273 | |
GVAR_HW_DAC4 | 7274 | |
GVAR_HW_DAC5 | 7275 | |
GVAR_HW_DAC6 | 7276 | |
GVAR_HW_DAC7 | 7277 | |
GVAR_HW_PWM0 | 7278 | |
GVAR_HW_PWM1 | 7279 | |
GVAR_HW_PWM2 | 7280 | |
GVAR_HW_PWM3 | 7281 | |
GVAR_HW_PWM4 | 7282 | |
GVAR_HW_PWM5 | 7283 | |
GVAR_HW_PWM6 | 7284 | |
GVAR_HW_PWM7 | 7285 |
“Software PLC” means PLC system runs inside the myCNC software and controls CNC controllers peripherals through Software API.
The main advantage of Software PLC is multitasking. All PLC procedures are running simultaneously and independent from each other.
Software PLC cycle time is 100ms, so Software PLC is suitable for wide range of slow applications like automatic lubricant control, fume exhaust control, alarm sensors control etc.
__ (double underscore)
symbols. “System” procedures are not started automatically with the myCNC software start, but instead can be started automatically with some events or manually. There are a few pre-defined “System” PLC handlers-
Handler Name | Comments |
---|---|
__HANDLER_INIT | The procedure executed just after myCNC software started AND configuration is sent to the myCNC control board. The procedure can be used to perform some inputs testing and switch outputs just after the software started. |
__HANDLER_EXIT | The procedure executed just before the myCNC software closed. |
__HANDLER_GCODE_START | The procedure executed just before the myCNC software run g-code (after pressing PLAY button) |
__HANDLER_GCODE_STOP | The procedure executed just after the myCNC software finished g-code running (after pressing STOP/PAUSE button) |
__BV17 | Initially named BV17, this Software PLC had double underscores added to remove an issue with automatic enabling of the testing mode for the controller peripherals (by becoming a System PLC, it prevents the procedure from starting automatically when the program is loaded). |
Variables used in Software PLC:
Variable | Use | Example | Comment |
---|---|---|---|
100020 | Jog the selected axis (100020 through to 100027) | gvarset(100020, 100); | Negative values are not accepted, use 0-X (for example, gvarset(100020, 0-100);) |
100040 | Launch a Hardware PLC from within a Software PLC | gvarset(100040, 607); | This will launch Hardware PLC M607 |
100041 | Eparameter to feed into the Hardware PLC being launched using 100040 | gvarset(100041, 333); | Used before gvarset(100040, 607);, this will set eparam=333 |
Parameter | Software PLC | Hardware PLC |
---|---|---|
Loop time | 100ms | 1ms |
Hardware access | Slow, through the software and Ethernet communication | Fast, direct in myCNC controller |
Multitasking | Yes, all PLC procedures are running simultaneously in separate threads | No, starting new procedure will terminate a current procedure |
Code size for PLC procedure | 16k bytes | 512 bytes |
Total disk size for PLC procedures | Unlimited | 8k bytes |
Language Core for Software and Hardware PLC are almost the same so language syntax is pretty the same and similar to C-language but VERY VERY stripped down.
output0=15;
PLC builder will define variable “output0” and assign the value to “15”
timer=2;do{timer--;}while(timer>0);
Examples:
//send to myCNC software information spindle speed is "0" command=PLC_MESSAGE_SPINDLE_SPEED_CHANGED; parameter=0; message=PLCCMD_REPLY_TO_MYCNC; timer=2;do{timer++;}while (timer>0);
//Probing for Plasma Cutting. g0moveA(0x0,0x4,0-30000); //Start move down do { code=gvarget(6060); }while(code!=0); //Wait till Motion Controller confirm moving started do{ code=gvarget(6060); //check Motion Controller (MC) current status sens=portget(INPUT_IHC); //and check Probe input if (sens==0) { message=PLCCMD_LINE_STOP; //Send to MC command to Stop current line and wait next command code=1; //Set flag to exit from the loop }; }while (code==0); //Exit from loop is motion finished OR Probe sensor pressed
Name | Value | Comment |
PLCCMD_MOTION_CONTINUE | 1001 | |
PLCCMD_MOTION_SKIP | 1002 | |
PLCCMD_MOTION_SOFT_SKIP | 1003 | |
PLCCMD_MOTION_PAUSE | 1004 | |
PLCCMD_PLC_PAUSE | 1005 | |
PLCCMD_PLC_FORK_PAUSE | 1006 | |
PLCCMD_LINE_SOFT_STOP | 1007 | |
PLCCMD_LINE_STOP | 1008 | |
PLCCMD_REPLY_TO_MYCNC | 1100 | |
PLCCMD_SET_CNC_VAR | 1010 | |
PLCCMD_SET_THC_VAR | 1011 | |
PLCCMD_SET_DEVICE_VAR16 | 1012 | |
PLCCMD_SET_DEVICE_VAR32 | 1014 | |
PLCCMD_SET_CNC_EXTVAR | 1020 | |
PLCCMD_MOTION_ABORT | 1032 | |
PLCCMD_MOTION_BREAK | 1033 | |
PLCCMD_SAVE_POS | 1040 | |
PLCCMD_THC_START | 1050 | |
PLCCMD_THC_STOP | 1051 | |
PLCCMD_THC_PAUSE | 1052 | |
PLCCMD_THC_CONTINUE | 1053 | |
PLCCMD_WATCHBIT1_ON | 1060 | |
PLCCMD_WATCHBIT2_ON | 1061 | |
PLCCMD_WATCHBIT3_ON | 1062 | |
PLCCMD_WATCHBIT4_ON | 1063 | |
PLCCMD_TRIGGER1_ON | 1060 | |
PLCCMD_TRIGGER2_ON | 1061 | |
PLCCMD_TRIGGER3_ON | 1062 | |
PLCCMD_TRIGGER4_ON | 1063 | |
PLCCMD_TRIGGER1_OFF | 1064 | |
PLCCMD_TRIGGER2_OFF | 1065 | |
PLCCMD_TRIGGER3_OFF | 1066 | |
PLCCMD_TRIGGER4_OFF | 1067 | |
PLCCMD_WATCHBIT1_OFF | 1064 | |
PLCCMD_WATCHBIT2_OFF | 1065 | |
PLCCMD_WATCHBIT3_OFF | 1066 | |
PLCCMD_WATCHBIT4_OFF | 1067 | |
PLCCMD_WATCHBIT5_ON | 1068 | |
PLCCMD_WATCHBIT6_ON | 1069 | |
PLCCMD_WATCHBIT7_ON | 1070 | |
PLCCMD_WATCHBIT8_ON | 1071 | |
PLCCMD_WATCHBIT5_OFF | 1072 | |
PLCCMD_WATCHBIT6_OFF | 1073 | |
PLCCMD_WATCHBIT7_OFF | 1074 | |
PLCCMD_WATCHBIT8_OFF | 1075 | |
PLCCMD_WAIT_FOR_CAMERA | 1090 | |
PLCCMD_WAIT_VARSET | 1091 | |
PLCCMD_WAIT_VARCLEAR | 1092 | |
PLCCMD_CAMERA_START | 1093 | |
PLCCMD_CAMERA_FINISH | 1094 | |
PLCCMD_PLC_DEBUG | 1098 | |
PLCCMD_PLC_RESTART | 1099 | |
PLC_BROADCAST_INQUIRY0 | 1200 | |
PLC_BROADCAST_INQUIRY1 | 1201 | |
PLC_BROADCAST_INQUIRY2 | 1202 | |
PLC_BROADCAST_INQUIRY3 | 1203 | |
PLC_BROADCAST_INQUIRY4 | 1204 | |
PLC_BROADCAST_INQUIRY5 | 1205 | |
PLC_BROADCAST_INQUIRY6 | 1206 | |
PLC_BROADCAST_INQUIRY7 | 1207 | |
PLC_BROADCAST_INQUIRY8 | 1208 | |
PLC_BROADCAST_INQUIRY9 | 1209 | |
PLC_BROADCAST_INQUIRY10 | 1210 | |
PLC_BROADCAST_INQUIRY11 | 1211 | |
PLC_BROADCAST_INQUIRY12 | 1212 | |
PLC_BROADCAST_INQUIRY13 | 1213 | |
PLC_BROADCAST_INQUIRY14 | 1214 | |
PLC_BROADCAST_INQUIRY15 | 1215 | |
PLC_BROADCAST_OK | 1220 | |
PLC_BROADCAST_ABORTED | 1221 | |
PLCCMD_MODBUS_SPINDLE_SPEED | 1230 | |
PLCCMD_MODBUS_SPINDLE_CMD | 1231 | |
PLCCMD_SET_PIDTIME | 1240 |
PLC process name | Value | Comment |
plc_proc_check_plasma | 10 | |
plc_proc_venting | 11 | |
plc_proc_start_power | 12 | |
plc_proc_cooling | 14 | |
plc_proc_plasma | 15 | |
plc_proc_wait_plasma | 18 | |
plc_proc_pierce | 27 | |
plc_proc_no_plasma | 62 | |
plc_proc_check_preflow | 16 | |
plc_proc_check_cutflow | 17 | |
plc_proc_check_gases | 23 | |
plc_proc_test_out | 24 | |
proc_m_function | 30 | |
proc_zeroing | 32 | |
plc_proc_probing | 33 | |
plc_proc_ignition | 50 | |
plc_proc_preheat | 51 | |
plc_proc_soft_start | 52 | |
plc_proc_piercing | 53 | |
plc_proc_flame | 54 | |
plc_proc_cutting | 60 | |
plc_proc_purge | 61 | |
plc_proc_no_cutting | 62 | |
plc_proc_moveup | 65 | |
plc_proc_spindle | 70 | |
plc_proc_idle | 0 |
Normally PLC procedure should return code 99.
exit(99);
In case Error happened PLC procedure may return an error code. MyCNC software will catch exit code and report about Error if the error code is in PLC exit codes list.
Exit code name | Value | Comment |
plc_process | 0 | exit code is zero when PLC process is not finished yet |
plc_exit_gas_fail | 2 | PLC aborted, No air pressure sensor |
plc_exit_plasma_timeout | 3 | PLC aborted, No Arc sensor signal Timeout |
plc_exit_plasma_fail | 4 | PLC aborted, PLC Plasma Start cutting procedure error |
plc_exit_alarm_key | 5 | PLC aborted, Emergency key pressed |
plc_exit_coolant_fail | 6 | PLC aborted, No Coolant flow sensor |
plc_exit_probe_error | 7 | PLC aborted, No signal from probe sensor |
plc_exit_motor_shorted | 8 | |
plc_exit_broadcast_error | 10 | Error send broadcast message in multi-device configuration |
plc_exit_normal | 99 | Normal exit |
plc_exit_extern_break | 100 | PLC procedure aborted from outside of PLC core |
Message name | Value | Comment |
PLC_MESSAGE_PLASMA_OK | 101 | |
PLC_MESSAGE_WATCHBIT_ACTION | 110 | |
PLC_MESSAGE_SPINDLE_SPEED_CHANGED | 120 | |
PLC_MESSAGE_PULL_OUT_TOOL | 130 | |
PLC_MESSAGE_SPINDLE_TURNING 131 | ||
PLC_MESSAGE_ENCODER_DATA | 140 | |
PLC_MESSAGE_GVAR_VALUE | 141 | |
PLC_MESSAGE_UID | 142 | |
PLC_MESSAGE_HCONTROL_OFFSET | 144 | |
PLC_MESSAGE_TANGENT_ANGLE | 145 | |
PLC_MESSAGE_USER | 146 | |
PLC_MESSAGE_HCONTROL_JSPEED | 147 | |
PLC_MESSAGE_HCONTROL_DC | 148 | |
PLC_MESSAGE_ASK_RECALC | 150 | |
PLC_MESSAGE_SOFTLIMIT_STOP | 151 | |
PLC_MESSAGE_GVARSET | 153 | |
PLC_MESSAGE_IHC_NOT_CONNECTED | 155 | |
PLC_MESSAGE_IHC_ERROR | 156 | |
PLC_MESSAGE_LATHE_GEARS | 160 | |
PLC_MESSAGE_ERR_VM | -1 | |
PLC_MESSAGE_ERR_ROMFS | -2 | |
PLC_MESSAGE_MOTION_BUFFER_EMPTY | -5 | |
PLC_MESSAGE_ERR_SENSOR_COOLANT | -10 | |
PLC_MESSAGE_ERR_SENSOR_AIR | -11 | |
PLC_MESSAGE_ERR_SENSOR_GAS | -12 | |
PLC_MESSAGE_ERR_SENSOR_OXYGEN | -13 | |
PLC_MESSAGE_ERR_SENSOR_PLASMA | -14 | |
PLC_MESSAGE_ERR_PROBING | -15 | |
PLC_MESSAGE_ERR_PLASMA_FAIL | -20 | |
PLC_MESSAGE_ERR_PLASMA_TIMEOUT | -21 | |
PLC_MESSAGE_PRESSED_ALARM_KEY | -30 | |
PLC_MESSAGE_PRESSED_STOP_KEY | -31 | |
PLC_MESSAGE_MOTOR_SHORTED | -35 |
MAPPED_OUT_THC_LOWSPEED | 63 | |
MAPPED_OUT_LOW_MOTOR_CURRRENT | 65 |
A number of Global variable addresses are mapped to Hardware Inputs/Outputs. PLC procedure can access the controller peripherals through GVarGet/GVarSet function. Addresses to access to controller hardware are listed below
Variabe Name | Address | Description |
---|---|---|
GVAR_HW_INPUTS0… GVAR_HW_INPUTS3 | 7180… 7183 | |
GVAR_HW_OUTPUTS0… GVAR_HW_OUTPUTS3 | 7184… 7187 | |
GVAR_HW_INPUTS4… GVAR_HW_INPUTS7 | 7188… 7191 | |
GVAR_HW_OUTPUTS4… GVAR_HW_OUTPUTS7 | 7192… 7195 | |
GVAR_HW_ADC0… GVAR_HW_ADC7 | 7196… 7203 | |
GVAR_HW_DAC0… GVAR_HW_DAC7 | 7270… 7277 | |
GVAR_HW_PWM0… GVAR_HW_PWM7 | 7270… 7277 | |
GVAR_ET5_ENCODER … | ||
GVAR_ET5_ENCODER_Z … | ||
GVAR_ET5_ENCODER_WZ … | ||
GVAR_ET5_ENCODER … | ||
GVAR_MODBUS_READ | ||
GVAR_THC0_CONTROL | 7570 | |
GVAR_THC1_CONTROL | 7575 | |
GVAR_CURRENT_MOTION_CODE | 6060 | |
GVAR_MD_MASTER_MOTION_CODE | 7140 | |
GVAR_CURRENT_TOOL_NUMBER | 5400 | |
GVAR_HCONTROL2_VREF | ||
GVAR_PLC_MOVE_PROCESS | ||
GVAR_CAMERA_READY | 7090 | |
GVAR_CURRENT_MACHINE_POSITION | 5021, 5022, 5023, 5024, 5025, 5026 | |
GVAR_CURRENT_PROGRAM_POSITION | 5041, 5042, 5043, 5044, 5045, 5046 | |
GVAR_ENCODER_Z_EVENT | ||
— | 17001 | Return Current PROGRAM X Position in PLC units (0.01mm) |
— | 17002 | Return Current PROGRAM Y Position in PLC units (0.01mm) |
— | 17003 | Return Current PROGRAM Z Position in PLC units (0.01mm) |
— | 17004 | Return Current PROGRAM A Position in PLC units (0.01degree) |
— | 17005 | Return Current PROGRAM B Position in PLC units (0.01degree) |
— | 17006 | Return Current PROGRAM C Position in PLC units (0.01degree) |
— | 17007 | Return Current PROGRAM U Position in PLC units (0.01mm) |
— | 17008 | Return Current PROGRAM V Position in PLC units (0.01mm) |
— | 17009 | Return Current PROGRAM W Position in PLC units (0.01mm) |
— | 17021 | Return Current MACHINE X Position in PLC units (0.01mm) |
— | 17022 | Return Current MACHINE Y Position in PLC units (0.01mm) |
— | 17023 | Return Current MACHINE Z Position in PLC units (0.01mm) |
— | 17024 | Return Current MACHINE A Position in PLC units (0.01degree) |
— | 17025 | Return Current MACHINE B Position in PLC units (0.01degree) |
— | 17026 | Return Current MACHINE C Position in PLC units (0.01degree) |
— | 17027 | Return Current MACHINE U Position in PLC units (0.01mm) |
— | 17028 | Return Current MACHINE V Position in PLC units (0.01mm) |
— | 17029 | Return Current MACHINE W Position in PLC units (0.01mm) |
The following video illustrates the process of creating a button to launch a software PLC command (a similar process can also be used for hardware PLCs):