Prince980.github.io

History of PLC

. Before PLC’s Relay Logic was used for industrial automation which had several drawbacks, major one was that an damage to a single wire cause interruption to whole automation process.

. To solve this, PLC (Programmable logic controllers) were introduced.

. Very first PLC was invented in 1969, but it was very slow.

. In 1979 PLC’s new generation was introduced which was very fast and reliable than the old one.

About Twincat

. TwinCAT stands for “ The Windows Control and Automation Texhnology.

. It is a visual studio based software.

. TwinCAT has two parts - XAE: eXtanded Automtion Engineering - XAR: eXtanded Automation Runtime

. And three states: Config, Run, Stop/exception

. TwinCAT allows us to use any machine (for example this PC) as a PLC.

Advantage of using TwinCAT

. CPU process the various processes depending upon the priorities of the task. For industrial automation process should be fast so TwinCAT uses isolated cores of CPU for compling and to run code so here is a plus point for TwinCAT over other softwares.

HELLO WORLD Program

  1. Open TwinCAT application and click new project and name the project as Helloworld.

  2. Click “ok”.

  3. Use ADSLOGSTR fuction to display message.

  4. ADSLOGSTR consist message type, format string and string argument. image

Data Types in TwinCAT

A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error. A string, for example, is a data type that is used to classify text and an integer is a data type used to classify whole numbers. image image image

Enum

Enumeration (or enum) is a user defined data type. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. image

Adding Enum in TwimCAT:

image

Array

Array is set if integers

Arrays in TwinCAT:

Two methods to declare arrays in TwinCAT

image

image

TwinCAT program to Start and Stop motor axis Using Switch Case in ST

MAIN (PRG):

PROGRAM MAIN
VAR
	ExampleAxisName : AXIS_REF;
	ExAxPower 		: MC_Power;
	ExAxStop 		: MC_Stop;
	ExAxMvVelo 		: MC_MoveVelocity;
	
	State			: INT := 0;
	
	start			: BOOL;
	stop			: BOOL;
	
END_VAR

MAIN WIMDOW:

ExampleAxisName.ReadStatus();

CASE State OF
	0:	//power activation
		ExAxPower.Enable := TRUE; 
		ExAxPower.Enable_Positive := TRUE;
		ExAxPower.Enable_Negative := TRUE;
		State := 1;
		
	1:	//check power
		IF ExAxPower.Active THEN
			State := 2;
		END_IF
		
	2:	//start motion
		IF start THEN
			
			ExAxMvVelo.Velocity := 50; //arbitrary number
			ExAxMvVelo.Execute := TRUE;
			State := 3;
		END_IF
		
	3:	//check that motion is within range
		IF ExAxMvVelo.InVelocity THEN
			State := 4;
		END_IF
		
	4: //stop motion
		IF stop THEN
			ExAxStop.Execute := TRUE;
			start:= False;
			State := 5;
		END_IF

		
	5: //check for stop to finish
		IF ExAxStop.Done THEN
			stop := FALSE;
			State := 2;
		END_IF
		
END_CASE

ExAxPower(Axis := ExampleAxisName);
ExAxStop(Axis := ExampleAxisName);
ExAxMvVelo(Axis := ExampleAxisName);

Steps:

  1. The first step to use any of the motion control tools in a PLC program is to import the Tc2_MC2 library.To import the Tc2_MC2 library, go into the Solution Explorer and scroll down to the PLC section. Expand the PLC section and look for the References tab. Right click the References tab and select Add library…, then type “MC2” into the search box. Find the Tc2_MC2 library, select it and hit OK.

Screenshot (226) Screenshot (227)

  1. use ExAxName as the name of our function block. To declare our instance, we type ExAxName : AXIS_REF; into the Variable Declaration Window. AXIS_REF function block allows us to link our hardware to the PLC program. It also gives us access to the ReadStatus() function. Calling ReadStatus() will update the local I/O variables that are linked to our axis.

Variable Declaration Window:

PROGRAM MAIN
VAR
	ExAxName : AXIS_REF;
END_VAR

Code Window:

ExAxName.ReadStatus();
  1. MC_Power function block allows us to control the software enable for our axis. If we don’t enable MC_Power, any command we send to our axis will result in an error. Let’s add an instance of MC_Power to our POU. We’ll call our new function block ExAxPower. To declare our instance of MC_Power, we need to type ExAxPower : MC_Power into the Variable Declaration Window.
PROGRAM MAIN
VAR
	ExampleAxisName : AXIS_REF;
	ExAxPower 		: MC_Power;
END_VAR
  1. MC_Stop function block enables us to stop the axis. Call a function ExAxStop : MC_Stop` in variable declaration window.
PROGRAM MAIN
VAR
	ExampleAxisName : AXIS_REF;
	ExAxPower 		: MC_Power;
	ExAxStop 		: MC_Stop;
	
END_VAR
  1. MC_MoveVelocity function block enables us to control the velocity of rotation. Call a function ```ExAxMvVelo : MC_MoveVelocity; in variable declaration window.

     PROGRAM MAIN
     VAR
         ExampleAxisName : AXIS_REF;
         ExAxPower 		: MC_Power;
         ExAxStop 		: MC_Stop;
         ExAxMvVelo 		: MC_MoveVelocity
     END_VAR
    
  2. CASE Function is used to axcecute the code blocks at different states i.e. for starting, running and stoping. CASE function compares the variable passed into it, and executes the section of code where it finds a match. 8.. Integer variable “State” is declared in variable declaration window for case function which contains value “0”.

PROGRAM MAIN
VAR
	ExampleAxisName : AXIS_REF;
	ExAxPower 		: MC_Power;
	ExAxStop 		: MC_Stop;
	ExAxMvVelo 		: MC_MoveVelocity;
	State			: INT := 0;
END_VAR
  1. At “state = 0” the CASE function will run a code block. Power is enabled though first case, To power our axis, we use:
0:
ExAxPower.Enable := TRUE; //General software enable for the axis.
ExAxPower.Enable_Positive := TRUE; //Feed enable in positive direction.
ExAxPower.Enable_Negative := TRUE; //Feed enable in negative direction.
  1. State is set to 1, in first caste for execution of futher code blocks in case function.
  2. In next step, Check whether the power is enabled, if it is uptate state integer to 2, using
    1:
    IF ExAxPower.Active THEN
        state=2;
    
  3. Start the motor using code:
    ExAxMvVelo.Velocity := 50; //arbitrary number
            ExAxMvVelo.Execute := TRUE;
            State := 3;
    

    if start condition is true the code above excecutes and state is set to 3.

  4. next we check whether the velocity of axis within range if yes we set the state to 4.
    IF ExAxMvVelo.InVelocity THEN
            State := 4;
        END_IF
    
  5. Then we can stop the axis ysing the code:
    IF stop THEN
            ExAxStop.Execute := TRUE;
            start:= False;
            State := 5;
        END_IF
    

    the above code executes when stop condition is true.

Communication Setup between TwinCAT and Visual Studio

ADS: The Automation Device Specification (ADS) is the communication protocol of TwinCAT. It enables the data exchange and the control of TwinCAT systems. ADS is media-independent and can communicate via serial or network connections.

ADS enables:

▪ access to the process image

▪ consistent data exchange

▪ access to I/O tasks

▪ detection of status changes

▪ read-out of the PLC symbol informationn

▪ access by variable name

▪ sum commands

▪ synchronous and asynchronous access

Intallation

  1. Go to Tools » NuGet Package Manager » Manage NuGet Packages for Solution

  2. Browse a Pckage called “Beckhoff.Twincat.ADS”.

  3. Install this package.

  4. Use the command

     using (AdsClient client = new AdsClient())
                {
                 client.Connect(AmsNetId.Local, 851);
         }
    

    For setting communication between local PLC port and Visual Studio.

  5. First, local PLC created in Twincat always goes to port “851’

  6. If we create second PLC in our Twincat Project its going to be port “852” and so on.

Note. when you start a new project in Visual Studio, it is typically a separate and isolated environment from your previous projects. This means that any packages or dependencies that were installed for previous projects may not automatically be available for new projects.

If you need to use the same package in a new project, you will need to install it again in the new project. This is because the package needs to be registered with the new project and its dependencies may be different from your previous project. Additionally, installing the package again in the new project ensures that you are using the correct and up-to-date version of the package for your new project.