This article will show you how to create a retention timer in TwinCAT3.Actually, I received an inquiry from a social networking service asking me if I could teach TwinCAT3 how to create a retention timer, so I answered that inquiry.
Come on, let’s enjoy FA.
Foreword
Thank you from the bottom of my heart for visiting my technical blog and YouTube channel.
We are currently running the “Takahashi Chris” radio show with Full-san (full@桜 八重 (@fulhause) / X) which I deliver every Wednesday night.
Currently, our activities continue almost free of charge, and your warm support is very important for us to provide more content.If you are able, we would be very happy if you could support us by clicking on the links below.
Membership of Takahashi Chris
You can sign up for a membership to the radio we are doing with MR.Full (full@桜 八重 (@fulhause) / X from here.
https://note.com/fulhause/membership/join
AMAZON Gift List
This will be of great use to me in creating content for my blog and improving my facilities.
https://www.amazon.co.jp/hz/wishlist/ls/H7W3RRD7C5QG?ref_=wl_share
Patreon
Here is a small Patreon of support for the creation of content and equipment for my blog.
https://www.patreon.com/user?u=84249391
Your support will help us to enhance our activities.
Thank you in advance for your support.
Email Address(*=@)
X
Implementation1
First implement the program for the hold timer directly in the POU.
VAR
PROGRAM MAIN VAR xIN : BOOL; tPT : TIME; xPAUSE : BOOL; xQ : BOOL; tET : TIME; fbPause : R_TRIG; tTimePaused : TIME; fbton : TON; END_VAR |
Program
Here is the program.
//Reset the PT IF NOT xIN THEN tTimePaused := T#0s; END_IF //Detect the Pause fbPause(CLK := xPAUSE); //Keep the Time IF fbPause.Q THEN tTimePaused := tTimePaused + fbton.ET; END_IF //FB fbton( IN := xIN AND NOT xPAUSE ,PT := tPT – tTimePaused ,Q=>xQ ); //Output xQ := fbton.Q; tET := tTimePaused + fbton.ET; |
If xIN is FALSE, the timer’s recording time is reset to 0s.
R_TRIG FB is used to detect the xPAUSE start-up signal.
When xPAUSE startup is detected, the elapsed time (ET) of the current TON is added to the Buffer variable (tTimePaused).
Then call TON FB.Note that the PT (i.e., TON setting time) is
tPT (setting time) minus the time difference when xPAUSE starts up.
Result
To verify that the retention timer worked correctly, add a YT Scope project and define the appropriate Axis Group.
As shown in the figure below, when xIN (orange line) is ON, eET in TON is adding time.And when xPAUSE (green line) is TRUE, eET stops adding time.Finally, when eET=PT, xQ (blue line) is ON.
Implementation2
The next step is to convert the program created in Implementation 1 to FB.
fbRetainTimer
Add FB to the TwinCAT project.
VAR
This one defines IN/OUT/internal variables for the program you just created.
- xIn: Input, TRUE = timer enabled
- tPT:Input, timer set time
- xPause:Input, timer pause
- xQ:Output, TRUE=Timer ON
FUNCTION_BLOCK fbRetainTimer VAR_INPUT xIn :BOOL; tPT :TIME; xPause :BOOL; END_VAR VAR_OUTPUT xQ :BOOL; END_VAR VAR tET:TIME; fbPause : R_TRIG; tTimePaused : TIME; fbton : TON; END_VAR |
Program
Here is the program, which works the same as Implementation 1.
//Reset the PT IF NOT xIN THEN tTimePaused := T#0s; END_IF //Detect the Pause fbPause(CLK := xPAUSE); //Keep the Time IF fbPause.Q THEN tTimePaused := tTimePaused + fbton.ET; END_IF //FB fbton( IN := xIN AND NOT xPAUSE ,PT := tPT – tTimePaused ,Q=>xQ ); //Output xQ := fbton.Q; tET := tTimePaused + fbton.ET; |
MAIN
Finally, declare Instnace in the MAIN program and check its operation.
VAR fbtimers:ARRAY[0..3]OF fbRetainTimer; END_VAR // fbtimers[0](tPT:=T#30S); fbtimers[1](tPT:=T#4S); fbtimers[2](tPT:=T#5S); fbtimers[3](tPT:=T#6S); |
Result
Let’s check the operation again with the YT Scope project.
Done!
Download
You can download the project created in this article at this Link.
https://github.com/soup01Threes/TwinCAT3/blob/main/TwinCAT4026-RetainTimer.tszip