案例2

X语言案例2:水箱模型案例

  模型由三个部分构成,分别是蓄水池,水箱和智能体(PI控制器)。水箱从蓄水池获得水源,然后由pi控制器控制水位稳定在一定范围。 蓄水池模型在时间小于150s的时候输出水流量较小,在150s之后输出增大为三倍,所以可以看到水池水位在经过初始的稳定后在150s左右又再次增加并再次由PI控制器控制稳定。

总模型结构图

仿真结果图

1.顶层模型

X语言代码:

couple topmodel
 import TankPI
 import LiquidSourse
part:
 TankPI tpi;
 LiquidSource ls;
connection:
 connect(ls.qOut,tpi.qIn);
end;

2.水箱PI模型

X语言代码:

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.蓄水池模块

X语言代码:

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.水源模块

X语言代码:

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.智能体控制器模块(PI控制器)

X语言代码:

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;