Main Content

Simulate and Generate Code for Speed Cruise Control System

This example shows how to simulate and generate code for a speed cruise control system. The cruise control system is modeled using Simulink® and Stateflow®.

Open the Model

open_system('plcdemo_cruise_control')

The model consists of two subsystems at the top level:

  • Controller that contains the Stateflow® chart and a PID controller implemented using Simulink® library blocks.

  • Plant that contains an implementation of the dynamics of a cruise control system.

Generate Code

To generate structured text code,

  1. In the Apps tab, click PLC Coder.

  2. In the PLC Code tab, click Settings > PLC Code Generation. Change the Target IDE to 3S CoDeSys 2.3. Click OK.

  3. Select the top level Controller block. In the PLC Code tab, click Generate PLC Code.

Alternatively, to generate code from the MATLAB command line, use the plcgeneratecode function.

generatedfiles = plcgeneratecode('plcdemo_cruise_control/Controller');