OPC UA Companion Specification to Codesys
Table of Contents
Learning Outcome
You will be able to create a simple companio specification for OPC UA and import it into codesys. This enables your Codesys PLC to publish an objectoriented OPC UA interface.
Creating a Comp Spec with SiOME - Siemens Object Modelling Editor
SiOME is a graphical tool to create OPC UA Companion specifications.
Create a new CompSpec.
In the top middle part of SiOME, you can create a new namespace.

Give your namespace a URI. We chose iaam.fh-aachen.de/demo

When using Codesys, it may be necessary to use the "older" version of the basic OPC UA NodeSet. Chose the 1.04 Version like you see in the picture below.

Define Classes, Variables, Methods
Open the ObjectType Folder. Our Class will inherit from the base class Base Object Type. Right click on this, and click Add new objecttype

Give your objecttype (class) a name. We used LED here to make it easy. Later in the Example you will see the class FB_TransportBand

Now you can assign variables and methods to your class.
To do so, right click on the created object type and click Add child

When crating a child, you can choose if you want it to be a variable, an object, or a method.
Create variables and methods to your liking. You can change the variable type by clicking on the selector.

Methods with a signature
Methods may need input and output variables. When created vie SiOME the methods always have bool as the return type. This may not be changed.
If specfic output or input variables are needed, you can right click on the given method, and choose Add parameter. You can define which datapypy this parameter should have.
Export the CompSpec
When you are done creating your companion specification, export the nodeset. The nodeset will be saved in an XML format.

When exporting, choose the path, where your nodeset will be saved.

Import OPC UA Companion Specifications to Codesys
In this Example Codesys V3.5 SP19 P7 has been used.
In the upper menu bar, locate the tool menu and click OPC UA Information Model Repository

Here you will see all the installed information models for now.

Click on install, and choose the XML file of your exported compaion specification.
Communication Manager - Information model
To use this companion specification now. You have to add a Communication Manager Module to your application. Right Click on Application and chose this module.

After that, we have to define how the OPC Ua Server should publish the data. Either by using a give information model, or by using the simple OPC Ua Server (which is the newer version of the symbolconfig)
Right click on the Communication Manager and Click on OPC UA Information Model

Choose the information model, you want to use.

Double click on the created Information Model. You will see a split screen. On the left, you see the datatypes, that your Companion specification provides. On the right the you see the datatypes, that exist in Codesys. Drag the wanted class from the left to the right. Open the datatype, and make sure, that all the variables and methods are marked as Generate member
Click on Generate IEC Variables. This will map the UA-Datatypes to the IEC-Datatypes automatically.
You will see the generated Datatyped and FBs in your project:

Add Logic to the methods
Classes are imported as FBs. Methods are imported as Methods to those FBs. You need to add the body of the methods after they are imported.
The methods may be called from the OPC UA interface, and from within the Codesys project.
Comparision Comp Spec and generated Objects

Troubleshooting
Comp Spec Error (Version mismatch)
When importing the comp spec into codesys, you may get an error that codesys does not now the companion spec version opcfoundation.orf/UA and your custom comp spec needs another version.
Try choosing the older nodeset Version, like explained above when creating your custom nodeset.
If this is not working, you may need to install an older version of the base nodeset into codesys.