Control of an Electric Drive in Simulink
Introduction
Simulink is a dynamic simulation environment of Matlab, in which complex physical systems can be modelled through differential equations and their behaviour can be analysed. An electric drive can also be modelled in Simulink through the equations governing its operation. However, as the electric drive consists of different subsystems, such as an electric motor, a power electronic converter, a mechanical load, each of these subsystems can be modelled separately before combining them into a single model to emulate the behaviour of a complete electric drive.
Fig. 1 shows the basic structure of an electric drive. The type of the motor determines the configuration of the power converter, the number of sensors, and the control algorithm. For example, if the motor is a dc machine, then the power converter would be a half-bridge (two-quadrant drive) or a full H-bridge (four-quadrant drive), there will be one current sensor and one dc-link voltage sensor. The position/speed of the rotor is acquired through a shaft-mounted position sensor.
Fig. 1 A typical electric drive
Using the blocks and tools offered by Simulink, the physical behaviour of the blocks shown in Fig. 1 can be emulated. The scheme of Fig. 1 in terms of Simulink blocks is shown in Fig. 2. The highlighted areas represent different subsystems of Fig. 1. The area labelled ‘Display’ shows a scope on which different quantities can be plotted as a function of time to visualize the time evolution of different variables. The subsystems are briefly described below.
Fig. 2 Simulink block diagram of a dc motor drive
The motor
In Fig. 2, a dc motor is shown as the actuator, but it can also be any other electrical machine, such as a three-phase permanent magnet synchronous motor. The details of the ‘Motor’ subsystem are shown in Fig. 3. As observed, they are the electrical and mechanical state equations of a separately excited constant flux dc motor. The applied armature voltage is the electrical actuation signal and the torque produced by the machine acts as the mechanical actuation signal. The load torque is shown as a separate input, which can be either a constant, a step function or any other load torque profile depending on the application being analysed. The outputs of the dc motor block are the armature current and the rotor mechanical position. The user can choose to have the mechanical speed as another output.
The parameters of the dc motor can be set/changed by double clicking on the block and just inputting the new values in the dialog box. Fig. 4 shows the dialog box for the dc motor parameters. Since all the parameters shown in Fig. 4 are in their standard SI units, the inputs (voltage and load torque) and the outputs of (current and angle) of the motor block should also be interpreted in their standard SI units.
Fig. 3 Simulink block implementation of the state equations of a constant flux dc motor
Fig. 4 Parameter dialog box for a constant flux dc motor
The power converter
For a dc motor drive, the power electronic converter can consist of a half-bridge or a full H-bridge depending on whether the motor is required to rotate in one direction only (half-bridge) or in both directions (full-bridge). To preserve generality of the implemented drive system, a full H-bridge is simulated to give maximum flexibility to the user. The power converter block also includes a pulse width modulation (PWM) scheme that converts the duty cycles for the two legs of the H-bridge (da and db) into pulses of varying widths. The dc-link voltage is defined as a constant input decided by the user. The modulator block’s parameter dialog box is shown in Fig. 5, which requires the user to input the switching frequency in Hz. The details of the modulator block are shown in Fig. 6.
Fig. 5 Parameter dialog box for the modulator
Fig. 6 H-bridge modulation scheme
Sensors and ADCs
In electric drives, voltage, current and position sensors are used to measure the dc-link voltage, the load currents and the shaft position respectively. Since these quantities are in the analog domain while the control, in modern electric drives, is in digital domain, an analog to digital conversion is necessary. Analog to digital converters (ADCs) do this conversion and provide the controller with measurements at a fixed sampling frequency (decided by the drive designer). The sensors measuring the voltage and current also introduce noise on the measurements, which is normally a zero-mean, constant variance white noise. In addition to the white noise on the analog signal, the noise due to the quantization effect of the ADCs impacts the measurement in the digital domain further. All these effects are simulated inside the ‘Sensing subsystem’ of Fig. 2 as detailed in Fig. 7.
For the shaft position measurement, incremental or absolute position sensors are normally used in electric drives. The resolution of the position signal available to the controller depends on the number of pulses per revolution of the incremental encoder or the bit resolution of the absolute encoder. The fixed resolution of the position sensors introduces a quantization noise on the position signal. This quantization noise is emulated in the simulation for an incremental encoder.
Fig. 7 Sensor subsystem structure
It can be noticed from Fig. 7 that there is only one input current ia but two other currents ib and ic are included to allow the user to simulate a three-phase system. For a three-phase machine, the currents ib and ic must also be added as inputs to the block rather than constants as shown in Fig. 7.
Fig. 8 shows the parameter dialog box for the sensing subsystem. The range of the current and voltage measurement must be set such that this range is not exceeded at anytime. The resolution of the ADCs is usually 12-bit in commercial electric drives but can also be 14 to 16-bit in case of high-end drives. The pulse- per-revolution (ppr) value for incremental encoders starts from as low as 12ppr for very low-cost encoders and can be in excess of 10,000ppr for devices used for precision applications.
Fig. 8 Sensor subsystem parameters
Control algorithm
The control algorithm for the electric drives is normally executed on a digital signal processor (DSP) at a fixed control execution frequency, usually at the switching frequency of the power converter. The control routines are normally written in a high-level language such as C. The block labelled ‘Control’ in Fig. 2 emulates the behaviour of a DSP that samples the input data at a fixed frequency and outputs the duty cycles for the power converter after one execution cycle. The details of the block are shown in Fig. 9.
This block consists of a Matlab s-function. S-functions (system-functions) provide a powerful mechanism for extending the capabilities of the Simulink environment. An S-function is a computer language description of a Simulink block written in MATLAB, C, C++, or Fortran. The block labelled ‘simple_control’ is like any other Simulink block but its behaviour can be fully controlled by the user by modifying the program that describes it.
Fig. 9 Details of the block labelled ‘Control’ in Fig. 2
In electric drives, the control algorithm is executed on a DSP that can be programmed in C, BASIC and assembly languages with C being the most commonly used language. The s-function feature of Simulink is therefore used to program the functionality of the block ‘simple_control’ in C.
The program describing an s-function block must follow a certain structure and must contain some pre-defined functions and definitions. To program the s-function block properly, it is recommended to start with an example code such as ‘sfuntmpl_doc.c’ or ‘sfuntmpl_basic.c’ available from Matlab and modify according to the requirements of the application. The available templates are for a level 2 s-function.
The number of inputs, outputs and parameters of the s-function block are defined inside the C program and they must match the inputs and outputs in
Simulink. The parameters passed by Simulink to the s-function are listed in the dialog box of the s-function as shown in Fig. 10. In Fig. 10, the only parameter that Simulink passes to the s-function is Ts, the sampling time. Inside the C program describing the s-function, this parameter Ts is used to define the execution sample time of the s-function block i.e. the block is executed every Ts seconds.
Since the execution time of the s-function must match the switching period of the power converter and the sampling frequency of the current, voltage and position measurements, the parameter Ts is defined as a global constant for the simulation. To change this parameter, go to: File->Model Properties->Model Properties, click on the tab Callbacks and then click InitFcn.
Fig. 10 Parameter dialog box for the s-function shown in Fig. 9
Some screenshots from the C code for the s-function ‘simple_control’ are shown below with a brief explanation of the functions, variables and parameters.
S_FUNCTION_NAME: this constant defines the name of the s-function and it must correspond to the name of the file (without the extension .c) which is also used as the s-function name in the block (see Fig. 10).
The header files, such as aux_funcs.h and Constants.h are user-defined .h files that contain definitions of functions and constants used in the code. The two header files are included as an example, others can be defined and included as necessary.
Fig. 11 Code lines defining the type of the s-function, inputs, outputs and parameters
U(element): this function macro gets a pointer to the vector of inputs from Simulink and allows to get the inputs to local variables.
NUM_INPUTS, NUM_OUTPUTS, NUM_PARAMS: these must correspond to the inputs, outputs and parameters of the s-function block in Simulink. If these constants do not match the s-function block’s conditions, Matlab will generate an error and will not compile the code for execution.
The parameters passed by Simulink to the function can be accessed as elements of the parameter array starting from 0. For example, the first parameters will be read in as: (mxGetPr(ssGetSFcnParam(S,0))[0]). The second parameter can be read in as: (mxGetPr(ssGetSFcnParam(S,0))[1]).
The global variables should be defined outside of any functions so that they’re accessible to all the functions. In Fig. 11, TS, TS_INV and thm_prev are global variables. Variables that must hold their values between executions can be declared as global, although it is not strictly necessary.
In Fig. 12, the sizes of the inputs, outputs, sample times, and other arrays are defined. It is important to set the number of sample times to 1 through the function ssSetNumSampleTimes(S, 1); as the s-function is intended to be a single-execution-rate block in our application of an electric drive. The sample time of the s-function is then set by calling ssSetSampleTime(S, 0, Ts); as shown in Fig. 13. The figure also shows the initialization conditions that the user can set, for example, assigning initial values to the global variables.
Fig. 12 Definition of the s-function code array sizes
Fig. 13 s-function sample time and initialization conditions
The outputs for the model are calculated through the function mdlOutputs shown in Fig. 14. First, the inputs from the Simulink environment are read into the local variables and arrays. The calculations necessary for the control of an electric drive are performed on these local variables before passing the outputs to Simulink. This is the function where almost all of the code related to the drive’s control should reside.
Fig. 14 Some code lines for mdlOutputs function
To complete the process of building a Simulink block from a C program, the code must be compiled into a Matlab executable file. The command used for this is mex (that stands for Matlab executable). This command must be called in the Matlab command window by ensuring that the folder in which the code files are located is selected as the ‘current folder’ in Matlab (see Fig. 15). All the .c and .h files that contain the functions used inside the main file ‘simple_control.c’ should be within the current folder and all .c source files must be included as input arguments of the mex command as shown in Fig. 15, where aux_funcs.c is the second .c source file that must be compiled along with simple_control.c. Every time anything is changed in the code (e.g. changing a parameter in a .h file or adding/deleting a line in any .c file of the project), the mex command should be repeated before expecting a change in the behaviour of the s-function block. This process is similar to compiling and building the project files in a DSP code.
Fig. 15 Instructions for compiling the C code into a mex file
A C compiler will be needed to compile the code into a mex file. There are several compilers available from Mathworks, any of these can be used for compiling the code.
Once successfully mexed, the current folder will have a mexw file with the name of the s-function e.g. simple_control.mexw64. This file will be accessed by Simulink during simulation as the contents of the S-function block shown in Fig. 9.
The task
Your task is to understand the model and the basic project you are provided. Simulate it with different conditions to enhance your understanding and be familiar to the model and the C code. Then, starting from the basic project as described above, develop the model and C code for the following objectives:
1) Armature current control of the dc motor
2) DC motor’s speed control
3) Apply different load torque profiles to test your speed control
4) DC motor’s position control (optional)
5) A model to simulate a three-phase PMSM drive (advanced)
6) Simulate vector control of a three-phase PMSM (advanced)