Main Content

Temperature Control System Communicating with Messages

This example shows how to use message communication within a distributed system where the controller manages multiple incoming messages from different senders in an iterative manner and sends messages to communicate commands to the different receivers. The example uses a model of a control system managing temperatures in two different rooms with separate thermostats. The algorithmic modeling of the components basically follows the Stateflow exampleModel Bang-Bang Temperature Control System(Stateflow), while the communication between the components is modeled using Simulink® messages and SimEvents® blocks. The referenced models Controller and Thermometer, colored in blue, are software components expected to generate standalone code, while the other components model the environment.

Model Overview

The model containsNidentical rooms with thermostats (modeled by multi-instanced model blocks), whereN=2is a Simulink parameter defined in the Simulink data dictionary fileslddMsg.sldd, which is linked to the top model and referenced models. Each room can set the setpoint temperature separately. The thermostats for the rooms are remotely controlled by a controller, using the same control algorithm for all thermostats.

The thermostats send temperature messages to the controller every0.2秒,而控制器nds command messages to the thermostats to command heating on or off every1second. AnEntity Output Switch(SimEvents)block routes the messages from the controller to one of the thermostats according to the message bus data fielddeviceID。(The bus is also defined in the data dictionary and shared across all models.) AMessage Mergeblock routes the messages from different thermostats to the controller.

Initially, the model is configured using bus types with floating-point type fields that are defined in the data dictionary fileslddMsg.sldd。To switch from floating-point to fixed-point, change the data dictionary for the model fromslddMsg.slddtoslddMsgFixpt.sldd。On theModelingtab, go toModel Settings>Model Properties>External Data

The model is easily scalable by changing the value ofN, adding more instances of the model block, and increasing the port number of the Entity Output Switch and Message Merge blocks. Each Thermometer model inside the Room model has anIDargument, which must be set with a value that matches the output port index of the Entity Output Switch.

AQueueblock (FIFO, overwriting type queue) in front of the controller model buffers the message, which models the queue inside the message middleware of the controller. Here, a capacity ofN是配不上的队列覆盖老吗st messages with the new ones from each sender, assuming no message loss in transit. A capacity of5*Nis needed for the worst scenario with message loss, where5is the sample time of the controller divided by the sample time of the thermostats. In addition, a queue in front of each thermostat with capacity 1 is automatically inserted and shows a badge icon of sandwiched "1", because a capacity-1 queue is automatically inserted if you do not intentionally place a Queue block. SeeUse a Queue Block to Manage Messages

To view the sequences of messages and events, on the Simulink Toolstrip, on theSimulationtab, in theReview Resultssection, clickSequence Viewer。看到Sequence Viewer

Controller Model

In the Controller model, the Update Temperature subsystem connected with the Inport block first receives all messages containing temperature information from the rooms. The subsystem stores that information in two vectors of temperature setpoint and current temperature. Then, the For Each subsystem reads the vectors, processes the signals, and sends out the control messages via the Simulink functionsendCtrlMsg

The Update Temperature subsystem is a do-while subsystem whose termination condition port is fed by the Receive block's status port, meaning it runs until cannot receive any more messages from the external queue (in the top model). The message data is ofDeviceMsgbus type, which is defined in the data dictionary file, and has two fields:temperaturedeviceID。Thus, when the output signal of the Receive block propagates to the enabled subsystem whose enable port is connected to the Receive block's status port, the Bus Selector block decomposes the signal intodeviceID,temperature, andsetpointsignals. Thesetpointtemperaturesignals are then assigned to the respective vector elements associated with thedeviceID。Finally, the vectors maintained by the Unit Delay blocks are output as signals by the enabled subsystem and Update Temperature subsystem to the For Each subsystem.

The For Each subsystem, whose block settings are shown above, is set to haveNiterations, and both of its input ports are partitioned. A Stateflow chart models the Bang-Bang Controller, which resembles the one explained inModel Bang-Bang Temperature Control System(Stateflow)。Its output port outputs a Boolean signal indicating whether or not to turn on heating. This signal is packed into a nonvirtual signal at the Bus Creator block with the deviceID (one-based) from the iteration number (zero-based). The signal is given to the Function Caller block, which calls the Simulink FunctionSendCtrlMsg(placed outside the For Each Subsystem) to send the message out from the model.

Room Model

In the Room model, the Thermostat subsystem interacts with the environment. The Thermostat has two inputs, the control message and the setpoint temperature signal, and two outputs, the heating rate and the temperature message to the controller. The Gain and Integrator blocks simulate the physics of the room heating or cooling with respect to the heating rate and room size.

The Thermostat subsystem is composed of a Thermometer Sensor subsystem, a Thermometer Software model block, and a Temperature Actuator subsystem. The Thermometer Software model block periodically receives a control message from the controller and unpacks it into a Boolean command (on/off) to the Temperature Actuator subsystem, which determines the heating rate. The Thermometer Software also inputs a temperature signal from Thermometer Sensor subsystem, which detects the analog temperature, converts it to a digital signal, and sends the message back to the controller.

Thermometer Model

In the Thermometer model, a Receive block connects with the Inport block to receive a control message from the external queue at each time step. The message data is decomposed into a command signal, which is an output, and a deviceID signal, which must match the ID argument of the model. The ID argument should be set in the model block in the top model. The Receive block's initial value is set to a MATLAB® structure with thedeviceIDfield equal to the model argumentID和thecommandfield taking a value offalse。Meanwhile, the signals of digital temperature, setpoint, and deviceID are packed into a nonvirtual bus signal and sent as a message to the Outport block.

Code Generation

For code generation and deployment, the referenced models Controller and Thermometer (colored blue) can generate standalone embedded-target C++ code and can be deployed separately on embedded devices with message middleware. For more information, seeGenerate C++ Messages to Communicate Data Between Simulink and an Operating System or Middleware(Embedded Coder); see alsoModel Message-Based Communication Integrated with POSIX Message Queues(Embedded Coder)

Message root-level Inport/Outport does not support C code generation and code customization. If you need to generate C code and call into a message middleware API for sending messages, consider moving the Simulink functionsendCtrlMsgto the top model and customizing the name properly so that the referenced model generates a customizable call site of an external function. Similarly, for the receive side, consider using a Simulink function containing a Receive block in the top model and using a Function Caller block in the referenced model to replace the Receive block.

看到Also

|||||