X-Language Case 2: Water Tank Modeling Case
The model consists of three parts, which are a reservoir, a water tank and an intelligent body (PI controller). The water tank gets water from the reservoir and then the PI controller controls the water level to stabilize in a certain range. The reservoir model outputs a small amount of water when the time is less than 150s, and the output is tripled after 150s, so we can see that the water level in the reservoir is stabilized initially and then increased again after 150s and stabilized again by the PI controller.
General model structure diagram
Simulation result graph
1. Top-Level Model
X language code:
couple topmodel
import TankPI
import LiquidSourse
part:
TankPI tpi;
LiquidSource ls;
connection:
connect(ls.qOut,tpi.qIn);
end;
2. Water tank PI model
X language code:
couple TankPI
import PIcontinuousController
import Tank
part:
PIcontinuousController piContinuous;
Tank;
port:
event_input Real qIn;
equation:
connect (TankPI.qIn,tank.qIn);
connect (tank.tActuator, piContinuous.cOut};
connect (tank.tSensor, piContinuous.cIn);
end;
3. Reservoir module
X language code:
continuous Tank
parameter:
real area = 0.5;
real flowGain= 0.05;
real minV=0, maxV=10;
value:
real h = 0;
real qin;
port:
event input real qIn;//输入
event input real tActuator;//调节水流量信号
event output real tSensor; //测量水深信号
event output real qOut;输出
equation:
when receive(qIn) then
qin = qIn;
end receive;
der(h)=(qin-qOut)/area;
qOut = LimitValue(minV,maxV,-flowGain*tActuator);//LimitValue是函数
tSensor = h;
end;
4. Water module
X language code:
discrete LiquidSource
parameter real flowLevel = 0.02;
port:
event output real qOut;
state:
initial state init
when entry then
statehold(0);
end;
when timeover() then
transition(pass);
out
send(qOut,flowLevel);
end ;
end ;
state pass
when entry then
statehold(150);
end;
when timeover() then
transition(idle);
out
send(qOut,3*flowLevel);
end ;
end;
state idle
when entry then
statehold(infinity);
end;
end;
end;
5. Intelligent body controller module (PI controller)
X language code:
agent PIcontinuousController
plan plan1
value:
real cin;
real cout;
action:
cin = receive(false);
x = x + e*error/T ;
cout = K*(error + x);
error = ref - cin;
msg = message{cout};
send(msg);
end;
parameter:
real Ts=0.1;
real K=2;
real T=10;
real ref;
value:
real x;
real error = 0;
real outCtr;
action:
run(plan1);
end;