Main Content

Simulation and Code Generation of Motion Instructions

The Simulink® PLC Coder™ software supports a workflow for the behavioral simulation and structured text code generation for the Rockwell Automation® RSLogix™ motion control instructions.

Workflow for Using Motion Instructions in Model

This workflow uses the Simulating and Generating Structured Text Code for Rockwell Motion Instructions example in the plccoderdemos folder. This example provides a template that you can use with motion instructions. It contains the following files:


Simulink model containing an example Stateflow® chart for modeling motion instructions.


Simulink library with a Stateflow chart that is used for modeling a real world drive (axis) with trajectories, delays, and other parameters.


MAT-file containing the bus data types for the AXIS_SERVO_DRIVE and MOTION_INSTRUCTION. The MotioncontrollerExample.slx model loads the content of the MAT-file into the workspace. If you are creating a new model you must load this MAT-file for simulation and code generation.


MATLAB® class file for implementing trapezoidal velocity profile. This is used to simulate the behavior of the Motion Axis Move (MAM) command.


Supporting file for code generation.


MATLAB enumeration class file that represents the type of motion API calls. For example, isMAM, isMSF. This file is used only during simulation.


Helper file to avoid name mangling and reserved keyword limitations.


Function that transforms the chart in the model to make it suitable for code generation.

Before you start, copy the files in the example to the current working folder.

  1. Create a Simulink model with a Stateflow chart.

  2. Load the bus data types from the MotionTypesForSim.mat file into the workspace by using the load function.

  3. Create data that represents the drive and motion instructions for the chart. For information on adding data to Stateflow charts, see Add Stateflow Data (Stateflow)

  4. Copy the drive(axis) model from the DriveLibrary.slx file into the Stateflow chart. The drive model must be copied as an atomic subchart.

    The drive logic Stateflow chart models a real world drive with parameters such as trajectory and delay. Any drive subchart has the following data:

  5. Use the Subchart Mappings dialog to map the drive subchart data store memory data with the local data of the appropriate names in the container chart. For more information, see Map Variables for Atomic Subcharts and Boxes (Stateflow). The Simulating and Generating Structured Text Code for Rockwell Motion Instructions example has the following mapping for Drive1.

  6. Use graphical functions to create motion API instructions. For example, for the Motion Servo On (MSO) instruction:

    The mapping between the inputs to the outputs is through "pass by reference".

  7. Create the controller logic in another subchart and use the motion instructions created in the previous step in the chart. Controller1 in the example has the following Stateflow chart.

Simulation of the Motion API Model

You can run simulation on the model containing the motion instructions and see the state changes the controller chart and the Drive subchart. You can also log the local data of the chart such as AXIS and the MOTION_INSTRUCTION variables For more information, see Configure States and Data for Logging (Stateflow).

At the end of simulation, the logged signals are captured in the base workspace as a variable called logsout. This can be imported into Simulation Data Inspector.

Structured Text Code Generation

Use the plcgeneratemotionapicode function to prepare the model for code generation and generate structured text code. The plcgeneratemotionapicode takes the full path name of subsystem containing the original chart as an input and creates a new model from which structured text code can be generated.

Adding Support for Other Motion Instructions

The plcdemo_motion_api_rockwell example has support for only the following motion instructions:

  • MAM

  • MAS

  • MSF

  • MSO

To use other Rockwell Automation RSLogix motion instructions in the model (For example, Motion Axis Jog (MAJ)), you must perform the following steps:

  1. Because the MAJ instruction is similar to MAM instruction, create a bus for MAJ with elements similar to that of MAM.

  2. Update the MotionTypesForSim.mat file with the new definitions for MAJDATA and AXIS_SERVO_DRIVE.

  3. In the Stateflow chart, create a graphical function representing MAJ (similar to MAM). Assign the appropriate inputs and outputs.

  4. Create single transition with commands to set the output values.

  5. Remove the transition commands and copy the graphical function to the MotionApiStubs.slx.

  6. Update the functionName variable in the getDriveTemplateNames.m file to include MAJ.

  7. Update the DriveLibrary.slx file to respond to MAJ calls during simulation.

    • Create isMAJ graphical function (similar to isMAM).

    • Update the Drive subchart to respond to MAJ by implementing required transitions etc (similar to MAM as shown).

  8. Create or update the controller logic as required. Create a new state and add MAJ instruction to it (similar to the MAM )

  9. Perform simulation and generate code using the steps described earlier.