Emiko Hourston
Emiko Hourston

Reputation: 13

OpenModelica Interactive Simulation with OPCUA and Python

EDIT: See the linked forum discussion (here) for more info.

I'm a new user of OpenModelica. I'm using a Raspberry Pi as a (opc-ua) test server, which sets some of my server nodes automatically. I am also connected to the server via UaExpert to read/write nodes from my PC. I would like to connect an OM model to this server so I can pull server node values as inputs for the model's simulation.

I took a dive into the OM forum and found a topic similar to this. In that discussion, the OM model looked like this:

model SimulationTest
  input Real RPM;
  Real Torque;
equation
  RPM = Torque;
end SimulationTest;

They also had a python script that connected to the server and modified some node values. Here is a similar test script:

from opcua import Client
from opcua import ua
import time
import logging
from opcua.ua.uatypes import VariantType

# Define the URL on which the server is broadcasting
url = "opc.tcp://my.pi.ip.address:4840/"

if __name__ == "__main__":
    client = Client(url)
    logging.basicConfig(level=logging.WARN)

    try:
        client.set_user("username")
        client.set_password("password")
        client.connect()
        print("Client connected!")

        # Set default node values
        engineRPM_command = client.get_node("ns=4;s=engineRPM_command")
        engineRPM_command.set_value(800, VariantType.Int32)
        RPM = engineRPM_command.get_value()
        print("Current state of engineRPM_command : {}".format(RPM))

        engineTORQUE_current = client.get_node("ns=4;s=engineTORQUE_current")
        engineTORQUE_current.set_value(1.0, VariantType.Float)
        Torque = engineTORQUE_current.get_value()
        print("Current state of engineTORQUE_current : {}".format(engineTORQUE_current.get_value()))

        # Change node values over time
        while (True): 
            engineRPM_command.set_value(int(1.1*RPM), VariantType.Int32)
            RPM = engineRPM_command.get_value()

            engineTORQUE_current.set_value(1.5*Torque, VariantType.Float)
            Torque = engineTORQUE_current.get_value()
           
            print("RPM value is: ", RPM)
            print("Torque value is: ", Torque)
            time.sleep(2)
            print("="*40)

    except KeyboardInterrupt:
        print("Stopping sequence!")

    finally:
        print("Done!")
        client.disconnect()

To get the interactive simulation environment setup, I configured the Additional Simulation Flags (Optional): -rt 1.0 -embeddedServer opc-ua in the Simulation Flags tab under Simulation Setup. When I simulate the model, some files are produced (.exe, .log, .json, .xml, and .mat) but I can't see any plots in OMEdit.

The big questions are (1) how do I properly connect to my pi server from OMEdit, (2) how do I pull node values for the simulation, and (3) can I access this opcua node data from within OMEdit at Simulation time?

Thanks

Upvotes: 0

Views: 779

Answers (1)

Adeel Asghar
Adeel Asghar

Reputation: 587

OMEdit implements its own client and uses that for reading data and plotting.

If you want to use your own client then I suggest you should not use OMEdit and instead run the simulation from the command line using the simulate command. See https://build.openmodelica.org/Documentation/OpenModelica.Scripting.simulate.html. Basically you call something like simulate(SimulationTest, simflags="-rt=1.0 -embeddedServer=opc-ua -embeddedServerPort=4841") and then use the data you receive in python client to plot results.

Upvotes: 3

Related Questions