//50% duty cycle pulse generation
within ModelicaByExample.DiscreteBehavior.PulseGenerator;
model PulseGenerator "A model that produces pulses at a fixed interval"
type Time=Real(unit="s");
parameter Time width=100e-3;
Boolean pulse;
initialequation
pulse = false;
equation
when sample (width,width) then//form a pulse with 50% duty cycle
press = not pre (press);
endwhen;
end PulseGenerator;
//counter
within ModelicaByExample.DiscreteBehavior.Counter;
model Counter "Counting samples"
type Time = Real(unit="s");
parameter Time interval=100e-3;
Integer count;
initialequation
count = 0;
equation
whensample(interval, interval) then
count = pre(count)+1;
endwhen;
end Counter;
// sample and hold
within ModelicaByExample.DiscreteBehavior.SpeedMeasurement;
model SampleAndHold "Measure speed and hold"
extends BasicEquations.RotationalSMD.SecondOrderSystem;
parameterRealsample_time(unit="s")=0.125;
discreteReal omega1_measured;
equation
whensample(0,sample_time) then
omega1_measured = omega1;
endwhen;
end SampleAndHold;
//Bang-Bang control
within ModelicaByExample.DiscreteBehavior.Hysteresis;
model HysteresisControl "A control strategy that doesn't chatter"
type HeatCapacitance=Real(unit="J/K");
type Temperature=Real(unit="K");
type Heat=Real(unit="W");
type Mass=Real(unit="kg");
type HeatTransferCoefficient=Real(unit="W/K");
Boolean heat(start=false) "Indicates whether heater is on";
parameter HeatCapacitance C=1.0;
parameter HeatTransferCoefficient h=2.0;
parameter Heat Qcapacity=25.0;
parameter Temperature Tamb=285;
parameter Temperature Tbar=295;
Temperature T;
Heat Q;
initial equation
T = Tbar+5;
heat = false;
equation
Q = if heat then Qcapacity else 0;
C*der(T) = Q-h*(T-Tamb);
when {T>Tbar+1,T<Tbar-1} then
heat = T<Tbar;
end when;
end HysteresisControl;