> 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/machine-drive-example-models/pmsm-vdq-field-oriented-control-legacy-gen4.md).

# PMSM VdQ Field Oriented Control (Legacy Gen4)

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

The **PMSM VdQ Field Oriented Control (Gen4)** model illustrates the closed-loop control of a Permanent Magnet Synchronous Machine (PMSM) with variable DQ parameters, connected to a two-level voltage source converter developed in schematic editor software and solved using the eHS solver. This example is based on OPAL-RT's real-time simulator, which has CPU and FPGA as two major processing units: the PMSM and the converter are simulated on FPGA and the controller is simulated on CPU in real-time. One RT-LAB model and one Schematic Editor model is provided to illustrate the exchange of information between the controller and plant.

## Detailed explanation

The RT-LAB model consists of a master subsystem (i.e. "sm\_computation") and a console subsystem (i.e. "sc\_user\_interface").

* The **master subsystem** has power network interface ("eHS1") 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

The "eHS1" solver solves the power network built using schematic editor software during the real time simulation. The circuit built in schematic editor can be edited or viewed by choosing the edit option available in the solver block.

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

When the model is compiled in Simulink, the configuration of the eHS solver will be generated according to the schematic editor 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.

The motor configuration type is set as variable DQ and VdqPMSM\_BU2.json is imported. Please see the screenshot below for more detailed info about this setting. And this json file could be downloaded from Schematic Editor.

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

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 d axis aligned to phase A axis (Original Park). The modulating waves are clamped to a value between 0 and 1 using a clamping circuit. 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.

Modulating waveforms generated by the controller are converted to PWM via pwmo IO block of OPAL-RT. These pwm pulses are fed back through digital loop-back cable to fire the eHS converter internally. Here, the gating pulses are chosen to receive from Digital In card of the OPAL-RT simulator. Here, to receive the pwm in schematic editor, pin DI1 to DI6 are configured.

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

Measurements in the FPGA schematic editor model can be routed through a DAC by applying the appropriate configuration settings. Similarly, input signals can be sourced either from the CPU or by configuring the real-time system’s ADCs.

Users have the flexibility to read measurements directly within the CPU environment or to configure I/Os for interfacing with external hardware. Additionally, certain parameters can be defined either within the schematic itself or through the CPU model, depending on the application needs.

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

The OPAL-RT hardware chassis and FPGA module has to be chosen in the schematic editor prior to the building of the model. Goto: File - Simulator Setup - Choose the Simulator - Choose the Firmware. Mark the "Use this setup" to fit your simulation requirement.

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

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

RT-LAB, Schematic Editor/Unified Database and the eHS Power Electronics Toolbox must be installed in order to successfully run this example project.

<details>

<summary><strong>OP5607 and OP5707 simulators</strong></summary>

In this configuration, an external Digital Output–to–Digital Input **loopback connection** must be used to control the gate signals. Connect a loopback cable from a digital output to the corresponding digital input on the simulator. This connection is required for the simulation to run successfully.

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

</details>

<details>

<summary><strong>OP4512 and OP4610 simulators</strong></summary>

When configured in **bi-directional mode**, the OP5369 card supports an internal Digital Output–to–Digital Input loopback. This allows a Digital Input assigned to a channel to directly read the signal generated by the Digital Output on the same channel.

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

</details>

{% hint style="info" %}
If your IO Configuration contains bi-directional OP5369 card, you can always use its internal loopback capability.
{% endhint %}

## Simulation and results

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>>Schematic\_Editor\_Workflow>>Machine Drives>>PMSM VdQ Field Oriented Control (Legacy Gen4)**.
2. Compile the RT Model by clicking on **Build** in RT-LAB.
3. Once the model has been successfully built, **Load** and **Execute** the model in RT-LAB in order to begin the real-time simulation.
4. Once the real-time model and the eHS are running, observe the results of the eHS simulation by opening the **measurements** subsystem in the Simulink Console.

<details>

<summary><strong>S-function Workflow</strong></summary>

The **Mezzanine Option block** is an RT-LAB block used in the **S-Function workflow** to configure the settings of the I/O cards for each mezzanine. The available settings depend on the specific type of I/O card.

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

</details>

<details>

<summary><strong>OpalBoard workflow</strong></summary>

[OPAL-RT Board](https://www.opal-rt.com/docs/rt-lab/configure-ios/analog-digital-and-sfp-ios/opal-rt-board-workflow.md) allow the configuration of I/O card settings for each mezzanine.

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

If, for a given I/O mezzanine, this block is not used, then the corresponding settings will be kept at their default values. These default values are dependent on particularities of the bitstream, and they are not guaranteed to be exposed to the user loading their model. Further, the default values populating the Mezzanine Option block do not necessarily correspond to the default settings that will be seen if the Mezzanine Option block is not used.

Note that if a Mezzanine Option block is used, and the choice of mezzanine type is incorrect for the given slot, the model will still load and run. However, there will be errors in the load log in RT-LAB. For example, the following error is observed in the case that slot 1A on the simulator holds an OP5330-3, when we place a Mezzanine Option block that attempts to configure an OP5369 on slot 1A:

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

```
Mezzanine configurator: OP5369 Threshold configuration.
ERROR: OPAL-RT Board: The threshold control register for slot 1A is in an unexpected state. Please reprogram the bitstream.
ERROR: Mezzanine configurator: Threshold configuration failed...
```

{% endcode %}

Further, as documented in the aforementioned RT-LAB page, the block outputs its status: a 0 in the case when the block’s settings have been correctly applied, and a -1 otherwise. For example, in the below model I incorrectly configured one block by giving it the wrong I/O card, and set the other correctly. The result when we load the model reflects this:

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

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

</details>

{% hint style="info" %}
In S-Function workflow, example models support any chassis. You can contextualize your example model by selecting the chassis in the Chassis selection block.
{% endhint %}

### Setup the project

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 25 microseconds and all the variables required for controller is defined in the "ParameterInitialization.m" file that is automatically loaded during simulation.

Digital loop-back (internal or external) is used to transfer the gating pulses.

### Run the model

During real-time execution;

1. Speed reference is set to 1000rpm. It can be varied uiitl 1500rpm
2. Load Torque is 2 by default. Machine can handle 4N-m load torque
3. By default, Torque input is provided to the machine. To use speed as an input to the machine, provide '0' to the Mode.

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

The figure shows the variation of electromagnetic torque, currents drawn, and the shaft speed of PMSM with the addition of a mechanical load.

<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></figcaption></figure>


---

# 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/machine-drive-example-models/pmsm-vdq-field-oriented-control-legacy-gen4.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.
