## Building Neural Network for Virtual Position Sensing

Field-oriented control (FOC) is a widely used closed-loop (or vector) control technique that uses two-phase currents (of a balanced three-phase system) along with the real-time position feedback from a three-phase AC motor, such as a permanent magnet synchronous motor (PMSM), to control the reference currents and generate the stator phase voltages that drive the motor. Traditionally, FOC technique relies on physical sensors, such as quadrature encoders, resolvers, and Hall-effect sensors. With high accuracy and cost-effectiveness, many sensorless position estimation solutions can act as a better alternative to physical sensors.

Deep learning based position estimation is one such alternative. The example Field-Oriented Control of PMSM Using Position Estimated by Neural Network on STM32 Processor Based Boards utilizes Deep Learning Toolbox™ to train and build a neural network that acts as a virtual position sensor and estimates rotor position using artificial intelligence.

To act as a virtual position sensor, a neural network must accurately determine motor
electrical position, *θe* (along with *sinθe*, and
*cosθe*) for a set of *Vα*, *Vβ*,
*Iα*, and *Iβ* inputs.

Where:

*Vα*and*Vβ*are the voltages across*α*- and*β*-axes respectively.*Iα*and*Iβ*are the voltages across*α*- and*β*-axes respectively.*θe*is the motor electrical position.

Therefore, the neural network must be trained using *Vα*,
*Vβ*, *Iα*, *Iβ*, and
*θe* data so that it can accurately map the inputs with the outputs and act
like a virtual position sensor by estimating the rotor position using artificial
intelligence.

You can use the utility function `CreateNetwork`

to train, test, and
validate the neural network (using the *Vα*, *Vβ*,
*Iα*, *Iβ*, and *θe* data) as shown in
the example Field-Oriented Control of PMSM Using Position Estimated by Neural Network on STM32 Processor Based Boards.

While the preceding example explains about the entire workflow of creating and
implementing a virtual sensing solution based on non linear auto regressive neural network
(ARNN), this topic explains how to use the `CreateNetwork`

utility function
to learn about the training iterations, training and validation losses, along with the
mean-square error (MSE). In addition, it also shows how to visualize and understand the
architecture of the trained non linear auto-regressive neural network (ARNN).

The `CreateNetwork`

utility function uses Deep Learning Toolbox to create and train a neural network with two fully connected and one activation
layer. The utility function uses the following refined, concatenated, and processed version of
training data to train, test, and validate the network:

*traindatain**traindataout**testdatain**testdataout**validatedatain**validatedataout*

The `CreateNetwork`

utility function performs the following actions:

Creates a neural network with two fully connected and one activation layer using the function

`dlnetwork`

(Deep Learning Toolbox).Trains the neural network using the function

`trainnet`

(Deep Learning Toolbox).Tests the trained ARNN using the data (

*testdatain*and*testdataout*).Validates the ARNN using the data (

*validatedatain*and*validatedataout*).

Therefore, running the utility function creates the following object and variable:

*net*– This*dlnetwork*object stores the trained neural network (ARNN).*testdatamse*– This variable stores the mean-square error information that the utility function generated from the data (*testdatain*and*testdataout*).

The preceding figure shows that the mean square error value for the trained ARNN is small (1.4991e-05), which indicates that the network is trained successfully.

In addition, the utility function displays the training iterations in the MATLAB command window as shown in the following figure:

The utility function also tracks the losses that occur during the training and validation process and generates the following plot to visually represent this information.

The preceding figure shows the training and validation losses over the entire set of training iterations.

The following figure shows the definition of the `CreateNetwork`

utility
function:

The function uses the `dlnetwork`

object and `trainnet`

function from Deep Learning Toolbox. For more details, see ```
```

and `dlnetwork`

(Deep Learning Toolbox)
```
```

.`trainnet`

(Deep Learning Toolbox)

To access the `CreateNetwork`

utility function, click
`CreateNetwork`

.

### Visualize Trained Neural Network

You can also use the *analyzeNetwork* function from Deep Learning Toolbox to visualize and understand the architecture of the trained ARNN
(*net*). You can also use this utility function to check if the
architecture of the trained ARNN is correct and detect problems (if any).

Run the following command to open the **Deep Learning Network
Analyzer** window as shown in the figure.

analyzeNetwork(net)

For more information about this function as well as using deep learning network
analyzer, see `analyzeNetwork`

(Deep Learning Toolbox).

## See Also

`analyzeNetwork`

(Deep Learning Toolbox) | `fullyConnectedLayer`

(Deep Learning Toolbox) | `tanhLayer`

(Deep Learning Toolbox) | `dlnetwork`

(Deep Learning Toolbox) | `trainnet`

(Deep Learning Toolbox)

## Related Examples

- Field-Oriented Control of PMSM Using Position Estimated by Neural Network (Motor Control Blockset)
- Field-Oriented Control of PMSM Using Position Estimated by Neural Network on STM32 Processor Based Boards

## More About

- List of Deep Learning Layers (Deep Learning Toolbox)