TwinCAT tips and tricks
The TwinCAT manuals for the digital twin and the workstations in the Festo Lab described to you how to use your CIF model to create a solution in TwinCAT, generate PLC code in it, and use the PLC code to control the hardware/software. Besides those basic steps, TwinCAT has many more features to make your debugging life easier. The tips and tricks on this page work for the digital twin and the workstations in the Festo Lab.
Keep sensor and actuator names unchanged
First of all, take very good care that the names of all your sensors and actuators are correct. TwinCAT does NOT detect any errors in sensor or actuator names! Moreover, adding more actuators or sensors to your controller is NOT allowed. TwinCAT will NOT read/write any of them!
Some debugging options you can use
If your solution is not, or only partly working, or showing unexpected behavior there are some steps you can take in your search for the cause of this while your program is running. You can:
Keep in mind that this is not a full-fledged ‘debugging TwinCAT’ section, only the basic actions are described. If you want additional information you should consult the on-line Beckhoff information system at https://infosys.beckhoff.com. However, this is not necessary for this course.
Using Flow Control
Although there is no directly visible relationship between a line of CIF code and a line of generated PLC code, it may be useful to see the flow within the PLC code. To enable it, click the flow-control icon once. After OK-ing the warning message that appears flow control will be active. You can only see what is going on if you follow the
path in the Solution Explorer window. You can see the generated PLC code for the selected program part, by double-clicking on it in the Solution Explorer window. The status/value of each variable is shown in green directly behind it. Parts of the program that are not being cycled through will not show current information. Turning off program flow (by clicking again) will show the last-known values again.Examine the status of sensors and actuators
There are two ways to examine the status of sensors and actuators: directly at the I/O interface, or in your control program variables. Your program may also be running or stopped; there is a difference! The PLC program is executed cyclically: read input variables, run the program, and update output variables. One cycle is usually performed in milliseconds. So, looking at the status of a variable from within the PLC program shows you its last-known value.
In the Solution Explorer windows on the left side of the screen you should follow the
path to see the Inputs section. You should at least be logged-in () to the PLC. Double-click-ing on either the Inputs or Outputs label will show a table with columns as shown in the figure.The column Value shows the last-known status of the boolean variables, in blue TRUE, in black FALSE. If the program is running the values will change accordingly.
To examine the status of actuators open the MAIN (PRG) file in the
Solution Explorer windows on the left side of the screen (dvar_M_HardwareMap_out_a_...
, you will
recognize actuator names on the ...
. Like the sensors, the column
Value shows the last-known status of the boolean variables, in blue
TRUE, in black FALSE.
Festo Workstation only
Looking at a status from within the I/O device shows you the real-time value. In the Solution Explorer windows on the left side of the screen you should follow the
path. Double-click on Image to see all I/O related information as shown in the figure.Inputs (yellowish label) and Outputs (reddish label) are shown in the ‘Name’ column. Their real-time on-line status is shown in the ‘Online’ column. You can observe sensor status changes here, even if the program is not being executed. The ‘Linked to’ column on the right shows if the respective I/O point is linked to a program variable, and if so which one that is. This linkage is performed with the mapping info file that we provided for this course. Every input sensors MUST be linked to a specific progam variable, otherwise it will not be able to influence the program flow. Each output variable MUST be linked to a specific output point, otherwise it will not be able to control the actuator attached to it.
Examine the location and variables of automata
Checking the location of your automata gives insight into how your controller is
performing. As an example, the example-controller
automata as shown below.
From this automata, you would like to know its current location and the value of
a_Pstartled
, boolean
and t
. To do so, open MAIN (PRG) from
the Solution Explorer ( ). Locate the expression state0 and
unfold it (). Find expressions starting with
?var_M_example-controller
, where ?
is either a d
(discrete) or a
c
(continuous). You will recognize the variables and locations under columns
Expression and Value.
The example controller is currently in location wait_init
, boolean
is
TRUE and continuous variable t
is 7.68. To examine the status of the
algebraic variable (a_Pstartled
), search in the Solution Explorer under
Untitled1 Project for the file alg_M_example_controller_a_Pstartled
(FUN).
automaton example_controller:
alg bool a_Pstartled = running;
disc bool boolean = true;
cont t der 1;
event init, start, stop;
location wait_init:
initial;
edge init when s_Pinitialized goto wait_start;
location wait_start:
edge start when s_Pstartbutton goto running;
location running:
edge stop when (not s_Pstopbutton) do boolean := false goto wait_start;
end
(Un)Forcing sensors and actuators
Important
THIS IS POTENTIALLY DANGEROUS AND SHOULD BE DONE WITH CARE!!!
Forcing sensors could make your controller activate actuators when it could cause damage to the machine!
Forcing actuators could make you cause damage to the machine!
It is NOT dangerous to use this function on your digital twin.
The INPUTS, MAIN (PRG) files and I/O screens show a column named ‘Prepared value’. It is used to prepare a value for one or more variables that is to be forced onto it, regardless of the outcome of the controller program. After selecting the ‘Prepared value’ column, clicking once in a value field will show a value opposite to the current value; clicking again will invert that value; clicking a third time will remove it, so the field is cleared again. A value in the ‘Prepared value’ column will make three additional icons selectable: , , and .
Force values
Clicking will give the selected variables their prepared value, regardless of the program outcome. This is shown on screen by placing the forced-value symbol in the ‘Value’ column, just before the forced value itself. The ‘Prepared value’ column is empty after this.
Unforce values
Variables can be unforced and brought back under program control again by clicking the unforce value icon . This will apply to all forced variables at once. Unforcing just one variable can be done by first clicking the ‘Prepared value’ cell of that forced variable. Doing that will show a pop-up window with four choices to influence the force. Your choice will be active after clicking the force value icon .
Writing values
Changing the value of a variable for just one cycle can be done by using the write-value icon . Activating this icon will write the prepared value to the variable and change it back to its original state after one cycle has passed. (This sometimes happens faster than the eye can see.) You can generate a short pulse this way.
Resetting and restarting your program
The reset-cold and reset-origin options are two different ways to (stop and) restart your program from the beginning. Clicking reset-cold stops the program, resets all I/O and allows you the start your program from the beginning. Clicking reset-origin does the same thing, but only after logging in and downloading the program to the PLC environment again. So the program will be refreshed completely.
Remember: Stopping , followed by starting a program only stops execution and continues execution of the program. No start from the beginning in this case!