I'm new to OpenModelica.
My problem is equivalent, to take an example from the physical world, to filling or emptying a tank between two limits of min and max height that I can determine (parameters). Filling or emptying is linear.
I want to develop a dedicated block in OMEdit to do that, with the following specification:
- A Boolean input "+", if true, fills the tank with a time step that I can choose (parameter). If it is false, filling stops.
- A Boolean input "-", if true, allows the tank to be emptied, with the same time step as before. If it is false, emptying stops.
- In both cases, the level is stored. However, a Boolean reset input is used to empty the tank instantly.
- If both the + and - inputs are false at the same time, then nothing happens: neither filling nor emptying.
- The output y sends in real time (according to the time step) the value of the tank's level. For example, if time step=1s, then the output is updated every 1 second.
- Finally, I set a value for the tank level at start-up
I hope that a competent person will suggest an operational code to solve my problem.
I know how to create a block under OM EDIT. Even though I'm a beginner in openmodelica, I have already personally created several blocks and models on other matters, with their graphical representation, I/O, etc.. and they are working fine.
However, regarding this specific request, I don't understand how to create a counter which, when incremented or decremented, sends its value at each time step to the output of the block.
I tried using a counter, imitating the equivalent python code. The problem is that the output of my block directly displays the final value and not each value for each time step as the simulation progresses. I have tried different loops for, while, when without success.
Many thanks in advance.
My poor coding attempt (I'm aware that this code is totally rubbish but it tries to reflect my needs):
block Counter
parameter Real HiLim = 100.0 "maximum output value";
parameter Real LoLim = 0.0 "minimum output value";
parameter Real TimeStep = 1 "Time step in seconds";
parameter Real y_start_value = 0 "start value for output";
Modelica.Blocks.Interfaces.RealOutput y annotation(
Placement(visible = true, transformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(extent = {{100, -10}, {120, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.BooleanInput u_plus annotation(
Placement(visible = true, transformation(origin = {-120, 60}, extent = {{-20, -20}, {20, 20}}, rotation = 0), iconTransformation(origin = {-110, 58}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.BooleanInput u_minus annotation(
Placement(visible = true, transformation(origin = {-120, -60}, extent = {{-20, -20}, {20, 20}}, rotation = 0), iconTransformation(origin = {-110, -60}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Blocks.Interfaces.BooleanInput RST annotation(
Placement(visible = true, transformation(origin = {-78, 120}, extent = {{-20, -20}, {20, 20}}, rotation = -90), iconTransformation(origin = {-79, 109}, extent = {{-9, -9}, {9, 9}}, rotation = -90)));
equation
//coherence of Lolim and HiLim
assert(LoLim < HiLim, "LIM: the lower limit must be lower than the upper limit");
algorithm //simplified one (min and max values are not taken into account)
y := y_start_value;
if RST==true then
y:=0;
else
while u_plus == true loop
y:=y+1;
y:=delay(y,TimeStep);
end while;
while u_minus == true loop
y:=y-1;
y:=delay(y,TimeStep);
end while;
end if;
annotation(
Icon(graphics = {Rectangle(lineColor = {85, 0, 255}, lineThickness = 0.5, extent = {{-100, 100}, {100, -100}}), Text(origin = {49, 134}, extent = {{27, 10}, {-113, -34}}, textString = "%name"), Text(origin = {-81, 60}, extent = {{-29, 14}, {29, -14}}, textString = "+"), Text(origin = {-79, -58}, extent = {{-29, 14}, {29, -14}}, textString = "-"), Polygon(origin = {-2, 15}, points = {{-64, -15}, {64, -15}, {64, 15}, {64, 15}, {-64, -15}}), Polygon(origin = {-26, 9}, fillPattern = FillPattern.Solid, points = {{-40, -9}, {40, -9}, {40, 9}, {40, 9}, {40, 9}, {-40, -9}}), Text(origin = {-82, 4}, extent = {{-10, 6}, {10, -6}}, textString = "min"), Text(origin = {78, 4}, extent = {{-10, 6}, {10, -6}}, textString = "max")}, coordinateSystem(initialScale = 0.1)),
Diagram,
uses(Modelica(version = "3.2.3")));
end Counter;
*Remark: annotation() in the code are a way of automatically encoding the block's I/O and graphic symbol without the author having to worry about it. * The version of OpenModelica I'm using is v1.14.1
