Microcontroller-Based Direct Torque Control Servodrive

Robot technology has become an integral part of the automotive industry in several tasks such as material handling, welding, painting, and part assembly. 0erefore, the knowledge and skills to control the electric motors in these manipulators are essential for undergraduate electrical engineering students. Currently, the digital signal processor (DSP) is the core chip in industrial motor-control drives; however, the implementation of DSP control algorithms can be quite challenging for an experienced programmer, even more so for the novice. Considerable research has been done on this topic, although authors usually focus on DSP-based motor drives using popular control techniques such as field-oriented control (FOC). Although highly efficient, this approach is usually reserved for postgraduate education due to its complex structure and functionality. In this paper, the authors present a modular servodrive design on a low-cost, general-purpose microcontroller using the direct torque control (DTC) method, an alternative known for greater simplicity and torque response, compared with FOC. 0e system design was based on Micropython language allowing the software structure to be more manageable and the code to be more understandable. 0is design will be useful to undergraduates and researchers with interests in motor control design.


Introduction
It is well known that robotic machines are becoming an extensive and vital part of our everyday life. Especially in manufacturing industries such as the automotive, robotic manipulators are used for a wide variety of applications such as material handling, arc and spot welding, painting, and part assembly, among others. Almost any task, which may be repetitive, difficult, or hazardous for a human being, will usually involve some kind of robotic machinery [1]. For this reason, it is paramount for electrical engineering undergraduate students to learn the fundamentals of industrial servomotor control.
To achieve fast and accurate movements in a robot, servomotors are controlled employing high-performance techniques, such as field-oriented control [2,3]. is method allows control of the motor's speed while maintaining constant torque. Although FOC is commonly used in industrial motor controls due to high efficiency, its physical implementation can be quite challenging since complicated mathematics is involved. Also, it requires a fast and robust processor and great design efforts [3]. For these reasons, FOC-based servodrive design is a topic usually reserved for graduate or postgraduate engineering education.
Direct torque control (DTC) is another high-performance motor control method which may not be as popular or widely spread as FOC; however, it has many merits such as a more simplistic structure, low computational complexity, quick-response time, and a good dynamic performance [4]. is allows the implementation to require a less powerful processor [5], and a less experienced programmer for the software design. DTC has minor disadvantages compared with FOC, such as difficulty to control the motor at low speeds, variable switching frequency, and high-frequency ripple present on flux and torque signals [3]. Several techniques have been proposed to overcome these issues in classical DTC drives; however, the torque dynamics and the simplicity of the algorithm structure are mostly lost [2,3].
In this paper, the authors present a prototype servocontroller based on the classical DTC technique. e electronic device used is the Espressif ESP32, which is a 32 bit, low-cost embedded processor. For the complete coding of DTC, Micropython programming language was used. Experimental results were carried out to validate the effectiveness of the design. is allowed for the implementation of an open-source, user-friendly controller for permanent magnet synchronous motors (PMSMs). Although the aforementioned disadvantages of the DTC technique are present, the aim of this project is the design of a modular DTC test-bed for educational purposes. is will allow a fundamental understanding of DTC, as well as a simplified implementation and experimental verification of future improvements.

Motor Control Strategies
Industrial AC motors are used in a wide variety of applications that run at a constant speed regardless of the load, for example, fans or pumps. But there are also instances where it may be necessary to control the speed or torque, and in this case, a motor drive is required. Currently, there are several techniques used by manufacturers for motion control, two of the most dominating are scalar controls and vector controls [6].
Scalar control is based on mathematical expressions that describe the machine in the steady-state. In this type of control, there is no motor feedback; therefore, torque control is inefficient [7]. Speed control is carried out by varying the voltage and frequency ratio (V/Hz) so that this quotient is always constant. Although these motor drives are simple and economical, speed regulation is only around 3 percent of the motor's base frequency, and more than that, torque is greatly diminished [8]. For this reason, this method is not adequate for high-precision applications, such as industrial robot manipulators or metal-machining centers.
Alternatively, vector control techniques such as FOC are more powerful and efficient, although the control algorithm is significantly more complicated than V/Hz [6]. is methodology, developed by Blaschke [9] in the early 1970s for induction machines, is a closed-loop system; motor output signals such as voltage, current, and shaft position are constantly monitored to estimate the current and magnetic flux spatial vectors. e basic idea is to reorient these vectors perpendicular to each other in order to achieve maximum torque per amp, regardless of the motor's speed [3].
Unlike scalar control, FOC is based on mathematical expressions that describe the steady and transient state model of the machine. is technique not only manipulates the magnitude and angular velocity but also controls the instantaneous position of the spatial vectors of voltage, current, and magnetic flux, allowing a more stable and precise speed and torque control [2].
Although FOC is a high-performance and well-established technology widely applied in power electronics, it is well known that certain features make it difficult to implement in a motor drive. For instance, it has a cascaded architecture that involves coordinate-transformation blocks, proportional-integral (PI) controllers which contain many parameters to be tuned, current regulators, and pulse width modulation (PWM) signal generators, all of which increases implementation complexity and execution time [10]. erefore, FOC usually requires a specialized electronic device optimized for processing digital signals in real time. Such a device is usually a DSP since its architecture is enhanced for this kind of task. In other cases, a field-programmable gate array (FPGA) may also be a good alternative, considering that it is possible to design in it, customized parallel operations to lower execution times [11][12][13]. e main requirement for using these devices is certain expertise in programming languages, such as C++ for the DSP; for the FPGA, it usually will be either VHDL (Very-High-Speed-IC Hardware Descriptive Language) or Verilog [14,15].

Classical DTC Principle
DTC is a motor control strategy that works differently than FOC. It was proposed in the mid-1980s by I. Takahashi for controlling induction motors [16]. e main idea in DTC is to estimate the stator flux and electromagnetic-torque spatial vectors, and control their magnitude separately and independently, with the use of hysteresis controllers [17,18]. e outputs of the hysteresis controllers are applied directly to a predefined table to select the optimum voltage vector for the voltage-source inverter (VSI). With it, torque and flux vectors are controlled directly and maintained within two hysteresis bands [19].
In DTC there is no need for PI or current controllers. Also, since DTC is in the stator reference frame, no coordinate transformation is applied. Additionally, due to the direct control feature of torque and flux, a PWM modulator is not necessary [20][21][22]. For these reasons, execution time is lower, and dynamic torque response is higher, compared with FOC [23]. A block diagram of the classical DTC strategy is shown in Figure 1.

Torque and Flux Estimation.
e DTC algorithm is quite straightforward. e process starts by sensing two lines of the three-phase motor current (i a , i b ). e values are converted from the stator three-phase reference frame (a, b, c) to a two-phase reference frame (α, β) as e DC link voltage (V dc ) is also measured and converted to a two-phase reference frame; together with the last vector applied to the VSI (S a , S b , S c ), the two-phase voltage reference components are obtained as To estimate the stator magnetic flux vector (ψ S ), the following expression is applied using stator resistance, voltage, and current (R s , V s , I s ) as 2 Journal of Robotics However, since the DTC strategy must be implemented digitally, this expression must be formulated as a discrete operation. erefore, by changing the integration to an accumulated sum and by using the stator voltage and current two-phase reference components, it can be rewritten as follows: where n is the notation to indicate the current discrete sample and n + 1 is the next discrete sample. As for T s , it represents the sampling time, where its value is based on factors such as microcontroller speed and algorithm execution time. Now, by using the flux components calculated in (4) and (5), the total magnitude of the stator flux vector will be Also, the angle of the flux vector can be calculated with the following expression: is angle is of major importance since it will help determine in which sector the flux vector is located.
To estimate the electromagnetic torque, the two-phase current and flux frame reference components and the motor pole pairs (p) are used as Once the torque and flux have been estimated, they are compared with their respective reference values to obtain the torque error (e T ) and the flux error (e ψ ). ese error values will be the input to the hysteresis comparators.

Hysteresis Comparators.
In DTC, hysteresis comparators are used as a fast and simple equivalent to the PI controllers used in FOC. eir function is to restrict the magnitude of the torque and flux errors to values between two limits. Narrow hysteresis bands lead to smooth sinusoidal current and torque waveforms, although they will also cause high switching frequencies and greater switching losses [10]. erefore, the hysteresis comparators must have the appropriate limits for a good DTC performance. e flux-controller output (ϕ) has two possible values, 0 or 1, depending if the flux error (e ψ ) is under the lower limit or over the upper limit, respectively. On the contrary, the torque-controller output (τ) has three possible values, − 1, 1, or 0, depending on if the torque error (e T ) is either under the lower limit, over the upper limit, or between both, respectively. Both hysteresis controllers are shown in Figure 2.
e output values of both controllers, together with the spatial placement of the flux vector, are used to select the optimal voltage vector for the VSI from the vector table.

Optimal Vector Selection.
e flux vector rotation space is divided into six equally spaced sectors (1-6) containing six active vectors (V 1 to V 6 ) and two null vectors (V 0 and V 7 ). Figure 3 shows how the voltage vectors are distributed in each sector.
To select the optimum vector to apply to the VSI, the hysteresis outputs (ϕ, τ) are used. e flux vector angle θ s is also required but only to determine in which sector the flux vector is located.
Both torque and flux can be controlled directly by selecting the appropriate active voltage vector and regulate their magnitudes within their respective hysteresis limits. When a null vector is applied to the VSI, it serves as a soft transition from one switching state to the next. Also, they prevent a short circuit in the DC bus due to a delay in the turn-off time of the power switches [24,25].
To understand the effect of the voltage vector on the flux or torque vector, an example is presented. In Figure 3, the flux vector ψ s is located in sector 1 and is currently rotating  (Table 1), the vectors that would have a greater impact on both torque and flux are V 2 , V 3 , V 5 , and V 6 . Applying V 2 would increment both the torque and the flux, whereas V 5 would decrease both. Instead, if V 3 is applied, it would increase the torque but would decrease the flux. Similarly, if V 6 is applied, it would increase the flux and decrease the torque. Applying V 1 or V 4 would have an impact mostly on the flux vector, either decreasing or increasing it, respectively. Lastly, if either null vector is applied, it would slowly decrease both vectors. erefore, for this example, if sector � 1, ϕ � 1, and τ � 1, it can be seen in Table 1 that the vector to be applied would be V 2 .
Once the optimal vector is selected, it is applied directly to the VSI for torque and flux correction.

DTC Servodrive Description
e DTC servodrive was designed as a customized and modular test-bed for industrial servocontrol. e main idea is that each one of the modules may be replaced by another with different types of hardware. For example, the current sensor module in this design uses inductive sensors but can be replaced easily by a Hall-effect sensor module or a shunt resistor module. Also, the processing module which currently uses a 32 bit microcontroller may be replaced in the future by a different type of embedded system. Even the DTC algorithm has an open-source architecture so that the main blocks can be modified or enhanced, or even new functionalities may be easily added. e primary reason is to be able to experimentally test and visualize the effects these changes have on the motor performance.
e servodrive system is shown in Figure 4.
A list of the electronic components used in this prototype is presented in Table 2.

Current-Voltage Sensing and Signal Conditioning
Circuitry. One of the most important stages of the DTC process starts in current sensing. Since the flux and torque estimations are based on the current values, it is necessary to have a precise current sensor. Firstly, the current signals of two of the motor lines are measured and converted to a DC signal by adding an offset voltage. Secondly, the signal is amplified and filtered so it can be converted to a digital signal using the analog to digital converters (ADC). Antialiasing filters are employed to effectively cut off frequencies higher than about half the maximum frequency of interest.
For this project, a MURATA 56300C inductive current sensor was used. As shown in Figure 5, the current is sensed, positive offset, and amplified using operational amplifiers.
To calculate the two-phase reference components for the voltage, the DC power bus must be monitored in case of voltage drops. For the DC voltage sensing (Figure 6), a resistive divider circuit was used and also amplified using JFET-Input operational amplifiers. Since this voltage is always positive, no offset is required. e signal is scaled, amplified, and filtered so it can be converted to a digital signal.   Table 1: List of all optimal vectors for the VSI based on the sector number (1-6), the torque error (τ), and the flux error (ϕ).   Journal of Robotics 5

Power Electronics Circuitry.
In this stage, the threephase voltage is converted to a DC signal so that it can be converted back again to a controlled AC signal for the motor. For this stage, industrial-grade power modules were used for the rectifier bridge and the VSI (Figure 7). Two 1000 μF electrolytic capacitors are connected in parallel for the DC filtering stage.

DTC Processing
Module. e processor used for the implementation of the DTC technique is the ESPRESSIF ESP32-WROVER-B 32 bit microcontroller, which runs at 240 MHz. ESP32 is a great development board for IoT applications as it is a low-cost device and has many powerful features (Table 3). ESP32 can be programmed in various environments and programming languages such as Arduino IDE, Espressif-IDF, Lua, Micropython, and C/C++.
In this project, Micropython was used for the design and implementation of the DTC technique. Micropython is a reimplementation of Python 3 programming language, targeted to microcontrollers and embedded systems. Both programming languages are very similar. Apart from a few exceptions, all the language features of Python are available in Micropython. e main reason for using Micropython in the DTC prototype is that today Python is one of the most widely used, simple and easy to learn programming languages currently available, and well suited for computational analysis and design [26,27]. With the emergence of Micropython, it becomes easy to program microcontrollers and embedded devices. is makes ESP32 and Micropython, a great experimental platform for students, teachers, and researchers [28]. e DTC scheme presented previously in Figure 1 was implemented in Micropython and the ESP32 with the use of peripherals such as ADCs, DACs (digital to analog converters), and timers. A summarized workflow of the DTC algorithm is shown in Figure 8.
e DTC algorithm is programmed in a Micropython interrupt service routine (Figure 9) which is triggered every 250 μS by the timer. Once initiated, the current and voltage analog signals are converted to digital values. ese values are used to estimate flux components, flux, and torque values and then compared with reference values to select the optimal vector for the VSI. Any of these signals can be visualized in an external oscilloscope utilizing the internal DACs.

Experimental Results
e experimental testing was carried out using a standard 1 kW PMSM servo, and the main parameters of which are given as follows: R s � 15.57 Ω; pole pairs, p � 8; rated voltage, V � 130 V; rated current, I � 2.5 A; rated torque, T � 2.7 Nm, and rated speed, ω e � 5000 RPM.
To validate the DTC servodrive performance, several tests were executed with the main focus being on the flux, torque, and current signals. If the output signals are similar to the simulated signals, then it means that the algorithm has been properly implemented. Also, the torque output was measured to verify that it is as close as possible to the torque reference value.
Although it is important to clarify that the performance of the DTC algorithm and the quality of the output signals are directly related to the hysteresis controllers [29,30]. For this reason, most of the tests were made by observing the effect of the hysteresis bands on the output signals.

Effects of the Hysteresis Flux Band.
e hysteresis flux band has a considerable effect on the output signals. For instance, if the band is narrow, the switching frequency will be higher, the flux locus will tend to a circular path, and the current signal is very close to a sine wave; however, current ripple will increase since a narrow band will make it easier for the hysteresis limits to be exceeded. On the contrary, a wide band will lower the ripple content, but the switching frequency will decrease, the flux locus will tend to a hexagonal path, and the current signal is distorted [30]. is effect can be observed in simulations ( Figure 10) and experimental tests (Figure 11). e effect of the flux hysteresis band can also be observed in the current signals. When the band is narrow, the current signal will have greater ripple content, but the shape will be close to a sine wave. If instead, the band is wider, the ripple will be lower, but the signal will be heavily distorted. is behavior can be seen in simulations ( Figure 12) and the experimental motor current signal ( Figure 13).

Effects of the Hysteresis Torque Band.
e width of the hysteresis torque band also has a considerable effect on DTC performance, mainly on the inverter switching frequency. Although it also influences the total harmonic distortion  Journal of Robotics (THD) in the current signals, this effect is due to both hysteresis bands. Firstly, the switching frequency is defined as where N s is the number of commutations per period and T f is the period of the fundamental signal.
To observe the effect on the switching frequency, both hysteresis bands were varied. In Figure 14, it can be seen that even though both hysteresis bands affect the switching frequency, the torque band has a greater influence.
As for the THD on the current signal, it is defined as

Journal of Robotics
where I is the RMS value of the current and I 1 is the RMS value of the fundamental harmonic component. Both hysteresis bands were varied, but in this case, the flux band had a greater influence on the current signal distortion as shown in Figure 15.

Output Torque Measurements.
To validate the torque performance of the servocontroller, measurements were made using a 10 Nm rotary torque sensor. In these tests, it was important to verify that the torque output corresponds to the torque reference and that the ripple content is relatively low.
Once again, the hysteresis bands were varied to observe the effect on the torque signal. Since the original DTC scheme with no enhancements was implemented, it was expected to have relatively high ripple content. e best torque performance with the lowest ripple was obtained    Journal of Robotics when the flux band was set to 2% of the reference value (0.8 Wb), and the torque band was set to 1% of the reference value. e output torque signal is shown in Figure 16.
Needless to say, the verification of the torque response in the servodrive is very important since this ensures that changes in torque and direction will be carried out by the drive. However, the use of a physical torque sensor is usually expensive and requires additional installation and software. Additionally, torque sensors have a specific range; if a wider range is required, the cost becomes prohibitive. In future projects, it would be interesting to make use of virtual instruments [31] for the monitoring of torque and magnetic flux signals, and in this way avoid the limitations of physical sensors, especially in electromechanical projects aimed to education [32] where funding is usually limited.   Figure 13: Experimental motor current output to observe the effect of the flux hysteresis band with (a) 2% and (b) 5% of the flux reference value. e current amplitude is 2 A/div, and the period is 5 mS/div. microcontroller. e aim of this prototype is to be used in didactic applications for undergraduate engineering students. Experimental and simulation results validate the effectiveness of the design and show that good torque performance can be achieved with DTC on a low-cost, general-purpose processor. e main benefit of this prototype is its simplicity since it did not require any modification to the original DTC structure, and even so current and torque ripple content was acceptable. e code written on Micropython made it easy to understand and to adjust for different testing parameters such as modifying hysteresis bands to observe the quality of current, flux, and torque output signals.

Conclusion
is will allow students and researchers interested in motion control to experiment with a simple and practical tool and in future projects to improve even the performance and structure of this prototype.

Data Availability
No data were used to support this study.