> 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-field-oriented-control.md).

# PMSM Field Oriented Control

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

The **PMSM Field Oriented Control** model illustrates the closed-loop control of an eFPGASIM-based Permanent Magnet Synchronous Machine (PMSM), interconnected with a two-level voltage source converter built using the eHS solver. This example is flexible for different real-time simulators, efsChassisSelection drop-down list gives five options in total. This model has CPU and FPGA as two major processing units so the PMSM and the converter are simulated on FPGA and the controller is simulated on CPU in real-time. Two RT-LAB models are provided to illustrate the exchange of information between the controller and plant:

* Firmware selection for different platforms
* Exchange happening within the software
* Exchange through analog and digital I/O cards present on the simulator

An **SPS Software** equivalent model "PMSM\_FOC\_ref" is also provided along with these models for the users reference.

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

The **RT-LAB**, **RT-EVENTS**, **SPS Software** (formerly SimPowerSystems) 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-and-connections" id="setup-and-connections"></a>

This model must be run with both the Hardware Synchronized and XHP modes 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 20 microseconds and all the variables required for eFPGASIM PMSM, eHS power network and controller block are defined in the "ParameterInitialization.m" file that is automatically loaded during simulation.

* **Model with I/O configured**: DB37 pin-based analog and digital loop-back cables and a DB9 pin-based digital RS422 loop-back cable must be connected at the rear 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>

* **Model without I/O**: no cables are required.

### Firmware Selection <a href="#firmware-selection" id="firmware-selection"></a>

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

It supports five options, three for OP4510 (325T with eHSx32 and x64 and 410T with eHSx128) while two for OP5700 (eHSx64 and x128).

### Run the model without I/O configuration \[Software-in-the-loop] <a href="#rt-lab-model-without-i-o-configuration-software-in-the-loop" id="rt-lab-model-without-i-o-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**.
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 RT-LAB model master subsystem. 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 info" on the target context menu in RT-LAB and find the index for your hardware.
4. To run this example, users must ensure that the RT-LAB and eHS models are present in the same folder.
5. The RT-LAB model consists of a master subsystem (i.e. “SM\_Controller”) and a console subsystem (i.e. "SC\_Console").
   * The master subsystem has a General Framework Machine Mask interface (“PMSM”), converter interface ("eHS CPU Block") and field-oriented control of the machine.
   * The console subsystem is used to control set points, such as magnitude and frequency of source voltages, speed reference, mechanical torque, and control activation during real-time simulation. Users can also monitor voltages, currents, machine torque and speed of the machine at any point during the simulation.
6. The eHS model consists of a power network and is solved by the eHS solver present in the master subsystem of the RT-LAB model. The eHS model “PMSM\_eHS\_UB” is built by ensuring that sources, switches, and measurement conventions are 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 received 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-a63a73801b17fe0da6e6484f293fa999c3fc2125%2Fehs-model-sps-PMSMfoc-3.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 the machine interface block. Thus, we ensure a proper loading/coupling between eHS solver and eFPGASIM machine.

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

6. When the model is compiled in Simulink, the configuration of the eHS solver will be generated according to the SPS Software (formerly SimPowerSystems) circuit characteristics. Elements will be put into matrices and stored in .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.
7. The PMSM available in the eFPGASIM library can solve two motors with hardware. This machine model solves only the electrical side of the system; the mechanical model is computed in the CPU. The converter terminal voltages measured by the eHS solver will feed the machine terminals. Similarly, the machine stator currents are fed back to the converter terminals, as mentioned in point 5. This is the information needed to configure the PMSM.
   * DataIn Port Number 28
   * LoadIn Port Number 18
   * DataOut Port Number 9
8. Field Oriented Control of PMSM is implemented on the CPU, which runs at a sampling time of 50 microseconds. This has a standard outer loop speed control and inner loop current control implemented. The transformation used is quadrature transformation with d axis aligned to phase A axis (Original Park). The RTE\_SPWM block is used to generate PWM pulses for eHS converter upon activation of control from 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 output of the controller that is connected to eHS solver in RTE format.

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

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-21c0249a45f2272c8a2cc47ddf631f5542cc6ed3%2Fehs-model-sps-PMSMfoc-4%20(1).png?alt=media" alt=""><figcaption><p>variation of electromagnetic torque, currents drawn and the shaft speed of PMSM with the addition of the mechanical load.</p></figcaption></figure>

To run simulations that include eHS solver and eFPGASIM machines in real-time, create an RT-LAB project and add the RT-LAB model to 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 the target.

* During real-time execution;
  1. Start the control algorithm: set Start\_Control value to 1;
  2. Set the reference speed: set Ref\_Speed\_rad/s value between 0 and 150;
  3. Add mechanical torque to the machine: set Tmech value between 0 and 10.

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

Run this demo

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

```matlab
efsOpenExample('PMSM_FOC_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. The hardware-in-the-loop-based RT-LAB model, “PMSM\_FOC\_IOs\_rtlab”, is provided to illustrate the exchange of information between the controller and plant.

1. Click on “Run this demo” at the top of this section (if this page is displayed in the MATLAB demo browser). The RT-LAB model with the eHS solver will open automatically, as will the [SPS Software](https://opal-rt.atlassian.net/wiki/spaces/PSPS) (formerly SimPowerSystems) model describing the circuit to be solved.
2. Repeat steps 2 to 8 from the previous section 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 boards present on the 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-b978ec6c6b3bdbd65a860aacb40a5d1927dd449e%2Fehs-model-sps-PMSMfoc-5.png?alt=media" alt=""><figcaption></figcaption></figure>

4. The “Analog Output Mapping and Rescaling Block Control Panel” block in the console subsystem is customised to map measurements from the eHS converter, and signals from the CPU model to the analog out (DAC) board. In 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. The Resolver and encoder of the machine can be configured from the PMSM block in the master subsystem. Cosine, Sine and Excitation outputs of Resolver1 are mapped to the analog out board. The Encoder are mapped on the RS422 interface.

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

6. Machine currents and DC Link voltages are provided to the controller using the simulator's Analog In (ADC).
7. The “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 via the digital out board present on the simulator and fed back through the 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 a mechanical load.</em></p></figcaption></figure>

* To run simulations that include eHS solver and eFPGASIM machines in real-time, create an RT-LAB project and add the RT-LAB model to 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 the target.
* During real-time execution;
* Start the control algorithm: set the Start\_Control value to 1;
* Set the reference speed: set the Ref\_Speed\_rad/s value between 0 and 150;
* Add mechanical torque to the machine: set the Tmech value between 0 and 10.


---

# 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-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.
