. 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.
. 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.
. 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.
Open TwinCAT application and click new project and name the project as Helloworld.
Click “ok”.
Use ADSLOGSTR fuction to display message.
ADSLOGSTR consist message type, format string and string argument.
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.
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.
Array is set if integers
Arrays in TwinCAT:
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:
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();
PROGRAM MAIN
VAR
ExampleAxisName : AXIS_REF;
ExAxPower : MC_Power;
END_VAR
ExAxStop : MC_Stop
` in variable declaration window.PROGRAM MAIN
VAR
ExampleAxisName : AXIS_REF;
ExAxPower : MC_Power;
ExAxStop : MC_Stop;
END_VAR
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
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
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:
IF ExAxPower.Active THEN
state=2;
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.
IF ExAxMvVelo.InVelocity THEN
State := 4;
END_IF
IF stop THEN
ExAxStop.Execute := TRUE;
start:= False;
State := 5;
END_IF
the above code executes when stop condition is true.
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
Go to Tools » NuGet Package Manager » Manage NuGet Packages for Solution
Browse a Pckage called “Beckhoff.Twincat.ADS”.
Install this package.
Use the command
using (AdsClient client = new AdsClient())
{
client.Connect(AmsNetId.Local, 851);
}
For setting communication between local PLC port and Visual Studio.
First, local PLC created in Twincat always goes to port “851’
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.