TwinCAT 3 C++ module providing methods
This article describes the creation of a TwinCAT 3 C++ module that provides an interface with several methods that can be called by a PLC and also by other C++ modules.
The idea is to create a simple state machine in C++ that can be started and stopped from the outside by other modules, but which also allows the setting or reading of the particular state of the C++ state machine.
Two further articles use the result from this C++ state machine.
- Calling the function from the PLC logic - i.e. affecting the C++ code from the PLC.
- Calling the function from the C++ logic - i.e. interaction between two C++ modules.
This article describes:
- Step 1: create a new TwinCAT 3 project.
- Step 2: create a new TwinCAT 3 C++ driver.
- Step 3: create a new TwinCAT 3 interface.
- Step 4: add methods to the interface.
- Step 5: add a new interface to the module.
- Step 6: start the TwinCAT TMC Code Generator to generate code for the module class description.
- Step 7: implement the member variables and the constructor.
- Step 8: implement methods.
- Step 9: implement cyclic update.
- Step 10: compile code
- Step 11: create an instance of the C++ module.
- Step 12: done. Check the results.
Step 1: create a new TwinCAT 3 project
First of all, create a TwinCAT project as usual.
Step 2: create a new TwinCAT 3 C++ driver
- 1. Right-click on C++ and Add New Item…
- 2. Select the template TwinCAT Driver Project and enter a driver name, "StateMachineDrv" in this sample. Click on Add to continue.
- 3. Select a template to be used for this new driver. In this sample "TwinCAT Module Class with Cyclic IO" is selected, since the internal counter of the state machine is available for assigning to the IO.
- 4. Click on Add to continue.
- 5. Specify a name for the new class in the C++ driver "StateMachineDrv".
The names of the module class and the header and source files are derived from the specified "Short Name". - 6. Click on OK to continue.
- The wizard then creates a C++ project, which can be compiled error-free.
Step 3: create a new TwinCAT 3 interface
Note | |
Name conflict A name collision can occur if the driver is used in combination with a PLC module. |
- 1. Start the TMC editor by double-clicking on StateMachineDrv.tmc.
- 2. Select Data Types within the TMC editor.
- 3. Add a new interface by clicking on Add a new interface .
- A new entry IInterface1 is then listed.
- 4. Open IInterface1 by double-clicking in order to change the properties of the interface.
- 5. Enter a meaningful name - in this sample "IStateMachine".
- The interface has been created.
Step 4: add methods to the interface
- 1. Click on Edit Methods... to get a list of the methods of this interface:
Click on the + button to create a new default method, Method1. - 2. Replace the default name Method1 by a more meaningful name, in this sample "Start".
- 3. Add a second method and name it "Stop".
- 4. Add a third method and name it "SetState".
- 5. Subsequently, you can add parameters by clicking on Add a new parameter or edit parameters of the SetState method.
- The new parameter, Parameter1, is generated by default as Normal Type INT.
- 6. Click on the name "Parameter1" and change the name in the edit box to "State".
- 7. After Start, Stop and SetState have been defined, define a further method.
- 8. Rename it "GetState".
- 9. Add a parameter and name it "pState" (which is conceived to become a pointer later on).
- 10. Change Normal Type to Is Pointer.
- You then obtain a list of all methods. You can change the order of the methods with the buttons.
Step 5: add a new interface to the module
- 1. Select the module that is to be extended by the new interface - in this case select the destination Modules->CStateMachineModule.
- 2. Extend the list of implemented interfaces by a new interface with Add a new interface to the module by clicking on the + button.
- 3. All available interfaces are listed - select the new interface IStateMachine and end with OK.
- The new interface IStateMachine is part of the module description.
Step 6: Start the TwinCAT TMC Code Generator
- 1. In order to generate the C/C++ code on the basis of this module, right-click in the C/C++ project and then select the TwinCAT TMC Code Generator.
- The module StateMachineModule.cpp now contains the new interfaces
CModule1: Start()
CModule1: Stop()
CModule1: SetState(SHORT State)
CModule1: GetState(SHORT* pState).
Step 7: implementation of the member variables and the constructor
Add the member variables to the header file StateMachineModule.h.
Step 8: implementation of methods
Implement the code for the four methods in the StateMachineModule.cpp:
Step 9: implementation of a cyclic update
The C++ module instance is cyclically called, even if the internal state machine is in Stop mode.
- If the state machine is not to be executed, the m_bRun Flag signals that the code execution of the internal state machine is to be quit.
- If the state is "1" the counter must be incremented.
- If the state is "2" the counter must be decremented.
- The resulting counter value is assigned to Value, which is a member variable of the logical output of the data area. This can be assigned to the physical IO level or to other data areas of other modules at a later time.
.
Step 10: compilation of code
- 1. Following the implementation of all interfaces, compile the code by right-clicking on the state machine and selecting Build.
- 2. Repeat the compilation and optimize your code until the result looks like this:
Step 11: creating an instance of the C++ module
- 1. Right-click on the C++ project and select Add New Item... to create a new module instance.
- 2. Select the module that is to be added as a new instance – in this case CStateMachineModule.
- 3. Assign the instance to a task:
Step 12: finished - check the result
- 1. Navigate to the module listed in the solution tree and select the Interfaces tab on the right-hand side.
- The new interface IStateMachine is listed.