PLC programming. The page is Under Construction (2012-0517) myCNC controller contains Motion Controller (MC) and Programmable Logic Controller (PLC) onboard. PLC is a virtual machine running inside myCNC. All the peripherals of myCNC controller mapped into PLC registers, so PLC has access to it. icro program can be loaded into the PLC from internal flash memory. Most of PLC micro-programs are assosiated with Miscellaneous M-functions.
PLC procedure can be called directly from NC-program by running selected M-function or from the myCNC GUI. Calling PLC function can be assigned to any event (press, release, toggle) of the GUI on-screen buttons. While running PLC procedure from NC-code there are two possible modes - synchronous and asynchronous-
- In synchronous mode myCNC Motion controller calls PLC procedure and wait till the procedure
is finished; then continue motion program; - In asynchronous mode Motion program and PLC procedure are running simultaneously.
There are inter-process messages from PLC to the Motion controller. By using theis messages it's possible to skip running current motion-line, soft-stop, abort running motion program. All source files for PLC procedures are situated in "profiles/Current_Profile/plc/src" folder. PLC source files should be compilated with PLC-compiler. Executable binary PLC-microprograms are situated in "profiles/Current_Profile/plc/bytecode" folder. Compilation scripts ("plc.make.sh" for Linux and "plc.make.bat" for MS Windows) are situated in "profiles/Current_Profile/plc". Duriing compilation with the scripts all bytecode binary files are packed into ROMFS disk image "eeprom.rom", that is placed in "profiles/Current_Profile/plc.data". While the myCNC software starting the "eeprom.rom" disk image is uploaded into myCNC control board. Since it's done, PLC programs can be loaded and executed by calling PLC procedure name (filename of PLC procedure) PLC control system variables. PLC controller contains list of system variables that may be used in PLC program for peripherals access, interprocess communication, variables initialization and general purpose. List of variables is shown below PWM, ADC and DAC are unsigned 12-bit values. Complete range for these variables is 0…4095 (in hex 0…0xfff) Table 1. PLC variables for access to ADC, DAC and PWM Variable name | Description | Comments | eparam | External parameter.
| Valule of this parameter may be initialized in the myCNC GUI before running PLC procedure | message | Message parameter. Non-zero value in this variable make signal to send message from the PLC either to Motion controller or Host PC software or any other units (ie THC control) | | variable var00
| Address of system register. This parameter is used together with Message parameter and "parameter" while sending mesages. | | parameter var01
| Value of system register. This parameter is used together with Message parameter and "variable" or "command" while sending mesages. | | var01, var02, ... var16 | General purpose variables. | Value of this variables may be initialized from xml configuration files before PLC procedure running. | proc | PLC process id
| (Obsolete) May be used by myCNC GUI to display current running state of PLC procedure. | vexit | PLC Virtual machine exit code
| Exit code of PLC procedure. May be used to identify whether the procedure normally finished or aborted with error code. | command var00 | Enumerated command/message id to myCNC control software. This variable is used with PLCCMD_REPLY_TO_MYCNC message to send info to Host PC software. | | | | | | | | pwm01 | PWM channel #1
| 12 bit unsigned value
| pwm02 | PWM channel #2 | 12 bit unsigned value
| pwm03 | PWM channel #3
| 12 bit unsigned value | pwm04 | PWM channel #4 | 12 bit unsigned value | adc01
| ADC channel #1
| 12 bit unsigned value | adc02 | ADC channel #2 | 12 bit unsigned value | adc03 | ADC channel #3 | 12 bit unsigned value | adc04 | ADC channel #4 | 12 bit unsigned value | dac01
| DAC channel #1 | 12 bit unsigned value | dac02
| DAC channel #2 | 12 bit unsigned value |
There are PLC functions:- getport(int port_number);
- setport(int port_number);
to read/write binary inputs & outputs of myCNC control board.
do{}while loops and PLC processing time. PLC virtual machine runs do-while loop and make a sleep for 1msec before perfrom jump to the start of the loop. This is mechanism to make delays and timeouts in PLC procedures.
A sample below is procedure for clear Servo alarm. It turn on servo-alarm relay, wait 0.5 sec and then turn-off the relay. Clear Servo-Alarm PLC procedure | portset(1); //turn ON relay to clear alarm
timer=0; do{ timer++;}while(timer<500); //make delay for 500 msec
portset(1); //turn OFF relay
exit(99); //exit from PLC procedure with normal state. |
Access to the boards peripherals through PLC. myCNC controller boards (myCNC-ET1, myCNC-ET2, myCNC-UP3) contains wide range of peripherals (PWM, binary Inputs, relay and transistor open collector outputs, DAC outputs, ADC inputs). All listed peripherals are mapped onto predefined PLC variables. PWM, ADC and DAC are unsigned 12-bit values. Complete range for these variables is 0…4095 (in hex 0…0xfff) Table 1. PLC variables for access to ADC, DAC and PWM Variable name | CNC control peripheral | pwm01 | PWM channel #1
| pwm02 | PWM channel #2 | pwm03 | PWM channel #3
| pwm04 | PWM channel #4 | adc01
| ADC channel #1
| adc02 | ADC channel #2 | adc03 | ADC channel #3 | adc04 | ADC channel #4 | dac01
| DAC channel #1 | dac02
| DAC channel #2 |
There are PLC functions:- getport(int port_number);
- setport(int port_number);
to read/write binary inputs & outputs of myCNC control board.
Interprocess communication.PLC controller may send messages to either to Motion controller or to myCNC cotrol software. There are variables: - "message" - non-zero value in this varable is a signal to myCNC control to parse the message;
- "variable" - Value is Address of parameter, ttah should be changed in Motion of myCNC controll;
- "parameter" - There is new value of the register;
Table 2. Enumerated PLC messages Message
| Number | Description
| PLCCMD_MOTION_CONTINUE
| 1001 | Command to Motion control to continue motion program
| PLCCMD_MOTION_SKIP
| 1002 | Command to Motion control to skip current motion command and continue running from the next motion line. | PLCCMD+MOTION_SOFT_SKIP
| 1003
| Command to Motion control to skip current motion command, perform soft deceleration and continue running motion program from the next line.
| PLCCMD_MOTION_PAUSE | 1004
| Pause running Motion program
| PLCCMD_SET_CNC_VAR
| 1010 | Set global CNC register/variable to given value.
| PLCCMD_SET_THC_VAR
| 1011
| Set THC (Torch height control board) register to given value.
| PLCCMD_MOTION_ABORT | 1032 | Abort running Motion program.
| PLCCMD_THC_START | 1050 | Start torch height control (THC) system
| PLCCMD_THC_STOP | 1051
| Stop torch height control (THC) system | PLCCMD_THC_PAUSE
| 1052
| THC pause
| PLCCMD_THC_CONTINUE | 1053
| Resume working THCsystem | PLCCMD_WATCHBIT1_ON ... PLCCMD_WATCHBIT4_ON | 1060 ... 1063
| Turn On watching for watch-bit #1 ... #4
| PLCCMD_WATCHBIT1_OFF | 1064 ... 1067
| Turn Off watching for watch-bit #1 ... #4
| PLCCMD_WATCHBIT5_ON ... PLCCMD_WATCHBIT8_ON
| 1068 ... 1071
| Turn On watching for watch-bit #5 ... #8
| PLCCMD_WATCHBIT5_OFF ... PLCCMD_WATCHBIT8_OFF
| 1072 ... 1075
| Turn Off watching for watch-bit #5 ... #8 | PLCCMD_PLC_RESTART
| 1099 | Restart running PLC program.
|
The message will be parsed by the controller while PLC controller is in Idle(Pause) mode. To do the pause do-while loop for 2-10 miliseconds should be added after each message. There is "torch jump" implemented via THC control from PLC procedure is shown on a example below- - Message to THC control - Turn on jog motion up;
- Do 1s pause (the torch will be moving up for this time);
- Message to THC control - Turn off jog motion;
- Do 10ms pause (wait toill the message will be delivered to the THC);
"Torch jump" for 1 second
| //include files that describe pre-defined constant parameters like //PLCCMD_MOTION_CONTINUE, PLCCMD_MOTION_SKIP etc #include src/common.const.h
// ----- start jog-up motion ----- variable=THC_VAR_JOG; parameter=1; //up direction message=PLCCMD_SET_THC_VAR; texit=timer+1000; do{timer++;}while(timer<texit); //1s pause - time for jogging-up // ----- stop torch ----- variable=THC_VAR_JOG; parameter=0;<-->//stop message=PLCCMD_SET_THC_VAR; texit=timer+10; do{timer++;}while(timer<texit); //10ms pause to deliver the message to THC;
|
Asynchronous mode (simultaneous running PLC procedure and Motion program).There are Motion commands and PLC procedures calls in NC program normally. myCNC controller runs the program step-by-step. Just one command (motion command or PLC procedure) is running at one time. This is synchronous mode.
In asynchronous mode PLC procedure may send a message to the Motion controller to resume running. If next commands in the program are Motion commands, PLC procedure and Motion program will be running simultaneously. If some other PLC procedure will appear in the program, current PLC procedure will be aborted and the next will start. The next example shows how to make "1 second blink" of relay #1 (output 0) while motion program running. PLC procedure should be called in the start of NC program "1 second realy blink" while motion
| //include files that describe pre-defined constant parameters like //PLCCMD_MOTION_CONTINUE, PLCCMD_MOTION_SKIP etc #include src/common.const.h #include src/pins.h
// ----- send message to Motion controller to resume motion ----- variable=PLC_MOTION_CONTINUE; texit=timer+10; do{timer++;}while(timer<texit); //10ms pause to deliver the message to Motion control; //Perform blink of relay #1 each second (1000 ms) timer=0; blink=0; do{ timer++; if (timer>1000) { timer=0; blink=blink^1; if (blink!=0) { portset(0); //turn on relay #1 }; if (blink==0) { portclr(0); //turn off relay #1 }; } }while(1); //10ms pause to deliver the message to Motion control; exit(99); //the procedure is endless and will be aborted by loading and running the next PLC procedure |
PLC procedure Variable initialization from the myCNC GUI.Let's see an example. PLC procedure M03 perform spindle turn-on. Beside spindle relay turn on we need to perform a pause and wait till the spindle achieve working speed. t's possible to make fixed delay in the M03 procedure, but it is not really convenient and we like to have this value configurable from the myCNC GUI.
The solution is described below. We have "M03.plc" procedure in plc-source folder. We need to create xml configuration file with the same name and "xml" extension on this place- "M03.xml" Content of the xml os shown below:
"M03.xml" - delay configuration for spindle turn-on procedure
| <?xml version='1.0' encoding='UTF-8'?> <plc-configuration version="1.0">
<plc-function name="M03"> <message>M03 Spindle CW</message> <message_ru>M03 Старт шпиндель</message_ru> <message_pl>M03 Włącz wrzeciono</message_pl> <item> <value number="5" name="plc-var-spindle-on-delay" suffix="ms" type="slider" min="0" step="100" max="5000">1000</value> <message>Spindle On Delay,ms</message> <message_ru>Время на включение шпинделя,мс</message_ru> <message_pl>opóźnienie włączenia wrzeciona, ms</message_pl> </item>
</plc-function> </plc-configuration>
|
There should be xml-section "plc-function" in the file. attribute "name" of plc-function section should be the same as PLC procedure name ("M03" for this example). Fields "message", "message_ru", ... "message_xx" contain message that will be displayed on myCNC GUI Settings dialogfor this PLC procedure (message text will be selected depends on current language settings). Section "item" describes address of PLC variable, that we need to initialize, initial value, values range, step and text labels to build control item on Settings dialog: - Attribute "name" contains name of PLC variable to have access to it from other GUI control elements;
- Attribute "number" is the Address of variable in PLC procedure. Address "5" means that variable "var05"
will be initialized to given value; - Attributes "min", "max", "step" define values range for the variable;
- Attribute "suffix" defines label text after displayed variable value in the GUI control;
- Attribute "prefix" defines label text before displayed variable value in the GUI control;
- Attribute "type" defines type of GUI control (most used types are "slider", "radiobutton");
GUI element for the variable initialization is shown on a picture-
M03 procedure listing is shown below "M03.plc" - spindle turn on procedure with delay implementation
| //Turn on Spindle procedure #define spindle_relay 1 #define on_delay var05
portset(output_spindle); //turn on spindle relay
timer=0; do{timer++;}while (timer<on_delay); //delay for Spindle
exit(99); //normal exit.
|
There are "define" statements are used in the PLC program to simplify reading/understanding the code; According to "M03.xml" configuration file "var05" variable contains right value for turn-on delay. External parameter (eparam) initialization.myCNC control software makes initialization of "eparam" variable for most of miscelanneous (M-) codes. For spindle turn-on assosiated procedures ("M03" - turn on cw, "M04" -turn on ccw, "SPN" - change speed on-the-fly) "eparam" contains spindle speed value (S-value). For rest of functions "eparam" contains L-register value in low 16-bit word and P-register value in high 16-bit word. Modified M03 procedure, that reads spindle speed from "eparam" variable and set it through DAC output is shown below- "M03.plc" - spindle turn on procedure with delay implementation and setting spindle speed through DAC channel
| //Turn on Spindle procedure #define spindle_relay 1 #define on_delay var05 //include files that describe pre-defined constant parameters like //PLCCMD_MOTION_CONTINUE, PLCCMD_MOTION_SKIP etc #include src/common.const.h #include src/pins.h portset(output_spindle); //turn on spindle relay #include src/common.const.h #include src/pins.h //define var00 and var01 as command and parameter to convenient reading the code #define command var00 #define parameter var01
#define on_delay var05 //assingn spindle speed and test for correct range (12-bit unsigned value) val=eparam; if (val>0xFFF) {val=0xFFF;}; if (val<0) {val=0;};
//set given spindle speed through DAC channel dac01=val; //turn ON spindle relay portset(output_spindle);
//send to myCNC Host software new data about current spindle speed for correct displaying command=PLC_MESSAGE_SPINDLE_SPEED_CHANGED; parameter=eparam; message=PLCCMD_REPLY_TO_MYCNC; timer=0;do{timer++;}while (timer<10);//pause to push the message with Spindle Speed data
//perform pause to spindle achieve given speed timeout=on_delay+timer; do{timer++;}while (timer<timeout); //delay for Spindle reach given speed
//normal exit from the procedure exit(99);
|
Next sample shows how to make probing with flexibly given port number and pin state. It is "M88" procedure. "M88.plc" - probing procedure
| //procedure for watching on given sensor number, given state and break current motion //used for homing, surface measure, tool length measure etc //include files that describe pre-defined constant parameters like //PLCCMD_MOTION_CONTINUE, PLCCMD_MOTION_SKIP etc #include src/common.const.h #include src/pins.h
#define input var00 #define state var01 //read from eparam port number and port state input=eparam&0xFFFF; state=eparam>>16; timer=0; //send to the Motion controller message to resume motion message=PLCCMD_MOTION_CONTINUE; texit=timer+2; do{ timer++;}while(timer<texit); //pause to push the message
//initialize ready flag ready=0;
do { timer++; //read given port current state a=portget(input); //set ready flag if given port is activated; active state depends on given "state" if (state==0) { if (a==0) {ready=1;}; }; if (state!=0) { if (a!=0) {ready=1;}; };
}while(ready==0); //if ready flag set, exit from the loop //send message to the Motion controller to STOP running current line //and go to the next line (SKIP current line); message=PLCCMD_MOTION_SOFT_SKIP; texit=timer+2; //make 2ms delay to deliver the message to the Motion controller do { timer++; }while(timer<texit); exit(99);
|
Direct running PLC procedures form myCNC GUI.myCNC GUI active elements (Push Buttons and Toggle Buttons) generate events while press/release. There are a few types of actions that may be assigned for this events. Action "direct-plc" runs given PLC procedure with "eparam" assigned to given value. Sample of myCNC GUI screen for 3x mill is shown below.
A line from cnc-config.xml configuration file, that describes on-screen M03 button are shown below- M03 on-screen button configuration
| <gitem where="M-toolbar" image="button-m03" action="plc-run:M03/#5524" height="90" type="button"/>
|
While pressing the M03 button PLC procedure "M03" will be running with "eparam" variable initialized to value from Global Register number 5524 (which is actually is Spindle speed)
|