Reputation: 95
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.
FAST_CNT = some rising edge detection
FAST_RUNIDX += 1
<-- Atomic Operation
pulseCount = FAST_CNT
elapsedTicks = FAST_RUNIDX
Atomic Operation -->
Upvotes: 1
Views: 217
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:
Upvotes: 3
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:
Upvotes: 3