plc:plc_examples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
plc:plc_examples [2020/01/30 12:56] – ivan | plc:plc_examples [2024/02/21 13:59] (current) – ivan | ||
---|---|---|---|
Line 1: | Line 1: | ||
==== PLC Examples ==== | ==== PLC Examples ==== | ||
+ | |||
+ | //**NOTE**: The myCNC team recommends utilizing the examples provided in this manual (as well as other manuals in this documentation) as a starting point for your machine setup. When possible (and applicable), | ||
* [[plc:M07 Mist Coolant ON]] | * [[plc:M07 Mist Coolant ON]] | ||
Line 43: | Line 45: | ||
</ | </ | ||
+ | ===Spindle Control through Triggers=== | ||
+ | |||
+ | In this case, a couple of lines are added to standard M03 (spindle ON), M05 (spindle OFF) and M02 (End program) PLCs, typically to allow the system to interpret some spindle feedback signal. Spindle control is done through a trigger, with the trigger flag indicating whether the trigger is ON or OFF. If the spindle is ON and the trigger is activated, then the program will be stopped. | ||
+ | |||
+ | The following code can be used to enable the trigger: | ||
+ | < | ||
+ | message=PLCCMD_TRIGGER1_ON; | ||
+ | timer=10; | ||
+ | </ | ||
+ | |||
+ | The following code can be used to disable the trigger: | ||
+ | |||
+ | < | ||
+ | timer=10; | ||
+ | | ||
+ | The code to enable the trigger should be inserted into M03, while the code to disable the trigger should be inserted into M05 and M02. The full resultant PLC procedure code can be found below: | ||
+ | |||
+ | |||
+ | ++++ Click to expand the M03 code | | ||
+ | < | ||
+ | #include pins.h | ||
+ | #include vars.h | ||
+ | main() | ||
+ | { | ||
+ | timer=0; | ||
+ | proc=plc_proc_spindle; | ||
+ | |||
+ | val=eparam; | ||
+ | if (val> | ||
+ | if (val<0) {val=0;}; | ||
+ | |||
+ | dac01=val; | ||
+ | |||
+ | portclr(OUTPUT_CCW_SPINDLE); | ||
+ | portset(OUTPUT_SPINDLE); | ||
+ | |||
+ | gvarset(7370, | ||
+ | gvarset(7371, | ||
+ | |||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | |||
+ | //delay after spindle started | ||
+ | timer=spindle_on_delay; | ||
+ | do{timer--; | ||
+ | |||
+ | message=PLCCMD_TRIGGER1_ON; | ||
+ | timer=10; | ||
+ | |||
+ | exit(99); | ||
+ | };</ | ||
+ | ++++ | ||
+ | |||
+ | ++++Click to expand the M05 code | | ||
+ | < | ||
+ | //Spindle Stop | ||
+ | //set Spindle speed control via DAC & PWM1 channel | ||
+ | |||
+ | #include pins.h | ||
+ | #include vars.h | ||
+ | |||
+ | |||
+ | main() | ||
+ | { | ||
+ | portclr(OUTPUT_SPINDLE); | ||
+ | portclr(OUTPUT_CCW_SPINDLE); | ||
+ | proc=plc_proc_idle; | ||
+ | |||
+ | message=PLCCMD_TRIGGER1_OFF; | ||
+ | timer=10; | ||
+ | |||
+ | if (spindle_off_delay!=0) | ||
+ | { | ||
+ | timer=spindle_off_delay; | ||
+ | do { timer--; } while (timer> | ||
+ | }; | ||
+ | |||
+ | dac01=0x0; | ||
+ | |||
+ | gvarset(7370, | ||
+ | gvarset(7371, | ||
+ | |||
+ | exit(99); | ||
+ | };</ | ||
+ | |||
+ | ++++ | ||
+ | |||
+ | ++++Click to expand the M02 code | | ||
+ | < | ||
+ | #include pins.h | ||
+ | #include vars.h | ||
+ | |||
+ | // g0moveA - start motion: | ||
+ | // flags - | ||
+ | // bit 0 - absolute programming | ||
+ | // bit 1 - machine coordinates | ||
+ | // bit 7 - delayed start. | ||
+ | // axes mask | ||
+ | // bit 0 - X axis; bit 1 - Y axis;bit 2 - Z axis;bit 3 - A axis;bit 4 - B axis;bit 5 - C axis | ||
+ | |||
+ | lift_up() | ||
+ | { | ||
+ | |||
+ | if (proc==plc_proc_spindle) | ||
+ | { | ||
+ | z1=gvarget(17003); | ||
+ | timer=10; do{timer--; | ||
+ | |||
+ | z2=gvarget(7020); | ||
+ | z2=z2*100; | ||
+ | |||
+ | if (absolute==0) { z2=z1+z2; | ||
+ | |||
+ | z1=z1+100; | ||
+ | |||
+ | if (z2>z1) | ||
+ | { // | ||
+ | gvarset(7080, | ||
+ | g0moveA(1, | ||
+ | timer=300; do{timer--; | ||
+ | //wait motion stopped | ||
+ | do | ||
+ | { ex=0; code=gvarget(6060); | ||
+ | if (code==0x4d) {ex=1;}; | ||
+ | if (code==0x57) {ex=1;}; | ||
+ | } while(ex==0); | ||
+ | }; | ||
+ | }; | ||
+ | |||
+ | }; | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | message=PLCCMD_TRIGGER4_ON; | ||
+ | timer=2; | ||
+ | |||
+ | if (absolute!=0) { absolute=1; }; | ||
+ | |||
+ | portclr(OUTPUT_MIST); | ||
+ | portclr(OUTPUT_FLOOD); | ||
+ | gvarset(7372, | ||
+ | timer=30; | ||
+ | gvarset(7373, | ||
+ | timer=30; | ||
+ | |||
+ | lift_up(); | ||
+ | |||
+ | dac01=0x0; | ||
+ | |||
+ | portclr(OUTPUT_SPINDLE); | ||
+ | portclr(OUTPUT_CCW_SPINDLE); | ||
+ | gvarset(7370, | ||
+ | gvarset(7371, | ||
+ | |||
+ | message=PLCCMD_TRIGGER1_OFF; | ||
+ | timer=10; | ||
+ | |||
+ | proc=plc_proc_idle; | ||
+ | exit(99); | ||
+ | };</ | ||
+ | |||
+ | ++++ | ||
+ | |||
+ | //More info on the M02 procedure available here: [[mycnc: | ||
+ | |||
+ | The trigger itself can be set up in Settings > Config > Inputs/ | ||
+ | |||
+ | {{: | ||
+ | |||
+ | * **Input Number** can be set to the port which the spindle feedback signal is using (#0 in this case) | ||
+ | * **Trigger** will be set to Falling Edge | ||
+ | * **Output** set to Not Connected | ||
+ | * **Slot** set to Stop Program | ||
=== Spindle Speed control for ET10_DAC === | === Spindle Speed control for ET10_DAC === | ||
Line 177: | Line 354: | ||
</ | </ | ||
+ | |||
+ | A tutorial on how to use global variable 7184 present in the code above is available on our YouTube channel: | ||
+ | |||
+ | {{youtube> | ||
=== Eliminating tangential knife spin at the start of the program (M212) === | === Eliminating tangential knife spin at the start of the program (M212) === | ||
+ | |||
+ | {{youtube> | ||
Because of how the system records angles, the software shows angles larger than 360 degrees (one full revolution) if a number of turns in the same direction have been taken by the knife. For example, if the knife has turned around its axis from 0 degrees twice in the positive direction, the angle now will be recorded as 720 degrees (2 full revolutions). After the program completes, and the angle is left at this number, the next time the program starts, the knife will rotate back until the angle is equal to zero. This behaviour is not ideal for some users, as it can extend the cutting process time. | Because of how the system records angles, the software shows angles larger than 360 degrees (one full revolution) if a number of turns in the same direction have been taken by the knife. For example, if the knife has turned around its axis from 0 degrees twice in the positive direction, the angle now will be recorded as 720 degrees (2 full revolutions). After the program completes, and the angle is left at this number, the next time the program starts, the knife will rotate back until the angle is equal to zero. This behaviour is not ideal for some users, as it can extend the cutting process time. | ||
Line 217: | Line 400: | ||
This PLC can be added to the DXF footer in **Settings > Config > DXF Import Settings** to run every time when the program generated from an imported DXF file finishes running. | This PLC can be added to the DXF footer in **Settings > Config > DXF Import Settings** to run every time when the program generated from an imported DXF file finishes running. | ||
+ | **// | ||
+ | When analyzing the program, by default the software does not know that the coordinate inside the PLC has changed, and thus the subsequent G0 command must be used to update it (G1/G2/G3 are NOT suitable for this task). However, the G0 code is only necessary if the M212 is embedded into a G-code program. If the M212 PLC is started independently (for example by clicking a the button which launches the M212 alone), then the system will automatically see that the coordinate has been changed. | ||
- | ==== Gantry Alignment Procedure (with Homing) | + | |
+ | === Gantry Alignment Procedure (with Homing) === | ||
<code c M132> | <code c M132> | ||
Line 440: | Line 626: | ||
exit(99); | exit(99); | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | |||
+ | === Automatic plate rotation and offset adjustment === | ||
+ | |||
+ | {{youtube> | ||
+ | |||
+ | <code C># | ||
+ | #define INPUT_PROBE 7 | ||
+ | #define ROLLBACK 2000 | ||
+ | wait_move() | ||
+ | { | ||
+ | do { code=gvarget(6060); | ||
+ | }; | ||
+ | show_error() | ||
+ | { | ||
+ | gvarset(9121, | ||
+ | timer=50; | ||
+ | message=PLCCMD_MOTION_BREAK; | ||
+ | exit(99); | ||
+ | }; | ||
+ | find_start() | ||
+ | { | ||
+ | gvarset(8631, | ||
+ | gvarset(8634, | ||
+ | gvarset(8634, | ||
+ | gvarset(8635, | ||
+ | timer=0; | ||
+ | do | ||
+ | { | ||
+ | sensor0=portget(INPUT_PROBE); | ||
+ | if ((timer& | ||
+ | timer++; | ||
+ | }while(sensor0==0); | ||
+ | timer=2000; | ||
+ | do | ||
+ | | ||
+ | }; | ||
+ | do_rollback_y() | ||
+ | { | ||
+ | sensor0=portget(INPUT_PROBE); | ||
+ | if (sensor0==0) | ||
+ | { | ||
+ | do{ | ||
+ | g0moveA(0, | ||
+ | sensor0=portget(INPUT_PROBE); | ||
+ | }while(sensor0==0); | ||
+ | }; | ||
+ | }; | ||
+ | do_rollback_x() | ||
+ | { | ||
+ | gvarset(8632, | ||
+ | g0moveA(0, | ||
+ | do | ||
+ | { | ||
+ | sensor0=portget(INPUT_PROBE); | ||
+ | if (sensor0==0) { message=stop_code; | ||
+ | code=gvarget(6060); | ||
+ | }while(code!=0x4d);// | ||
+ | }; | ||
+ | find_edge() | ||
+ | { | ||
+ | gvarset(8632, | ||
+ | sensor0=portget(INPUT_PROBE); | ||
+ | len=length; | ||
+ | if (sensor0!=0) {len=0-len; | ||
+ | g0moveA(0, | ||
+ | edge=100; | ||
+ | do{ | ||
+ | | ||
+ | if (code==0x4d) | ||
+ | { | ||
+ | edge=0; | ||
+ | }else | ||
+ | { | ||
+ | sensor1=portget(INPUT_PROBE); | ||
+ | if (sensor1!=sensor0) | ||
+ | { | ||
+ | edge=1; | ||
+ | message=stop_code; | ||
+ | do { code=gvarget(6060); | ||
+ | }; | ||
+ | }; | ||
+ | }while (edge==100); | ||
+ | }; | ||
+ | move_x() | ||
+ | { | ||
+ | gvarset(8632, | ||
+ | g0moveA(0, | ||
+ | do | ||
+ | { | ||
+ | sensor1=portget(INPUT_PROBE); | ||
+ | if (sensor1==0) | ||
+ | { | ||
+ | edge=0; | ||
+ | message=stop_code; | ||
+ | wait_move(); | ||
+ | }; | ||
+ | code=gvarget(6060); | ||
+ | }while(code!=0x4d);// | ||
+ | }; | ||
+ | move_y() | ||
+ | { | ||
+ | gvarset(8632, | ||
+ | g0moveA(0, | ||
+ | do { code=gvarget(6060); | ||
+ | }; | ||
+ | move_y0() | ||
+ | { | ||
+ | gvarset(8632, | ||
+ | g0moveA(0, | ||
+ | do { code=gvarget(6060); | ||
+ | }; | ||
+ | save_pos() | ||
+ | { | ||
+ | if (edge==0) | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | //point+=2; | ||
+ | gvarset(5730, | ||
+ | gvarset(8632, | ||
+ | }; | ||
+ | main() | ||
+ | { | ||
+ | gvarset(5740, | ||
+ | portset(7); | ||
+ | gvarset(300, | ||
+ | gvarset(301, | ||
+ | point=0; | ||
+ | gvarset(8631, | ||
+ | gvarset(5539, | ||
+ | edge=0; | ||
+ | stop_code=PLCCMD_LINE_SOFT_STOP;// | ||
+ | length=6000; | ||
+ | speed_probe=gvarget(300); | ||
+ | speed_fast=gvarget(301); | ||
+ | speed=speed_fast; | ||
+ | find_start(); | ||
+ | axis=0x1; //X axis | ||
+ | do_rollback_x(); | ||
+ | do | ||
+ | { | ||
+ | axis=0x2; //Y axis | ||
+ | speed=speed_fast; | ||
+ | do_rollback_y(); | ||
+ | do{ find_edge(); | ||
+ | if (edge!=0) | ||
+ | { | ||
+ | speed=speed_probe; | ||
+ | find_edge(); | ||
+ | save_pos(); | ||
+ | move_x(); | ||
+ | }; | ||
+ | }while(edge!=0); | ||
+ | axis=0x1; //X axis | ||
+ | speed=speed_fast; | ||
+ | find_edge(); | ||
+ | if (edge==0) | ||
+ | speed=speed_probe; | ||
+ | find_edge(); | ||
+ | save_pos(); | ||
+ | move_y(); | ||
+ | speed=speed_fast; | ||
+ | find_edge(); | ||
+ | if (edge==0) | ||
+ | speed=speed_probe; | ||
+ | find_edge(); | ||
+ | if (edge==0) | ||
+ | save_pos(); | ||
+ | gvarset(5740, | ||
+ | exit(99); | ||
+ | }; </ | ||
+ | |||
+ | === Simultaneous homing for two axes === | ||
+ | |||
+ | The code below allows simultaneous homing for both the X and the Y axes. The example is using a lot of the functionality described in the Jog from PLC section of the PLC manual ([[plc: | ||
+ | |||
+ | <code C># | ||
+ | |||
+ | wait_move() | ||
+ | { | ||
+ | do { code=gvarget(6060); | ||
+ | }; | ||
+ | |||
+ | show_error() | ||
+ | { | ||
+ | gvarset(9121, | ||
+ | timer=50; | ||
+ | message=PLCCMD_MOTION_BREAK; | ||
+ | exit(99); | ||
+ | }; | ||
+ | |||
+ | find_home_xy() | ||
+ | { | ||
+ | |||
+ | gvarset(5521, | ||
+ | gvarset(5525, | ||
+ | |||
+ | gvarset(8631, | ||
+ | |||
+ | statex=0; | ||
+ | statey=0; | ||
+ | speed=500; | ||
+ | speed_slow=50; | ||
+ | |||
+ | direction=(1<< | ||
+ | |||
+ | gvarset(8634, | ||
+ | gvarset(8634, | ||
+ | |||
+ | gvarset(8635, | ||
+ | timer=0; | ||
+ | do | ||
+ | { | ||
+ | changed=0; | ||
+ | if (statex==0) //to home X | ||
+ | { | ||
+ | sens=portget(INPUT_HOME_X); | ||
+ | if (sens!=0) | ||
+ | { | ||
+ | statex=1; | ||
+ | gvarset(8634, | ||
+ | changed=1; | ||
+ | }; | ||
+ | }; | ||
+ | if (statex==1) //rollback from home X | ||
+ | { | ||
+ | sens=portget(INPUT_HOME_X); | ||
+ | if (sens==0) | ||
+ | { | ||
+ | statex=2; | ||
+ | changed=1; | ||
+ | }; | ||
+ | }; | ||
+ | |||
+ | if (statey==0) //to home Y | ||
+ | { | ||
+ | sens=portget(INPUT_HOME_Y); | ||
+ | if (sens!=0) | ||
+ | { | ||
+ | statey=1; | ||
+ | gvarset(8634, | ||
+ | changed=1; | ||
+ | }; | ||
+ | }; | ||
+ | if (statey==1) //rollback from home X | ||
+ | { | ||
+ | sens=portget(INPUT_HOME_Y); | ||
+ | if (sens==0) | ||
+ | { | ||
+ | statey=2; | ||
+ | changed=1; | ||
+ | }; | ||
+ | }; | ||
+ | |||
+ | if (changed!=0) //if any of the sensors state is changed | ||
+ | { | ||
+ | direction=0; | ||
+ | if (statex==0) { direction=direction | (1<< | ||
+ | if (statex==1) { direction=direction | 1; }; //direction set to X+ | ||
+ | if (statey==0) { direction=direction | (2<< | ||
+ | if (statey==1) { direction=direction | 2; }; //direction set to Y+ | ||
+ | gvarset(8635, | ||
+ | }; | ||
+ | |||
+ | |||
+ | |||
+ | if ((timer& | ||
+ | timer++; | ||
+ | ready=(statex==2)& | ||
+ | |||
+ | }while(ready==0); | ||
+ | |||
+ | |||
+ | | ||
+ | |||
+ | }; | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | |||
+ | gvarset(8631, | ||
+ | gvarset(5539, | ||
+ | |||
+ | find_home_xy(); | ||
+ | |||
+ | | ||
+ | exit(99); | ||
}; | }; | ||
</ | </ |
plc/plc_examples.txt · Last modified: 2024/02/21 13:59 by ivan