Topic: DMD0233 Making Effective Use of Tasks |
|
|
|
Tasks are code-blocks that can be individually enabled to run as part of the current controller scan. Tasks are the proper choice for these two types of behavior:
The programmer controls when Tasks are executed by using the Enable Task (ENTASK) instruction, which is configured to execute its Task either one time on a leading edge of the input logic, or to execute its Task continuously on power-flow at a specified, millisecond-level rate.
The key to effectively using Tasks is to configure their runtime behavior through a combination of the Code-Block Configuration and the Enable Task (ENTASK) instruction that executes the Task.
|
|
Code-Block ConfigurationThe Code-Block Configuration is automatically invoked each time a new Task is created, and can be manually invoked by right-clicking on the Task in the Project Browser and selecting Configure Code-Block. The dialog presents the following configuration options:
Name - displays the name of the Task
Looping instructions in ladder logic (FOR / NEXT, WHILE / WEND, REPEAT / UNTIL, GOTO / LABEL) have the potential to cause problems during the controller's scan, ranging from variations in the scan times to infinite loops. Do-more controllers employ a mechanism - called 'yielding' - to manage this problem by allowing individual Tasks to specify how much processor time per scan to allow looping instructions contained in the Task to execute before they 'yield'. When a Task yields it will suspend execution of a looping instruction during the current scan, and will begin executing the looping instruction on the next scan, taking up right where it yielded. The amount of time allotted for loop execution in a Task is called the TimeSlice and the value is stored in the .TimeSlice field of each Task's structure.
The Task's .TimeSlice value will be set to this Initial TimeSlice value on every Task mode -to- RUN mode change. The Task's TimeSlice value can be changed at runtime by modifying the .TimeSlice structure member.
|
|
|
|
|
|
Code-Block Protection deals with accessibility and visibility issues for a Task code-block by allowing the programmer to secure the contents of user-created code-blocks from being viewed and/or edited by means of a password and encrypting the code-block contents. This dialog will show the current state of the protection and allow the programmer to set the protection level that will be enforced once the dialog is closed or the project is closed.
Full Access - (the default value) - the contents of the code-block can be viewed and modified without restriction.
Unlocked for this Project Session - the code-block is password protected, and once the password is correctly entered it's contents can be viewed and modified. The code-block will be locked automatically after the project session is closed.
Locked
- the code-block name will be listed in the Control Logic group, and placeholders
for each rung will be present, but the contents of the
Note:
the contents of
Click Set Protection Level to save the protection level changes.
Click Cancel to exit without saving any protection level changes. |
|
|
|
Set Password - click this button to invoke the Modify Code-Block Password dialog. Code-block passwords must be at least 4 characters in length, can be any reasonable length, and can contain any combination of letters, numbers, and characters.
Current Password - will be disabled if no password currently exists, otherwise enter the current password for this code-block.
New Password - enter the new password for this code-block
Confirm New Password - re-enter the password to confirm |
|
Click Clear Password to clear the existing password. Click Set Password to save the new password. Note: it is imperative that the programmer remember the code-block password. Once a password has been set for a code-block it is encrpyted as part of the code-block, and therefore it will be impossible to retrieve a lost code-block password.
Click Cancel to exit without saving the new password.
|
|
|
|
Task Structure MembersEach Task in a Do-more project contains a structure that provide status information and control options for the Task that are programmatically accessible. The values in the structure are updated each time the Task is executed. The syntax for using them is <task name>.<field name>.
.Once - a read-only Bit location that indicates that the Task is configured to run to completion one time.
.Continuous - a read-only Bit location that the Task is configured to run Continuously on Power Flow.
.Running - a read-only Bit location that indicates whether the Task is currently running
.RanThisScan - a read-only Bit location that shows whether the Task ran as part of the current PLC scan
.Done - a read-only Bit location that shows whether the Task has completed
.FirstScan - a read-only Bit location that indicates whether this is the Task's first scan after a PROGRAM -to- RUN transition
.FirstRun - a read-only Bit location that indicates whether the Task has completed one full pass through all of it's ladder code after a PROGRAM -to- RUN transition
.DoneThisScan - a read-only Bit location that indicates whether the Task has executed the final rung of ladder logic in the Task
.InstrSuspend - a read-only Bit location that indicates the Task is currently being suspended by a Suspend Program or Task (SUSPEND) instruction.
.DebugSuspend - a read-only Bit location that the Task is currently being suspended by a Debug View operation - a Task that is suspended will also have a yellow background in the Project Browser.
.TimeSlice - a read-only, unsigned 32-bit location that contains the amount of time (in microseconds) per PLC scan to allow any looping instructions in the Task to run.
.Interval - a read-only, signed 32-bit location that contains how much time to delay before the Task will run again (only valid if the Task is configured to run Continuously on Power Flow)
.ScanCounter - a read-only, signed 32-bit location that contains the number of PLC scans during which the Task has run. This value is reset to 0 when the Task is first enabled and will retain the last value until the Task is enabled to run again.
.RunCounter - a read-only, signed 32-bit location that contains the number of times the Task has run to completion. This value is reset to 0 when the Task is first enabled to run and will retain the last value until the Task is enabled to run again.
Note: If there are no looping constructs (FOR / NEXT, WHILE / WEND, REPEAT / UNTIL, GOTO / LABEL) the .RunCounter value and the .ScanCounter value will be the same.
.IntervalAcc - a read-only, unsigned 32-bit location that contains the amount of time remaining before the Task will run again. Note: this field is only valid if the Task is configured to run Continuously on Power Flow at xxx Interval
|
|
|
|
The Enable Task (ENTASK) InstructionThe Enable Task (ENTASK) instruction is used to enable a user-defined Task as part of the subsequent controller scan. This instruction does NOT cause the controller to immediately begin execution of the Task, rather it configures the Task to run when the controller gets to it as part of it's normal scan processing. If the Task exists later in the execution order the Task will begin execution on the same PLC scan. If the Task exists earlier in the execution order the Task will begin execution on the subsequent scan.
The Enable selection in the Enable Task (ENTASK) instruction determines both when, and how often the Task will run based on which of the following methods is selected:
|
|
Once on Leading Edge - select
this option to have the Task run to completion exactly one time. Depending
on the contents of the Task, this may take more than one PLC scan, but
the entire Task will be executed then it will be disabled. Configured this way the Enable Task instruction will have a gray triangle
at the right end of an input leg indicating the input is edge triggered
|
|
Continuous on Power Flow - select
this option to have the Task run as long as the ENTASK instruction has
power flow. Once the end of the Task is reached, it will remain enabled
and execution will start over at the top of the Task.
|
|
|
|
Beyond using Tasks to modularize a ladder logic program, and help with segmenting the ladder logic into functional blocks that can be enabled and./or disabled, utilizing the Task related instructions can help tailor the Project's execution to better fit the application's needs. Using the Yield Program or Task (YIELD) instructionSuppose, for example, that the most pressing need is to minimize the scan time of the controller. If the Task has ladder logic that does not need to run every scan, one or more Yield Program or Task (YIELD) instructions can be embedded in the Task. YIELD instructions will cause the Task to suspend execution at that point in the Task, and begin processing to the next project element (this could be the next Program or the main ladder logic section). The next scan this Task is run, processing will begin with the programming element right after the YIELD instruction that was last executed. Refer to the following pseudo-code:
Task Start
Ladder Code
Ladder Code
Yield
Ladder Code
Yield
Ladder Code
Ladder Code
Task End
This process of segmenting the Task into subsections that will be run on subsequent executions allows the Do-more Controller to process fewer instructions per scan, which keeps the scan time as low as possible. Of course it is the responsibility of the developer to decide where to place the YIELD instructions so that the application still functions and the scan time is minimized.
|
|
|
|
Managing Looping Instructions within TasksInside a Task you can use looping instructions (FOR / NEXT, WHILE / WEND, REPEAT / UNTIIL, GOTO / LABEL) that will process the ladder logic instructions inside the loop as long as the input condition is TRUE. Using a loop in your Task brings with it the potential to cause the scan time to get really long, or if something goes really wrong, it can cause the CPU's watchdog timer to fire. So in order to prevent this from happening, each Task has a .TimeSlice value that defines the amount of processing time (in microseconds) that you want to allow a Task to process looping instructions before it yields to other processes.
The default value for the .TimeSlice value is 100, which means that the Task will process looping instructions for 100 microseconds before it yields to the next ladder logic element.
Setting the .TimeSlice value at 0 effectively means you will get one pass through the loop each time the Task is allowed to run. Refer to the following pseudo-code:
Task Start
Ladder Code
Ladder Code
While condition is true
Ladder Code
Ladder Code
Wend
Ladder Code
Ladder Code
Task End
To allow the loop to run more than once per pass through the Task, set the .TimeSlice structure member to the number of micro-seconds you want to allow the Task to run, up to a maximum value of 65534 micro-seconds.
To allow the loop to complete on one scan, set the .TimeSlice structure member to the 65535 micro-seconds.
|
|
|
|
Using Restart Program or Task (RESTART)Another way to further control the processing of a task is through the Restart Program or Task (RESTART) instruction. If a Task containing one or more YIELD instructions has been allowed to run at least once so that the execution pointer - that is where the task will begin executing when it is allowed to run again - is not at the beginning of the Task, you can use the Restart Program or Task instruction to reset the execution pointer (and the task's flags) so that the next time the Task is allowed to run, it will begin at the first rung of the Task instead of at the point of the last YIELD instruction.
Task Start
Ladder Code
Ladder Code
Yield
Ladder Code
Yield
Restart Program or Task was done in the main program after this Yield was executed, so the execution pointer was reset to the first instruction in this Task, so the following two rungs were not processed.
Ladder Code
Ladder Code
Task End
The Halt Program or Task (HALT) instruction is used to stop the execution of a Task without setting it to restart on the next scan.
|
|
|
|
Data View Display Formats for Tasks:There are 4 variations of the Data View display of Task, each with different amounts of data and/or different display orientations.
Note: These formats are for display purposes only, the Enable Task variables cannot be edited when displayed using any of the formats.
Short Single Line: |
|
Short Multi-Line (the default): |
|
Long Single Line: |
|
Long Multi-Line:
|
|
|
|
See Also:
|
|
|
|
Related Topics:Related Instruction Topics:
|
|
|