User Tools

Site Tools


plc:plc_examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
plc:plc_examples [2020/01/30 12:56] ivanplc:plc_examples [2020/02/04 13:19] ivan
Line 43: Line 43:
 </code> </code>
  
 +===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: 
 +<code>
 +  message=PLCCMD_TRIGGER1_ON;
 +  timer=10;do{timer--;}while(timer>0);
 +</code>
 +
 +The following code can be used to disable the trigger: 
 +
 +<code>  message=PLCCMD_TRIGGER1_OFF;
 +  timer=10;do{timer--;}while(timer>0);</code>
 +  
 +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 | 
 +<code>//Turn on Spindle clockwise
 +#include pins.h
 +#include vars.h
 +main()
 +{
 +  timer=0;
 +  proc=plc_proc_spindle;
 +
 +  val=eparam;
 +  if (val>0xfff) {val=0xfff;};
 +  if (val<0) {val=0;};
 +
 +  dac01=val;
 +
 +  portclr(OUTPUT_CCW_SPINDLE);
 +  portset(OUTPUT_SPINDLE);
 +
 +  gvarset(7370,1); timer=30;do{timer--;}while (timer>0); //Spindle State
 +  gvarset(7371,eparam); timer=30;do{timer--;}while (timer>0); //Spindle Speed Mirror register
 +
 +  //gvarset(7372,0);//Mist State
 +  //timer=30;do{timer--;}while (timer>0); //
 +  //gvarset(7373,0);//Flood State
 +  //timer=30;do{timer--;}while (timer>0); //
 +
 +  //delay after spindle started
 +  timer=spindle_on_delay;
 +  do{timer--;}while (timer>0); //delay for Spindle reach given speed
 +
 +  message=PLCCMD_TRIGGER1_ON;
 +  timer=10;do{timer--;}while(timer>0);
 +
 +  exit(99); //normal exit 
 +};</code>
 +++++
 +
 +++++Click to expand the M05 code |
 +<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;do{timer--;}while(timer>0);
 +
 +  if (spindle_off_delay!=0)
 +  {
 +    timer=spindle_off_delay;
 +    do { timer--; } while (timer>0);
 +  };
 +
 +  dac01=0x0;
 +
 +  gvarset(7370,0);  timer=30;do{timer--;}while(timer>0); //Spindle State
 +  gvarset(7371,0);  timer=30;do{timer--;}while(timer>0); //Spindle Speed
 +
 +  exit(99);  //normal exit 
 +};</code>
 +
 +++++
 +
 +++++Click to expand the M02 code |
 +<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--;}while (timer>0);//wait till motion started
 +
 +    z2=gvarget(7020);
 +    z2=z2*100;
 +
 +    if (absolute==0) {  z2=z1+z2;  };
 +
 +    z1=z1+100;  //add 1mm gap
 +
 +    if (z2>z1)
 +    {   //position coordinate in given axis in 0.01 units (mm)
 +        gvarset(7080,speed_z);  //set speed
 +        g0moveA(1,0x4,z2);       //absolute programming; Z axis;
 +        timer=300; do{timer--;}while (timer>0);  //wait motion started
 +        //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;do{timer--;}while(timer>0);
 +
 +  if (absolute!=0) { absolute=1; };
 +
 +  portclr(OUTPUT_MIST);
 +  portclr(OUTPUT_FLOOD);
 +  gvarset(7372,0);//Reset Mist State
 +  timer=30;do{timer--;}while(timer>0);
 +  gvarset(7373,0);//Reset Flood State
 +  timer=30;do{timer--;}while(timer>0);
 +
 +  lift_up();
 +
 +  dac01=0x0; //off DAC output
 +
 +  portclr(OUTPUT_SPINDLE);
 +  portclr(OUTPUT_CCW_SPINDLE);
 +  gvarset(7370,0);//Spindle State
 +  gvarset(7371,0);//Spindle Speed Mirror register
 +
 +  message=PLCCMD_TRIGGER1_OFF;
 +  timer=10;do{timer--;}while(timer>0);
 +
 +  proc=plc_proc_idle;
 +  exit(99);
 +};</code>
 +
 +++++
 +
 +The trigger itself can be set up in Settings > Config > Inputs/Outputs/Sensors > Triggers/Timers to look the following way:
 +
 +{{:plc:hardware-plc-002-spindle-control-trigger.png}}
 +
 +  * **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 ===
plc/plc_examples.txt · Last modified: 2024/02/21 13:59 by ivan

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki