2. Discretization of classical controllers

The proportional-integral derivative (PID) controller is widely used in industry due to its high performance with most of the plants even if they are nonlinear [2]. Besides, its parameters can be tuned empirically and still achieve a good performance. Due to the complexity of the algorithm, its implementation has been limited to microcontrollers or digital signal processors [3], and furthermore, most of the researchers who are experts in control theory do not have a deep knowledge on reconfigurable logic [4, 5].

PID controller has the following form:

$$u(t) = K\_p \left[ e(t) + \frac{1}{T\_i} \int\_0^t e(\tau)d\tau + T\_d \frac{de(t)}{dt} \right] \tag{1}$$

where eðtÞ is the difference between the desired (wðtÞ) and measured (yðtÞÞ response of the system, i.e.:

$$e(t) = w(t) - y(t) \tag{2}$$

and uðtÞ is the control signal, used to control the actuator's operation to obtain a desired closed-loop performance. Finally, the parameters Kp, Ti, and Td are the proportional gain, integral, and derivative time constants, respectively. A more popular form of Eq. (1) can be obtained by using the Laplace transform as follows:

$$\mathcal{U}L(\mathbf{s}) = \left(K\_p + \frac{K\_i}{\mathbf{s}} + K\_d \mathbf{s}\right) E(\mathbf{s}),\tag{3}$$

where Ki <sup>¼</sup> Kp Ti and Kd ¼ KpTd.

microcontroller-based designs. Furthermore, it has benefits such as improved accuracy and

In the industry, the FPGA technology began to be used by the designers in areas like telecommunications, signal processing, image processing, and control systems such as robotic arms and assembly lines. Later, this technology began to be utilized in applications where the fast processing of information is desired, such as medical equipment, robotics, aeronautics, etc. [1]. Proportional integral derivative (PID) controller is one of the most commonly used design, due to its simple design and its robustness with respect to the parameter uncertainty [2–4]. They are usually used in the speed controlling applications of direct current or permanent magnet motors, through pulse-width modulation (PWM) pulses [5], output current, voltage, or frequency. It is possible to find out in the scientific literature PID implementations on hardware [6–8] whose authors have demonstrate the effectiveness of their designs; however, most of these implementations are not easy to develop and for some cases they are destined to be

In order to enhance the designer experience, the FPGA card manufacturers incorporate multicore processors equipped with flash memory into their designs for enhancing the computing capacity and data parallel processing. In this way, the controllers can implement functions that require fast processing in hardware and computationally intensive algorithms

Implementing functions into the FPGA chip, the platforms that are available on the market works with HDL codes, which decreases hardware resource use and therefore, at the same time, reduces the cost and energy consumption of the system. Moreover, these platforms

Section 2 of this chapter is related to the digital controllers, which describes the PID and other controllers in discrete form. Section 3 provides the hardware description of the PID controller in VHDL language, and finally, the fourth section provides the simulation and experimental validation, which demonstrates how to perform numerical simulations using Simulink and Modelsim. Furthermore, an experimental validation on a DC motor system is

The proportional-integral derivative (PID) controller is widely used in industry due to its high performance with most of the plants even if they are nonlinear [2]. Besides, its parameters can be tuned empirically and still achieve a good performance. Due to the complexity of the algorithm, its implementation has been limited to microcontrollers or digital signal processors [3], and furthermore, most of the researchers who are experts in control theory do not have a

manage simulators for assessment of the design before its implementation.

efficiency of the algorithms.

58 Field - Programmable Gate Array

implemented only in some FPGA families.

2. Discretization of classical controllers

deep knowledge on reconfigurable logic [4, 5].

PID controller has the following form:

into the processor.

also provided.

Eqs. (2) and (3) are time-dependent functions; therefore, they cannot be implemented directly into a digital system. In that case, it is necessary to find out the discrete form of Eq. (1) by applying numerical methods.

The proportional part of the equation does not require any additional transformation because it involves a simple multiplication, but the integral and derivative require a numerical approximation. First, the integral of the error function can be considered as the sum of the area of small rectangles of base longitude of Ts (which is commonly termed as the sampling period), and height eðkÞ at a given time instant, t ¼ kTs, i.e.:

$$\int\_{0}^{t} e(\tau)d\tau \approx T\_s \sum\_{i=1}^{k} e(i). \tag{4}$$

Similarly, the derivative term can be approximated as:

$$\frac{de(t)}{dt} \approx \frac{e(k) - e(k-1)}{T\_s} \tag{5}$$

for a given time t ¼ kTs. Now substituting Eqs. (4) and (5) into Eq. (1), it is possible to rewrite the PID controller in its discrete form as:

$$u(k) = K\_p \left\{ e(k) + \frac{T\_s}{T\_i} \sum\_{i=1}^k e(i) + \frac{T\_d}{T\_s} [e(k) - e(k-1)] \right\} \tag{6}$$

Eq. (6) provides the storage of error samples from t ¼ 0 until t ¼ kTs, which can be easily implemented by software on a microprocessor or DSP target. It is common to have kilobytes of memory in microprocessor platforms and such storage does not carry any problem; however,

when we deal with reconfigurable logic, it is of vital importance to save logic resources; therefore, a more suitable form of Eq. (6) is needed. Above is achieved by computing the differential term ΔuðkÞ instead of computing directly uðkÞ. Let us define the differential term ΔuðkÞ as:

$$
\Delta \mathfrak{u}(k) = \mathfrak{u}(k) - \mathfrak{u}(k-1),
\tag{7}
$$

and

$$u(k-1) = K\_p \left\{ e(k-1) + \frac{T\_s}{T\_i} \sum\_{i=1}^{k-1} e(i) + \frac{T\_d}{T\_s} [e(k-1) - e(k-2)] \right\} \tag{8}$$

Subtracting Eq. (8) from Eq. (6) yields:

$$
\Delta u(k) = K\_p \left\{ e(k) - e(k-1) + \frac{T\_s}{T\_i} e(k) + \frac{T\_d}{T\_s} [e(k) - 2e(k-1) + e(k-2)] \right\} \tag{9}
$$

From Eq. (7), it is possible to rewrite the control output uðkÞ in terms of uðk � 1Þ and ΔuðkÞ as: uðkÞ ¼ ΔuðkÞ þ uðk � 1Þ: (10)

It is worth to note that during the first iteration, i.e., for t ¼ kTs ¼ 0, the term uðk � 1Þ becomes zero, while for subsequent iterations, this term holds the previously computed value of uðkÞ. Finally, substituting Eq. (9) in Eq. (10), the PID control law becomes

$$u(k) = K\_p \left\{ e(k) - e(k-1) + \frac{T\_s}{T\_i} e(k) + \frac{T\_d}{T\_s} [e(k) - 2e(k-1) + e(k-2)] \right\} + u(k-1) \tag{11}$$

The common terms in Eq. (11) can be grouped so that the control law takes the form of a digital filter, i.e.:

$$u(k) = q\_0 e(k) + q\_1 e(k-1) + q\_2 e(k-2) + u(k-1) \tag{12}$$

where

$$q\_0 = K\_p \left( 1 + \frac{T\_s}{T\_i} + \frac{T\_d}{T\_s} \right)$$

$$q\_1 = -K\_p \left( 1 + 2\frac{T\_d}{T\_s} \right)$$

$$q\_2 = K\_p \frac{T\_d}{T\_s}$$

Proceeding with the same analysis, the reader could easily derive the formulas for a proportional-integral (PI) digital controller, which has the form:

$$u(t) = q\_0 e(k) + q\_1 e(k-1) + u(k-1)\tag{13}$$

where q<sup>0</sup> ¼ Kp 1 þ Ts=Ti � <sup>Þ</sup> and <sup>q</sup><sup>1</sup> ¼ �Kp. Similarly, the proportional-derivative (PD) controller may be written as:

#### Motion Control with FPGA http://dx.doi.org/10.5772/67200 61

$$u(t) = q\_0 e(k) + q\_1 e(k-1) + q\_2 e(k-2) + u(k-1)\tag{14}$$

where

when we deal with reconfigurable logic, it is of vital importance to save logic resources; therefore, a more suitable form of Eq. (6) is needed. Above is achieved by computing the differential term ΔuðkÞ instead of computing directly uðkÞ. Let us define the differential term ΔuðkÞ as:

Ti

Ti

X k�1

<sup>e</sup>ðiÞ þ Td Ts

� �

i¼1

<sup>e</sup>ðkÞ þ Td Ts

From Eq. (7), it is possible to rewrite the control output uðkÞ in terms of uðk � 1Þ and ΔuðkÞ as:

It is worth to note that during the first iteration, i.e., for t ¼ kTs ¼ 0, the term uðk � 1Þ becomes zero, while for subsequent iterations, this term holds the previously computed value of uðkÞ.

The common terms in Eq. (11) can be grouped so that the control law takes the form of a digital

Ts Ti þ Td Ts

� �

Td Ts

Td Ts � �

� <sup>Þ</sup> and <sup>q</sup><sup>1</sup> ¼ �Kp. Similarly, the proportional-derivative (PD) controller

<sup>u</sup>ð<sup>k</sup> � <sup>1</sup>Þ ¼ Kp <sup>e</sup>ð<sup>k</sup> � <sup>1</sup>Þ þ Ts

<sup>Δ</sup>uðkÞ ¼ Kp <sup>e</sup>ðkÞ � <sup>e</sup>ð<sup>k</sup> � <sup>1</sup>Þ þ Ts

Finally, substituting Eq. (9) in Eq. (10), the PID control law becomes

Ti

tional-integral (PI) digital controller, which has the form:

<sup>e</sup>ðkÞ þ Td Ts

q<sup>0</sup> ¼ Kp 1 þ

q<sup>1</sup> ¼ �Kp 1 þ 2

q<sup>2</sup> ¼ Kp

Proceeding with the same analysis, the reader could easily derive the formulas for a propor-

� �

Subtracting Eq. (8) from Eq. (6) yields:

<sup>u</sup>ðkÞ ¼ Kp <sup>e</sup>ðkÞ � <sup>e</sup>ð<sup>k</sup> � <sup>1</sup>Þ þ Ts

8 < :

and

60 Field - Programmable Gate Array

filter, i.e.:

where

where q<sup>0</sup> ¼ Kp 1 þ Ts=Ti

may be written as:

ΔuðkÞ ¼ uðkÞ � uðk � 1Þ, (7)

½eðk � 1Þ � eðk � 2Þ�

½eðkÞ � 2eðk � 1Þ þ eðk � 2Þ�

uðkÞ ¼ ΔuðkÞ þ uðk � 1Þ: (10)

½eðkÞ � 2eðk � 1Þ þ eðk � 2Þ�

uðkÞ ¼ q0eðkÞ þ q1eðk � 1Þ þ q2eðk � 2Þ þ uðk � 1Þ (12)

uðtÞ ¼ q0eðkÞ þ q1eðk � 1Þ þ uðk � 1Þ (13)

9 = ;

(8)

(9)

þ uðk � 1Þ (11)

$$q\_0 = K\_p \left(1 + \frac{T\_d}{T\_s}\right)$$

$$q\_1 = -K\_p \left(1 + 2\frac{T\_d}{T\_s}\right)$$

$$q\_2 = K\_p \frac{T\_d}{T\_s}$$

Other controllers represented in the Laplace domain can be discretized by using approximations, e.g., the Tustin formulae:

$$s = \frac{2(z-1)}{T\_s(z+1)}\tag{15}$$

For example, let us consider the following lead compensator:

$$\frac{dU(s)}{E(s)} = k \frac{s + \omega\_1}{s + \omega\_2} \tag{16}$$

Substituting Eq. (15) in Eq. (16), we obtain:

$$\frac{\Pi(z)}{E(z)} = k \frac{\frac{2(z-1)}{T\_s(z+1)} + \omega\_1}{\frac{2(z-1)}{T\_s(z+1)} + \omega\_2}$$

$$= k \frac{2(z-1) + \omega\_1 T\_s(z+1)}{2(z-1) + \omega\_2 T\_s(z+1)}$$

$$= k \frac{(\omega\_1 T\_s + 2)z + \omega\_1 T\_s - 2}{(\omega\_2 T\_s + 2)z + \omega\_2 T\_s - 2}$$

$$= k \frac{(\omega\_1 T\_s + 2)z + \omega\_1 T\_s - 2}{z + \frac{\omega\_2 T\_s - 2}{\omega\_2 T\_s + 2}}$$

$$= k \left(\frac{\omega\_1 T\_s + 2}{\omega\_2 T\_s + 2}\right) \frac{z + \frac{\omega\_1 T\_s - 2}{\omega\_1 T\_s + 2}}{z + \frac{\omega\_2 T\_s - 2}{\omega\_2 T\_s + 2}}$$

The above equation can be rewritten as:

#### 62 Field - Programmable Gate Array

$$\frac{\mathbf{U}(z)}{E(z)} = \mathbf{K} \frac{z+A}{z+B} \tag{17}$$

where

$$K = k \frac{\omega\_1 T\_s + 2}{\omega\_2 T\_s + 2}$$

$$A = \frac{\omega\_1 T\_s - 2}{\omega\_1 T\_s + 2}$$

$$B = \frac{\omega\_2 T\_s - 2}{\omega\_2 T\_s + 2}$$

From a digital point of view, Eq. (17) is still inconvenient. In order to obtain a suitable digital representation, it is necessary to represent this equation as a difference equation. This can be performed by multiplying the numerator and the denominator of the right-hand side of Eq. (17) by z�<sup>1</sup> . This is equivalent to the shifting operation in the time domain, where the signal is delayed by one sample. Thus, the lead compensator takes the following form:

$$\frac{\mathcal{U}(z)}{E(z)} = K \frac{1 + Az^{-1}}{1 + Bz^{-1}}$$

Further simplification yields

$$\mathcal{U}(z)(1+Bz^{-1}) = KE(z)(1+Az^{-1})$$

Expanding terms:

$$d\mathcal{U}(z) + B\mathcal{U}(z)z^{-1} = K\mathcal{E}(z) + K\mathcal{A}E(z)z^{-1}$$

Solving the above equation for UðzÞ we have:

$$\mathcal{U}(z) = K E(z) + K A E(z) z^{-1} - B \mathcal{U}(z) z^{-1}$$

It is well known that:

$$E(z) = e(k)\tag{18}$$

and

$$E(z)z^{-1} = \mathfrak{e}(k-1)\tag{19}$$

therefore, the discrete lead compensator filter can be expressed as:

$$u(k) = \text{Ke}(k) + \text{KAe}(k-1) - Bu(k-1) \tag{20}$$

which is quite similar to Eq. (14).
