- Index
- » Users
- » SudhakarK
- » Profile
Posts
Posts
Thanks for your response. Let us say, you have a C code, named as ComputeHeat.c and it is located in a folder whose path is /home/fossee/Desktop/testSim on a Linux Machine. So, I will use the following code chunk to include the C code in my model.
Code:
external "C" annotation(
Include = "#include \"ComputeHeat.c\"",
ncludeDirectory = "file:///home/fossee/Desktop/testSim");
Hope this helps!
I could solve it by specifying the directory where the C code is located.
I am working on a model given on https://mbe.modelica.university/behavio … ntroller/. The name of this model is HysteresisEmbeddedControl. In this model, there is a function in Modelica as given below:
Code:
impure function computeHeat "Modelica wrapper for an embedded C controller"
input Real T;
input Real Tbar;
input Real Q;
output Real heat;
external "C" annotation (Include="#include \"ComputeHeat.c\"",
IncludeDirectory="modelica://ModelicaByExample.Functions.ImpureFunctions/source");
end computeHeat;
As given above, the definition of this function is given as below:
Code:
#ifndef _COMPUTE_HEAT_C_
#define _COMPUTE_HEAT_C_
#define UNINITIALIZED -1
#define ON 1
#define OFF 0
double
computeHeat(double T, double Tbar, double Q) {
static int state = UNINITIALIZED;
if (state==UNINITIALIZED) {
if (T>Tbar) state = OFF;
else state = ON;
}
if (state==OFF && T<Tbar-2) state = ON;
if (state==ON && T>Tbar+2) state = OFF;
if (state==ON) return Q;
else return 0;
}
#endif
To reproduce this model, I have created a new Model named HysteresisEmbeddedControl in OMEdit. Then, I create a function named computeHeat and insert it into this model. Now, I am not sure how to use the C function in my model. I am having OpenModelica 1.17.0 on Ubuntu 20.04. Could anyone please provide some insights into this?
Hello everyone! I am simulating a model using OMShell-terminal. First, I am simulating this model as shown below:
Code:
simulate(FlatControlTB, startTime = 0, stopTime = 20)
In this case, the simulation is successful, and the result file is stored in the current working directory. Next, I simulate the same model by activating the OPC-UA server, as shown below:
Code:
simulate(FlatControlTB, startTime = 0, stopTime = 20, simflags = "-rt=1.0 -embeddedServer=opc-ua")
In this case, the simulation runs properly, and I can get the values on the client-side. However, at the end of the simulation, it throws an error, as shown below:
Code:
info/network Shutting down the TCP network layer with 1 open connection(s)
free(): invalid pointer
Aborted (core dumped)
Since the simulation was not successful, the result file is not stored. I cannot figure out why the simulation fails when I try simulating it with the OPC-UA server. Could anyone please provide some insights into this?
Can we use https://meet.google.com/dmb-rsaw-dck for connecting right now?
Hi. Thanks. I need to know the set-up actually like the things (sensors/ actuators) connected to the Raspberry Pi. Maybe, we can connect over a Google Meet.
Hi. Thank you for your response.
I would like you to know that OpenModelica (OM) has a built-in OPC-UA server. So, from my understanding, it is better to configure OM as a server and Raspberry Pi as a client. Could you please let me know whether you can achieve your motive with this set-up (OM as server and Raspberry PI as a client)?
Hi. Could you please explain the things which you want to achieve with this client-server model? Maybe, you can help me with the idea behind your project.
Hi! Thanks for sharing the code files. I believe that your OM Model (PlotTest.mo) needs to be fixed.
Code:
model PlotTest
input Real RPM;
input Real coolantTEMP;
Real plotting;
equation
plotting = RPM*(1+(1/coolantTEMP)); // equation just for test purposes
end PlotTest;
In this code, when you define RPM and coolantTEMP as input Real, Modelica assigns these variables as zero. So, the moment you simulate this model, the value of plotting would be
Code:
plotting = 0*(1+1/0)
That's why this model has a runtime error. You might want to fix it and get going. Please let me know in case the issue is not solved.
By step 5, I mean that you can make your client read certain values from the server and print it. That would help you know that the client is actually connected with the desired server.
In case you are facing any issues in connecting the Modelica model and your Python client, please share the code for these two and the error you are getting.
Hi.Let us assume that you have saved your Modelica model as simTest.mo and your OPC-UA client as some clientScript.py. Now, to establish an OPC-UA client-server model, you need to begin the simulation of simTest.mo first. For this, I (on my Linux Ubuntu 20.04) follow the steps as given below:
1. Open a Terminal and navigate the directory (by using the cd command) where simTest.mo is located.
2. Launch OMShell-terminal. For this, type OMShell-terminal in the Linux terminal and press Enter.
3. Enter the command loadModel(Modelica) followed by loadFile("simTest.mo"). You should expect output as true in response to each of these two commands.
4. Now, we are ready to simulate simTest.mo. For this, enter the command simulate(simTest, startTime = 0, stopTime = 100, simflags = "-rt=1.0 -embeddedServer=opc-ua").
5. Now, run your OPC-UA script (clientScript.py) from another terminal. Once this script is executed, the client-server communication is established. You can print some values from the OpenModelica Model in your client script to check that the client can talk to the server.
6. At the end of the simulation (100 seconds in this case), you would get a .mat file (something like simTest_res.mat) in the folder where your Modelica model simTest.mo is located.
7. Launch OMEdit and go to File -> Open Result File(s). Now, select the .mat file (from the previous step) and click Open. It will open the Plotting Perspective, and all the parameters of the simulation will appear under Variables Browser. You can select the required parameter to draw the plot.
I have installed OpenModelica on Ubuntu 20.04. With its installation, I can see OMEdit, OMShell, OMShell-terminal, and OMNotebook on my machine. I am not sure what is the difference between OMShell and OMShell-terminal? Could anyone provide some insights on this?
Thanks for your response.
As we know that OpenModelica is an open-source implementation of the Modelica language. Thus, it would adhere to all the features of Modelica. Modelica language is strongly typed. This means that the compiler is more likely to generate an error if, for example, the passed type to an argument does not match the expected type. Could anyone please share an example (a sample model) in OMEdit which verifies this claim?
xyfan wrote:
Hi:
I'm learning to use openmodelica and opc-ua these days. When using OMEdit, I can simulate with "Interactive Simulation", click the start button in the Plot interface and read(write) data with UaExpert or OPCUA package in python, which works fine. But I also want to simulate by running the executable file. So I followed the instruction in another topic(run "xxx.exe -embeddedServer=opc-ua -rt=1" in command line ). By doing this I can successfully initialize the simulation and connect it with UaExpert. But how can I start the simulation? The data I read remains unchange and I don't know how to make the simulation run. Is there any thing I missed?
Hi! I am also working on establishing server-client communication with OpenModelica. Though I am able to read and write data with a client script in Python, I am not able to get the required plots in OMEdit after the simulation is over. Could you please provide some insights into this?
I am developing a server-client communication between OpenModelica (OM) and an external OPC-UA client. So, I have written an OM file as given below:
Code:
model test
input Real x;
Real y;
equation
y = x;
end test;
Along with this, I have written a Python script for running an OPC-UA client, as shown below:
Code:
from opcua import Client
from opcua import ua
import time
import logging
import matplotlib.pyplot as plt
# Define the URL on which the server is broadcasting
url = "opc.tcp://1xx.1xx.0.2:4841"
# 0 Server
# 1 step
# 2 run
# 3 realTimeScalingFactor
# 4 enableStopTime
# 5 time
# 6 x 0.0
# 7 y 0.0
if __name__ == "__main__":
client = Client(url)
logging.basicConfig(level=logging.WARN)
try:
client.connect()
print("Client connected!")
enableStopTime = client.get_node(ua.NodeId(10003, 0))
# enableStopTime.set_value(False)
print("Current state of enableStopTime : {}".format(enableStopTime.get_value()))
run = client.get_node(ua.NodeId(10001, 0))
run.set_value(True)
print("Current state of run : {}".format(run.get_value()))
root = client.get_root_node()
print("Root node is : ", root)
objects = client.get_objects_node()
print("Objects\' node is : ", objects)
writeID = 6 # x
readID = 7 # y
modelicaId = {}
modelicaId = objects.get_children()
set_point = 10
while True:
error = set_point - modelicaId[readID].get_value()
valWrite = 0.7*error
print("Error = {}, valWrite = {}".format(error, valWrite))
if (error > 0):
modelicaId[writeID].set_value(valWrite)
else:
print("Setpoint achieved!\n")
print("y value is: ", modelicaId[readID].get_value())
print("x value is: ", modelicaId[writeID].get_value())
time.sleep(2)
print("="*40)
except KeyboardInterrupt:
print("Stopping sequence!")
finally:
print("Done!")
client.disconnect()
Now, I simulate the OM file from OMEdit with the following arguments, as shown below:
General:
Start Time: 0
Stop Time: 15
Simulation Flags:
Additional Simulation Flags (Optional): -rt 1.0 -embeddedServer opc-ua
Next, I execute the client script, and server-client communication is established. As the Stop Time is set as 15, the simulation is halted after that duration. Thus, I am assuming that the client and server have communicated, and I should get a plot. However, when I click on the Plotting perspective of OM, I am not able to get any plot. I have also simulated the plant in OM from OMShell-terminal by executing the commands (simulate(test, startTime = 0.0, stopTime = 20.0, simflags = "-rt=1.0 -embeddedServer=opc-ua")) in a Linux Terminal . In that case, I can get the simulation result as a .mat file. Upon loading this result in OM, I can see the variables and can plot them as well.
Could anyone please provide some insights into this?
sjoelund.se wrote:
That's not possible. Impure functions may only be called at event instants. So you need to use when sample(...) or something similar.
Thanks for your response. Could you please elaborate on how to use when sample(...) for my task?
Hi! I am working on developing a client-server model between OpenModelica (OM) and external hardware. For this, I have written a sample OM file, as given below:
Code:
model test
input Real x;
Real y;
equation
y = sqrt(x);
end test;
Now, I wish to modify the code such that x generates a random value at each instant. Accordingly, I will read those values from the client end and change the values of x that will ultimately affect the value of y. Could you please provide some insights on this?
- Index
- » Users
- » SudhakarK
- » Profile