Hauptinhalt

Manually Add and Delete Global Variables from External C Code into Simulink

This example shows how you can manually add and delete global variables from an external C code into Simulink® . These global variables define different functionalities of the C code. To add and delete the global variables from an external C Code, use the C Caller block. By manual addition and deletion, you can ensure that the block contains only the global variables required on the block interface.

In this example, a C Caller block integrates an external C code that contains a C function representing a bang-bang controller. This controller is used in a closed-loop control system that controls boiler temperature.

Open the model named templateMdl. This model has two components:

  • Boiler Plant subsystem that represents a mathematical model of a boiler. This subsystem receives a Boolean signal from the Controller that turns the boiler on and off.

  • C Caller block that calls the C function representing a bang-bang controller. The controller turns the boiler on or off by sending a signal of value 1 or 0, respectively, to the Boiler Plant subsystem.

Initially, the C Caller block is commented out. The workflow helps you to configure the block.

open_system("templateMdl.slx"); 

Template for a model that controls a boiler temperature using bang-bang controller.

Configure C Caller Block to Add and Delete Global Variables

To add and delete global variables into the C Caller block from an external C code:

  1. Uncomment the C Caller block. Select the block and pause of the ellipsis (...) that appears. In the action bar that expands, click Uncomment displayed by Percentage sign .

  2. Change the block name to Controller. Click the C Caller annotation at the bottom of the block and enter the new name.

  3. Ensure automatic addition of global variables in the C Caller block Port specification table is disabled. In the C Caller block dialog box, click the Gear icon. This opens the Configuration Parameters dialog box. In this dialog box, search for Automatically infer global variables as function interfaces parameter and ensure that the checkbox is not selected.

  4. Specify the header and source files. In the Configuration Parameters dialog box, under the Simulation Target pane, select the Code information tab. For Include header, specify Controller.h. For Source files, specify Controller.c. Click Apply and OK to apply the changes to the model.

  5. Configure the C Caller block to call the C function that represents a bang-bang controller. In the C Caller block dialog box:

    • Click the Refresh button. A message in the Diagnostic Viewer indicates that custom code is successfully parsed.

    • Set Function name to BangBangController. The function uses these global variables — actualTemp, boilerSig, currentState, previousState, setPointTemp, and temporalCounter.

  6. Add and delete global variables from the BangBangController function. Expand the Port specifications table:

    • To add one variable at a time, click the Add button. In this example, add actualTemp, boilerSig, currentState, previousState, and setPointTemp. Alternatively, to add the global variables used by the function all at once, use the Magic wand button.

    • To delete the variables that you do not need on the block interface, click the Remove button. In this model, select the rows corresponding to the currentState and previousState variables, and delete those.

    • To expose the global variables on the C Caller block interface, change the scope values of the variables you added to the table. In the Scope column, double-click each field and from the drop-down options, select the scope value.

      • For actualTemp, select Input.

      • For boilerSig, select Output.

      • For setPointTemp, select Input.

After you complete the previous steps, your Port specification table looks like this.

Compare the Port specification table you created with this Port specification table.

Now click Apply and OK to apply the changes to the block. Verify that the variables you specify in the Port specification table appear on the C Caller block interface.

Observe that the variables specified in the Port specification table appears on the block interface.

Configure Model For Simulation

  1. To complete the modeling, connect these ports and blocks:

    • To set the temperature for the controller loop, connect the output port of the Set Point Temperature block to the setPointTemp port of the Controller block.

    • To transmit the controller signal to the boiler, connect the output port of the Controller block to the input port of the Boiler Plant block. .

    • For temperature feedback, connect the output port of the Boiler Plant block to the actualTemp port of the C Caller block. Branch this signal and connect this to the Scope block.

  2. To generate a Simulink® representation of the enum type used in the header file, use the Simulink.importExternalCTypes function. In the Simulink® Toolstrip, on the Modeling tab, in the Setup section, click the down arrow next to Model Settings and select Model Properties. Then, in the Callbacks tab, select PreLoadFcn and enter Simulink.importExternalCTypes("Controller.h").

After saving your changes, close and reopen the model. This process prompts Simulink® to parse the header file and create a Simulink® representation of the enum type that is stored in a struct in the MATLAB® base workspace.

Optionally, you can compare the model you created with the model named CCallerGlobalIOExample. This ensures that your model configuration is correct.

open_system("CCallerGlobalIOExample.slx")

Compare the model you created with this model to ensure the model configuration is correct.

Simulate Model

Simulate the model you created and observe the Scope block display. To ensure correct results, compare your Scope block display with the Scope block display mentioned here. Observe that, the initial boiler temperature is set at 15 deg C. The controller brings the boiler to reach its set point temperature of 20 deg C at approximately t = 450s. To know more about the Controller and the Boiler Plant subsystem, see Observe C Caller Block Exported Global Variables Without Model Modification.

out = sim("CCallerGlobalIOExample.slx");

See Also

Topics