Sample26: Order of execution in a task

This article describes the determination of the task execution order if more than one module is assigned to a task.

Download

Here you can access the source code for this sample.

1. Unpack the downloaded ZIP file.
2. Using a Visual Studio with TwinCAT installed, open the project via Open Project ....
3. Configure signing for this project by switching on TwinCAT signing with a right-click on Project->Properties->Tc Sign and configure your certificate and password if necessary.
For more information on signing C++ projects, click here.
4. Select your target system.
5. Build the sample (e.g. Build->Build Solution).
6. Activate the configuration by clicking on Sample26: Order of execution in a task 1:.
The sample is ready for operation.

Description

The sample contains the SortOrder module, which is instanced twice. The sort order determines the execution order, which can be configured via the TwinCAT Module Instance Configurator.

For example, the CycleUpdate method tracks the object name and ID along with the sort order of this module. On the console window you can see the order of execution:

Sample26: Order of execution in a task 2:

In the sample, one instance is configured with Sort Order 150 and one with 170, while both instances are assigned to a task.

Understanding the sample

A TcCOM C++ module with cyclic IO.
1. The module requires a context-based parameter Sort order of task, which will automatically select PID_Ctx_TaskSortOrder as name.
Note that the parameter must be an alias (specification) of data type UDINT:
Sample26: Order of execution in a task 3:
2. Start the TMC Code Generator in order to obtain the standard implementation.
3. Since the code is modified in the next step, disable the code generation for this parameter now. Sample26: Order of execution in a task 4:
4. Make sure you accept the changes before restarting the TMC Code Generator:
Take a look at the CPP module (SortOrderModule.cpp in the sample). The instance of the smart pointer of the cyclic caller includes information data, including a field for the sorting order. The parameter value is stored in this field.
///////////////////////////////////////////////////////////////////////////////
// Set parameters of CSortOrderModule
BEGIN_SETOBJPARA_MAP(CSortOrderModule)
    SETOBJPARA_DATAAREA_MAP()
///<AutoGeneratedContent id="SetObjectParameterMap">
    SETOBJPARA_VALUE(PID_TcTraceLevel, m_TraceLevelMax)
    SETOBJPARA_ITFPTR(PID_Ctx_TaskOid, m_spCyclicCaller)
///</AutoGeneratedContent>
    SETOBJPARA_TYPE_CODE(PID_Ctx_TaskSortOrder, ULONG, m_spCyclicCaller.GetInfo()->sortOrder=*p) //ADDED
    //generated code: SETOBJPARA_VALUE(PID_Ctx_TaskSortOrder, m_TaskSortOrderContext1Parameter)
END_SETOBJPARA_MAP()

///////////////////////////////////////////////////////////////////////////////
// Get parameters of CSortOrderModule
BEGIN_GETOBJPARA_MAP(CSortOrderModule)
    GETOBJPARA_DATAAREA_MAP()
///<AutoGeneratedContent id="GetObjectParameterMap">
    GETOBJPARA_VALUE(PID_TcTraceLevel, m_TraceLevelMax)
    GETOBJPARA_ITFPTR(PID_Ctx_TaskOid, m_spCyclicCaller)
///</AutoGeneratedContent>
    GETOBJPARA_TYPE_CODE(PID_Ctx_TaskSortOrder, ULONG, *p=m_spCyclicCaller.GetInfo()->sortOrder) //ADDED
    //generated code: GETOBJPARA_VALUE(PID_Ctx_TaskSortOrder, m_TaskSortOrderContext1Parameter)
END_GETOBJPARA_MAP()
5. In this sample the object name, ID and sort order are tracked cyclically:
    // TODO: Add your cyclic code here
     m_counter+=m_Inputs.Value;
     m_Outputs.Value=m_counter;
    m_Trace.Log(tlAlways, FNAMEA "I am '%s' (0x%08x) w/ SortOrder %d ", this->TcGetObjectName(), this->TcGetObjectId() , m_spCyclicCaller.GetInfo()->sortOrder); //ADDED
6. The sorting order can also be transferred as the fourth parameter of the method ITcCyclicCaller::AddModule(), which is used in CModuleA::AddModuleToCaller().
7. Allocate a task with a long cycle interval (e.g. 1000 ms) to the instances of this module, in order to limit the tracking messages sent to the TwinCAT Engineering system.
8. Assign a different sorting order to each instance via the TwinCAT Module Instance Configurator: Sample26: Order of execution in a task 5: