This section describes an example that illustrates how to link a simulation program to the BCVTB in such a way that they exchange data at a fixed time step through a BSD socket connection. We will consider a system with two rooms. Each room has a heater that is controlled by a proportional controller. We will implement the simulation program for the two rooms in a C program, and we will link it to a controller that is implemented in Ptolemy II.

Let $k\in \mathrm{\{1,\; 2,\; ...\}}$ denote equally spaced time steps and let $i\in \mathrm{\{1,\; 2\}}$ denote the number of the room. For the $k$-th time step and the room number $i$, let ${T}^{i}\left(k\right)$ denote the room temperature and let ${u}^{i}\left(k\right)$ denote the control signal for the heater. The room temperature is governed by

${T}^{i}(k+1)={T}^{i}\left(k\right)+\genfrac{}{}{0.1ex}{}{\Delta t}{{C}^{i}}\phantom{\rule{0.167em}{0ex}}{\left(UA\right)}^{i}\phantom{\rule{0.167em}{0ex}}({T}_{out}-{T}^{i}(k\left)\right)+\genfrac{}{}{0.1ex}{}{\Delta t}{{C}^{i}}\phantom{\rule{0.167em}{0ex}}{Q}_{0}^{i}\phantom{\rule{0.167em}{0ex}}{u}^{i}\left(k\right),$

with initial conditions ${T}^{i}\left(0\right)={T}_{0}^{i},$ where $\mathrm{\Delta \; t}$ is the time interval, ${C}^{i}$ is the room thermal capacity, ${\mathrm{(UA)}}^{i}$ is the room heat loss coefficient, ${T}_{\mathrm{out}}$ is the outside temperature, ${Q}_{0}^{i}$ is the nominal capacity of the heater and ${T}_{0}^{i}$ is the initial temperature. In these equations, we assumed that the communication time step is small enough to be used as the integration time step. If this is not the case, we could use a different integration time step and synchronize the integration time step with the communication time step.

The governing equation for the control signal is ${u}^{i}(k+1)=\mathrm{min}(1,\phantom{\rule{0.167em}{0ex}}\mathrm{max}(0,\phantom{\rule{0.167em}{0ex}}{\gamma}^{i}\phantom{\rule{0.167em}{0ex}}({T}_{set}^{i}-{T}^{i}(k\left)\right)\left)\right),$ where ${\gamma}^{i}>0$ is the proportional gain, ${T}_{\mathrm{set}}^{i}$ is the set point temperature and the $\mathrm{min}(\cdot ,\cdot )$ and $\mathrm{max}(\cdot ,\cdot )$ functions limit the control signal between $0$ and $1$.

Figure 7.1 shows a source code snippet of the implemented client. This source code can be found in the directory `bcvtb/examples/c-room`

. A similar implementation in Fortran 90 can be found in the directory `bcvtb/examples/f90-room`

.

To compile the source code, type on a command shell

cd bcvtb/examples/c-room ant all

This will invoke the ant build system, which calls the file `bcvtb/examples/c-room/build.xml`

that contains the compiler and linker commands.

To simulate this example, we implemented the controller directly in the middleware, using actors from the Ptolemy II library. However, the controller could as well be implemented in Modelica, MATLAB, Simulink or in a user written program that communicates through a BSD socket similarly to the C client above. Figure 7.2 shows the system diagram with the actor for the controller and the actor that interfaces the simulation program.