Design of a Mathematical Unit in FPGA for the Implementation of the Control of a Magnetic Levitation System

This paper presents the design and implementation of an automatically generated mathematical unit, from a program developed in Java that describes the VHDL circuit, ready to be synthesized with the Xilinx ISE tool. The core contains diverse complex operations such as mathematical functions including sine and cosine, among others. The proposed unit is used to synthesize a sliding mode controller for a magnetic levitation system. This kind of systems is used in industrial applications requiring high level of mathematical calculations in small time periods. The core is designed to calculate trigonometric and arithmetic operations in such a way that each function is performed in a clock cycle. In this paper, the results of the mathematical core are shown in terms of implementation, utilization, and application to control a magnetic levitation system.


Introduction
Mathematical control equations in an FPGA reconfigurable device is an important aspect in the design of arithmetic blocks when implementing control algorithms [1].A wellknown method utilized in the implementation of arithmetic operations in FPGAs is based upon the coordinate rotation digital computer (CORDIC) algorithm [2][3][4][5][6] which has become the standard solution for the implementation of complex operations in FPGAs.
This paper proposes the design of a mathematical unit dedicated to the implementation of control algorithms that involve several sequences of complex mathematical functions calculations.
Traditionally, the development of complex arithmetic functions in FPGA devices has resulted in difficulties to implement such operations.Therefore, the elaboration of mathematical operations in Xilinx FPGAs is proposed through the core generator [7].The objective of this paper is to explain the development of a core capable of performing mathematical operations such as trigonometric functions in a clock cycle, using an alternative method of the core generator suggested by the manufacturer.
In order to construct such cores, the architecture of the mathematical unit is established by the user with Java software, in which the input and output parameters are defined as well as the functions needed to perform the desired control algorithm.This tool facilitates the users' implementation of mathematical blocks in FPGAs, simplifying the flow design to the adjustment of the interconnection of the required blocks in the main program described in VHDL.This reduces the designer's workload during the implementation stage of control algorithms.The tool is capable of implementing 16 different types of mathematical functions which may be described according to the required algorithm.The maximum number of functions that can be implemented depends on the available resources of the FPGA.
When the VHDL code generator is activated, a window initially appears, asking the characteristic of the inputoutput variables.The longitude of the input data indicating integer and decimal bits must be specified.At this point, selection of the functions to be implemented according to the control algorithm is made, and finally the code generator creates a file containing the description of each block in VHDL language ready to be synthesized by the Xilinx ISE tool [8].Each function might be an independent module that can be interconnected with the rest of the blocks in order to represent the equations that describe the desired algorithm.Trigonometric functions are implemented in the embedded memory of the FPGA.The advantage of solving complex functions with preloaded tables can be clearly seen in computing time, simplifying the execution of a mathematical function to the transfer of data from memory to the accumulator register.
The control algorithm of a magnetic elevation system is presented in order to provide an implementation study for the proposed mathematical unit.This system deals with the levitation of steel objects aided by a controlled electromagnetic force that is equal and opposed to the gravitational force acting on the steel object.This type of control is actually applied in commercial magnetic levitation (MAGLEV) trains [9].

Description of the Mathematical Unit
The mathematical unit has been developed with a Java program that generates blocks of mathematical functions in VHDL.The complete system is composed of 5 main modules, as shown in Figure 1, (1) VHDL code generator, (2) RAM or ROM memory block for mathematical operations, (3) control unit for instructions, (4) accumulator registers for results, and (5) magnetic levitation system.
The mathematical unit was functionally designed in VHDL code with instantiation of RAM or ROM memories that were created through the program generator functions, elaborated in Java language, especially for this job which is described in Section 2.1.The memories were programmed with input parameters assigned by the user, allowing the data input to have a suitable format according to the designers needs.

VHDL Code Generator
As previously mentioned, the proposed mathematical unit is capable of solving trigonometric functions in a clock cycle by using preestablished data tables.To accomplish this, a program was developed in Java language that calculates the values of the trigonometric or mathematical functions within the range of values defined by the user, followed by the creation of tables with the calculated values, and uses a RAM or ROM memory to store these data values and then to translate them into the description hardware language VHDL.The program defines the architecture, entity, and process which automatically adds to the libraries, reducing user time and the definition of each block to only the definition of ranges and precise values of input and output data in its integer and decimal parts.
The software function generator reduces the computational burden to the FPGA by using a standard computer to calculate the possible results of mathematical functions that require only one parameter in the instantiation of a RAM or ROM memory.
The program creates the desired function as an entity in VHDL with an input and an output of the selected size.The VHDL has syntax standards, which are contained in the libraries.The program generates the necessary lines for use by the corresponding libraries.The entity block is also created at the same time, along with the input data, ready to be synthesized by the Xilinx ISE simulator.The list of mathematical function values is calculated with the program code generator.
An example is shown in Table 1.This table corresponds to the calculation of the cosine function, which is implemented in a ROM memory of 16 bits × 1024 lines.The address bus is identified with the letters a9 to a0, where a0 is the least significant bit.Before executing the program generating code, the data format specifies the required bits for the integer part and the decimal part.The value of the angle is defined in radians at an interval from 0 to 3.99.In the example in Table 2, this quantity may be defined by the user in the program generator.The calculation of the cosine function is made considering the bits from a6 to a0 as the decimals of the parameter and the bits from a9 to a7 as the integer part.The result of the function is located in the data bus where d0 is the sign bit, d1 to d13 is the decimal part, and from d14 to d16 is the integer part of the data.
The following program code fragment is an example of the result of the VHDL mathematical functions, where numbers 9 and 16 are the defining entrance parameters that were programmed: The critical functions programmed in C language turned a floating chain of bits as well as the same operation in inverse form.An example of the code follows: In order to complete the conversion of the floating value to chain of bits, we followed a 2-stage process; firstly, the whole part becomes a chain of bits, and after the part decimal is turned into a chain of bits.Later they are united in a single decimal number in binary code.
The conversion process starts with the whole part of the function; this requires rounding the smallest number (when positive) or rounding the largest number (when negative).Using the "ceil" function one can obtain the rounding of the number and using the "floor" function one can round the whole part.Since the conversion algorithm uses positive numbers, the "abs" function is used to take the absolute value from the rounded number.The variable "res" keeps the final result from the conversion.
The code generator program allows the usage of RAM or ROM memories and selection of these will depend on the application required.For example, when using ROM memories, these are implemented with the internal resources of the FPGA augmenting the utilization of the circuit; the flexibility of using these memories is their facility to adjust the size of the word and required address for the precise calculations that will be stored in them.When RAM memories are selected, as these are embedded, they do not impact the available resources in the FPGA, allowing a huge logic capacity for other circuit implementation, the disadvantage that it is limited to the implementation of variable arrays in the word longitude and address bus.
With the objective of observing the units behavior during the calculation of different trigonometric functions, a sequence of operations was established for the resolution of the functions with different angles.The obtained results are shown in Table 2.The first column corresponds to the evaluation angles; the second column is equivalent to the first column in radians; the third column shows the results of the cosine function obtained with the mathematical unit presented; the fourth column has the results obtained with Matlab; the last column presents the difference between the value calculated with Matlab and the value obtained with the mathematical unit.
It is important to emphasize that the mathematical function sequence can be carried out to form complete equations which are calculated and stored in a ROM or RAM memory, to be used later in the implementation of individual

Description of the Mathematical Unit Operation
The mathematical unit was implemented in a FPGA Virtex II.The results of the utilization are shown in Table 3.The utilization of slices, LUTs, and total equivalent gate (TEG) is presented in independent columns.The column "sel" refers to the instruction code that mathematical unit executes.This makes 16 trigonometric and mathematical functions which may be selected through a control word of 4 bits.The total circuit utilization is 95% of the available slices in the FPGA and 74% of LUTs, being equivalent in TEG to 58 157 out of 1 000 000 of the available total on the Xilinx Virtex II.

Application to the Control of a Magnetic Levitation System
In order to prove the capacities of the mathematical unit, a sliding mode controller [10] was used to regulate a magnetic levitation system.This type of system is used in several applications such as frictionless bearings [11], highspeed MAGLEV passenger trains [12], wind tunnel levitation models [13], molten metal levitation [14], and the levitation of metal slabs during industrial manufacturing process [15].These systems have natural unstable nonlinear dynamics requiring closed-loop control designs for stabilization.Several control techniques have been applied to the stabilization of MAGLEV systems, such as I/O linearization [16,17], backstepping [18], and sliding mode control [19], among others.The sliding mode control [10] has been extensively used in electromechanical systems due to its robustness to unknown bounded perturbations.Another characteristic of sliding mode control is the discontinuous nature of its control signal which switches from two states.This is an advantage because it avoids using pulse width modulation (PWM).The drawback of sliding mode control is that the switching signal has an infinite frequency and when implemented with common switching power devices with a frequency around 20 KHz, produces an output phenomenon called chattering; small oscillations around the set-point.Nowadays, there are power devices available with a switching frequency of at least 150 KHz, which common digital signal processor boards cannot support.To take full advantage of such switching devices, one needs high speed digital media such as FPGAs that can support and match high switching frequencies.In this case, the chattering problem is considerably reduced.

Mathematical Model and Problem Formulation for the MAGLEV System
Figure 2 shows an schematic diagram of a MAGLEV system.The mathematical model of the MAGLEV system is given in the following equations [17]: with state vector defined as x = (x 1 , x 2 , x 3 ) T , where x 1 represents the position of the steel ball of mass M which is positively increasing in the downward position, x 2 is the velocity of the steel ball, x 3 is the current through the coil, v is the input voltage applied to the coil, and y is the output of the system.The constant parameters are the resistance of the coil denoted by R, the inductance denoted by L, g which is the gravitational constant and is considered as a known perturbation term, finally k m which is the magnetic constant of the electromagnet.The control problem is based upon forcing the output y = x 1 to track a reference signal q(w).Therefore, one can consider the following output tracking error: e = x 1 − q(w). (2)

Sliding Mode Output Regulation for the MAGLEV System
The applied control design methodology is a combination of two important control techniques, output regulation theory (ORT) [20] and sliding mode control (SMC) [10].The advantage of using ORT is that it plays an important role in trajectory output tracking and in the rejection of known disturbances.ORT deals with the problem of finding a control law such that the output of the controlled system can asymptotically track a signal generated by an exosystem and at the same time reject perturbations possible generated by the same exosystem.The nature of the control signal is continuous or smooth, and in this case PWM is required for implementation.When ORT is combined with SMC one obtains a control methodology commonly known as sliding mode output regulation (SMOR) [10] resulting in robust protection against unknown perturbations and avoids the use of PWM as just mentioned before.The exosystem is proposed as follows: with initial conditions w 1 (0) = w 2 (0) = a, w 3 (0) = b, and w 4 (0) = c, such that, the exosystem generates a reference output tracking signal for an MAGLEV system, which is chosen as q(w) = w 1 + w 3 , that is, a sinusoidal shape signal with frequency α, peak value of √ 2a, and a dc bias value b.

International Journal of Reconfigurable Computing
Exosystem SMOR controller The reference signal is chosen in this way in order to test some trigonometric functions of the mathematical unit.In this case, the steel ball will move upward and downward as dictated by the amplitude and frequency of the reference signal.
What follows is the ideal steady state of operation for the MAGLEV system, that is, π(w) = (π 1 (w), π 2 (w), π this state is such that, if the original states of the MAGLEV, x = (x 1 , x 2 , x 3 ) T , are driven to the ideal steady-state, then the output tracking error will asymptotically decay to zero, accomplishing the control objective.In order to find the steady state of operation one must solve the well-known Francis-Isidori-Byrnes [20] equations.In the case of the MAGLEV system results are as follows: with s(w) = (−αw 2 , αw 1 , 0, 0) T .Note that the ideal steadystate value for e is obviously zero.Using this fact, one easily calculates from (6) π 1 (w) = w 1 + w 3 , replacing π 1 (w) in ( 4) one finds that π 2 (w) = −αw 2 .Substituting π 2 (w) in ( 5), one reckons the expression for π 3 (w) as π 3 (w) = (w 1 + w 3 ) (M/k m )(w 4 + α 2 w 1 ).The c(w) variable represents the steady-state value for the control input v, but it is not neccesary to calculate such expression when using SMC actions.Let us define the steady-state error as The dynamic equation for (7) with tracking error e (2) can be obtained from (1) as Now, one defines the sliding function and control as where sign is the typical signum function, with Making use of a rigorous stability analysis by means of a Lyapunov function [10], one finds a stability condition for gain k: where v eq (z, w) is a solution of σ = 0, namely, If condition ( 12) is satisfied then σ = 0 is guaranteed, implying that z 3 can be calculated from (11) as That is, the differential equation ( 10) is unnecessary as its solution ( 14) is now known.The remaining differential equations for z 1 and z 2 are obtained by replacing ( 14) in ( 8) and ( 9).This residual dynamic is known as the sliding mode dynamic.This dynamic is made stable by the proper choice of Σ 1 .An easy way to stabilize the sliding mode dynamic is by using its linear approximation at the origin as shown here: (15) with A 11 and A 12 being as proper dimensions matrices obtained from linear approximation, and where H.O.T. stands for higher-order terms, that vanish at the origin.Now, Σ 1 is chosen so that the matrix (A negative real part poles.In this case lim t → ∞ z 1 (t) = 0 and as a consequence by ( 14) z 3 tends to zero too.By continuity, using π 1 (w) = w 1 + w 3 one finally finds that the output tracking error e asymptotically tends to zero, satisfying the control objective.Finally a closed-loop block diagram is presented in Figure 3.

Control Algorithm Implementation Results
The control algorithm was tested using an FPGA virtexII XE2V1000-4fg256, and the plant dynamic was simulated using the DSP board DS1104 from DSPACE.This type of simulation is known as hardware-in-the-Loop (HIL) simulation [21].HIL simulation is a real-time simulation form.It differs from real-time simulation by the addition of a hardware component in the loop as an FPGA.This technique is increasingly being used in the development and testing of complex real-time embedded systems.Moreover, the complexity of the plant dynamic under control is commonly simulated in a graphical environment as SIMULINK from Matlab.In our case the plant dynamic was created in SIMULINK and then downloaded to the DSP board DS1104 in order to arrange the I/O ports.Figure 4 shows a simple diagram of the HIL simulation that was performed.

FPGA Implementation Results
The system is declared as an entity of three inputs that represents the position of the ball x 1 , the velocity of the ball x 2 , the current through the coil x 3 , and the output voltage v closed loop with the MAGLEV system.The internal variables used for the calculation of the equations use a word of 32 longitude bits-15 bits to represent the integer part, 16 bits for the decimal part, and 1 bit to represent the sign.The variable υ corresponds to the final calculation of the system and has a word longitude of 64 bits-4 for  integers, 1 for signs, and 59 for decimals, providing the necessary accuracy for the stability of the system.The total processing time of the calculations of one cycle in the FPGA is 202 nanoseconds, representing maximum processing speed of up to 21 nanoseconds.Figure 5 shows the utilization of the components in the FPGA virtexII XE2V1000-4fg256, with 3% of slices, 3% of LUTs, 7% of RAMs, and 20% of multipliers.The device has sufficient resources available to implement additional circuits.

Closed-Loop System in Implementation Results
The nominal parameters of the MAGLEV system are k m = 3 kgm 3 /s 2 A 2 , M = 0.14 kg, g = 9.8 m/s 2 , R = 1.2 Ω, L = 1 × 10 −3 H.The constant values of the exogenous signals ( 2) are a = 0.0070716, b = 0.05 m, and c = 9.8.Taking the nominal parameters of the MAGLEV system, the following pairs of matrices are calculated: The control parameters that appear in (11) are as follows: The matrix Σ 1 in (11) is calculated using the LQR function provided in Matlab.
To verify the robustness properties, some plant parameter variations are introduced which can be seen in Figure 6, where R and k m may change up to 100% from their nominal values.It is worth to mention that the perturbation term generated by the variation of R satisfies the matching condition [10], but not the variations on k m .
Figure 7 shows the tracking of the output signal where can be appreciated a good performance for 0 ≤ t < 5.But for 5 ≤ t < 10 where the perturbation term due to the variation in R is present, and the output still performs well due to the matching conditions.Finally, the unmatched perturbation term due to the variation of k m , appearing at t ≥ 10 adversely affects the MAGLEV system but the output still performs well.
Figure 8 shows the output tracking error where can be appreciated the transient and steady-state responses can be observed.
Figure 9 shows π 3 , which represents the ideal steadystate behavior of the current.It can be seen that the current becomes different to π 3 for t ≥ 10 due to the unmatched perturbation.
Finally, Figure 10 shows the voltage input signals where the discontinuous nature of the control signal can be appreciated.The main advantage of having discontinuous control signals is that it avoids the use of PWM as mentioned in [10], therefore, facilitating a straightforward implementation of the control action.

Conclusion
This work has presented the results of a program generator for VHDL code developed in Java language and designed to implement a mathematical unit prototyped and implemented in reconfigurable FPGA circuits from Xilinx.The mathematical unit was used to implement the control algorithm of a magnetic levitation system, accomplishing the requirements of speed and precision necessary to operate under nominal conditions.The code generator tool allows the implementation of blocks containing complex operations which may be grouped in the same memory, letting operations to run in a clock pulse, based on the calculation of functions through preestablished tables.Moreover, the HIL simulation test platformed has facilitated the verification of the results obtained when the physical plant is not available.
library IEEE; use IEEE.STD LOGIC 1164.ALL; use IEEE.stdlogic arith.all;use IEEE.stdLogic UNSIGNED.ALL; entity block is Port(angle:in std logic vector(9 downto 0); result:out std logic vector(16 downto 0)); end block; architecture behavior of block is type func is array (0 to 1024) of std logic vector (

Figure 2 :
Figure 2: Block diagram of the FPGA control.

8 Figure 5 :
Figure 5: Utilization of the components in the FPGA Virtex II.

Table 1 :
Selection of preestablished data for a cosine function.

Table 2 :
Results obtained from the mathematical unit.

Table 3 :
Utilization of the mathematical unit blocks.