0

In the model that I am working on i have a Boolean of that controls the turn ON and OFF of a system, that means during simulation my system turns ON and OFF many times, so I want to calculate the frequency of the ON/OFF, does anyone have an idea, please

thank you

  • Please try to utilise the pre(x) operator within a conditional clause to update a counter variable and try to scope within frequency's definition – Akhil Nandan Nov 06 '22 at 16:52

3 Answers3

2
model Boolean_Test
  Modelica.Blocks.Sources.BooleanPulse BooleanPulse(period = 20e-3, width = 20)  annotation(
    Placement(visible = true, transformation(origin = {-66, 6}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));

Real counter(start=0); // Counter to count the number of sets and resets
Real frequency(start=0); 
equation
when Modelica.Math.BooleanVectors.oneTrue([BooleanPulse.y,pre(BooleanPulse.y)]) then
counter =pre(counter)+1;
frequency=0.5*counter/time;
end when;
 
annotation(
    uses(Modelica(version = "3.2.3")));
end Boolean_Test;

You can use the above example to achieve your objective. Here I use xor principle to sense the toggle in the boolen output (BooleanPulse.y). A similar idea can be implemented in your source code. The above code is a simple example where I used to validate the computed frequency. For me the computed is equal to frequency value defined in Modelica.Blocks.Sources.BooleanPulse

Akhil Nandan
  • 315
  • 2
  • 9
1

Just trying to simplify Akhil Nandan's answer a bit, could result in:

model Boolean_Test2
  Modelica.Blocks.Sources.BooleanPulse BooleanPulse(period = 20e-3, width = 20)  annotation (
    Placement(visible = true, transformation(origin = {-66, 6}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));

  Real counter(start=0); // Counter to count the number of sets and resets
  Real frequency(start=0);

equation 
  when BooleanPulse.y then
    counter =pre(counter)+1;
    frequency=counter/(max(time,1e-6));
  end when;

annotation(uses(Modelica(version="4.0.0")));
end Boolean_Test2;
Markus A.
  • 6,300
  • 10
  • 26
0

To calculate the boolean frequency (F) where ON is the boolean in my model, I used the following model:

when ON and ON <> pre(ON) then 
F=pre(ON)+1;
end when;
  • Hope it works! I coded the same logic initially but if you could see difference between if vs when in modelica, @Markus answer is the best solution. See https://stackoverflow.com/questions/20016232/difference-between-when-and-if-in-openmodelica – Akhil Nandan Nov 09 '22 at 16:06
  • I would add that a comparison with pre(x) is an overkill, I apologize for my initial suggestion. – Akhil Nandan Nov 09 '22 at 16:10