Setting boolean for a time period

269 views Asked by At

I'm writing a controller for a heatingSystem that responds on the state of the electric grid in which the house of the heatingsystem is placed. I'm trying several controllers but the one that is giving me problems is the next.

I'm trying to write a model that takes into account the amount of renewable energy that is produced in the grid. When a certain treshold for the amount of energy going externally is met the heatingsystems should switch on (as to use the energy locally). It is a form of demand side management. The problem I'm having is that the moment the treshold the heatingsytems switches on. Which in turn means that the treshold isn't met anymore because the energy is used locally. The controller is represented by a boolean. If the treshold is met it is true otherwise false.

This is the part of the code I think is problematic:

algorithm

 if CurrentGoingExternal > 5 then SwitchOn :=true;
  elseif  CurrentGoingExternal < 5 and pre(SwitchOn) then SwitchOn :=true;
  elseif  CurrentGoingExternal < 1 then SwitchOn :=false;
  else SwitchOn :=false;
  end if;

When simulating I get the error that the boolean can't figured out because meeting the treshold ensures heating switches on which ensures that the boolean becomes false within the same timeperiod. So I'm looking for a way to 'set or lock or whatever' the boolean to true from the moment the treshold is met untill the beginning of the next time period when it should be checked again. Even if within that timeperiod the treshold wouldn't be met because of the heatingsystems switched on at the beginnning.

I tried things like noEvent but that doesn't seem to work for continous problems.

Thankk for your help.

2

There are 2 answers

0
Michael Tiller On

If I understand your question correctly, I think what you really want is hysteresis. You can see a detailed discussion in this chapter of Modelica by Example.

0
sjoelund.se On

You could always sample the system:

model M
  Real currentGoingExternal = time;
  Boolean switch(start=true);
equation
  when sample(0, 0.01) then
    switch = pre(currentGoingExternal) < 0.5;
  end when;
end M;

It is also possible to set the next time to check the condition:

model M
  Real currentGoingExternal = time + (if time>0.6001 then -2*time else 0);
  Boolean switch(start=true);
  Real checkTime(start=0.01);
equation
  when currentGoingExternal < 0.5 and not pre(switch) and time>pre(checkTime) then
    switch = true;
    checkTime = time+0.1;
  elsewhen currentGoingExternal > 0.6 and pre(switch) and time>pre(checkTime) then
    switch = false;
    checkTime = time+0.1;
  end when;
end M;