Example: AC Module Teaching
Product: CODESYS Application Composer
This example shows how to create and use learnable modules by means of a simple lighting controls.
Description
The CODESYS Application Composer provides the capability of a modular configuration of an application. When the structure is defined one time in the module tree, a static IEC application is generated from it. When this happens, the modules are connected directly with their submodules and cannot be changed at runtime.
This example shows how to create a dynamic configuration from a pool of separate modules at runtime.
In addition, this configuration is stored persistently so that the last set configuration can be downloaded each time the application is restarted.
You can use the included library without any modifications. The project simply shows a possible use case of this library. All required modifications have been marked in the code by means of comments using the keyword AC_ModuleTeaching
.
Additional information
Architecture
The idea of learnable modules is based on the pupil–subject–teacher principle.
Pupils can learn subjects which are taught to them by a teacher. The decision as to whether or not a pupil can learn a particular subject is up to the pupil. Therefore, a functioning module configuration always has to consist of a teacher, at least one pupil, and at least one subject.
Considering the architecture of the AC_ModuleTeaching
library, the following design results:
![]() |
The TeacherModule
, SubjectModule
, and PupilModule
(or TeacherRegistration
) function blocks derive from the AC.Module
function block. This allows these three module types to be configured in a module tree.
Only two function blocks are relevant for the implementation of learnable modules:
SubjectModule
andPupilModule
.TeacherModule
can be inserted as a module into the module tree without any modification or additional configuration.
Sequence
The initialization phase proceeds as follows:
![]() |
Init()
is called from the base Application Composer context in the first PLC cycle.
TeacherModule
: This module scans all module instances and reports back for each instance ofSubjectModule
andPupilModule
(Register(THIS^)
). This establishes the basic framework for cross communication of the modules.SubjectModule
: Each of these modules assigns itself a uniqueSubjectID
(UDINT
).PupilModule
: Each module has to register it ownSubjectIDs
, which it should be able to learn, viaAddSubjectID(UDINT)
.
Learning proceeds as follows:
![]() |
For any event (for example, clicking the "Learn" button in a visualization), a SubjectModule
instance reports to a TeacherModule
about the call of ReportTeacher()
. In ReportTeacher()
, Report(THIS^)
of the TeacherModule
is called implicitly. The TeacherModule
provides the SubjectModule
interface to all PupilModule
instances via Learn(ISubject)
with the possibility of learning the SubjectModule
. Learning a SubjectModule
means that the PupilModule
possibly has to cast and mark the SubjectModule
itself (for example, by storing in a member variable).
Application
For any specific application, you have to think about what and who will take on the role of "subjects" and "pupils", respectively. For example, subjects can be represented by sensors and pupils by actors. In the example, a light sensor is used for a subject and a light for a pupil. The light can learn to listen to different sensors or forget these again later.
Regarding implementation, the following should be noted:
Light EXTENDS ACMT.Pup
Init()
: Add the learnableSubjectID
Learn()
: This method has to be overwritten. At the beginning, the base implementation should then be called:IF SUPER^.Learn(itfSubjectToLearn := itfSubjectToLearn) = ACMT.ERROR.NO_ERROR THEN
If that is successful, then the subject can be learned. Otherwise, the subject is not learnable and can be ignored.
END_IF
ForgetAll()
: This method should also be overwritten and the local stored subjects removed again.SwitchPush EXTENDS ACMT.SubjectModule // indirectly via inheritance chain SwitchOnOff -> SwitchBase
Init()
: Define theSubjectID
CallPrioMediumStart()
: Call ofReportTeacher
if the sensor was pressed longer than 3 seconds.Persistence Manager
By adding the Persistence Manager, the learned subjects can be saved. As a result, the last status is restored when the application is restarted.
System requirements and restrictions
Programming system | CODESYS Development System (version 3.5.5.0 or higher) |
Runtime system | CODESYS Control Win (version 3.5.5.0) |
Add-on components | CODESYS Application Composer |
Note
DOWNLOAD Project
After installing the package, you will find the sample project in the directory C:\Users\<user>\CODESYS Examples\AC Module Teaching Example
.