MATLAB Examples

Troubleshoot Conversion of Simscape™ Permanent Magnet Synchronous Motor to HDL-Compatible Simulink Model

This example shows how to modify a Simscape™ plant model to generate an HDL-compatible Simulink™ model with HDL Coder™. HDL code is then generated from this Simulink model.

Contents

Introduction

The Simscape plant model is converted to an HDL-Compatible Simulink model by using the Simscape HDL Workflow Advisor. To run the Advisor, call the sschdladvisor function for the model.

The Simscape HDL Workflow Advisor generates an HDL Implementation model from which you can generate HDL code. Before you generate the implementation model, make sure that the Simscape plant model is compatible for generation of the implementation model by using the Simscape HDL Workflow Advisor. For more information, see Generate HDL Code from Simscape Models.

In some cases, the Simscape plant model might not be compatible for generation of the implementation model using the Simscape HDL Workflow Advisor. For HDL compatibility, you can modify the Simscape plant model and then run the Simscape HDL Workflow Advisor.

This example illustrates how to modify Permanent Magnet Synchronous Motor model in Simscape for HDL compatibility. The model is nonlinear and not compatible for generation of implementation model by using the Simscape HDL Workflow Advisor. This example illustrates how you can modify a continuous-time nonlinear model to a discrete-time switched linear model. The model becomes compatible for generation of the implementation model from which you can generate HDL code.

Permanent Magnet Synchronous Motor Model

This model is a physical system developed in Simscape. The model contains nonlinear elements and needs modifications for generating the implementation model.

open_system('pe_pmsm_drive')

This model shows a Permanent Magnet Synchronous Machine (PMSM) and inverter sized for use in a typical hybrid vehicle. The inverter is connected directly to the vehicle battery. You can use the model to design the PMSM controller, selecting architecture and gains to achieve performance that you want. The Gmin resistor provides a very small conductance to ground that improves the numerical properties of the model when using a variable-step solver. To see how the model works, simulate the model.

sim('pe_pmsm_drive')

This model is a continuous time system. To work with Simscape HDL Workflow Advisor, you must convert the model into a discrete system.

Convert Continuous-Time Model to Fixed-Step Discrete Model

1. Configure the solver options for HDL code generation by using a Solver Configuration block. In the block parameters of this block:

  • Select Use local solver.
  • Use Backward Euler as the Solver type.
  • Specify a discrete Sample time, Ts.

2. Modify Solver settings in Configuration Parameters dialog box, on the Solver pane:

  • Set Solver selection type to Fixed-Step.
  • Set Solver to discrete (no continuous states).
  • Set Fixed-step size (fundamental sample time) to Ts.
  • Clear Treat each discrete rate as a separate task in the section Tasking and sample time options.

3. Modify the display settings of your model in the Simulink Editor.

  • On the Display tab, set the Sample Time to All. Review the generated report for any blocks that have a sample time other than Ts, or which are running on a continuous time scale.

4. Double-click the Step block in the model and set its Sample time to Ts.

5. For faster simulation, ignore the zero-sequence parameters of the PMSM. Double-click the Permanent Magnet Synchronous Motor block and set Zero Sequence to Exclude.

The model is now a fixed-step discrete system. Simulate the model and compare signals in the Simulation Data Inspector.

Some Pulse Width Modulation (PWM) signals transition earlier or later between simulations, but they are well within the step-size of the controller. The only signals that differ beyond tolerance are the Torque Demand and Motor Torque, toward the end of simulation, within acceptable limits.

Make Three Phase PMSM Drive Compatible with HDL Advisor

To make the Simscape plant model HDL-Compatible, identify the presence of any nonlinear components or blocks in the model. Enter:

simscape.findNonlinearBlocks('pe_pmsm_drive')
Found network that contains nonlinear equations in the following blocks:
    'pe_pmsm_drive/Permanent Magnet Synchronous Motor'

The number of linear or switched linear networks in the model is 0.
The number of nonlinear networks in the model is 1.

ans =

  1×1 cell array

    {'pe_pmsm_drive/Permanent Magnet Synchronous Motor'}

The Simscape plant model has a nonlinear block, which is the PMSM block.

Remove the PMSM block, Encoder block, Gmin resistor, and, Motor & load inertia block. To delete the Simscape elements and blocks, enter:

delete_block('pe_pmsm_drive/Permanent Magnet Synchronous Motor')
delete_block('pe_pmsm_drive/Encoder')
delete_block('pe_pmsm_drive/Gmin')
delete_block('pe_pmsm_drive/Motor & load inertia')

For ease of implementation, the Simscape plant model is converted into an HDL Advisor compatible model by using a two-step process:

1. Implement an initial nonlinear Simulink model by using double-precision data type, and blocks that are not compatible with HDL coder.

2. Modify this nonlinear model to make it suitable for deployment with HDL Coder by using single-precision data types and blocks that are compatible with HDL Coder for more efficient HDL code generation.

Step 1: Convert Simscape Plant Model to Initial Simulink Model

Replace the PMSM block, Encoder block, Gmin resistor and, Motor & load inertia block with Electrical Interface Simulink block and Permanent Magnet Synchronous Motor Simulink block.

The Electrical Interface Simulink block is implemented by using Controlled Current Sources. The interface to the PMSM is isolated from the implementation.

load_system('pe_pmsm_drive_initialSL')
set_param('pe_pmsm_drive_initialSL','SimulationCommand','update')
open_system('pe_pmsm_drive_initialSL/Electrical Interface')

The PMSM Simulink block is implemented by using Electrical Equations and Mechanical Equations. For more information, see Permanent Magnet Synchronous Motor.

open_system('pe_pmsm_drive_initialSL/Permanent Magnet Synchronous Motor (Simulink)')

Save the updated model as pe_pmsm_drive_singleSL.

Step 2: Convert Simulink Machine Model to Single-Precision HDL-Compatible Simulink Model

1. Update Output data type in Signal Attributes tab of Gain and Product blocks to Single.

To search the Gain blocks in the Simulink model, enter:

load_system('pe_pmsm_drive_singleSL')
set_param('pe_pmsm_drive_singleSL','SimulationCommand','update')
blockconstant=find_system('pe_pmsm_drive_singleSL', 'blocktype', 'Gain')
blockconstant =

  2×1 cell array

    {'pe_pmsm_drive_singleSL/PMSM controller/rpm ->↵rad//s [mech.]'}
    {'pe_pmsm_drive_singleSL/rad//s2rpm'                           }

The array blockconstant stores the exact location of Gain blocks as variables. There are two Gain blocks in the Simulink model.

To set the output data type of these Gain blocks to single, enter:

set_param(blockconstant{1},'OutDataTypeStr', 'single')
set_param(blockconstant{2},'OutDataTypeStr', 'single')

To search the Product blocks in the Simulink model, enter:

find_system('pe_pmsm_drive_singleSL', 'blocktype', 'Product')
ans =

  0×1 empty cell array

There are no Product blocks in the Simulink model.

2. The Park Transform and Inverse Park Transform blocks inside the PMSM Simulink block are modified to eliminate the 6 Sine and 6 Cosine blocks.

open_system('pe_pmsm_drive_singleSL/Permanent Magnet Synchronous Motor (Simulink)')

3. Reduce the hardware required for the Park Transform block by adding Clarke Transform and Clarke to Park Angle Transform blocks.

open_system('pe_pmsm_drive_singleSL/Permanent Magnet Synchronous Motor (Simulink)/Park Transform')

4. Reduce the hardware required for the Inverse Park Transform block by adding Inverse Park to Clarke Angle Transform and Inverse Clarke Transform blocks.

open_system('pe_pmsm_drive_singleSL/Permanent Magnet Synchronous Motor (Simulink)/Inverse Park Transform')

5. Replace the Three-Phase Current Sensor Simscape block by feeding the controller with three-phase currents coming from the PMSM model.

6. Add a Digital Clock with Sample time Ts. Connect the clock to a Display block.

open_system('pe_pmsm_drive_singleSL')

Run Simscape HDL Workflow Advisor

To open the Simscape HDL Workflow Advisor, run the sschdladvisor for your model.

sschdladvisor('pe_pmsm_drive_singleSL')

To generate the implementation model, in the Simscape HDL Workflow Advisor, leave the default settings and then run the tasks. You see a link to the model in the Generate implementation model task. To open the implementation model, enter:

load_system('gmStateSpaceHDL_pe_pmsm_drive_singleSL')
open_system('gmStateSpaceHDL_pe_pmsm_drive_singleSL')

To execute the implementation model, modify the Fixed-step size to Ts/5 as Number of Iterations in sschdladvisor are 5.

Reconfigure Simulink Model for HDL Code Generation

The single precision implementation model is reconfigured for HDL code generation. To reconfigure, place the PMSM model within the HDL Subsystem. Save the model as gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL.

To open the reconfigured implementation model, enter:

load_system('gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL')
open_system('gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL')

To open the HDL Subsystem block, enter:

open_system('gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL/HDL Subsystem')

Generate HDL Code and Validation Model

Before you can generate HDL code, you must:

  1. Select the Treat each discrete rate as a separate task check box.
  2. Set Single task rate transition to error.
  3. Set Multitask rate transition to error.
set_param('gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL','EnableMultiTasking', 'on', ...
                                        'SingleTaskRateTransMsg', 'error')
set_param('gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL','EnableMultiTasking', 'on', ...
                                        'MultitaskRateTransMsg', 'error')

It is recommended to enable generation of the validation model. The validation model compares the output of the generated model after code generation and the modified Simscape plant model. To learn more, see Generated Model and Validation Model.

To save validation model generation settings on your Simulink model, run these commands:

modelname = 'gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL';
hdlset_param(modelname, 'TargetDirectory', 'C:/Temp/hdlsrc')
hdlset_param(modelname, 'GenerateValidationModel', 'on');

To generate HDL code, run this command:

makehdl('gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL/HDL Subsystem')

By default, HDL Coder generates VHDL code. To generate Verilog code, run this command:

makehdl('gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL/HDL Subsystem', 'TargetLanguage', 'Verilog')

The generated HDL code and the validation model is saved in C:\Temp\hdlsrc folder. The generated code is saved as HDL_Subsystem_tc.vhd.

Optionally, you can:

  1. Verify the model generated after HDL code generation by using the validation model. To open the validation model, click the link to gmStateSpaceHDL_pe_pmsm_drive_GenerateHDL.slx when you generate code.
  2. Deploy the generated HDL code on Speedgoat FPGA I/O boards or other target platforms. For more information, see Deploy Simscape plant models to Speedgoat FPGA IO modules.