Suske Lambik
Suske Lambik

Reputation: 95

How can I make an atomic statement in Twincat3 PLC?

I am working with a fast loop (0.5 ms cycle time) and slow loop (10 ms cycle time) which communicate with each other. How can I make the in- and outputs to be consistent?

Consider the example below, I want the assignments in the SlowLoop to be atomic, to be sure that both referenced inputs from the FAST loop correspond with values from the same cycle.

Example

FastLoop [0.5 ms]

FAST_CNT = some rising edge detection
FAST_RUNIDX += 1

SlowLoop [10 ms]

<-- Atomic Operation
pulseCount = FAST_CNT
elapsedTicks = FAST_RUNIDX 
Atomic Operation -->

Upvotes: 1

Views: 217

Answers (2)

Filippo Boido
Filippo Boido

Reputation: 1206

If you use two different tasks with different cycle times maybe also running on different cores, you need a way to synchronize the two tasks when doing read/write operations.

In order to access the data in an atomic way use the synchronization FBs that Beckhoff provides like for example FB_IecCriticalSection. More info here on the infosys Website:

https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/45844579955484184843.html&id=

Upvotes: 3

Steve
Steve

Reputation: 1018

Anytime that anything needs to be 'Atomic', you need to handle an object (STRUCT or FUNCTION_BLOCK). In this case as there is no associated logic, a STRUCT should do the job nicely.

TYPE st_CommUnit :
STRUCT
    Count       : UINT;
    Index       : UINT;
END_STRUCT
END_TYPE

You can then have this STRUCT be presented as either an Input or Output between tasks using the %Q* and %I* addressing.

- Fast Task - 
SourceData AT %Q* : st_CommUnit
- Slow Task - 
TargetData AT %I* : st_CommUnit

Using this you end up with a linkable object such that you can link:

  • The entire unit
  • Each individual component

Example Atomic Unit

Upvotes: 3

Related Questions