2017-03-15 1 views
1

私は01:00 10回のビープ音(1秒に1回)毎朝を生成目覚まし時計モデル化したいと仮定:上記のモデルでは時間イベントの生成を制限するためにModelicaのwhenステートメントをゲートするにはどうすればよいですか?

model DailyBeep 
    import SI = Modelica.SIunits; 
    import Modelica.SIunits.Conversions.*; 

    constant SI.Time oneDay = 86459.17808 "one day in seconds"; 

    parameter SI.Time startTime = from_hour(1) "time to start beeping"; 
    parameter Real numBeeps  = 10   "the number of beeps to make"; 

    Boolean beeping  "true when we should beep every second"; 
    Real beepsRemaining "the number of beeps remaining"; 
initial equation 
    beeping  = false; 
    beepsRemaining = numBeeps; 
algorithm 
    when sample(startTime, oneDay) then 
    beeping := true; 
    beepsRemaining := numBeeps; 
    end when "starts the beeping state"; 

    when beeping and sample(0, 1) then 
    if beepsRemaining == 0 then 
     beeping := false; 
    else 
     // beep() // makes a sound 
     beepsRemaining := beepsRemaining - 1; 
    end if; 
    end when "beep every second, only when beeping is enabled"; 
end DailyBeep; 

を、私は「ビープ音」を毎秒(生産しますsample(0,1)beepingが真である限りです。数日間シミュレーションを実行すると、毎朝01:00にシミュレーションで10回のイベントが発生することが予想されます。

しかし、OpenModelicaで3600秒に設定してシミュレーションを実行すると、1秒に1回、3600回を超えるイベントが発生します。

### STATISTICS ### 
    events 
     3601 time events 

アラームクロックを数ヶ月にわたってシミュレートする場合、これはうまくスケールされません。モデムのwhenステートメントをゲートする方法はありますか?この場合、whenの代わりに何か別のものを使用すべきですか?

答えて

3

Modelicaは、時間イベントを使用すると常に高いレートでサンプリングしますが、状態イベントを使用してその周りを回る可能性があります。

私はModelica 3.xのクロックと新しい同期機能を使用する別の方法があると思うかもしれません。

+0

パターン 'を使用することも可能です。サンプル(...)then nextTime:= time + 1.0; numTicks:= 10;終了時。 time> = nextTimeかつnumTicks> 0の場合、numTicks:= pre(numTicks)-1; nextTime:= time + 1.0;いつ終わるか。 Modelicaツールが状態イベントではなくタイムイベントに最適化する可能性があります(または、おそらく 'time> = nextTimeを使用してからnumTicks:= pre(numTicks)-1; nextTime:= numTicks> 0 then time +1.0 else pre(nextTime);終了時; '代わりに)。 –

+0

@Adrian私は時計について読んできました。私が今まで考え出してきた唯一のコンセプトは、単一の時計を持ち、ビープ音の変化として間隔を調整することです。 – watkipet

2

これは、Modelicaツールで実行される最適化によって異なります。 Dymolaはこれに対して12回のタイムイベントしか生成しません(10回から0回までは11回のタイムイベントを生成する必要があります - 10回だけではなく、12回目をチェックしていません)。

when time>=nextTime then 
    if beepsRemaining>0 then 
     nextTime:=nextTime+1.0; 
     beepsRemaining:=pre(beepsRemaining)-1; 
    end if; 
    end when; 

すべてのModelicaツールがそれを処理する私は信じている:イベントが存在するときにtime>=nextTime使用することができますが、代わりにtime>=nextTime and numTicks>0を簡単かつ安全であるとsjoelund.se @によって示されるように

nextTimeを設定します。

真実を比較するため、beepsRemaining==0はModelicaが正しくありません。私は整数を使用し、beepsRemaining<=0(または上記の逆数)でテストを置き換えることをお勧めします。

oneDayの値は私には奇妙に見えます。私はfrom_hour(24)を使用します。

関連する問題