> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/rt-lab/example-models/ehs-example-models/sps-software-example-models/machines-models/pmsm-spatial-harmonics-model-with-field-oriented-control.md).

# PMSM Spatial Harmonics model with Field Oriented Control

## Description <a href="#description" id="description"></a>

The **PMSM Spatial Harmonics model with Field Oriented Control** model illustrates the closed-loop control of an eFPGASIM-based Permanent Magnet Synchronous Motor (PMSM) model using the JMAG-RT 10 kW spatial harmonics data, interconnected with a two-level voltage source converter built with an eHS solver. This example is based on OPAL-RT's OP4510 real-time simulator, which has CPU and FPGA as two major processing units. Here, the PMSM and the converter are simulated on FPGA, and the controller on the CPU in real time. Two RT-LAB models are provided to show the exchange of information between the controller and plant:

* Exchange happening within the software
* Exchange through analog and digital I/O cards present on the OP4510 simulator

## Requirements <a href="#requirements" id="requirements"></a>

The **RT-LAB**, **RT-EVENTS**, **SPS Software** and **eFPGASIM toolboxes** must be installed on the host and target computers to run this example model properly. Please refer to the product documentation for details on version compatibility.

## Simulation and results

### Setup and connections <a href="#setup-parameters" id="setup-parameters"></a>

This model must be run with the Hardware Synchronized option and with the XHP mode enabled. The firmware used in this model is generated using the RT-XSG tool, and it can be modified to generate firmware to fit another I/O hardware configuration. The CPU model simulation time step is set to 25 microseconds and all the variables required for eFPGASIM PMSM, eHS power network and controller block are defined in “ParameterInitialization.m” file which gets loaded during simulation automatically.

* For the **model with I/O configured**, DB37 pin based analog and digital loopback cables and a DB9 pin-based digital RS422 loopback cable are required to be connected at the rear side of the simulator.

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-344c05cf57732fb37fc76bc42c8689ae224795d8%2Fehs-model-spsPMSMSH-1.png?alt=media" alt=""><figcaption></figcaption></figure>

* For the **model without I/O**, no cables are required.

### Run the model without IO configuration \[Software-in-the-loop] <a href="#rt-lab-model-without-io-configuration-software-in-the-loop" id="rt-lab-model-without-io-configuration-software-in-the-loop"></a>

In order to run the example, follow the procedure below:

1. If eFPGASIM is installed, add the example project to RT-LAB by selecting **File>>New>>RT-LAB Project>>eFPGASIM>>SimPowerSystems\_Workflow>>Machine Models>>PMSM\_SH**.
2. Compile the RT Model by clicking on **Build** in RT-LAB.
3. Verify that the board type is set to TE0741 in the OpCtrl block in the master subsystem of the RT-LAB model. In the “OpCtrl” block, select the “Board ID” corresponding to the hardware on which eHS is executed. To get this information, select “Tools > Get I/O infos” on the target context menu in RT-LAB and find the index of your hardware.
4. To run this example, user needs to ensure that the RT-LAB model and eHS model are present in the same folder.
5. RT Lab model consists of a master subsystem i.e. “SM\_Controller” and a console subsystem i.e. “SC\_Console”.
   1. The master subsystem has an FPGA interface of PMSM, i.e. “Dual PMSM Motors SH”, converter interface i.e. “eHS CPU Block” and Field oriented control of the machine.
   2. The **console subsystem** is used to control set points, like magnitude and frequency of source voltages, speed reference, mechanical torque, and control activation during the real-time simulation. Also, users can monitor voltages, currents, machine torque and speed of the machine at any time during the simulation.
6. **eHS model** consists of power network and is solved by the eHS solver present in the master subsystem of the RT-LAB model. eHS model “PMSM\_eHS\_UB” is built by keeping the conventions of sources, switches, and measurements compatible with solver requirements. Out of 5 voltage/current sources needed by the eHS circuit, three are generated and mapped via the RT-LAB model (Voltage Source Va, Vb and Vc) and the remaining are fed back from eFPGASIM PMSM stator terminals (Currents Ia and Ib). The convention to follow in the eHS solver “Input Settings” tab is shown below.

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-af693bdaf7e46a15be587619f9e87e4f2af2177c%2Fehs-model-spsPMSMSH-2.png?alt=media" alt=""><figcaption></figcaption></figure>

Here, the switch configuration is also shown. The user can choose to send the gating pulses from the CPU or configuring Digital In. The converter terminal voltage measurements, namely, Y01, Y02, and Y03, are routed to machine terminals internally using machine interface block. By doing it this way, we ensure a proper loading/coupling between the eHS solver and eFPGASIM machine. LoadIn port number to configure this block is 17.

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-67983e892716985d3a85120806c04ebd806217b3%2Fehs-model-spsPMSMSH-3.png?alt=media" alt=""><figcaption></figcaption></figure>

7. When the model is compiled in Simulink, the configuration of the eHS solver will be generated according to the [SPS Software](https://opal-rt.atlassian.net/wiki/spaces/PSPS) (formerly SimPowerSystems) circuit characteristics. Elements will be put into matrices and stored into .mat files that will be transferred into the solver when the model is run from the RT-LAB interface. Matrices are generated during model compilation in RT-LAB.
8. The PMSM available in the eFPGASIM library has the capability to solve two motors with OP4510 hardware. This machine model solves only the electrical side of the system, and the mechanical model is computed in the CPU. The converter terminal voltages measured from the eHS solver will feed the machine terminals and similarly, the machine stator currents are fed back to the converter terminals as mentioned in point 5. Following is the necessary information to configure PMSM.

   * DataIn Port Number 15
   * LoadIn Port Number 8
   * DataOut Port Number 9

   The spatial harmonics data used in this model is located in the model path with the name “10k\_S\_D\_I-.rtt.” This data corresponds to a 10 kW machine with three pole pairs.
9. Field-Oriented Control of PMSM is implemented on the CPU, which runs at a sampling time of 25 microseconds. This has a standard outer loop speed control and inner loop current control implemented. The transformation used is quadrature transformation with the d axis aligned to the phase A axis (Original Park). RTE\_SPWM block is used to generate PWM pulses for the eHS converter upon activation of control from the console. The modulating waves are clamped to a value between 0 and 1 using a clamping circuit. The carrier frequency is 9000 Hz and the number of events chosen is 4. Exchange of information between the controller and plant happens within the software model. Machine generated currents, speed and theta information are routed to the controller as an input, and the output of the controller is connected to the eHS solver in RTE format.

Note: To capture the data during the real-time simulation, the OpWriteFile block can be used. A mat file is available in the following folder model\_name\model\_name\_SM\_xxxx\OpREDHAWKtarget upon model reset. This .mat file can be used to visualize more accurately the outputs or to compare the results of several simulations together.

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-501c30791853dba70efb2f9facb839e4865eb354%2Fehs-model-spsPMSMSH-4.png?alt=media" alt=""><figcaption><p><em>Variation of electromagnetic torque, currents drawn and the shaft speed of PMSM with the addition of a mechanical load</em></p></figcaption></figure>

* To run simulations that include eHS solver and eFPGASIM machines in real time, create a RT-LAB project and add the RT-LAB model into the project. The eHS model should not be added to the RT-LAB project, as it does not need to be transferred to the target computer for execution. Compile the model, then assign a target node to run it in real time, then load the model onto it.
* During real time execution;
  1. To start the control algorithm, give value 1 to Start\_Control;
  2. To set the reference speed, give a value between 0 and 150 to Ref\_Speed\_rad/s;
  3. To add mechanical torque to the machine, give a value between 0 and 3 to Tmech.

### Run the model with IO configuration \[Hardware-in-the-loop] <a href="#rt-lab-model-with-io-configuration-hardware-in-the-loop" id="rt-lab-model-with-io-configuration-hardware-in-the-loop"></a>

Run this demo:

{% code overflow="wrap" lineNumbers="true" %}

```matlab
efsOpenExample('PMSM_SH_FOC_OP4510_IOs_rtlab');
```

{% endcode %}

The following procedure will help the user understand how to work with the eHS solver and eFPGASIM based PMSM with field oriented control. Hardware-in-the-loop-based RT-LAB model, “PMSM\_SH\_FOC\_OP4510\_IOs\_rtlab,” is provided to show the exchange of information between the controller and plant.

1. Click on “Run this demo” on the top of this section (if this page is displayed in the MATLAB demo browser). The RT-LAB model using the eHS solver will open automatically, as well as the SPS Software (formerly SimPowerSystems) model describing the circuit to be solved.
2. Steps 2 to 8 from the previous section must be repeated to ensure proper configuration of the demo.
3. Unlike the previous model, exchange of information between the controller and plant happens through analog and digital I/O cards present on the OP4510 simulator as shown in the figure below.

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-31e8fd5240bc5f07b8d3e674b846ce8eb8ac21dc%2Fehs-model-spsPMSMSH-5.png?alt=media" alt=""><figcaption></figcaption></figure>

4. “Analog Output Mapping and Rescaling block Control Panel” block in the console subsystem is customized to map measurements from the eHS converter as well as signals from the CPU model to the analog out (DAC) card. For this example, analog out channels Ch00 to Ch06 are fixed as the controller expects the inputs in the following order {'PMSM1.Ia', 'PMSM1.Ib', 'PMSM1.Ic', 'PMSM1.Vdc', 'Resolver1.Cosine', 'Resolver1.Sine', 'Resolver1.Excitation'}.
5. “DualAngleSensors\_wfaults” block in master subsystem configures Resolver and Encoder sensors. Cosine, Sine and Excitation outputs of resolver1 are mapped to channels 5, 6 and 7 of the analog out card and Encoder signals are looped back using RS422 connectors.
6. Machine currents and DC Link voltage are provided to the controller using Analog In (ADC) of the simulator.
7. “Encoder In” block reads the encoder output and generates speed and angle information of the machine shaft.
8. Firing pulses generated by the controller are sent out via a digital out card present on the simulator and fed back through a digital loop back cable to fire the eHS converter internally.

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-f3c1271a0103151e2faaeecc1861c4a9364dcddd%2Fehs-modelPMSMSH5.png?alt=media" alt=""><figcaption><p><em>Variation of electromagnetic torque, currents drawn, and the shaft speed of PMSM with the addition of mechanical load</em></p></figcaption></figure>

9. To run simulations that include eHS solver and eFPGASIM machines in real time, create a RT-LAB project and add the RT-LAB model into the project. The eHS model should not be added to the RT-LAB project, as it does not need to be transferred to the target computer for execution. Compile the model, then assign a target node to run it in real time, then load the model onto it.
10. During real time execution;
    1. To start the control algorithm, give value 1 to Start\_Control;
    2. To set the reference speed, give a value between 0 and 150 to Ref\_Speed\_rad/s;
    3. To add mechanical torque to the machine, give a value between 0 and 10 to Tmech.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.opal-rt.com/docs/rt-lab/example-models/ehs-example-models/sps-software-example-models/machines-models/pmsm-spatial-harmonics-model-with-field-oriented-control.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
