# Cycle time in FB

**URL:** <https://community.br-automation.com/t/cycle-time-in-fb/4372>\
**Category:** B&R Software & Runtime\
**Tags:** automation-runtime\
**Created:** [September 23, 2024, 6:56pm UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372 "2024-09-23T18:56:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pavel\_Bezrodnyi](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Pavel\_Bezrodnyi](https://community.br-automation.com/u/Pavel_Bezrodnyi)\
**Post date:** [September 23, 2024, 6:56pm UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/1 "2024-09-23T18:56:10Z")

</div>

Greetings.  
In order for my function block (written in ST) to work properly, I need to get the cycle time during which it is called. How can I do this?

---

<div class="post-metadata">

**Author:** ![marcusbnr](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/marcusbnr/32/256_2.png) [@marcusbnr](https://community.br-automation.com/u/marcusbnr)\
**Post date:** [September 23, 2024, 7:06pm UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/2 "2024-09-23T19:06:18Z")

</div>

Hi Pavel,

Welcome to the Community!

You can use the [RTInfo](https://help.br-automation.com/#/en/4/libraries%2Fbrsystem%2Ffbks%2Frtinfo.html) function block from the BRSystem library to get the task’s cycle time.

---

<div class="post-metadata">

**Author:** ![Pavel\_Bezrodnyi](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Pavel\_Bezrodnyi](https://community.br-automation.com/u/Pavel_Bezrodnyi)\
**Post date:** [September 24, 2024, 5:57am UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/3 "2024-09-24T05:57:42Z")

</div>

Thanks Marcus!  
RTInfo inside the function block will be called every cycle, although 1 time would be enough for me.  
Or should I not worry about it?

---

<div class="post-metadata">

**Author:** ![christoph.hilchenbac](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christoph.hilchenbac/32/423_2.png) [@christoph.hilchenbac](https://community.br-automation.com/u/christoph.hilchenbac)\
**Post date:** [September 24, 2024, 6:16am UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/4 "2024-09-24T06:16:48Z")

</div>

Hi Pavel @Pavel_Bezrodnyi ,  
that should be a ‘cheap’ operation, but you can check this with the profiler.

btw: an elegant way to call such ‘one time operations’ would be to check the rising edge of an ‘Enable’ or ‘Execute’ input of your function block.

---

<div class="post-metadata">

**Author:** ![Pavel\_Bezrodnyi](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Pavel\_Bezrodnyi](https://community.br-automation.com/u/Pavel_Bezrodnyi)\
**Post date:** [September 24, 2024, 6:20am UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/5 "2024-09-24T06:20:59Z")

</div>

Thank you, Christoph.  
I’ll think about it.

---

<div class="post-metadata">

**Author:** ![michael\_w](https://avatars.discourse-cdn.com/v4/letter/m/FF8800/32.png) [@michael\_w](https://community.br-automation.com/u/michael_w)\
**Post date:** [September 24, 2024, 7:48am UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/6 "2024-09-24T07:48:46Z")

</div>

Hi,

I want to add: Usually inside a FUB you implement a state machine, so doing such an operation is a thing of one step only. Also, you can always remember the value somewhere in some “Internal” structure and only call the FUB in case the value is still zero.  
Note: Often times I see people who use cycle times doing calculations with them to achieve timings. I’d like to say: “don’t”. If you need to act on specific timings, use FUBs like TON, TOFF, … 🙂

---

<div class="post-metadata">

**Author:** ![Pavel\_Bezrodnyi](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Pavel\_Bezrodnyi](https://community.br-automation.com/u/Pavel_Bezrodnyi)\
**Post date:** [September 24, 2024, 8:10am UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/7 "2024-09-24T08:10:49Z")

</div>

Yes, I agree, I will highlight the INIT state in the internal finite automaton to get the cycle time and validate the input parameters.  
TON / TOF and other timers will not work for me, because I need to change the output value from 0 to MAX in a given time (input parameter) by ramp. For this purpose I use the product of the output value by the alpha factor (0.0 - 1.0). Alpha is incremented (with limit check) for each cycle by a fixed value calculated from the cycle time and the given change time.

---

<div class="post-metadata">

**Author:** ![krausertm](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/krausertm/32/98_2.png) [@krausertm](https://community.br-automation.com/u/krausertm)\
**Post date:** [September 24, 2024, 8:26am UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/8 "2024-09-24T08:26:19Z")

</div>

I guess you could still use a TON 😃

```auto
	TON_Ramp.PT := InputTime;
	TON_Ramp();
	IF TON_Ramp.IN THEN
		OutputValue := TIME_TO_REAL(TON_Ramp.ET)/TIME_TO_REAL(TON_Ramp.PT);
	ELSE
		OutputValue := 0.0;
	END_IF

```

Edit: See Christophs answer below: Only precise down to 10ms (TON base)

---

<div class="post-metadata">

**Author:** ![michael\_w](https://avatars.discourse-cdn.com/v4/letter/m/FF8800/32.png) [@michael\_w](https://community.br-automation.com/u/michael_w)\
**Post date:** [September 24, 2024, 8:28am UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/9 "2024-09-24T08:28:01Z")

</div>

Hi,

there are, as your usecase shows, cases where it’s definitely legit to do those calculations. I just often enough stumbled upon the highlighted wrong (imo) usage of it and wanted to point to it 🙂 I have also seen people count cycles to implement timings, which is even worse since it’s loosing functionality once you move the task class 🤮

Please feel free to ask again for further questions, good luck with your new FUB and have a nice day

---

<div class="post-metadata">

**Author:** ![Pavel\_Bezrodnyi](https://avatars.discourse-cdn.com/v4/letter/p/FF8800/32.png) [@Pavel\_Bezrodnyi](https://community.br-automation.com/u/Pavel_Bezrodnyi)\
**Post date:** [September 24, 2024, 8:33am UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/10 "2024-09-24T08:33:24Z")

</div>

Marcel, thanks for the interesting solution!

Hmm, maybe I should really stop using cycle time, I’ll think about it, thanks!

---

<div class="post-metadata">

**Author:** ![christoph.hilchenbac](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christoph.hilchenbac/32/423_2.png) [@christoph.hilchenbac](https://community.br-automation.com/u/christoph.hilchenbac)\
**Post date:** [September 24, 2024, 10:55am UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/11 "2024-09-24T10:55:47Z")

</div>

don’t underestimate the counting of cycles.  
TON works on a 10ms base so it is less precise.

[B&R Online Help (br-automation.com)](https://help.br-automation.com/#/en/4/libraries/standard/fbks/ton.html)

---

<div class="post-metadata">

**Author:** ![alexander.hefner](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/alexander.hefner/32/111_2.png) [@alexander.hefner](https://community.br-automation.com/u/alexander.hefner)\
**Post date:** [September 24, 2024, 3:19pm UTC](https://community.br-automation.com/t/cycle-time-in-fb/4372/12 "2024-09-24T15:19:07Z")

</div>

If you need the exact information of time exceeded between 2 calls, you also could use the [AsIoTimestamp()](https://help.br-automation.com/#/en/4/libraries%2Fasiotime%2Ffbks%2Fasiotimestamp.html) function, which delivers a actual system time counter in microseconds.

This then has to be done cyclic, and you always have to store the timestamp value of the last call also. As it’s a DINT value, you can subtract “old” from “actual” without any further calculation to get the difference between the 2 calls:

```auto
act := AsIoTimestamp();
diff := act - old;
old := act;

```

Of course using RTinfo() like mentioned above is the right way to determine the cycle time of the calling task class, there’s no doubt about.  
I only mentioned AsIoTimestamp() additionally if you need to know the exact time between 2 calls (independent of if a task class maybe also consumed some tolerance time, or if someone “accidently” calls the same FB instance twice in one cycle … who knows 😉 ).

Best regards!
