OPCUA

Introduction

OPCUA is a standard, maintained by the OPC foundation, for industrial communication. Most of the providers of automation devices´, like PLC and Robot manufacturers, are providing OPCUA servers. Sometimes the OPCUA server is directly integrated into the automation device and sometimes a special software neeeds to be installed on a PC. OPCUA is one important upcoming standard for Industry4.0.

For more information please check: https://opcfoundation.org/about/opc-technologies/opc-ua/

OPCUA4Unity is available as a standalone asset as well as part of realvirtual.io Professional. The connection to the behavior models (PLCInputs and PLCOutputs) as well as the display of the Signals in the Hierarchy view is part of the realvirtual.io Framework and not available in the OPCUA4Unity standalone asset.

Installation

Both assets, realvirtual.io Professional as well as OPCUA4Unity are installed by using the Unity Asset Store.

If you are using the realvirtual.io Framework you are finding the OPCUA Interface after the installation under the path game4automation/private/interfaces/OPCUA4Unity If you are using the Standalone version you will find it under the path OPCUA4Unity in the Asset folder.

if you are using the realvirtual.io Professional version you can use OPCUA4Unity also independent from realvirtual.io. You just need to move the full OPCUA4Unity folder to a new project.

Demo model

In the sub-folder, DemoModel we provide a small demo which is showing you how to use the OPCUA interface independent from the realvirtual.io Framework. The demo model is working with a demo OPCUA Demo server (OPC UA C++ Demo Server V1.6.3 (Windows)) from Unified Automation (https://www.unified-automation.com/downloads/opc-ua-servers.html). You can download the OPCUA server for free, if you register before at Unified Automation.

Interface Configuration

If you are using realvirtual.io you can use the tool-bar to add an OPCUA interface to your scene:

Otherwise you will need to create an empty Gameobject and add the Script OPCUA_Interface to your Gameobject.

For the interface you will need to define at least

PropertyDescription

Server IP

The IP address of the server, whether on your machine like the demo server or on an external device

Server Port

The port of the OPCUA server

Security Policy

Dropdown for selecting the security policy - standard is NONE

Path

Defines a subpath of the server (e.g. like 127.0.0.1/opcuaserver/). Should be emtpy if not needed

Session Timeout Ms

The timeout of the session in Ms if the OPCUA server does not sends an answer

Top Node Id

The ID of the topnode under which all nodes should be imported. Please note, that you don’t need to import nodes if you would like to use them in your own script code. You can access in script any node the OPCUA server is importing.

Debug Mode

If set to true the console log will display additional messages for debugging

Application Name

The OPCUA application name

Appication URN

The OPCUA Application URN

Product URI

The OPCUA Product URI

Client Private Certificate

The path to the client private certificate inside the Streaming Assets Folder (please only use the subpath or the filename directly if certificate is directly inside StreamingAssets). Keep this value emtpy if you are not using certificates for security.

Client Public Certificate

The path to the client public certificate inside the Streaming Assets Folder. Keep empty if no certificates are used.

Username

The OPCUA username if username and password are used for security. Otherwise keep it emtpy.

Password

The password for the user

Password Signature Algorithm

Password signature algorythm - if password is used.

Reconnect Time

The time in ms a reconnection attempt is made if connection is broken.

Max Number of Nodes per Subscription

OPCUA allows multiple nodes in one subscription. Dependend on the OPCUA server the number of subscriptions or the number of nodes in one subscription might be limited.

Regex Write Nodes

Regex defined here will be used to define WriteNodes and in case of realvirtual.io for all nodes matching this Regex a PLCInput is generated.

Automatically Input On Write Signals

if true, if a node has a User Access Level of CurrentWrite, automatically WriteValue is set to true and in case of realvirtual.io a PLCInput is generated

Create Signals (only realvirtual.io)

true if you want to create the G4A signals automatically during importing nodes.

Automatically Subscribe On Import

if true all Nodes treated as an Input (CurrentRead Nodes) are automatically subscribed for receiving data changes

Unity Events

You can assign Methods to the Events OnConnected, OnDisconnected, OnReconnecting and OnReconnected to get informed in your components about the status of the OPCUA interface.

Connection troubleshouting

If you have problems in connecting to your OPCUA server please follow these steps.

  1. Make your own test model. Copy the demo scene and use the configuration without security certificates. Please set server to the IP address of your OPC Server (e.g. 127.0.0.1 if the server is running on the same machine):

  2. If you are not able to browse the server check you server configuration. Your server should run with no security settings. No security certificates and passwords should be used for first tests.

  3. If you are still unable to browse the server, try to ping the server in a command line (e.g. ping 192.168.0.32). If there is no response from the ping you should check your firewall settings. Please turn the firewall on the server and your computer off.

  4. If you are able to browse the server get back to Unity. Check again, that in Unity the Server address in your model is the same as in your test with OpcWatch. Open the demo model and delete all sub nodes.

  5. Select node id you where able to browse with OpcWatch and put it into the field Top Node Id of the OPCUA_Interface configuration. Then press import nodes.

  6. You should now see the subnodes of your OPC UA server. In the Console you should see the message OPCUA Interface - connected to OPCUA server [127.0.0.1] on port [48010]”

  7. Compile the application on your target device (Windows, Android, IOs) and test it.

  8. If you are still not able to connect on your target device please use a tool to see the log file on your target device like for example https://assetstore.unity.com/packages/tools/gui/in-game-debug-console-68068

  9. Check the debug log on your mobile device. The log message should give you a hint about the problems on your mobile device.

Generating your own Application certificate

Important Currently certificate based authentification and communication is not supported. We are working on the problem and try to find as soon as possible a solution.

Importing OPCUA Nodes

If the interface is configured and the server is accessible you can import the Nodes from the OPCUA server.

Push on Import Nodes and all nodes (under the Top Node ID) are imported and generated as Gameobjects with attached OPCUA_Node Scripts:

The OPCUA_Node Script is showing the Node information imported from the OPCUA server:

With the Button Read Node the data can be updated from the server.

If the realvirtual.io framework is installed, additionally all corresponding PLCSignals are created. These signals can be dragged and dropped with sensors or drives of the realvirtual.io framework.

When you now start the Simulation the OPCUA signals will be exchanged between Unity and the OPCUA Server.

OPCUA Nodes with realvirtual.io PLCInputs and PLCOutputs

This section is only valid for customers who are using realvirtual.io together with OPCUA4Unity. OPCUA4Unity is included in realvirtual.io Professional.

The OPCUA interface uses in the source code several compile switches based on the Define realvirtual.io to define a special behavior for realvirtual.io. During signal import to all imported OPCUA nodes the corresponding PLCInputs and PLCOutput signals are added. PLCOutputs are automatically subscribed on the OPCUA server. PLCInputs should not be subscribed, because they are sent to the OPCUA server by Unity.

Because in most cases it is not possible to determine, if the OPCUA node should be a PLC input or a PLC output, the booleans Write Value, what means that the value is written to the OPCUA server and Read Value, what means that the value is read from the OPCUA server. Based on this and on the data type the corresponding PLCInput or PLCOutput script is added to the OPCUA node.

It is not possible to define an OPCUA node at the same time as PLCInput and PLCOutput. If you try to do that a warning message will be displayed in the log and no PLCInput or PLCOutput script will be added. If you want to use one node as input and output you need to code the interaction yourself (see below).

It is possible to define a RegEx in the OPCUA interface configuration to determine automatically which OPCUA symbols should be treated as Input or Output during signal import. One ore multiple Regex can be defined symbol names or parts of symbol names which should be treated as Write Signals - that means PLCInputs or outputs from Unity.

If you change anything on the above described realvirtual.io related settings you can update all imported nodes by pushing on the button Update nodes.

Polling OPCU nodes

Subscribing to values is generally the recommended approach. However, in certain situations, it may be more practical to poll the value instead. When polling, you can choose the specific cycle in which you want to retrieve the data for each node.

To enable polling, you should disable the subscription. Set the poll value to true and specify the polling update rate in milliseconds using PollCycleMs. If you enter 0, the polling will be performed in each FixedUpdate, which could potentially slow down the application.

Using OPCUA Nodes with scripting

You find a <a href”https://realvirtual.io.com/documentation/current/apidoc/html/classrealvirtual.io_1_1_o_p_c_u_a___interface.html"> full API class documentation online.

This section give you just an overview how to use OPCUA nodes based on the demo model.

Reading Values

For reading values you can use two methods.

Direct reading

First one (not recommended one) is to read directly from the OPCUA server with the commands

var myvar = Interface.ReadNodeValue ("ns=2;s=Demo.Dynamic.Scalar.UInt64")

You can find this methods on the OPCUA_Node Script as well as on the interface itself. If you have a direct reference to the Interface in your method you can do like this:

using UnityEditor;
using UnityEngine;
using realvirtual.io;

public class DemoReadNodeNotRecommended : MonoBehaviour
{
 
    public OPCUA_Interface Interface;
    public string NodeId;

    // Update is called once per frame
    void Update()
    {
        
        float myvar = (float)Interface.ReadNodeValue(NodeId);
    }
}

This method is not recommended because the read is not done multithreaded and this might block or slow your application.

Subscribing to node value changes

This is the recommended method. You should register to the EventOnConnected Event and when the Interface gets connected you can subscribe to the node changes.

Here is an example:

public class DemoMoveCubeWithDelegate : MonoBehaviour
  {
      public OPCUA_Interface Interface;
      public string NodeId;
      public float PositionY;

      private OPCUA_Node node;
      private OPCUANodeSubscription subscription;

      // Start is called before the first frame update
      void Start()
      {
  
          if (Interface != null)
              Interface.EventOnConnected.AddListener(OnConnected);

      }

      public void OnConnected()
      {
          subscription = Interface.Subscribe(NodeId, NodeChanged);
      }
      
      public void
          NodeChanged(OPCUANodeSubscription sub, object obj) // Is called when Node Value of Node nodeid is changed
      {
          PositionY = (float) obj; // sets the new position based on the new value 
      }

      // Update is called once per frame
      void Update()
      {
          transform.rotation = Quaternion.Euler(new Vector3(0, PositionY, 0));
      }
  }

Writing Values

You can write values like this:

bool success = Interface.WriteNodeValue ("ns=2;s=Demo.Dynamic.Scalar.Float",100)

© 2022 in2Sight GmbH https://realvirtual.io - All rights reserved. No part of this publication may be reproduced, distributed, or transmitted in any form or by any means, including printing, saving, photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

Last updated