Tags and keywords
The Modelica By Example target code (using 'initial equation') is:
model SecondOrderSystem "A second order rotational system"
type Angle=Real(unit="rad");
type AngularVelocity=Real(unit="rad/s");
type Inertia=Real(unit="kg.m2");
type Stiffness=Real(unit="N.m/rad");
type Damping=Real(unit="N.m.s/rad");
parameter Inertia J1=0.4 "Moment of inertia for inertia 1";
parameter Inertia J2=1.0 "Moment of inertia for inertia 2";
parameter Stiffness c1=11 "Spring constant for spring 1";
parameter Stiffness c2=5 "Spring constant for spring 2";
parameter Damping d1=0.2 "Damping for damper 1";
parameter Damping d2=1.0 "Damping for damper 2";
Angle phi1 "Angle for inertia 1";
Angle phi2 "Angle for inertia 2";
AngularVelocity omega1 "Velocity of inertia 1";
AngularVelocity omega2 "Velocity of inertia 2";
initial equation
phi1 = 0;
phi2 = 1;
omega1 = 0;
omega2 = 0;
equation
// Equations for inertia 1
omega1 = der(phi1);
J1*der(omega1) = c1*(phi2-phi1)+d1*der(phi2-phi1);
// Equations for inertia 2
omega2 = der(phi2);
J2*der(omega2) = c1*(phi1-phi2)+d1*der(phi1-phi2)-c2*phi2-d2*der(phi2);
end SecondOrderSystem;
model SampleAndHold "Measure speed and hold"
extends BasicEquations.RotationalSMD.SecondOrderSystem;
parameter Real sample_time(unit="s")=0.125;
discrete Real omega1_measured;
equation
when sample(0,sample_time) then
omega1_measured = omega1;
end when;
end SampleAndHold;
A HACK was required to get the SysPhS trail version working, because of this limitation:
MagicDraw/Cameo could not handle export of the following expression:
when sample(0,sample_time) then
omega1_measured = omega1;
end when;
It gave this:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnull;
The full exported Modelica code but with the error replaced with the correct when/then statement is:
model SampleAndHold
SampleAndHold _SampleAndHold;
model SampleAndHold
extends SecondOrderSystem;
parameter Time sample_time(start=0.125,fixed=true);
discrete Real omega1_measured;
equation
when sample(0,sample_time) then
omega1_measured = omega1;
end when;
omega1=der(phi1);
j1*der(omega1)=c1*(phi2-phi1)+d1*der(phi2-phi1);
omega2=der(phi2);
j2*der(omega2)=c1*(phi1-phi2)+d1*der(phi1-phi2)-c2*phi2-d2*der(phi2);
end SampleAndHold;
model SecondOrderSystem
parameter Inertia j1(start=0.4,fixed=true);
parameter Inertia j2(start=1.0,fixed=true);
parameter Stiffness c1(start=11.0,fixed=true);
parameter Stiffness c2(start=5.0,fixed=true);
parameter Damping d1(start=0.2,fixed=true);
parameter Damping d2(start=1.0,fixed=true);
Angle phi1(start=0.0,fixed=true);
Angle phi2(start=1.0,fixed=true);
AngularVelocity omega1(start=0.0,fixed=true);
AngularVelocity omega2(start=0.0,fixed=true);
equation
omega1=der(phi1);
j1*der(omega1)=c1*(phi2-phi1)+d1*der(phi2-phi1);
omega2=der(phi2);
j2*der(omega2)=c1*(phi1-phi2)+d1*der(phi1-phi2)-c2*phi2-d2*der(phi2);
end SecondOrderSystem;
type Time=Real(unit="s");
type Inertia=Real(unit="kg.m2");
type Stiffness=Real(unit="N.m/rad");
type Damping=Real(unit="N.m.s/rad");
type Angle=Real(unit="rad");
type AngularVelocity=Real(unit="rad/s");
end SampleAndHold;
There's one minor concern about the Modelica export from MagicDraw/Cameo, it seems to be repeating this part of the 'equation' instead of inheriting it into
SampleAndHold
from SecondOrderSystem
omega1=der(phi1);
j1*der(omega1)=c1*(phi2-phi1)+d1*der(phi2-phi1);
omega2=der(phi2);
j2*der(omega2)=c1*(phi1-phi2)+d1*der(phi1-phi2)-c2*phi2-d2*der(phi2);
All of the rest of the Modelica By Example cases on the same page involve when/then statements, so they are skipped here.