User Tools

Site Tools


plc:plc

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
plc:plc [2018/12/27 14:30] skirillovplc:plc [2022/11/07 16:03] (current) ivan
Line 1: Line 1:
-{{mycnc:plc:mycnc-plc-builder-001.png?600}} 
- 
- 
 ====== PLC ====== ====== PLC ======
-PLC - programmable logic controller.+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:plc_builder|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. 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:plc_builder|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:plc:mycnc-plc-builder-001.png}}
  
 myCNC control has 2 types of built-in PLC systems that named myCNC control has 2 types of built-in PLC systems that named
-[[#Hardware PLC]] and [[#Software PLC]]+[[#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 ==== ==== Hardware PLC ====
Line 32: Line 31:
 === PLC Language === === PLC Language ===
  
-  * PLC operates with 32 bits integer values only. Floating point operations don't work in PLC.+  * PLC operates with 32 bits integer values only. Floating point operations don't work in PLC (typically need to use coefficient conversions to allow for more granular control).
   * There is no need to declare variables. There is very limited space of 32 elements for variables.    * There is no need to declare variables. There is very limited space of 32 elements for variables. 
   * There a number of pre-defined variables in the PLC   * There a number of pre-defined variables in the PLC
-    * eparam - External Parameter variable. If M-code running from G-code program, 16-bit integer parameters P and L are sent to the PLC procedure in the **eparam** variable. \\ P-parameter in a low word of the eparam value and \\ L-parameter in high word of eparam value. To decode P and L parameters from **eparam** simple 2-lines code can be used <code>P=eparam&0xFFFF;//P-parameter +    * eparam - External Parameter variable. A short manual is available here: [[plc:plc:eparam|Eparam]] 
-L=eparam>>16; //L-parameter</code>+
     * **proc** - variable is used to identify a process running in the PLC. \\ The value is sent to the CNC control software and can be used to display Current PLC state (like Idle, Ignition (for plasma cutting), preheat (gas cutting), Tool change, Probing etc)     * **proc** - variable is used to identify a process running in the PLC. \\ The value is sent to the CNC control software and can be used to display Current PLC state (like Idle, Ignition (for plasma cutting), preheat (gas cutting), Tool change, Probing etc)
 +      *  The **proc** variable can impact many behaviours of the myCNC software, such as jogging speed (using the User Settings value for when the **proc** is Idle, and using the cutting speed for **Plasma** and **Cutting** states). 
     * **timer** - value can be used as time counter inside PLC procedure     * **timer** - value can be used as time counter inside PLC procedure
     * vexit - variable contains additional exit code. In case of error CNC control software can display Error message depends on **vexit** exit code.     * vexit - variable contains additional exit code. In case of error CNC control software can display Error message depends on **vexit** exit code.
Line 49: Line 48:
     * adc01 - variable mapped to ADC #0 register. Reading this variable will return ADC #0 value.     * adc01 - variable mapped to ADC #0 register. Reading this variable will return ADC #0 value.
     * adc02 - variable mapped to ADC #1 register. Reading this variable will return ADC #1 value. Variables adc01, adc02 are a bit obsolete. It's better to use **gvarget**, **gvarset** functions and [[#The hardware access registers]] to access to the control board peripherals.     * adc02 - variable mapped to ADC #1 register. Reading this variable will return ADC #1 value. Variables adc01, adc02 are a bit obsolete. It's better to use **gvarget**, **gvarset** functions and [[#The hardware access registers]] to access to the control board peripherals.
 +
 +For example, changing the value of PWM01 can be done using the following PLC procedure:
 +
 +<code>main()
 +{
 +   val=eparam;
 +   if (val>0xfff) {val=0xfff;};
 +   if (val<0) {val=0;};
 +   pwm01=val;
 +   exit(99); //normal exit
 +};</code>
  
 <code>PLC is a tiny virtual machine with a very limited register and memory space.  <code>PLC is a tiny virtual machine with a very limited register and memory space. 
Line 84: Line 94:
  
 ^ Address ^ Description ^ ^ Address ^ Description ^
-| 20000...20100 | Print variable value in myCNC control message widget for debugging purpose. Values written to this registers will be printed in myCNC control software in Message widget {{mycnc:mycnc-print-variable-001.png?600}} |+| 20000...20100 | Print variable value in myCNC control message widget for debugging purpose. Values written to this registers will be printed in myCNC control software in Message widget - this is useful to see values of certain variables directly in the log window as the program is running. {{mycnc:mycnc-print-variable-001.png?600}} | 
 + 
 +It is possible to access the state of the output via gvarget commands from within the PLC process: 
 + 
 +<code>a=gvarget(0x400); //OUT0 
 +b=gvarget(0x407); //OUT7</code> 
 + 
 +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 [[https://www.hexadecimaldictionary.com/hexadecimal/0x400/|here]]). Thus, for example, //gvarget(0x40d);// will return the state of Output #13.  
  
 === The hardware access registers === === The hardware access registers ===
Line 93: Line 111:
 | GVAR_HW_INPUTS2 | 7182 | | | GVAR_HW_INPUTS2 | 7182 | |
 | GVAR_HW_INPUTS3 | 7183 | | | GVAR_HW_INPUTS3 | 7183 | |
-| GVAR_HW_OUTPUTS0 | 7184 | |+| GVAR_HW_OUTPUTS0 | 7184 | [[https://www.youtube.com/watch?v=iEXeUX0CedM|YouTube tutorial]] |
 | GVAR_HW_OUTPUTS1 | 7185 | | | GVAR_HW_OUTPUTS1 | 7185 | |
 | GVAR_HW_OUTPUTS2 | 7186 | | | GVAR_HW_OUTPUTS2 | 7186 | |
Line 155: Line 173:
  
   * All Software PLC procedures (except "system" procedures) are compiled and started simultaneously in separate threads with myCNC software start or after "Build All" button pressed in **PLC Builder/Software PLC**.   * All Software PLC procedures (except "system" procedures) are compiled and started simultaneously in separate threads with myCNC software start or after "Build All" button pressed in **PLC Builder/Software PLC**.
-  * "System" PLC procedures are procedures with names start with \\ <code>__ (double underscore)</code> 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 pre-defined "System" PLC handlers- +  * "System" PLC procedures are procedures with names start with \\ <code>__ (double underscore)</code> 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- 
  
  
Line 163: Line 181:
 | <code>__HANDLER_GCODE_START</code> | The procedure executed just before the myCNC software run g-code (after pressing PLAY button) | | <code>__HANDLER_GCODE_START</code> | The procedure executed just before the myCNC software run g-code (after pressing PLAY button) |
 | <code>__HANDLER_GCODE_STOP</code> | The procedure executed just after the myCNC software finished g-code running (after pressing STOP/PAUSE button) | | <code>__HANDLER_GCODE_STOP</code> | The procedure executed just after the myCNC software finished g-code running (after pressing STOP/PAUSE button) |
 +| <code>__BV17</code> | 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 | 
  
   * [[software_plc_examples|Software PLC Examples]]   * [[software_plc_examples|Software PLC Examples]]
Line 225: Line 251:
  
 === PLC defines === === PLC defines ===
- 
  
 | Name | Value | Comment | | Name | Value | Comment |
Line 302: Line 327:
 | PLCCMD_SET_PIDTIME | 1240 | | | PLCCMD_SET_PIDTIME | 1240 | |
  
 +== Note on PLC define naming==
 +
 +Please note that there exist some limitations of the preprocessor that parses the //#define// lines. Specifically, it is not possible to have a full name of one parameter be part of the name of another parameter.
 +
 +For example, an argument name such as //OUTPUT_SPINDLE// (present by default in the **pins.h** file), means that there must be no other //define//-names containing this substring. As such, names such as 
 +
 +<code>OUTPUT_SPINDLE_COOL
 +OUTPUT_SPINDLE_BEARING
 +OUTPUT_SPINDLE_CONE</code>
 +
 +are NOT allowed, ssince they all contain the string "OUTPUT_SPINDLE". Instead, you can make argument names such as
 +
 +<code>OUTPUT_BEARING_SPINDLE</code>
 +
 +or 
 +
 +<code>OUTPUT_CONE_SPINDLE</code>
 +
 +since those do not contain the exact string from before. Failure to properly define your arguments in such a manner and then utilizing them in your PLC commands will result in a error during compilation with a label //"syntax error, unexpected ID"//.
 +
 +
 +== PLCCMD_MOTION_CONTINUE and PLCCMD_MOTION_SKIP ==
 +
 +The abovementioned PLCCMD_MOTION_CONTINUE and PLCCMD_MOTION_SKIP commands are highly useful for certain applications since typically the motion controller runs commands one by one. By design, if within a running program the next code is a PLC M-code, then the movement will be stopped and the controller will run the PLC program. A message from within the PLC called PLCCMD_MOTION_CONTINUE is used to instruct the Motion controller to read and run the next code from the buffer (thus starting the next motion command).
 +
 +After this code, the PLC procedure continues running through its code while at the same time the next motion code is launched. In this way, both the PLC procedure and the motion command will be running simultaneously.
 +
 +This is useful for applications such as homing since it makes it possible to both move the axis and monitor the home sensor in the PLC procedure at the same time. That way when the sensor is activated, the current movement command will need to be stopped. A different message called PLCCMD_MOTION_SKIP is then used - the motion controller will cancel current motion (it will stop moving) and will read the next code from the buffer.
 + 
 === PLC processes named === === PLC processes named ===
  
Line 442: Line 496:
 | --- | 17029 | Return Current MACHINE W Position in PLC units (0.01mm) | | --- | 17029 | Return Current MACHINE W Position in PLC units (0.01mm) |
  
 +====Launching a PLC command using an on-screen button====
 +
 +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):
 +
 +{{youtube>uII_1znYNE4?large}}
 +
 +====Launching a Hardware PLC procedure from Software PLC====
 +
 +It is possible to launch Hardware PLC procedures from within a Software PLC command. This can be done for purposes such as utilizing certain commands are not available from the Software PLC, and that some require low-latency sensor monitoring. It also has the added benefit of utilizing the unlimited number of procedures that can work simultaneously in Software PLC, allowing the user create things such as permanent while loops, etc.
 +
 +For example, a code such as:
 +
 +<code C>main()
 +{
 +
 +gvarset(100040,602);
 +
 +exit(99);
 +
 +};</code>
 +
 +
 +will launch Hardware PLC M602 from the specified Software PLC.
 +
 +Additionally, the Param variable can also be used to call a Hardware PLC with a certain eparam variable. 
 +
 +For example, adding a line such as 
 +
 +<code C>
 +gvarset(100041, Param);
 +</code>
 +
 +will launch the M602 Hardware PLC with the Param variable. 
 +
 +====Jog from PLC====
 +
 +//**NOTE**: Jog from PLC requires a firmware update. As of February 2022, the feature is available in the Testing firmware branch.//
 +
 +myCNC allows the user to call for a jog command from within PLCs. The advantage of this motion mode is that it allows to perform tasks (such as changing the speed and direction of movement, as well as turning the motion for a particular axis on or off), all without stopping. This is useful in such applications as homing along multiple (for example, three) axes. Such an example (with simultaneous positioning along the X and Y axes) is available here: [[plc:plc_examples#simultaneous_homing_for_two_axes|PLC Examples]]
 +
 +In this mode, the acceleration is set via the following command:
 +
 +<code C>gvarset(8631,100); //acceleration time 100ms = 0.1s</code>
 +
 +while the speed is set via the global variable #8634 (the axis mask is stored in the high-order byte):
 +
 +<code C>gvarset(8634,((1<<24)|1000)); //Jog speed for X
 +gvarset(8634,((2<<24)|1000)); //Jog speed for Y
 +gvarset(8634,((4<<24)|1000)); //Jog speed for Z</code>
 +
 +A sample motion code may therefore look the following way: 
 +
 +<code C>jog()                           
 +{                 
 + gvarset(8631,100); //acceleration time 100ms = 0.1s       
 + gvarset(5539,1); //switch to fast g0moveA implementation 
 + gvarset(8632,1000); //Jog Speed 1m/min 
 + gvarset(8634,((1<<24)|1000)); //Jog speed for X            
 + gvarset(8634,((2<<24)|1000)); //Jog speed for Y             
 + gvarset(8634,((4<<24)|1000)); //Jog speed for Z              
 + 
 + gvarset(8635,1); //Jog X+         
 + timer=2000;       
 + do       
 +  { 
 +     timer--;             
 +     if ((timer&0xff)==0)  {    gvarset(8635,1);   };
 +   }while(timer>0);         
 +   
 + gvarset(8634,((1<<24)|500)); //Speed 500 for X 
 + gvarset(8635,1); //Jog X+
 + timer=2000;    
 + do
 +  { 
 +     timer--;  
 +     if ((timer&0xff)==0)  {    gvarset(8635,1);   };                
 +   }while(timer>0);
 +  
 + gvarset(8634,((1<<24)|200)); //Speed 200 for X       
 + gvarset(8635,1); //Jog X+           
 + timer=2000; 
 + do                       
 +  {                              
 +     timer--;  
 +     if ((timer&0xff)==0)  {    gvarset(8635,1);   };      
 +   }while(timer>0);                         
 +   
 + gvarset(8635,2+1<<8); //Jog X- AND Y+ Simultaneously  
 + timer=2000;                  
 + do                                
 +  {           
 +     timer--;         
 +     if ((timer&0xff)==0) 
 +                             
 +       gvarset(8635,2+1<<8); //Jog X- AND Y+ Simultaneously 
 +     };                                
 +   }while(timer>0);                 
 +
 +   gvarset(8635,0);             
 +   };</code>
 +   
 +The following variables may be used for this jog functionality:
 +
 +^ Variable name ^ Variable # ^   
 +| GVAR_G0PLC_SPEED | 8630 |
 +| GVAR_G0PLC_TIME | 8631 |
 +| GVAR_G0PLC_SPEED_UNITS | 8632 |
 +| GVAR_PLC_JOGSPEED_UNITS | 8634 |
 +| GVAR_PLC_JOG | 8635 |
 +
 +An example of a homing PLC that utilized this functionality is shown below. This PLC is designed for the Z-axis only:
 +
 +<code C>#include pins.h
 + 
 +wait_move()
 +{
 +    do { code=gvarget(6060); }while(code!=0x4d);  //wait till motion finished
 +};
 + 
 +show_error()
 +{
 +    gvarset(9121,1); //bring up popup message 21 - this can be customized
 +    timer=50;do{timer--;}while(timer>0);
 +    message=PLCCMD_MOTION_BREAK; //1033
 +    exit(99);
 +};
 + 
 +find_home_z()
 +{
 + 
 +  gvarset(5521,1); //disable hardware limits
 +  gvarset(5525,1); //disable software limits
 + 
 +  gvarset(8631,50); //acceleration time 100ms = 0.05s
 + 
 +  statez=0; //we set the "triggered" state to 0 by default
 +  speed=500;
 +  speed_slow=100; //used on rollback for better precision
 + 
 +  direction=(4<<8); //set the direction variable to Z-
 + 
 +  gvarset(8634,(4<<24)|speed); //Jog speed for Z
 + 
 +  gvarset(8635,direction); //jog in the set direction Z-
 +  timer=0;
 +  do
 +   {
 +      changed=0;
 +      if (statez==0) //to home X
 +       {
 +          sens=portget(INPUT_HOME_Z); //get state of home x sensor
 +          if (sens!=0)
 +          {
 +            statez=1;
 +            gvarset(8634,(4<<24)|speed_slow); //Jog speed for Z
 +            changed=1;
 +          };
 +       };
 +      if (statez==1) //rollback from home Z
 +       {
 +          sens=portget(INPUT_HOME_Z); //receive the state of the sensor 
 +          if (sens==0)
 +          {
 +            statez=2; 
 +            changed=1; 
 +          };
 +       };
 + 
 + 
 +  if (changed!=0) //if any of the sensors state is changed
 +  {
 +    direction=0; //set direction to 0 (no movement) before flipping
 +    if (statez==0) { direction=direction | (4<<8);  };  //direction set to X-
 +    if (statez==1) { direction=direction | 4;  }; //direction set to X+
 +    gvarset(8635,direction); //jog in new direction for the axes
 +  };
 + 
 + 
 + 
 +      if ((timer&0xff)==0)       gvarset(8635,direction);    };
 +      timer++;
 +      ready=(statez==2);
 + 
 +    }while(ready==0);
 + 
 + 
 +   gvarset(8635,0); wait_move(); //stop jog
 + 
 +};
 + 
 +main()
 +{
 + 
 +  gvarset(8631,50); //acceleration time 50ms = 0.05s
 +  gvarset(5539,1); //switch to fast g0moveA implementation
 + 
 +  find_home_z();
 + 
 + 
 +  exit(99);  //normal exit 
 +};</code>
 +
 +The PLC above can be combined with the XY homing (linked at the start of this section) to create simultaneous XYZ homing. In general, the Jog from PLC functionality allows for this simultaneous movement, cutting down on the previously required separate procedures per each axis. 
plc/plc.1545939027.txt.gz · Last modified: 2018/12/27 14:30 by skirillov

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki