Call C Function Containing Enumerated Data Type into Simulink
This example shows how to map a custom C function containing an enumerated data type into Simulink® using a C Caller block. The C Caller block maps the enumeration data type from the custom C function trafficLightControl.c
into Simulink® using enum
.
Generate Simulink Representation of Enum
Type
Run the following command before opening the model.
Simulink.importExternalCTypes('trafficLightControl.h');
The Simulink.importExternalCTypes
function parses the header file trafficLightControl.h
for the enum
type definition and generate Simulink representation of this types. By default, this import command creates the enum
type as file (enumName.m
) in the current working folder. In this example, you can observe that the import command creates TrafficLightColor.m
from the enum
type TrafficLightColor
.
Open the model containing the C Caller block.
mdl= "trafficLightCCaller.slx";
open_system(mdl)
Traffic Light Control Algorithm
The lights are defined using the enumerated data type. To implement the traffic light control algorithm, the C function (trafficLightControl.c
) uses a switch-case statement that uses the current light type and returns the next light type per general traffic rules. For example, for an input red light at t = 0.1 s, the model outputs green light at t = 0.2 s.
The prototype of the getNextTrafficLight
function is: TrafficLightColor getNextTrafficLight(TrafficLightColor current)
.
C Caller Block Configuration
In this example, the traffic light control algorithm is defined in the file trafficLightControl.c
, and the function is declared in the header file trafficLightControl.h
. The header file contains an enumeration data type, TrafficLightColor
, whose members are traffic light colors: red, yellow and green. To observe the configuration of the C Caller block that calls the trafficLightControl
function:
Open the Block Parameters dialog box and observe that the Function name field specifies
getNextTrafficLight
.In the dialog box, click
. This action opens the Configuration Parameters dialog box.
In the Configuration Parameters dialog box, under the Simulation Target pane, select the Code information tab. Observe that Include headers specifies
"trafficLightControl.h"
, and the Source files specifiestrafficLightControl.c
.
C Caller Block Parameter Settings to Map Enumerated Data Type
Observe that the block populates the data type of both the input argument current
and output out
as Enum:TrafficLightColor
. The block populates the Scope value for the input argument and function return as input
and output
, respectively. Similarly, the block populates the names of the input argument and function return to their corresponding Label fields.
Simulate and Visualize Results
Simulate the model and visualize the results. The lamp shows the changing traffic light color during the simulation. The Scope shows how the signal type changes. Observe that at t = 0.1 s, the traffic light changes from yellow to red, and at t = 0.2 s, the light changes from red to green. In this simulation, the Simulation time per wall clock is set to 0.4
. To change the simulation pacing, in the Simulink® toolstrip, on the Simulation, Debug, or Modeling tab, click the Run button arrow. Then, select Simulation Pacing.
out = sim(mdl);
See Also
C Caller | Unit Delay | Enumerated Constant