Next Article in Journal
Collection Auctions-Based Autonomous Intersection Management
Previous Article in Journal
An Onboard Point Cloud Semantic Segmentation System for Robotic Platforms
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
Article

UPAFuzzySystems: A Python Library for Control and Simulation with Fuzzy Inference Systems

by
Martín Montes Rivera
1,*,
Ernesto Olvera-Gonzalez
2 and
Nivia Escalante-Garcia
2
1
Research and Postgraduate Studies Department, Universidad Politécnica de Aguascalientes (UPA), Aguascalientes 20342, Mexico
2
Laboratorio de Iluminación Artificial, Tecnológico Nacional de México Campus Pabellón de Arteaga, Carretera a la Estación de Rincón Km. 1, Pabellón de Arteaga 20670, Mexico
*
Author to whom correspondence should be addressed.
Machines 2023, 11(5), 572; https://doi.org/10.3390/machines11050572
Submission received: 17 April 2023 / Revised: 7 May 2023 / Accepted: 10 May 2023 / Published: 22 May 2023
(This article belongs to the Topic Intelligent Systems and Robotics)

Abstract

:
The main goal of control theory is input tracking or system stabilization. Different feedback-computed controlled systems exist in this area, from deterministic to soft methods. Some examples of deterministic methods are Proportional (P), Proportional Integral (PI), Proportional Derivative (PD), Proportional Integral Derivative (PID), Linear Quadratic (LQ), Linear Quadratic Gaussian (LQG), State Feedback (SF), Adaptative Regulators, and others. Alternatively, Fuzzy Inference Systems (FISs) are soft-computing methods that allow using the human expertise in logic in IF–THEN rules. The fuzzy controllers map the experience of an expert in controlling the plant. Moreover, the literature shows that optimization algorithms allow the adaptation of FISs to control different processes as a black-box problem. Python is the most used programming language, which has seen the most significant growth in recent years. Using open-source libraries in Python offers numerous advantages in software development, including saving time and resources. In this paper, we describe our proposed UPAFuzzySystems library, developed as an FISs library for Python, which allows the design and implementation of fuzzy controllers with transfer-function and state-space simulations. Additionally, we show the use of the library for controlling the position of a DC motor with Mamdani, FLS, Takagi–Sugeno, fuzzy P, fuzzy PD, and fuzzy PD-I controllers.

1. Introduction

The main goal of control theory is to be tracking the input or system stabilization that produces an adjusted output associated with a process that responds to different behaviors. Achieving this implies designing a control law adapted to the process model. To obtain this model means using physics and mathematical principles with parametrizing stage after modeling to achieve similar behavior to the process [1,2].
A feedback Computer-Controlled System (CCS) has an input that samples the Process Variable (PV) with an Analog–Digital Converter (ADC). Then, the CCS uses this information to calculate an algorithm that considers the Reference Variable (RV) or desired value for PV by producing an analog output—converted from digital with a Digital–Analog Converter (DAC). All the conversions and steps in the CCS are shown in Figure 1 [3].
CCSs use algorithms ranging from deterministic to soft methods with probabilistic techniques. Some examples of deterministic methods are Proportional (P), Proportional Integral (PI), Proportional Derivative (PD), Proportional Integral Derivative (PID), Linear Quadratic (LQ), Linear Quadratic Gaussian (LQG), State Feedback (SF), and Adaptive Regulators [1,3].
Alternatively, Fuzzy Inference Systems (FISs) are soft-computing methods that offer a linguistic way of dealing with complex processes and the possibility of translating human experience into logic in IF–THEN rules.
The FISs were introduced in the early 1970s by Lotfi A. Zadeh. This invention represented a breakthrough in set theory, as fuzzy logic mimics the human decision process. In 1975, Ebrahim Mamdani initiated the FIS to control a steam engine and boiler by creating linguistic synthesis control rules based on human expert operators, obtaining the first fuzzy controller [4].
The fuzzy controllers make it possible to design the control law with rules that represent the experience of the experts in controlling the plant. Moreover, the literature shows that algorithms such as Least Square Estimator (LSE), Genetic Algorithms (GAs), Particle Swarm Optimization (PSO), and Gradient Descent (GD), among others, allow the optimization of FISs for controlling different processes such as black-box problems [5,6,7,8,9,10].
FIS controllers simplify the control of complex systems without linearities and time variations, but their programming and configuration are more complex than classical approaches. However, there are several commercial alternatives for implementing and simulating FIS controllers.
The MATLAB™ fuzzy logic toolbox enables the design and implementation of FIS for control, modeling, and simulation [11]. Current research papers that implement the MATLAB™ fuzzy logic toolbox investigate the following: reducing chemical oxygen demand in low-strength wastewater [12], controlling the speed of motors for robots [13], designing the control of a three-phase grid-connected inverter using a Raspberry Pi system [14], the design, modeling, and simulation of one-degree of freedom inverted pendulum [15], and designing a single-stage photovoltaic system with energy recovery control [16].
Additionally, “IT2-FLS” is another toolbox of MATLAB™ for implementing fuzzy logic of interval type 2. It includes different construction stages, including design, description, and implementation. However, it does not contain parameters or methods specific to control systems [17].
A commercial alternative for implementing and controlling FIS is National Instruments, whose LabVIEW™ software provides a graphical programming framework. In addition, LabVIEW™ allows the implementation of real-time controllers using the same brand of diving instruments, such as USB-6001/6002/6003/6211 and others [18]. LabVIEW with FIS has shown promising results in controlling Carbon Dioxide Fertilization in a Greenhouse Environment [18], Air Valve for soprano recorders with automatic note recognition [19], Gain Scheduling of PID Controller Based on Fuzzy Systems [20], and a suspension system for a quarter-hour car [21], to name a few.
On the other hand, according to the 2022 TIOBE (The Importance of Being Earnest) index, Python is the most widely used programming language and has experienced significant growth in recent years (Figure 2) [22].
Python has specific libraries for implementing simulations and control using classical approaches with model analysis and control laws. Therefore, Python should have an open-source library for implementation and simulation with FIS controllers. In addition, most of the control libraries or toolboxes in Python are open-source—a philosophy for access to programs that allows code modifications, the sharing of contributions, and use under license restrictions intended to limit responsibility.
Using open-source libraries in Python offers numerous advantages for software development. First, open-source libraries approve developers to save time using algorithms already implemented for different proposals [23]. Second, they provide high quality and reliability because they are constantly updated and maintained by a large and diverse community of developers [24]. Third, open-source libraries are cost-effective because they do not require licensing fees, aiding organizations to allocate more resources to other areas of their project [25]. Finally, using open-source libraries in Python has led to faster time-to-market and better product quality [26].
Despite that, there are other open-source alternatives for simulating FISs. They are related to developing expert systems and fuzzy logic in software applications. In addition, the control domain is not their priority, and they cannot simulate controllers with mathematical model descriptions, transfer functions, and inputs as a ramp or step response.
According to the Python Package Index (PyPI), the most relevant libraries for the implementation of FIS and fuzzy control include the average number of downloads based on Shield’s IO statistics [27]:
  • “Fuzzy-logic-toolbox”: Library licensed in 2020 based on the behavior in MATLAB™ without simulation of fuzzy controllers with an average of 47 downloads per month [28].
  • “Scikit-fuzzy”: A library licensed in 2012 to popularize fuzzy logic in Python, agreeing to simulate and describe FIS using rounding arithmetic with IEEE (Institute of Electrical and Electronics Engineers) standards. However, simulations with transfer functions or mathematical models lack control structures such as fuzzy PID controllers. Shield’s IO statistics state that it has an average of 26,000 times per month.
  • “fuzzylab”: Licensed in 2007, this is a library based on Octave Fuzzy Logic Toolkit 0.4.6. This library allows the simulation of FISs without including the implementation of controls with transfer functions, mathematical models, or other control structures in its methods. However, its developers used it to control an autonomous robot’s navigation system [29,30], averaging 53 downloads per month.
  • “fuzzython”: Released in 2013, it allows the construction of FIS, including the Mamdani, Sugeno, and Tsukamoto models, but it misses tools for working with fuzzy control or the simulation of systems with transfer function or state-space model descriptions [31], with an average of 12 downloads per month.
  • “Type2Fuzzy”: Licensed in 2007, this library allows work with type 2 FIS, in general descriptions for software applications, but it does not include methods for working with transfer functions, state-space models, and fuzzy control [32] with an average of 53 downloads per month.
  • “Fuzzy-machines”: This is a 2018 licensed library for working with FIS but does not include methods for working with fuzzy controllers, transfer functions, or state-space descriptions, with an average of 18 times per month.
  • “pyfuzzylite”: A 2007 licensed Library for developing FIS and controllers 2007 over a graphic interface. It allows working in Mamdani, Takagi–Sugeno, Larsen, Tsukamoto, Inverse Tsukamoto, and Hybrids. However, it does not include fuzzy PID controllers or methods for simulation with transfer functions and state-space representations [33] from an average of 302 downloads per month.
  • “Simpful”: It depends on “numpy” and “scipy” libraries. It has properties of polygonal and functional models. It allows the definition of fuzzy rules as text strings in natural language, the description of complex fuzzy two rules built with logical operators, and Mamdani and Takagi–Sugeno interference methods. However, it does not consider parameters for automatic control [33]. Shield’s IO statistics state that it has an average of 113,000 times per month.
  • “pyFume”: It collects classes and methods for the antecedent set and associated parameters of a Takagi–Sugeno (TS) fuzzy system from data using the Simpful library. The antecedent set and related parameters of a Takagi–Sugeno fuzzy model are extracted from data and then building an executable fuzzy model using the Simpful library. It only applies fuzzy logic and does not consider automatic control parameters [34]. Shield’s IO statistics state that it has an average of 120,000 times per month.
In this work, we describe our proposal UPAFuzzySystems library developed in the Universidad Politécnica de Aguascalientes (UPA) as an FISs library for Python, which supports the design and implementation of the fuzzy controller with transfer function and state-state representations already published in open-source license in [34]. In addition, our proposal uses the “control” library for simulation. According to Shield’s statistics IO, the “control” library is the most used library for simulating controllers in Python, with 57,000 downloads per month. Furthermore, our proposal includes P, PD, PI, and PID fuzzy controller structures. This proposal is a novel idea since no other Python library allows the simulation and test of fuzzy controllers with transfer functions and state-space models. Furthermore, there are no other alternatives in Python for designing fuzzy controllers with PID structures. Moreover, we compare all these libraries in terms of their capabilities in designing FISs and fuzzy controllers and simulating them (Table 1).

2. Materials and Methods

FISs belong to the soft-computing methods because, unlike hard-computing methods, they consent to work with tolerances and imprecisions and make decisions under uncertainty. Moreover, our natural language deals with imprecision in inference because we allow fuzzy boundaries instead of well-defined ones [35,36].
FISs authorize using membership degrees in sets to express logic with fuzzy boundaries. The membership value μ defines the degree of membership in the ranges 0 , 1 .
A membership function μ x defines μ for each x point of the universe X in a fuzzy set A as in Equation (1).
A = x , μ ( x ) | x X
For example, deciding whether a person is young or old, with an age’s universe in the range 0 , 100 years, one could use well-defined bounds (Figure 3a) to express a falsely narrow categorization or use FISs with trapezoidal μ x to correctly represent the imprecisions and uncertainties of the problem, as shown in Figure 3b.
The μ x forms have a significant impact on the FISs behavior. The most common membership functions include the triangle in Equation (2) as a function of the vertices a , b , c ; the trapezoid in Equation (3) depending on vertices a , b , c , d ; the Gaussian in Equation (4) as a function of the parameters c and σ ; the generalized bell in Equation (5) subject to parameters a , b , c , and others. Moreover, one can assign μ directly based on empirical values or in the statistical metrics of a dataset by specifying the raw membership values and the corresponding element of the universe as in Equation (6) [37].
t r i a n g l e x ; a , b , c = 0 , x a x a b a , a x b c x c b , b x c 0 , c x
t r a p e z o i d x ; a , b , c , d = 0 , x a x a b a , a x b 1 , b x c d x d c , c x d 0 , d x
g a u s s i a n x ; c , σ = e 1 2 x c σ 2
b e l l x ; a , b , c = 1 1 + x c a 2 b
r a w x , μ = x 1 , μ 1 , x 2 , μ 2 , , x n , μ n } ,     i = 1 , , n
The FISs derive conclusions using logical IF–THEN rules, with premises using fuzzy sets and consequences that can use fuzzy sets for Mamdani, FLSmith; functions for Takagi–Sugeno; and raw numbers in fuzzy linear and PID controllers [5,36]. The FISs structure includes the following phases: preprocessing, fuzzification, rule base, inference engine, defuzzification, and post-processing (Figure 4).
IF–THEN rules imply working with connectives to define interactions midst premises represented with fuzzy sets. Moreover, these connectives agree with the definition of fuzzy composite sets in n dimensions that map interactions between premises. Connectives include the intersection (AND) and union (OR) operations, which are based on classical set theory but adapted to fuzzy logic. Equations (7) and (8) define the AND ( ) and OR ( ) connectives for nonlinear FIS, and Equations (9) and (10) define the AND ( ) and OR ( ) connectives for linear systems respectively, in that order [35,36].
A B m i n μ A x , μ B x
A B m a x μ A x , μ B x
A B μ A x μ B x
A B μ A x + μ B x μ A x μ B x
After defining premises with their connectives, Implications ( ), or Equivalences ( ) enable the derivation of conclusions or consequences with rules in the form IF–THEN of Equation (11). Implications more commonly used in controllers include the Mamdani implication in Equation (12), which gathers the consequence based on the fuzzy input universe X to the fuzzy output universe Y . The Mamdani and FLS controllers (FL Smidth) use this implication [5].
If   f e 1   is   A 1 , e 2   is   A 2 , , e k   is   A k   then   f y 1   is   B 1 , y 2   is   B 2 , , y k   is   B k
x , y , μ A B x , y x X , y Y , μ A B x , y = m i n μ A x , μ B y
Alternatively, Takagi and Sugeno contain fuzzy rules that generate a set of linear functions depending on the premises. Takagi–Sugeno controllers generate output functions depending on the error and change in error as in Equation (13). Linear and fuzzy PID controllers and Takagi–Sugeno controllers use this approach [5].
If   f e 1   is   A 1 , e 2   is   A 2 , , e k   is   A k   then   y = g e 1 , e 2 , , e k
Finally, defuzzification allows an appropriate scalar output for the controlled process or specific application of the FIS. There are several defuzzification methods, including Center of Gravity (COG) for continuous fuzzy sets in Equation (14), Center of Gravity for Singletons (COGS) (Equation (15) for singletons and Equation (16) for discrete systems), Bisector of Area (BOA) in Equation (17), Mean of Maxima (MOM) in Equation (18), Leftmost Maxima or Smallest of Maxima (LM) in Equation (19), and Rightmost Maxima or Largest of Maxima (RM) in Equation (20) [5,36].
C O G = X μ A x x d x X μ A x d x
C O G S = k α k * S k k α k *
C O G S = i μ A x i x i i μ A x i
B O A = a r g m i n x j   i = 1 j μ c x i i = j + 1 i m a x μ c x i   ,   1 < j < i m a x
M O M = i I   x i I , I = i μ c x i = μ m a x
L M = x m i n I
R M = x m a x I
FIS controllers apply different combinations of universes, membership functions, inference rules, connectives, implications, and defuzzification. However, all the configurations have common input premises that depend on the error and output consequences to change the process state. Table 2 describes some of these configurations.
The fuzzy P, PD, and PID controllers start with a linear structure, as in Table 2. The goal of using the linear approach is to first design a twin of a P, PD, or PID controller for the process with classic controllers. Then, the structure is changed to a no-linear form. This change allows having a no-linear FIS controller with the benefits of derivatives and integrals without having too complex rules [36].
The P controller has two gains, G E and G U , as shown in Figure 5. First, G E sets the control deviation of the plant to be in the ranges [−100, 100] required for this controller (Table 2). Then, G U sets the gain so that it is equal to K p of the previously designed classical P controller. Equations (21) and (22) determine the G E and G U , respectively [36].
G E = 100 m a x e
G U = K p G E
The controller FIS PD in Figure 6 has three gains G E , G U , and G C E . FIS PD, of the FIS P, uses G E to adjust the error to be in the ranges [−100, 100] and then uses Equation (22) to determine G U . Then, G C E is determined from the lead time T d using Equation (23).
G C E = G E · T d
The controller FIS PID in Figure 7 uses the same structure as the controller PD with G E and G U for scaling the input with Equation (21) and obtaining the equivalent K p with Equation (22). Similarly, it uses G C E to map the derivative gain from the derivative time T d using Equation (23). However, FIS PID includes an integrative effect controlled by the gain G I E and maps the effect of the integrative time T i , thus Equation (24).
G I E = G E T i
Having defined the linear structures FIS P, PD, and PID, let us explain the inference system with the IF–THEN rules. The conditions and consequences for these rules depend on the type of FIS controller. Linear P, Linear PD, and Linear PID, for example, use rules similar to those of the linear controller to create a clone of a classical controller in the design stage. Then, they switch to no-linear rules such as those used in Mamdani, FLS, or Takagi–Sugeno controllers [36]. Table 3 shows the type of rules used in different FIS controllers.
Our proposed library UPAFuzzySystems for Python is already published as an open-source license. It implements fuzzy universes with fuzzy sets, FIS systems, and FIS controllers with a simple definition of all required parameters by three different Python classes fuzzy_universe, inference_system, and fuzzy_controller. All the codes of UPAFuzzySystems are in [34].
The class fuzzy_universe is the first-level definition, i.e., all other classes require it for describing premises or consequences with fuzzy universes. A continuous or discrete fuzzy universe contains fuzzy sets defined in the universe with membership values for defining certain situations. The class fuzzy_universe also has several methods for adding and removing fuzzy sets with all membership functions from Equations (2)–(6), all of which can be found in [34].
The class fuzzy_inference is the second-level class. It allows the description of the IF–THEN rules using the premises and consequences defined with the class fuzzy_universe. Moreover, the class fuzzy_controller uses it to specify its IF–THEN rules. All methods related to fuzzy_inference class are in [33]. Finally, the class controller defines the fuzzy controllers with specific structures and simulates their behavior with transfer functions and state-space equations. All methods for working with the class fuzzy_controllers are in [34].

3. Results and Discussion

The results in this section cover the steps and results in implementing fuzzy universes, fuzzy inference systems, and fuzzy controllers with UPAFuzzySystems.

3.1. Important Libraries

When working with figures in Python, the “matplotlib” library is used and imported in line 1 of Code 1. Then, in line 2, the proposed library “UPAFuzzySystems” is inserted to implement fuzzy universes, inference systems, and controllers. Then, we import the “numpy” library in line 3 to define vectors, matrices, and operations with numbers. Finally, the “control” library in line 4 allows defining transfer functions and state-space equations for simulation. The Python code to import the libraries is in Code 1.
Code 1. Importing main libraries in Python.
1
2
3
4
import matplotlib.pyplot as plt
import UPAFuzzySystems as UPAfs
import numpy as np
import control as cn

3.2. Fuzzy Universes with Fuzzy_Universe Class

After importing the main libraries, an example of fuzzy universes with a description of Collision Distances from 0 to 60 m in near, middle, and long-range allows explaining the use of the fuzzy_universe class in the “UPAFuzzySystems” library.
Code 2 in line 1 defines the universe from 0 to 60 with 100 samples using a “numpy” library. Then, line 2 corresponds to an instance of the class fuzzy_universe, passing the name of the universe and the instruction to perform a continuous universe.
Next, we add fuzzy sets for all collision regions in lines 3–5 by passing the name of the fuzzy set, the type of fitness function, and its vertices. This example uses trapezoidal and triangular membership functions according to Equations (3) and (2), respectively.
Finally, in line 6, we access the plot of the fuzzy_universe, and in lines 7–10, the figure was modified to change the axis names. All the Python code for the implementation is in Code 2.
Code 2. Python code for the description of a fuzzy universe.
1
2
3
4
5
6
7
8
9
10
distances = np.linspace(0,60,100)
DistanceColision = UPAfs.fuzzy_universe('Collision Distance',distances,'continuous')
DistanceColision.add_fuzzyset('close-range','trapmf',[0,0,5,15])
DistanceColision.add_fuzzyset('mid-range','trimf',[10,20,30])
DistanceColision.add_fuzzyset('long-range','trapmf',[25,40,60,60])
DistanceColision.view_fuzzy()
ax = plt.gca()
ax.set_xlabel('Collision Distance (m)')
ax.set_ylabel("$\mu$")
plt.show()
Figure 8 shows the results obtained with the view_fuzzy method executed in line 6 of Code 2 and the updated information in the axis.

3.3. Fuzzy Inference System with Inference_System Class

The IF–THEN rules of an inference system require premises and consequences. Therefore, we define a consequence of speed for the premise defined in Section 3.2. We follow the approach described in Code 2 and create the consequence in Figure 9.
After that, we set the rules for the FIS: in this case, a slow speed for a near collision distance, a medium speed for a medium-range collision distance, and a fast speed for a far-range collision distance. In other words, the rules:
  • IF Collision Distance is short-rangeSpeed is slow
  • IF Collision Distance is mid-rangeSpeed is medium
  • IF Collision Distance is long-rangeSpeed is fast
Code 3 defines the inference system. First, we create an instance of the class inference_system and pass the name to assign in line 1. Then, lines 2 and 3 define the premise and the consequence, respectively. Next, lines 4–6 describe the rules for the FIS, passing a list of premises with a list per premise with its name and the corresponding fuzzy set name, a list of connectives, and a list of consequences with a list per consequence with its name and the corresponding fuzzy set name.
Hence, in line 7, the type of rules is configured. In this case, the Mamdani system with its configuration is shown in Table 2. Then, in line 8, the FIS is created. To display the system’s surface in line 9, call the method surface_fuzzy_system and pass a list of “numpy” arrays, one per input premise, as before. Finally, lines 10–13 handle the axis labels. The Python code to define the inference system is in Code 3. The surface obtained is in Figure 10.
Code 3. Python code for defining inference system and its rules.
1
2
3
4
5
6
7
8
9
10
11
12
13
Speed_Collision = UPAfs.inference_system("Speed based in Collision Distance")
Speed_Collision.add_premise(DistanceColision)
Speed_Collision.add_consequence(Speed)
Speed_Collision.add_rule([['Collision Distance','close-range']],[],[['Recommended Speed','slow']])
Speed_Collision.add_rule([['Collision Distance','mid-range']],[],[['Recommended Speed','medium']])
Speed_Collision.add_rule([['Collision Distance','long-range']],[],[['Recommended Speed','fast']])
Speed_Collision.configure('Mamdani')
Speed_Collision.build()
Speed_Collision.surface_fuzzy_system([distances])
ax = plt.gca()
ax.set_xlabel(r"Collision Distance (m)")
ax.set_ylabel(r"Speed ($\frac{km}{h}$)")
plt.show()

3.4. Fuzzy Controller with Fuzzy_Controller Class

Let us define the Direct Current (DC) motor position as the process variable to control. The parameters and the transfer function are in Table 4. Ref. [38] details the DC motor used and the process for obtaining its model.
Code 4 shows the code for defining the transfer function, its parameters, a test input, and a test signal with disturbances using the control library. The test input starts at zero and gradually changes in 5 ms to the equivalent radian for 45° (0.78539816 rad) 0.25 s later. Lines 1–7 give the parameters, line 8 describes the universe, line 9 generates the input, line 10 defines the starting seed for randomness, line 11 represents the uniform random perturbation around 10% of the reference input, line 12 defines the 𝑠 term for the frequency space, and line 13 stipulates the transfer function.
Code 4. Code for defining transfer function and its parameters in a DC motor.
1
2
3
4
5
6
7
8
9
10
11
12
13
J = 3.2284E-6
b = 3.5077E-6
K = 0.0274
R = 4
L = 2.75E-6
te = 1.0
ns = 500
T = np.linspace(0,te,ns)
Input = np.array([(np.radians(45)*min((t-0.25)/0.005,1)) if t> 0.25 else 0 for t in T])
np.random.seed(0)
Perturbation = np.array([np.random.uniform(-1,1)*i*0.1 for i in Input])
s = cn.TransferFunction.s
TF = K/(s*((J*s+b)*(L*s+R)+K**2))
Once we define the input and the plant’s transfer function, we describe the controller’s structure. In this case, a feedback controller with a fuzzy control system (Figure 11).
Moreover, we define a control structure, including random perturbations expected as a 10% reference input or X s , to test the fuzzy controllers’ robustness (Figure 12).

3.4.1. One-Input Mamdani Fuzzy Controller

After defining the system, we design the fuzzy controller with premises and consequences concerning the error and the control behavior. Therefore, we implement these premises following the approach in Code 2. First, defining a single error input with a universe in the ranges 100 , 100 of the angular position in rad and then a single output controller with a universe 20 , 20 volt (V). Figure 13 shows the premise, and Figure 14 shows the consequence.
Subsequently obtaining premises and consequences, we define the rules for the FIS using the approach described in Code 3. The fuzzy rules used for the controller are:
  • IF Error is NegativeControl is Negative;
  • IF Error is ZeroControl is Zero;
  • IF Error is PositiveControl is Positive.
Next, we set the system configuration given in Table 2 for a Mamdani inference system. Then, we simulate the FIS. The surface generated for the simulation of the rules is shown in Figure 15.
Then, using the inference system defined and stored in the variable Mamdani1, we create an instance of the class fuzzy_controller, passing the inference system, the type of inference system (in this case, fuzzy with one input), the transfer function, and the sampling time, as in line 1 of Code 5. Line 2 builds the controller. Line 3 provides the fuzzy controller for simulation with processes defined in the space of the state model. Finally, line 4 returns the system with the transfer function for simulation.
Code 5. Python code for defining fuzzy controller with UPAFuzzySystems library.
1
2
3
4
FuzzController = UPAfs.fuzzy_controller(Mamdani1,typec='Fuzzy1',tf=TF,DT = T[1])
FuzzController.build()
FuzzControllerBlock = FuzzController.get_controller()
FuzzSystemBlock = FuzzController.get_system()
Once we obtained the system with the transfer function for simulation in line 4 of Code 5, we solved the simulation using the control library in line 1 and plotted the results in lines 2–7 of Code 6.
Code 6. Python code for simulation of fuzzy controller and plotting results.
1
2
3
4
5
6
7
T, Theta = cn.input_output_response(FuzzSystemBlock,T,Input,0)
plt.plot(T,Theta,label='Process Variable')
plt.plot(T,Input,label='Reference')
plt.xlabel("time (s)")
plt.ylabel("position (rad)")
plt.legend()
plt.show()
Figure 16 shows the simulation results of the DC motor position (blue) controlled with the fuzzy controller while summited to the input created (orange). The absolute error is 1.92 × 10 11 , and the absolute percentage error is 2.45 × 10 9 % in steady conditions. The maximum value reached is 8.47 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.85 × 10 1 rad, the time rising is 3.86 × 10 2 s, the overshoot is 7.87 %, the time peak is 7.46 × 10 2 s, and the settling time is 1.25 × 10 1 s.
Similarly, we test the Mamdani controller with one input, but this time including the disturbances at the input of the plant as in the block diagram in Figure 12. The obtained results are shown in Figure 17.
Figure 17 shows the simulation results of the DC motor position (blue) controlled with the fuzzy controller while summited to the input created (orange) and the disturbance signal (green). The absolute error is 1.20 × 10 2 , and the absolute percentage error is 1.53 % in steady conditions. The maximum value reached is 8.60 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.97 × 10 1 rad, the time rising is 3.86 × 10 2 s, the overshoot is 7.86 %, the time peak is 7.26 × 10 2 s, and the settling time is 6.38 × 10 1 s.

3.4.2. One-Input FLS Controller

Using the same rules as for the Mamdani controller with one input and the same fuzzy sets, we define the FLS controller. Nevertheless, we configure the UPAFuzzySystems library to work with the FLS structure and modify the connectives and implications described in Table 2. Code 7 shows the lines that define the inference system for the FLS controller. Line 7 is the one that differs from a Mamdani controller and configures the inference system for the FLS controller structure.
Code 7. Lines for defining one input FLS controller in the UPAFuzzySystems library.
1
2
3
4
5
6
7
8
FLS1 = UPAfs.inference_system('FLS controller')
FLS1.add_premise(Error_universe)
FLS1.add_consequence(Control_universe)
FLS1.add_rule([['Error','negative']],[],[['Control','negative']])
FLS1.add_rule([['Error','zero']],[],[['Control','zero']])
FLS1.add_rule([['Error','positive']],[],[['Control','positive']])
FLS1.configure('FLSmidth')
FLS1.build()
For coding the FLS inference system, we simulate the system’s surface over the input universe using the method described in Code 3 and obtain the results shown in Figure 18.
In the next step, we build and simulate the controller using the methods in Code 5 and Code 6. The simulation results show a smoother response than the Mamdani controller and avoid overshooting the system response (Figure 19). The absolute error is 1.35 × 10 3 , and the absolute percentage error is 1.72 × 10 1 % at steady state. The maximum value achieved is 7.84 × 10 1 rad, the minimum value 2.39 × 10 16 rad, the start value 8.40 × 10 17 rad, the final value 7.84 × 10 1 rad, the rise time 1.71 × 10 1 s, the overshoot 0.00 %, the peak time 7.22 × 10 1 s, and the settling time 3.71 × 10 1 s.
Similarly, we test the FLS controller with one input, including the disturbances at the input of the plant, as in the block diagram in Figure 12. The results are shown in Figure 20.
Figure 20 shows the results of the FLS controller with one input and signal disturbance for the DC motor position (blue) while summited to the input created (orange) and the disturbance signal (green). The absolute error is 7.18 × 10 3 , and the absolute percentage error is 9.14 × 10 1 % in steady conditions. The maximum value reached is 7.93 × 10 1 rad, the minimum value is 2.39 × 10 16 rad, the start value is 8.40 × 10 17 rad, the end value is 7.93 × 10 1 rad, the time rising is 1.99 × 10 1 s, the overshoot is 0.00 %, the time peak is 7.22 × 10 1 s, and the settling time is 6.80 × 10 1 s.

3.4.3. One Input Takagi–Sugeno Controller

In this case, we define a control sequence in the Takagi–Sugeno configuration, as shown in Table 2. The premise is the error as in the Mamdani and FLS controllers with one input, but now we define two fuzzy sets. Therefore, the consequent definition has two functions related to the error in the premises. Afterward, we introduce the rules with the inference system class. The Python code to define the fuzzy universe with fuzzy sets and the inference system for the Takagi–Sugeno controller with one input is in Code 8.
Lines 1–4 of Code 8 define the fuzzy sets of the premise error using the same procedure as for the Mamdani and FLS controllers. Next, lines 10 and 13 specify the fuzzy sets consequence with second-order functions defined concerning the input denoted by x. Then, lines 19–23 describe the premises, consequences, and rules. After that, line 25 configures the FIS to work with the Takagi–Sugeno structure in Table 2. Finally, line 27 builds the controller.
Code 8. Python code for the fuzzy universe and inference system for the one-input Takagi–Sugeno controller.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Error_universe = UPAfs.fuzzy_universe('Error', np.arange(-100,101,1), 'continuous')
Error_universe.add_fuzzyset('negative','trimf',[-200,-100,100])
Error_universe.add_fuzzyset('positive','trimf',[-100,100,200])
Error_universe.view_fuzzy()
ax = plt.gca()
ax.set_xlabel("Error (rad)")
ax.set_ylabel("$\mu$")
plt.show()
 
Control_universe = UPAfs.fuzzy_universe('Control', np.arange(-20,22,2), 'continuous')
Control_universe.add_fuzzyset('negative','eq','-0.001*(x[0])**2+0.4*x[0]')
Control_universe.add_fuzzyset('positive','eq','0.001*(x[0])**2+0.4*x[0]')
Control_universe.view_fuzzy()
ax = plt.gca()
ax.set_xlabel("Control (Volts)")
ax.set_ylabel("$\mu$")
plt.show()
 
TSG1 = UPAfs.inference_system('Takagi-Sugeno One Input')
TSG1.add_premise(Error_universe)
TSG1.add_consequence(Control_universe)
TSG1.add_rule([['Error','negative']],[],[['Control','negative']])
TSG1.add_rule([['Error','positive']],[],[['Control','positive']])
 
TSG1.configure('Sugeno')
 
TSG1.build()
Figure 21 and Figure 22 display the plots of the premise and consequences of the fuzzy sets in the one-input Takagi–Sugeno controller. The consequence plot shows the functions depending on the error premise.
The surface simulation using the method described in Code 3 is shown in Figure 23, where the output is a second-order response resulting from the second-order functions in the output universe.
Finally, we configure the controller using the same method as in Code 5 and present the simulation results with the approach in Code 6. Figure 24 shows the results of the simulation. The absolute error is 1.22 × 10 8 , and the absolute percentage error is 1.55 × 10 6 % under stable conditions. The maximum value obtained is 7.85 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.85 × 10 1 rad, the rise time is 1.25 × 10 1 s, the overshoot is 0.00 %, the peak time is 7.22 × 10 1 s, and the settling time is 1.91 × 10 1 s.
Again, we test the Takagi–Sugeno controller with one input, including the disturbance at the input of the plant, as in the block diagram in Figure 12. The results are in Figure 25.
Figure 25 shows the results of the Takagi–Sugeno controller with one input and signal disturbance for the DC motor position (blue) while summited to the input created (orange) and the disturbance signal (green). The absolute error is 1.35 × 10 2 , and the absolute percentage error is 1.72 % in steady conditions. The maximum value reached is 8.00 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.99 × 10 1 rad, the time rising is 1.33 × 10 1 s, the overshoot is 1.60 × 10 1 %, the time peak is 5.28 × 10 1 s, and the settling time is 6.54 × 10 1 s.

3.4.4. Two-Input Mamdani Controller

The definition of the two-input Mamdani controller implies the definition of two premises, the first for the error and the second as the change in it or its derivative. Therefore, we specify three universes: one for the error premise, one for the change in the error premise, and another for the control consequence. The required code uses the same approach as in Code 2. The premises and consequences for the two-input Mamdani controller are in Figure 26, Figure 27 and Figure 28.
Having specified the premises and consequences of the two-input Mamdani controller, we define the nine rules in the instance of the inference_system class of the UPAFuzzySystems library for each possible combination between inputs, following the structure described in Table 3. Thus, the specified rules are:
  • If error is Neg and change error is Neg then control is Neg;
  • If error is Neg and change error is Zero then control is Neg;
  • If error is Zero and change error is Neg then control is Zero;
  • If error is Neg and change error is Pos then control is Zero;
  • If error is Zero and change error is Zero then control is Zero;
  • If error is Zero and change error is Pos then control is Zero;
  • If error is Pos and change error is Neg then control is Zero;
  • If error is Pos and change error is Zero then control is Pos;
  • If error is Pos and change error is Pos then control is Pos.
The code to define these rules in the proposed library with the two-input Mamdani controller is in Code 9.
Code 9. Python code with rules for a two-input Mamdani controller using the UPAFuzzySystems library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Mamdani2 = UPAfs.inference_system('Mamdani')
Mamdani2.add_premise(Error_universe)
Mamdani2.add_premise(ChError_universe)
Mamdani2.add_consequence(Control_universe)
Mamdani2.add_rule([['Error','negative'],['Change Error','negative']],['and'],[['Control','negative']])
Mamdani2.add_rule([['Error','negative'],['Change Error','zero']],['and'],[['Control','negative']])
Mamdani2.add_rule([['Error','zero'],['Change Error','negative']],['and'],[['Control','zero']])
Mamdani2.add_rule([['Error','negative'],['Change Error','positive']],['and'],[['Control','zero']])
Mamdani2.add_rule([['Error','zero'],['Change Error','zero']],['and'],[['Control','zero']])
Mamdani2.add_rule([['Error','positive'],['Change Error','negative']],['and'],[['Control','zero']])
Mamdani2.add_rule([['Error','zero'],['Change Error','positive']],['and'],[['Control','zero']])
Mamdani2.add_rule([['Error','positive'],['Change Error','zero']],['and'],[['Control','positive']])
Mamdani2.add_rule([['Error','positive'],['Change Error','positive']],['and'],[['Control','positive']])
Mamdani2.configure('Mamdani')
Mamdani2.build()
Then, we obtain the surface response for the two-input Mamdani controller by simulating the error and the change in error inputs as defined in Code 3. Nevertheless, we now use two inputs for the surface simulation. Figure 29 shows the simulated surface.
After defining the rules and simulating the surface, we configure the fuzzy_controller as before, but this time, we set the type of the controller to Fuzzy2, since the controller now has two inputs. The Python code for the configuration changes only in line one with the Fuzzy2 definition (Code 10).
Code 10. Python code for configuring two-input Mamdani controller using the UPAFuzzySystems library.
1
2
3
4
MamdaniController = UPAfs.fuzzy_controller(Mamdani2,typec='Fuzzy2',tf=TF,DT = T[1])
MamdaniController.build()
MamdaniControllerBlock = MamdaniController.get_controller()
MamdaniSystemBlock = MamdaniController.get_system()
Finally, we simulate the controller using the approach described in Code 6. Figure 30 shows the obtained controller’s response. The absolute error is 2.00 × 10 6 rad, and the absolute percentage error is 2.54 × 10 4 % at steady state. The maximum value reached is 8.12 × 10 1 rad, the minimum value is 7.99 × 10 18 rad, the start value is 4.28 × 10 18 rad, the final value is 7.85 × 10 1 rad, the rise time is 2.66 × 10 2 s, the overshoot time is 3.33 %, the peak time is 4.46 × 10 2 s, and the settling time is 1.47 × 10 1 s.
Similarly, we test the Mamdani controller with two inputs, including the disturbances at the input of the plant, as shown in the block diagram in Figure 12. The results are shown in Figure 31.
Figure 31 shows the results of the Mamdani controller with two inputs and signal disturbance for the DC motor position (blue) while summited to the input created (orange) and the disturbance signal (green). The absolute error is 2.88 × 10 2 , and the absolute percentage error is 3.66 % in steady conditions. The maximum value reached is 8.25 × 10 1 rad, the minimum value is 7.99 × 10 18 rad, the start value is 4.28 × 10 18 rad, the end value is 7.57 × 10 1 rad, the time rising is 2.66 × 10 2 s, the overshoot is 9.09 %, the time peak is 6.62 × 10 1 s, and the settling time is 7.14 × 10 1 s.

3.4.5. Two-Input FLS Controller

Using the same rules as for the two-input Mamdani controller and the same fuzzy sets, we define the two-input FLS controller. Nevertheless, we configure the UPAFuzzySystems library to work with the FLS structure and change the connectives and implications described in Table 2. Figure 32 displays the simulated surface for the two-input FLS controller.
Finally, we simulate the FLS controller using the approach described in Code 6. Figure 33 represents the controller’s response obtained. This time, the absolute error is 3.59 × 10 4 , and the absolute percentage error is 4.58 × 10 2 %. The maximum value reached is 7.85 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the final value is 7.85 × 10 1 rad, the rise time is 2.37 × 10 1 s, the overshoot is 0.00 %, the time peak is 7.22 × 10 1 s, and the settling time is 3.79 × 10 1 s.
Again, we test the FLS controller with two inputs, including the disturbances at the input of the plant, as shown in the block diagram in Figure 12. The results are shown in Figure 34.
Figure 34 shows the results of the FLS controller with two inputs and signal disturbance for the DC motor position (blue) while summited to the input created (orange) and the disturbance signal (green). The absolute error is 1.11 × 10 2 , and the absolute percentage error is 1.41 % in steady conditions. The maximum value reached is 7.96 × 10 1 rad, the minimum value is 5.79 × 10 5 rad, the start value is 0.00 rad, the end value is 7.96 × 10 1 rad, the time rising is 2.67 × 10 1 s, the overshoot is 0.00 %, the time peak is 7.22 × 10 1 s, and the settling time is 6.74 × 10 1 s.

3.4.6. Two Inputs Takagi–Sugeno Controller

In this case, we define the control consequence in the Takagi–Sugeno configuration for two inputs, as described in Table 1. The premises are identical to the Mamdani and FLS controllers with two inputs. Therefore, the consequence definition now has three functions related to the error and change in error premises. Next, we introduce the rules with the inference system class. Again, these are the same as the Mamdani and FLS controllers with two inputs. The Python code to define the fuzzy universe with fuzzy sets and the inference system for the Takagi–Sugeno controller with two inputs is in Code 11.
Code 11. Code for defining the inference system and the fuzzy universe for the two-input Takagi–Sugeno controller.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Error_universe = UPAfs.fuzzy_universe('Error', np.arange(-100,101,1), 'continuous')
Error_universe.add_fuzzyset('negative','trapmf',[-100,-100,-40,0])
Error_universe.add_fuzzyset('zero','trimf',[-10,0,10])
Error_universe.add_fuzzyset('positive','trapmf',[0,40,100,100])
Error_universe.view_fuzzy()
 
ChError_universe = UPAfs.fuzzy_universe('Change Error', np.arange(-100,101,1), 'continuous')
ChError_universe.add_fuzzyset('negative','trapmf',[-100,-100,-40,0])
ChError_universe.add_fuzzyset('zero','trimf',[-10,0,10])
ChError_universe.add_fuzzyset('positive','trapmf',[0,40,100,100])
ChError_universe.view_fuzzy()
 
Control_universe = UPAfs.fuzzy_universe('Control', np.arange(-20,22,2), 'continuous')
Control_universe.add_fuzzyset('negative','eq','0.8*x[0]+0.1*x[1]')
Control_universe.add_fuzzyset('zero','eq','0.8*x[0]+0.005*x[1]')
Control_universe.add_fuzzyset('positive','eq','0.8*x[0]+0.1*x[1]')
Control_universe.view_fuzzy()
 
TSG2 = UPAfs.inference_system('Takagi-Sugeno Two Inputs')
TSG2.add_premise(Error_universe)
TSG2.add_premise(ChError_universe)
TSG2.add_consequence(Control_universe)
 
TSG2.add_rule([['Error','negative'],['Change Error','negative']],['and'],[['Control','negative']])
TSG2.add_rule([['Error','negative'],['Change Error','zero']],['and'],[['Control','negative']])
TSG2.add_rule([['Error','zero'],['Change Error','negative']],['and'],[['Control','zero']])
TSG2.add_rule([['Error','negative'],['Change Error','positive']],['and'],[['Control','zero']])
TSG2.add_rule([['Error','zero'],['Change Error','zero']],['and'],[['Control','zero']])
TSG2.add_rule([['Error','positive'],['Change Error','negative']],['and'],[['Control','zero']])
TSG2.add_rule([['Error','zero'],['Change Error','positive']],['and'],[['Control','zero']])
TSG2.add_rule([['Error','positive'],['Change Error','zero']],['and'],[['Control','positive']])
TSG2.add_rule([['Error','positive'],['Change Error','positive']],['and'],[['Control','positive']])
 
TSG2.configure('Sugeno')
 
TSG2.build()
Figure 35 displays the output consequence, which differs from the two-input Mamdani and FLS controllers, in that the output premises are functions of the error and its change. Here, the premises are identical to those in Figure 26 and Figure 27.
After defining the universes and configuring the FIS, we simulate the Takagi–Sugeno surface reaction with two inputs according to the method in Code 3. The results are in Figure 36.
We then define the controller configuration according to Code 5, modified for the Takagi–Sugeno structure, and plot the results using the approach in Code 6. Figure 37 shows the controller response. The absolute error is 3.16 × 10 12 , and the absolute percentage error is 4.02 × 10 10 % under stable conditions. The maximum value obtained is 7.96 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the final value is 7.85 × 10 1 rad, the rise time is 6.66 × 10 2 s, the overshoot is 1.37 %, the time peak is 1.21 × 10 1 s, and the settling time is 8.47 × 10 2 s.
Similarly, we test the Takagi–Sugeno controller with two inputs, including the disturbances at the input of the plant, as shown in the block diagram in Figure 12. The results are in Figure 38.
Figure 38 shows the results of the two-input Takagi–Sugeno controller with signal disturbance for the DC motor position (blue) while summited to the input created (orange) and the disturbance signal (green). The absolute error is 1.07 × 10 2 , and the absolute percentage error is 1.37 % in steady conditions. The maximum value reached is 8.02 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.96 × 10 1 rad, the time rising is 6.66 × 10 2 s, the overshoot is 7.07 × 10 1 %, the time peak is 5.22 × 10 1 s, and the settling time is 6.34 × 10 1 s.

3.5. P, PD, and PID Fuzzy Controllers

3.5.1. Linear P Fuzzy Controller

The UPAFuzzySystems library also contains the linear P controller with the structure shown in Figure 5. To work with this structure, the configured inference base configured is linear. Therefore, the fuzzy_universe and inference_system classes configure the premises, consequences, and rules to define a linear controller. Since the P fuzzy controller requires no change in error input, the linear system definition uses only a premise in the error case and a consequence controller, which are comparable in Table 2. Code 12 provides the premise, consequence, and required rules.
Code 12. Code for defining the one-input fuzzy linear system used in the linear P fuzzy controller.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Error_universe = UPAfs.fuzzy_universe('Error', np.arange(-100,101,1), 'continuous')
Error_universe.add_fuzzyset('negative','trimf',[-200,-100,100])
Error_universe.add_fuzzyset('positive','trimf',[-100,100,200])
Error_universe.view_fuzzy()
ax = plt.gca()
ax.set_xlabel("Error (rad)")
ax.set_ylabel("$\mu$")
plt.show()
 
Control_universe = UPAfs.fuzzy_universe('Control', np.arange(-200,202,2), 'continuous')
Control_universe.add_fuzzyset(‘negative’,’ eq’, ‘-200’)
Control_universe.add_fuzzyset('positive','eq','200')
Control_universe.view_fuzzy()
ax = plt.gca()
ax.set_xlabel("Control (Volts)")
ax.set_ylabel("$\mu$")
plt.show()
 
LinearP = UPAfs.inference_system('Linear One Input')
LinearP.add_premise(Error_universe)
LinearP.add_consequence(Control_universe)
LinearP.add_rule([['Error','negative']],[],[['Control','negative']])
LinearP.add_rule([['Error','positive']],[],[['Control','positive']])
 
LinearP.configure('Linear')
 
LinearP.build()
Figure 39 and Figure 40 display the premise and consequence plot for the linear system defined for working with the linear fuzzy P controller according to Table 2.
Employing the same approach in Code 3, we simulate the linear system’s surface, which must be a line, as shown in Figure 41.
When the linear inference system is defined, the linear P fuzzy controller must include the G U and G E gains according to Equations (21) and (22). The Python code for configuring the controller with those gains is in Code 13.
Code 13. Python code for configuring the linear fuzzy proportional controller with the UPAFuzzySystems library.
1
2
3
4
LinearPFuzzController = UPAfs.fuzzy_controller(LinearP,typec='P',tf=TF,DT = T[1], GE=15.91545709, GU=0.094248)
LinearPFuzzController.build()
LinearPFuzzControllerBlock = LinearPFuzzController.get_controller()
LinearPFuzzSystemBlock = LinearPFuzzController.get_system()
After defining the controller, we simulate it following the method in Code 6. The controller response is shown in Figure 42. The absolute error is 1.26 × 10 9 , and the absolute percentage error is 1.60 × 10 7 % in steady conditions. The maximum value reached is 9.88 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the final value is 7.85 × 10 1 rad, the rise time is 1.45 × 10 2 s, the overshoot is 2.58 × 10 + 1 %, the time peak is 3.26 × 10 2 s, and the settling time is 1.27 × 10 1 s.
Similarly, we test the linear proportional fuzzy controller, including the disturbances at the input of the plant, as shown in the block diagram in Figure 12. The results are in Figure 43.
Figure 43 shows the results of the linear proportional fuzzy controller with signal disturbance for the DC motor position (blue) while summited to the input created (orange) and the disturbance signal (green). The absolute error is 3.60 × 10 3 , and the absolute percentage error is 4.58 × 10 1 % in steady conditions. The maximum value reached is 9.93 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.89 × 10 1 rad, the time rising is 1.45 × 10 2 s, the overshoot is 2.58 × 10 + 1 %, the time peak is 3.46 × 10 2 s, and the settling time is 1.65 × 10 1 s.

3.5.2. Linear PD Fuzzy Controller

The Linear PD controller with the structure defined in Figure 6 implies working with a linear inference base with two inputs. Therefore, the class fuzzy_universe and inference_system configure the premises, consequences, and rules to define a two-input linear controller. The PD fuzzy controller requires two premises: the error and its change. A consequence controller, comparable in Table 2, obtains the premise, consequence, and rules needed for a two-input linear fuzzy system. Code 14 shows the complete implementation of the linear fuzzy system with two inputs.
Code 14. Code for defining the two-input fuzzy linear system with UPAFuzzySystems library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Error_universe = UPAfs.fuzzy_universe('Error', np.arange(-100,101,1), 'continuous')
Error_universe.add_fuzzyset('negative','trimf',[-200,-100,100])
Error_universe.add_fuzzyset('positive','trimf',[-100,100,200])
Error_universe.view_fuzzy()
ax = plt.gca()
ax.set_xlabel("Error (rad)")
ax.set_ylabel("$\mu$")
plt.show()
 
ChError_universe = UPAfs.fuzzy_universe('Change Error', np.arange(-100,101,1), 'continuous')
ChError_universe.add_fuzzyset('negative','trimf',[-200,-100,100])
ChError_universe.add_fuzzyset('positive','trimf',[-100,100,200])
ChError_universe.view_fuzzy()
ax = plt.gca()
ax.set_xlabel(r"Change Error ($\frac{rad}{s}$)")
ax.set_ylabel("$\mu$")
plt.show()
 
Control_universe = UPAfs.fuzzy_universe('Control', np.arange(-200,202,2), 'continuous')
Control_universe.add_fuzzyset(‘negative’,’ eq’, ‘-200’)
Control_universe.add_fuzzyset(‘zero’, ‘eq’, ‘0’)
Control_universe.add_fuzzyset('positive','eq','200')
Control_universe.view_fuzzy()
ax = plt.gca()
ax.set_xlabel("Control (Volts)")
ax.set_ylabel("$\mu$")
plt.show()
 
Linear = UPAfs.inference_system('Linear')
Linear.add_premise(Error_universe)
Linear.add_premise(ChError_universe)
Linear.add_consequence(Control_universe)
 
Linear.add_rule([['Error','negative'],['Change Error','negative']],['and'],[['Control','negative']])
Linear.add_rule([['Error','negative'],['Change Error','positive']],['and'],[['Control','zero']])
Linear.add_rule([['Error','positive'],['Change Error','negative']],['and'],[['Control','zero']])
Linear.add_rule([['Error','positive'],['Change Error','positive']],['and'],[['Control','positive']])
 
Linear.configure('Linear')
 
Linear.build()
Lines 4 and 13 in Code 14 produce the plots of the error and change in error premises, as shown in Figure 44 and Figure 45, respectively. The control consequence plot obtained with line 23 is shown in Figure 46.
After defining the two-input linear inference system, we simulate its surface following the method described in Code 3. Figure 47 shows the obtained surface with the two-input linear system.
After obtaining the linear inference system, the definition of the linear PD fuzzy controller must include the G U , G E , and G C E gains defined in Equations (21)–(23). The Python code configures the PD fuzzy controller with those gains in Code 15.
Code 15. Python code for configuring the PD fuzzy controller in the UPAFuzzySystems library.
1
 
2
3
4
LinearPDFuzzController = UPAfs.fuzzy_controller(Linear,typec='PD',tf=TF,DT = T[1], GE=15.91545709, GU=0.094248, GCE=0.636618283)
LinearPDFuzzController.build()
LinearPDFuzzControllerBlock = LinearPDFuzzController.get_controller()
LinearPDFuzzSystemBlock = LinearPDFuzzController.get_system()
Finally, we simulate the linear PD fuzzy controller following the approach in Code 6. The response of the system is in Figure 48. The absolute error is 1.83 × 10 7 , and the absolute percentage error is 2.33 × 10 5 % in steady conditions. The maximum value achieved is 7.85 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.85 × 10 1 rad, the rise time is 3.86 × 10 2 s, the overshoot is 0.00 %, the peak time is 7.22 × 10 1 s, and the settling time is 1.23 × 10 1 s.
Again, we test the linear PD fuzzy controller, including the disturbances at the input of the plant, as shown in the block diagram in Figure 12. The results are shown in Figure 49.
Figure 49 shows the results of the linear PD fuzzy controller with signal disturbance for the DC motor position (blue) while summited to the input created (orange) and the disturbance signal (green). The absolute error is 3.47 × 10 3 , and the absolute percentage error is 4.42 × 10 1 % in steady conditions. The maximum value reached is 7.91 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.89 × 10 1 rad, the time rising is 3.86 × 10 2 s, the overshoot is 2.60 × 10 1 %, the time peak is 5.18 × 10 1 s, and the settling time is 1.61 × 10 1 s.

3.5.3. Linear PD-I Fuzzy Controller

The Linear PD-I fuzzy controller uses as a basis a two-input linear system as the linear PD controller. Therefore, we use the same system defined in Code 14. However, now, we add a third gain and the integration connection as shown in Figure 7. Thus, the simulation surface of the two-input linear system is the same as in Figure 47.
After that, we specify the configuration of the PD-I fuzzy controller with its gains G U , G E ,   G C E , and G I E defined in Equations (21)–(24). The Python code for configuring the PD-I fuzzy controller with its gains is in Code 16.
Code 16. Configuration of PD-I fuzzy controller with the UPAFuzzySystems library.
1
 
2
3
4
LinearPidFuzzController = UPAfs.fuzzy_controller(Linear,typec='PD-I',tf=TF,DT = T[1], GE=15.91545709, GU=0.094248, GCE=0.636618283, GIE=7.234298678)
LinearPidFuzzController.build()
LinearPidFuzzControllerBlock = LinearPidFuzzController.get_controller()
LinearPidFuzzSystemBlock = LinearPidFuzzController.get_system()
Finally, we simulate the controller following the approach in Code 6. The output response is shown in Figure 50. The absolute error is 5.77 × 10 15 , and the absolute percentage error is 7.35 × 10 13 % in steady conditions. The maximum value achieved is 7.85 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.85 × 10 1 rad, the time rising is 1.05 × 10 2 s, the overshoot is 0.00 %, the time peak is 7.22 × 10 1 s, and the settling time is 3.46 × 10 2 s.
Similarly, we test the linear PD-I fuzzy controller, including the disturbances at the input of the plant, as shown in the block diagram in Figure 12. The results are in Figure 51.
Figure 51 shows the results of the linear PD-I fuzzy controller with signal disturbance for the DC motor position (blue) while summited to the input created (orange) and the disturbance signal (green). The absolute error is 2.05 × 10 3 , and the absolute percentage error is 2.61 × 10 1 % in steady conditions. The maximum value reached is 7.91 × 10 1 rad, the minimum value is 0.00 rad, the start value is 0.00 rad, the end value is 7.87 × 10 1 rad, the time rising is 1.05 × 10 2 s, the overshoot is 4.11 × 10 1 %, the time peak is 5.12 × 10 1 s, and the settling time is 3.46 × 10 2 s.

3.6. Controllers Comparison

Each fuzzy controller implemented using the UPAFuzzySystems library has different control characteristics produced in the control response. These characteristics are the absolute error (absolute_error), the absolute percentage error (percentage_error), the maximum value (max_value), the minimum value (min_value), the start value (start_value), the end value (end_value), the time rising (time_rising), the overshoot (overshoot), the time peak (time_overshoot), and the settling time (settling_time). Table 5 compares the control characteristics for each controller applied to the position control of a DC motor.
Additionally, Figure 52 compares graphically the control responses of all fuzzy controllers implemented following an input or reference with an angular position of 45 degrees (7.85 × 10−1 rad). As the figure shows, there are variations in the overshoot, time rising, settling time, and time peak. Still, all the controllers maintain an error below 1% of the reference in steady conditions.
The more essential features of a controlled system are the absolute error, the absolute percentage error, the overshoot, the settling time, and the time rising. These features identify the response speed of the controller, its stability, and how well it follows the reference. Maintaining them near zero improves the controller response. As Figure 53 shows, the fuzzy linear PD-I controller has the best results controlling the position of a DC motor.
However, for controlling the position of the DC motor, all the controllers maintain the reference error minimally, and the settling time is also similar between them. At the same time, the more significant dispersions are in the overshoot and the time to achieve it, as the boxplot in Figure 54 shows.
We also performed an ANOVA test to verify our conclusions about the best controllers and their features of interest without disturbances. We evaluated the test without overshooting features because not all the controllers have overshot. Using the features absolute error, percentage error, settling time, and time rising as groups, we obtained the results in Table 6, which support that the fuzzy linear PD-I controller has the best results among all the fuzzy controllers because of the p v a l u e = 0.000172 .
Moreover, we also repeat the comparison between controllers analyzing their responses with disturbances in Table 7.
Additionally, we compare graphically the control responses of all fuzzy controllers implemented following an input or reference with an angular position of 45 degrees ( 7.85 × 10 1 rad) and 10% of the input reference uniform random disturbances (Figure 55). As the figure shows, there are variations in the overshoot, time rising, settling time, and time peak. However, due to the disturbances, all the controllers now increase the error to 3.66% of the reference in steady conditions.
Again, we also compare the more essential features of a controlled system with the fuzzy controllers and signal disturbances. As Figure 56 shows, the fuzzy linear PD-I controller has the best results controlling a DC motor’s position and is the most robust.
However, for controlling the position of the DC motor, all the controllers maintain a reference error below 4% of error, and the settling time is also similar between them. At the same time, the more significant dispersions are in the overshoot and the time to achieve it, as the boxplot in Figure 57 shows.
We also performed an ANOVA test to verify our conclusions about the best controllers and their features of interest with disturbances. Again, we evaluated the test without overshooting features because not all the controllers have overshot. Using the features absolute error, percentage error, settling time, and time rising as groups, we obtained the results in Table 8, which support that the fuzzy linear PD-I controller has the best results among all the fuzzy controllers in the presence of disturbances because of the p v a l u e = 0.000038 .

4. Conclusions

This work proposes the UPAFuzzySystems library for designing inference systems with fuzzy logic, simulation, and control with fuzzy controllers, transfer functions, and state-space models in discrete and continuous universes. To our knowledge, this proposal is the only open-source Python library that integrates these functions.
Section 3.2 showed that the UPAFuzzySystems library could define fuzzy universes for different situations, such as collision distance and recommended speed. Section 3.3 shows how to specify the rules in an FIS system linking premises, connectives, and consequences. Moreover, the library successfully simulates the problem’s surface response with input arrays defining the inputs for the premise and obtaining its corresponding consequence in a continuous universe.
Additionally, the UPAFuzzySystems library also successfully controls and simulates the position of a DC motor plant (transfer function defined in Table 4 and codified in Code 4) with the fuzzy controllers: one-input Mamdani controller in Section 3.4.1, one-input FLS controller in Section 3.4.2, Takagi–Sugeno controller in Section 3.4.3, two-input Mamdani controller in Section 3.4.4, two-input FLS controller in Section 3.4.5, two-input Takagi–Sugeno controller in Section 3.4.6. Similarly, the UPAFuzzySystems library allows the implementation of one-input and two-input linear fuzzy systems, together with the P (Section 3.5.1), PD (Section 3.5.2), and PD-I (Section 3.5.3) controllers.
The controllers implemented using the proposed library reduce the steady error below 1% without disturbances and below 4% in the presence of 10% uniform random disturbances. Furthermore, we obtained interest features in control systems, such as overshoot, steady time, time rising, and time peak, that vary depending on the controller. Those variations occur because of the different changes in the control structures, such as the premises, the consequences, the connectives, the implication, the fuzzification, and the defuzzification methods. Moreover, some of these structures even include derivatives that allow predicting changes in the error behavior or integrals that allow gradual error reduction.
After performing an ANOVA analysis with the values of the features and the error with each controller, supported with a p v a l u e = 0.000038 , we concluded that the PD-I controller obtains the best error reduction and features of interest. These best features include a faster response, no overshoot, and no oscillations after reaching the reference, even in the presence of disturbances, as detailed in Section 3.6.
Our proposal successfully implements different fuzzy structures for designing FISs systems in general problems or controlling the position of a DC motor. However, following the codes we use in this work, UPAFuzzySystems can help researchers and designers solve problems in general applications with FISs or even use fuzzy controllers in the literature to control and simulate other transfer functions or state-space models.

Future Work

Although our proposal offers several control structures mechanisms for designing and simulating FISs and fuzzy controllers, the users would also benefit if there were methods for simplifying the implementation process. Thus, we will develop mechanisms for automatically configuring the implementation of the controllers in embedded systems based on the Raspberry Pi and Arduino platforms.

Author Contributions

Conceptualization M.M.R. and E.O.-G.; methodology; software M.M.R.; validation, all authors; formal analysis, M.M.R. and E.O.-G.; investigation, all authors; resources, all authors; data curation M.M.R. and E.O.-G.; writing—original draft preparation, M.M.R. and N.E.-G.; writing—review and editing, all authors; visualization, E.O.-G. and N.E.-G.; supervision, M.M.R.; project administration, M.M.R. and N.E.-G.; funding acquisition, E.O.-G. and N.E.-G. All authors have read and agreed to the published version of the manuscript.

Funding

We acknowledge the support of Instituto Tecnológico de Pabellón and the Consejo Nacional de Ciencia y Tecnología (CONA-CYT) in Mexico for supporting this work through funds for projects INFRA-2016-01, Project No. 270665. CB-2016-01, Project No. 287818.

Data Availability Statement

Not applicable.

Conflicts of Interest

The authors declare no conflict of interest.

References

  1. Astrom, K.J.; Wittenmark, B. Adaptive Control—Astrom, 2nd ed.; Courier Corporation: North Chelmsford, MA, USA, 2008; pp. 17–38. [Google Scholar]
  2. Spirin, N.A.; Rybolovlev, V.Y.; Lavrov, V.V.; Gurin, I.A.; Schnayder, D.A.; Krasnobaev, A.V. Scientific Problems in Creating Intelligent Control Systems for Technological Processes in Pyrometallurgy Based on Industry 4.0 Concept. Metallurgist 2020, 64, 574–580. [Google Scholar] [CrossRef]
  3. Åström, K.J.; Wittenmark, B. Computer-Controlled Systems: Theory and Design; Courier Corporation: North Chelmsford, MA, USA, 2011; p. 557. [Google Scholar]
  4. Tarbosh, Q.A.; Aydogdu, O.; Farah, N.; Talib, M.H.N.; Salh, A.; Cankaya, N.; Omar, F.A.; Durdu, A. Review and Investigation of Simplified Rules Fuzzy Logic Speed Controller of High Performance Induction Motor Drives. IEEE Access 2020, 8, 49377–49394. [Google Scholar] [CrossRef]
  5. Jang, J.S.R.; Sun, C.T.; Mizutani, E. Neuro-Fuzzy and Soft Computing-A Computational Approach to Learning and Machine Intelligence [Book Review]. IEEE Trans. Autom. Control. 1997, 42, 482–1484. [Google Scholar] [CrossRef]
  6. Ferdaus, M.M.; Pratama, M.; Anavatti, S.G.; Garratt, M.A.; Lughofer, E. PAC: A Novel Self-Adaptive Neuro-Fuzzy Controller for Micro Aerial Vehicles. Inf. Sci. 2020, 512, 481–505. [Google Scholar] [CrossRef]
  7. Chen, Y.; Zhao, T.; Peng, J.; Mao, Y. Fuzzy Fraction-Order Stochastic Parallel Gradient Descent Approach for Efficient Fiber Coupling. Opt. Eng. 2022, 61, 016108. [Google Scholar] [CrossRef]
  8. Khanesar, M.A.; Branson, D. Robust Sliding Mode Fuzzy Control of Industrial Robots Using an Extended Kalman Filter Inverse Kinematic Solver. Energies 2022, 15, 1876. [Google Scholar] [CrossRef]
  9. Pereira, L.F.d.S.C.; Batista, E.; de Brito, M.A.G.; Godoy, R.B. A Robustness Analysis of a Fuzzy Fractional Order PID Controller Based on Genetic Algorithm for a DC-DC Boost Converter. Electronics 2022, 11, 1894. [Google Scholar] [CrossRef]
  10. Pozna, C.; Precup, R.E.; Horvath, E.; Petriu, E.M. Hybrid Particle Filter-Particle Swarm Optimization Algorithm and Application to Fuzzy Controlled Servo Systems. IEEE Trans. Fuzzy Syst. 2022, 30, 4286–4297. [Google Scholar] [CrossRef]
  11. Volosencu, C. MATLAB Applications in Engineering. Available online: https://www.researchgate.net/publication/358537759_MATLAB_Applications_in_Engineering (accessed on 16 February 2023).
  12. Zahmatkesh, S.; Klemeš, J.J.; Bokhari, A.; Rezakhani, Y.; Wang, C.; Sillanpaa, M.; Amesho, K.T.T.; Ahmed, W.S. Reducing Chemical Oxygen Demand from Low Strength Wastewater: A Novel Application of Fuzzy Logic Based Simulation in MATLAB. Comput. Chem. Eng. 2022, 166, 107944. [Google Scholar] [CrossRef]
  13. Maghfiroh, H.; Ahmad, M.; Ramelan, A.; Adriyanto, F. Fuzzy-PID in BLDC Motor Speed Control Using MATLAB/Simulink. J. Robot. Control 2022, 3, 8–13. [Google Scholar] [CrossRef]
  14. Mostafa, S.; Zekry, A.; Youssef, A.; Anis, W.R. Raspberry Pi Design and Hardware Implementation of Fuzzy-PI Controller for Three-Phase Grid-Connected Inverter. Energies 2022, 15, 843. [Google Scholar] [CrossRef]
  15. Lin, X.; Liu, X. Modeling and Control of One-Stage Inverted Pendulum Body Based on Matlab. J. Phys. Conf. Ser. 2022, 2224, 012107. [Google Scholar] [CrossRef]
  16. Kandemir, E.; Cetin, N.S.; Borekci, S. Single-Stage Photovoltaic System Design Based on Energy Recovery and Fuzzy Logic Control for Partial Shading Condition. Int. J. Circuit Theory Appl. 2022, 50, 1770–1792. [Google Scholar] [CrossRef]
  17. Taskin, A.; Kumbasar, T. An Open Source Matlab/Simulink Toolbox for Interval Type-2 Fuzzy Logic Systems. In Proceedings of the 2015 IEEE Symposium Series on Computational Intelligence (SSCI), Cape Town, South Africa, 7–10 December 2015; pp. 1561–1568. [Google Scholar] [CrossRef]
  18. Wang, J.; Niu, X.; Zheng, L.; Zheng, C.; Wang, Y. Wireless Mid-Infrared Spectroscopy Sensor Network for Automatic Carbon Dioxide Fertilization in a Greenhouse Environment. Sensors 2016, 16, 1941. [Google Scholar] [CrossRef] [PubMed]
  19. Wang, C.C. Fuzzy Theory-Based Air Valve Control for Auto-Score-Recognition Soprano Recorder Machines. J. Robot. Netw. Artif. Life 2021, 8, 278–283. [Google Scholar] [CrossRef]
  20. Singh, S.; Kaur, M. Gain Scheduling of PID Controller Based on Fuzzy Systems. MATEC Web Conf. 2016, 57, 01008. [Google Scholar] [CrossRef]
  21. Mehmet Karadeniz, A.; Ammar, A.; Geza, H. Comparison between Proportional, Integral, Derivative Controller and Fuzzy Logic Approaches on Controlling Quarter Car Suspension System. MATEC Web Conf. 2018, 184, 02018. [Google Scholar] [CrossRef]
  22. TIOBE Index—TIOBE. Available online: https://www.tiobe.com/tiobe-index/ (accessed on 16 February 2023).
  23. Xu, B.; An, L.; Thung, F.; Khomh, F.; Lo, D. Why Reinventing the Wheels? An Empirical Study on Library Reuse and Re-Implementation. Empir. Softw. Eng. 2020, 25, 755–789. [Google Scholar] [CrossRef]
  24. Rueden, C.T.; Schindelin, J.; Hiner, M.C.; DeZonia, B.E.; Walter, A.E.; Arena, E.T.; Eliceiri, K.W. ImageJ2: ImageJ for the next Generation of Scientific Image Data. BMC Bioinform. 2017, 18, 529. [Google Scholar] [CrossRef] [PubMed]
  25. Macmillan, M.; Eurek, K.; Cole, W.; Bazilian, M.D. Solving a Large Energy System Optimization Model Using an Open-Source Solver. Energy Strategy Rev. 2021, 38, 100755. [Google Scholar] [CrossRef]
  26. Guo, Y.; Leitner, P. Studying the Impact of CI on Pull Request Delivery Time in Open Source Projects—A Conceptual Replication. PeerJ Comput. Sci. 2019, 5, e245. [Google Scholar] [CrossRef] [PubMed]
  27. Shields.Io: Quality Metadata Badges for Open Source Projects. Available online: https://shields.io/ (accessed on 24 January 2023).
  28. GitHub—Luferov/FuzzyLogicToolBox: Fuzzy Logic Library for Python. Available online: https://github.com/Luferov/FuzzyLogicToolBox (accessed on 23 January 2023).
  29. Avelar, E.; Castillo, O.; Soria, J. Fuzzy Logic Controller with Fuzzylab Python Library and the Robot Operating System for Autonomous Robot Navigation: A Practical Approach. Stud. Comput. Intell. 2020, 862, 355–369. [Google Scholar] [CrossRef]
  30. ITTcs/Fuzzylab: Fuzzylab, a Python Fuzzy Logic Library. Available online: https://github.com/ITTcs/fuzzylab (accessed on 23 January 2023).
  31. GitHub—Yudivian/Fuzzython: Fuzzy Logic and Fuzzy Inference Python 3 Library. Available online: https://github.com/yudivian/fuzzython (accessed on 24 January 2023).
  32. GitHub—Carmelgafa/Type2fuzzy: Type-2 Fuzzy Logic Library. Available online: https://github.com/carmelgafa/type2fuzzy (accessed on 24 January 2023).
  33. Fuzzylite/Pyfuzzylite: Pyfuzzylite: A Fuzzy Logic Control Library in Python. Available online: https://github.com/fuzzylite/pyfuzzylite (accessed on 24 January 2023).
  34. Montes Rivera, M. GitHub—UniversidadPolitecnicaAguascalientes/UPAFuzzySystems. Available online: https://github.com/UniversidadPolitecnicaAguascalientes/UPAFuzzySystems (accessed on 1 March 2023).
  35. Nguyen, H.T. A First Course in Fuzzy and Neural Control; Chapman & Hall/CRC Press: London, UK, 2003; ISBN 9781584882442. [Google Scholar]
  36. Jantzen, J. Foundations of Fuzzy Control: A Practical Approach, 2nd ed.; John Wiley & Sons: Hoboken, NJ, USA, 2013; pp. 1–325. [Google Scholar] [CrossRef]
  37. Dehghani, M.; Taghipour, M.; Gharehpetian, G.B.; Abedi, M. Optimized Fuzzy Controller for MPPT of Grid-Connected PV Systems in Rapidly Changing Atmospheric Conditions. J. Mod. Power Syst. Clean Energy 2021, 9, 376–383. [Google Scholar] [CrossRef]
  38. Sao, K.; Kumar Singh, D.; Agrawal, A.; Scholar, P.; Professor, A.; Raman, D. Study of DC Motor Position Control Using Root Locus and PID Controller in MATLAB. IJSRD-Int. J. Sci. Res. Dev. 2015, 3, 183–190. [Google Scholar]
Figure 1. CCS diagram, including the ADC and DAC converters.
Figure 1. CCS diagram, including the ADC and DAC converters.
Machines 11 00572 g001
Figure 2. Python usage comparison according to the TIOBE index [22]. Source: www.tiobe.com (accessed on 16 February 2023).
Figure 2. Python usage comparison according to the TIOBE index [22]. Source: www.tiobe.com (accessed on 16 February 2023).
Machines 11 00572 g002
Figure 3. Person Age. (a) Crisp logic with clearly defined boundaries. (b) Fuzzy logic with unclear boundaries.
Figure 3. Person Age. (a) Crisp logic with clearly defined boundaries. (b) Fuzzy logic with unclear boundaries.
Machines 11 00572 g003
Figure 4. Structure of a Fuzzy Inference System.
Figure 4. Structure of a Fuzzy Inference System.
Machines 11 00572 g004
Figure 5. Structure of P fuzzy controller.
Figure 5. Structure of P fuzzy controller.
Machines 11 00572 g005
Figure 6. Structure of PD controller.
Figure 6. Structure of PD controller.
Machines 11 00572 g006
Figure 7. Structure of PID controller.
Figure 7. Structure of PID controller.
Machines 11 00572 g007
Figure 8. View obtained of a fuzzy universe of collision distance with UPAFuzzySystems library.
Figure 8. View obtained of a fuzzy universe of collision distance with UPAFuzzySystems library.
Machines 11 00572 g008
Figure 9. Fuzzy universe of recommended speeds with UPAFuzzySystems library.
Figure 9. Fuzzy universe of recommended speeds with UPAFuzzySystems library.
Machines 11 00572 g009
Figure 10. Surface obtained from inference system with UPAFuzzySystems library.
Figure 10. Surface obtained from inference system with UPAFuzzySystems library.
Machines 11 00572 g010
Figure 11. Feedback controller for controlling position in a DC motor with a fuzzy system.
Figure 11. Feedback controller for controlling position in a DC motor with a fuzzy system.
Machines 11 00572 g011
Figure 12. Feedback controller for controlling position in a DC motor with a fuzzy system and random disturbances.
Figure 12. Feedback controller for controlling position in a DC motor with a fuzzy system and random disturbances.
Machines 11 00572 g012
Figure 13. Error premise for position control in a DC motor.
Figure 13. Error premise for position control in a DC motor.
Machines 11 00572 g013
Figure 14. Control consequence for position control in a DC motor.
Figure 14. Control consequence for position control in a DC motor.
Machines 11 00572 g014
Figure 15. Surface response of Mamdani fuzzy controller for controlling position in a DC motor.
Figure 15. Surface response of Mamdani fuzzy controller for controlling position in a DC motor.
Machines 11 00572 g015
Figure 16. DC motor position controlled with a Mamdani defined with UPAFuzzySystems library.
Figure 16. DC motor position controlled with a Mamdani defined with UPAFuzzySystems library.
Machines 11 00572 g016
Figure 17. DC motor position controlled with a Mamdani controller, including disturbances in the input of the plant.
Figure 17. DC motor position controlled with a Mamdani controller, including disturbances in the input of the plant.
Machines 11 00572 g017
Figure 18. Surface obtained with the one-input FlS controller.
Figure 18. Surface obtained with the one-input FlS controller.
Machines 11 00572 g018
Figure 19. DC motor position controlled with an FLS controller defined with UPAFuzzySystems library.
Figure 19. DC motor position controlled with an FLS controller defined with UPAFuzzySystems library.
Machines 11 00572 g019
Figure 20. DC motor position controlled with an FLS controller, including disturbance signal.
Figure 20. DC motor position controlled with an FLS controller, including disturbance signal.
Machines 11 00572 g020
Figure 21. Premise fuzzy sets of the one-input Takagi–Sugeno controller.
Figure 21. Premise fuzzy sets of the one-input Takagi–Sugeno controller.
Machines 11 00572 g021
Figure 22. Consequence fuzzy sets of the one-input Takagi–Sugeno controller.
Figure 22. Consequence fuzzy sets of the one-input Takagi–Sugeno controller.
Machines 11 00572 g022
Figure 23. Surface response of the one-input Takagi–Sugeno controller.
Figure 23. Surface response of the one-input Takagi–Sugeno controller.
Machines 11 00572 g023
Figure 24. Controller response of the one-input Takagi–Sugeno controller.
Figure 24. Controller response of the one-input Takagi–Sugeno controller.
Machines 11 00572 g024
Figure 25. Controller response of the one-input Takagi–Sugeno controller with disturbances.
Figure 25. Controller response of the one-input Takagi–Sugeno controller with disturbances.
Machines 11 00572 g025
Figure 26. Error universe as a premise for a two-input Mamdani controller.
Figure 26. Error universe as a premise for a two-input Mamdani controller.
Machines 11 00572 g026
Figure 27. Change in error universe as a premise for a two-input Mamdani controller.
Figure 27. Change in error universe as a premise for a two-input Mamdani controller.
Machines 11 00572 g027
Figure 28. Control universe consequence for a two-input Mamdani controller.
Figure 28. Control universe consequence for a two-input Mamdani controller.
Machines 11 00572 g028
Figure 29. Two-input Mamdani controller surface using the UPAFuzzySystems library.
Figure 29. Two-input Mamdani controller surface using the UPAFuzzySystems library.
Machines 11 00572 g029
Figure 30. Two-input Mamdani controller response using the UPAFuzzySystems library.
Figure 30. Two-input Mamdani controller response using the UPAFuzzySystems library.
Machines 11 00572 g030
Figure 31. Two-input Mamdani controller response with disturbances.
Figure 31. Two-input Mamdani controller response with disturbances.
Machines 11 00572 g031
Figure 32. Two-input FLS controller surface with the UPAFuzzySystems library.
Figure 32. Two-input FLS controller surface with the UPAFuzzySystems library.
Machines 11 00572 g032
Figure 33. Two-input FLS controller response by the UPAFuzzySystems library.
Figure 33. Two-input FLS controller response by the UPAFuzzySystems library.
Machines 11 00572 g033
Figure 34. Two-input FLS controller response with disturbances.
Figure 34. Two-input FLS controller response with disturbances.
Machines 11 00572 g034
Figure 35. Consequence of the two-input Takagi–Sugeno controller by the UPAFuzzySystems library.
Figure 35. Consequence of the two-input Takagi–Sugeno controller by the UPAFuzzySystems library.
Machines 11 00572 g035
Figure 36. Surface of the two-input Takagi–Sugeno controller using the UPAFuzzySystems library.
Figure 36. Surface of the two-input Takagi–Sugeno controller using the UPAFuzzySystems library.
Machines 11 00572 g036
Figure 37. Control response of the two-input Takagi–Sugeno controller using the UPAFuzzySystems library.
Figure 37. Control response of the two-input Takagi–Sugeno controller using the UPAFuzzySystems library.
Machines 11 00572 g037
Figure 38. Control response of the two-input Takagi–Sugeno controller with disturbances.
Figure 38. Control response of the two-input Takagi–Sugeno controller with disturbances.
Machines 11 00572 g038
Figure 39. Premise of the one-input linear controller via the UPAFuzzySystems library.
Figure 39. Premise of the one-input linear controller via the UPAFuzzySystems library.
Machines 11 00572 g039
Figure 40. Consequence of the one-input linear controller using the UPAFuzzySystems library.
Figure 40. Consequence of the one-input linear controller using the UPAFuzzySystems library.
Machines 11 00572 g040
Figure 41. Surface of the one-input linear controller using the UPAFuzzySystems library.
Figure 41. Surface of the one-input linear controller using the UPAFuzzySystems library.
Machines 11 00572 g041
Figure 42. Response of the linear proportional fuzzy controller using the UPAFuzzySystems library.
Figure 42. Response of the linear proportional fuzzy controller using the UPAFuzzySystems library.
Machines 11 00572 g042
Figure 43. Response of the linear proportional fuzzy controller with disturbances.
Figure 43. Response of the linear proportional fuzzy controller with disturbances.
Machines 11 00572 g043
Figure 44. Premise error of the two-input linear controller using the UPAFuzzySystems library.
Figure 44. Premise error of the two-input linear controller using the UPAFuzzySystems library.
Machines 11 00572 g044
Figure 45. Premise change in error of the two-input linear controller using the UPAFuzzySystems library.
Figure 45. Premise change in error of the two-input linear controller using the UPAFuzzySystems library.
Machines 11 00572 g045
Figure 46. Consequence control of the two-input linear controller using the UPAFuzzySystems library.
Figure 46. Consequence control of the two-input linear controller using the UPAFuzzySystems library.
Machines 11 00572 g046
Figure 47. Surface of the two-input linear controller using the UPAFuzzySystems library.
Figure 47. Surface of the two-input linear controller using the UPAFuzzySystems library.
Machines 11 00572 g047
Figure 48. Response of the linear PD fuzzy controller using the UPAFuzzySystems library.
Figure 48. Response of the linear PD fuzzy controller using the UPAFuzzySystems library.
Machines 11 00572 g048
Figure 49. Response of the linear PD fuzzy controller with disturbances.
Figure 49. Response of the linear PD fuzzy controller with disturbances.
Machines 11 00572 g049
Figure 50. Response of the linear PD-I fuzzy controller using the UPAFuzzySystems library.
Figure 50. Response of the linear PD-I fuzzy controller using the UPAFuzzySystems library.
Machines 11 00572 g050
Figure 51. Response of the linear PD-I fuzzy controller with disturbances.
Figure 51. Response of the linear PD-I fuzzy controller with disturbances.
Machines 11 00572 g051
Figure 52. Comparison of control responses of fuzzy controllers implemented with the UPAFuzzySystems library.
Figure 52. Comparison of control responses of fuzzy controllers implemented with the UPAFuzzySystems library.
Machines 11 00572 g052
Figure 53. Comparison of the characteristics of interest in fuzzy controllers with the UPAFuzzySystems library.
Figure 53. Comparison of the characteristics of interest in fuzzy controllers with the UPAFuzzySystems library.
Machines 11 00572 g053
Figure 54. Boxplot of characteristics in fuzzy controllers implemented with the UPAFuzzySystems library.
Figure 54. Boxplot of characteristics in fuzzy controllers implemented with the UPAFuzzySystems library.
Machines 11 00572 g054
Figure 55. Comparison of control responses of fuzzy controllers with disturbances.
Figure 55. Comparison of control responses of fuzzy controllers with disturbances.
Machines 11 00572 g055
Figure 56. Comparative of characteristics of interest in fuzzy controllers with disturbances.
Figure 56. Comparative of characteristics of interest in fuzzy controllers with disturbances.
Machines 11 00572 g056
Figure 57. Boxplot of characteristics in fuzzy controllers with disturbances.
Figure 57. Boxplot of characteristics in fuzzy controllers with disturbances.
Machines 11 00572 g057
Table 1. Comparison of Python libraries for FISs and their capabilities, including UPAFuzzySystems.
Table 1. Comparison of Python libraries for FISs and their capabilities, including UPAFuzzySystems.
LibraryDesign of FISsDesign of FISs ControllersPID FISs ControllersSimulation of FISs Controllers with Transfer Functions and State-Space Models
Fuzzy-logic-toolboxYesNoNoNo
Scikit-fuzzyYesYes (Only Mamdani controller)NoNo
fuzzylabYesNoNoNo
fuzzythonYes NoNoNo
Type2FuzzyYes (Type 2)NoNoNo
Fuzzy-machinesYesNoNoNo
pyfuzzyliteYesYesNoNo
SimpfulYesNoNoNo
pyFumeYesNoNoNo
UPAFuzzySystemsYesYesYesYes
Table 2. Configurations for FIS controllers.
Table 2. Configurations for FIS controllers.
FIS ControllerInput UniversesConnectivesIF-THEN RulesDefuzzification
MamdaniNot definedEquations (7) and (8)Equation (11)Equations (14) and (16)
FLS[−1, 1]Equations (9) and (10)Equation (11)Equation (17)
Linear[−100, 100]Equations (9) and (10)Equation (13)Equation (15)
Takagi–SugenoNot definedEquations (9) and (10)Equation (13)Equation (15)
Linear P[−100, 100]Equations (9) and (10)Equation (13)Equation (15)
Linear PD[−100, 100]Equations (9) and (10)Equation (13)Equation (15)
Linear PID[−100, 100]Equations (9) and (10)Equation (13)Equation (15)
No-linear PNot definedEquations (7) and (8)Equation (11)Equations (14) and (16)
No-linear PDNot definedEquations (7) and (8)Equation (11)Equations (14) and (16)
No-linear PIDNot definedEquations (7) and (8)Equation (11)Equations (14) and (16)
Table 3. Rules for the different FIS controllers.
Table 3. Rules for the different FIS controllers.
FIS ControllerExample of Rules
MamdaniOne input:
If error is Neg then control is Neg
If error is Zero then control is Zero
If error is Pos then control is Pos
Two input:
If error is Neg and change error is Neg then control is Neg
If error is Neg and change error is Zero then control is Neg
If error is Zero and change error is Neg then control is Zero
If error is Neg and change error is Pos then control is Zero
If error is Zero and change error is Zero then control is Zero
If error is Zero and change error is Pos then control is Zero
If error is Pos and change error is Neg then control is Zero
if error is Pos and change error is Zero then control is Pos
If error is Pos and change error is Pos then control is Pos
FLSOne input:
If error is Neg then control is Neg
If error is Zero then control is Zero
If error is Pos then control is Pos
Two input:
If error is Neg and change error is Neg then control is Neg
If error is Neg and change error is Zero then control is Neg
If error is Zero and change error is Neg then control is Zero
If error is Neg and change error is Pos then control is Zero
If error is Zero and change error is Zero then control is Zero
If error is Zero and change error is Pos then control is Zero
If error is Pos and change error is Neg then control is Zero
if error is Pos and change error is Zero then control is Pos
If error is Pos and change error is Pos then control is Pos
LinearOne input:
If error is Neg then control is −100
If error is Zero then control is 0
If error is Pos then control is 100
Two inputs:
If error is Neg and change error is Neg then control is −200
If error is Neg and change error is Pos then control is 0
If error is Pos and change error is Neg then control is 0
If error is Pos and change error is Pos then control is 200
Takagi–SugenoOne input:
If error is Neg then control is a · e r r o r + b
If error is Zero then control is 0
If error is Pos then control is c · e r r o r + d
Two inputs:
If error is Neg and change error is Neg then control is a · e r r o r + b · c h e r r o r + c
If error is Neg and change error is Pos then control is 0
If error is Pos and change error is Neg then control is 0
If error is Pos and change error is Pos then control is d · e r r o r + e · c h e r r o r + c
Linear PIf error is Neg then control is −100
If error is Zero then control is 0
If error is Pos then control is 100
Linear PDIf error is Neg and change error is Neg then control is −200
If error is Neg and change error is Pos then control is 0
If error is Pos and change error is Neg then control is 0
If error is Pos and change error is Pos then control is 200
Linear PIDIf error is Neg and change error is Neg then control is −200
If error is Neg and change error is Pos then control is 0
If error is Pos and change error is Neg then control is 0
If error is Pos and change error is Pos then control is 200
Table 4. Parameters and transfer function for controlling position in a DC motor.
Table 4. Parameters and transfer function for controlling position in a DC motor.
ParameterDescriptionValue
J Inertial Coefficient 3.2284 × 10 6   k g · m 2
b Viscous Friction
Coefficient
3.5077 × 10 6   N s m
K Electromotive Force0.0274 R P M V
R Armor Resistance4 Ω
L Armor Inductance 2.75 × 10 6 H
t e Simulation Time1.0 s
n s Total Number of Samples1500
t f Transfer Function K s J s + b L s + R + K 2
Table 5. Comparison of control characteristics in fuzzy controllers implemented with the UPAFuzzySystems library.
Table 5. Comparison of control characteristics in fuzzy controllers implemented with the UPAFuzzySystems library.
Fuzzy ControllerOne-Input MamdaniOne-Input FLSTwo-Input MamdaniTwo-Input FLSOne-Input Takagi–SugenoTwo-input Takagi–SugenoFuzzy Linear PFuzzy Linear PDFuzzy Linear PD-I
max_val (rad) 8.47 × 10 1 7.93 × 10 1 8.25 × 10 1 7.96 × 10 1 8.00 × 10 1 8.02 × 10 1 9.993 × 10 1 7.91 × 10 1 7.91 × 10 1
min_val (rad)0.00 2.39 × 10 16 7.99 × 10 18 5.79 × 10 1 0.000.000.000.000.00
start_val (rad)0.00 8.40 × 10 17 4.28 × 10 18 0.000.000.000.000.000.00
end_val (rad) 7.97 × 10 1 7.93 × 10 1 7.57 × 10 1 7.96 × 10 1 7.99 × 10 1 7.96 × 10 1 7.89 × 10 1 7.89 × 10 1 7.87 × 10 1
time_rising (s) 3.86 × 10 2 1.99 × 10 1 2.66 × 10 2 2.67 × 10 1 1.33 × 10 1 6.66 × 10 2 1.45 × 10 1 3.86 × 10 2 1.05 × 10 2
Overshoot (s) 7.86 0.009.090.00 1.60 × 10 1 7.07 × 10 1 2.58 × 10 1 2.60 × 10 1 4.11 × 10 1
time_overshoot (s) 7.26 × 10 2 7.22 × 10 1 6.62 × 10 1 7.22 × 10 1 5.28 × 10 1 5.22 × 10 1 3.46 × 10 2 5.18 × 10 1 5.12 × 10 1
settling_time (s) 6.38 × 10 1 6.80 × 10 1 7.14 × 10 1 6.74 × 10 1 6.54 × 10 1 6.34 × 10 1 1.65 × 10 1 1.16 × 10 1 3.46 × 10 2
Absolute Error (rad) 1.20 × 10 2 7.18 × 10 3 2.88 × 10 2 11.1 × 10 2 1.35 × 10 1 1.07 × 10 2 3.60 × 10 3 3.47 × 10 3 2.05 × 10 3
Percentage Error (%) 1.53 9.14 × 10 1 3.661.41 1.72 1.37 4.58 × 10 1 4.42 × 10 1 2.61 × 10 1
Table 6. ANOVA analysis of fuzzy controllers without overshoot.
Table 6. ANOVA analysis of fuzzy controllers without overshoot.
sum_sqdfFPR(>F)
C(features)0.1644503.09.0661490.000172
Residual0.19348132.0
Table 7. Comparison of control characteristics in fuzzy controllers with disturbances.
Table 7. Comparison of control characteristics in fuzzy controllers with disturbances.
Fuzzy ControllerOne-Input MamdaniOne-Input FLSTwo-Input MamdaniTwo-Input FLSOne-Input Takagi–SugenoTwo-Input Takagi–SugenoFuzzy Linear PFuzzy Linear PDFuzzy Linear PD-I
max_val (rad) 8.60 × 10 1 7.93 × 10 1 8.25 × 10 1 7.96 × 10 1 8.00 × 10 1 8.02 E 01 9.93 × 10 1 7.91 × 10 1 7.91 × 10 1
min_val (rad) 0.00 2.39 × 10 16 7.99 × 10 18 5.79 × 10 5 0.00 0.00 0.00 0.00 0.00
start_val (rad) 0.00 8.40 × 10 17 4.28 × 10 18 0.00 0.00 0.00 0.00 0.00 0.00
end_val (rad) 7.97 × 10 1 7.93 × 10 1 7.57 × 10 1 7.96 × 10 1 7.99 × 10 1 7.96 × 10 1 7.89 × 10 1 7.89 × 10 1 7.87 × 10 1
time_rising (s) 3.86 × 10 2 1.99 × 10 1 2.66 × 10 2 2.67 × 10 1 1.33 × 10 1 6.66 × 10 2 1.45 × 10 2 3.86 × 10 2 1.05 × 10 2
Overshoot (s) 7.86 0.00 9.09 0.00 1.60 × 10 1 7.07 × 10 1 2.58 × 10 + 1 2.60 × 10 1 4.11 × 10 1
time_overshoot (s) 7.26 × 10 2 7.22 × 10 1 6.62 × 10 1 7.22 × 10 1 5.28 × 10 1 5.22 × 10 1 3.46 × 10 2 5.18 × 10 1 5.12 × 10 1
settling_time (s) 6.38 × 10 1 6.8 × 10 1 7.14 × 10 1 6.74 × 10 1 6.54 × 10 1 6.34 × 10 1 1.65 × 10 1 1.61 × 10 1 3.46 × 10 2
Absolute Error (rad) 1.20 × 10 2 7.18 × 10 3 2.88 × 10 2 1.11 × 10 2 1.35 × 10 2 1.07 × 10 2 3.60 × 10 3 3.47 × 10 3 2.05 × 10 3
Percentage Error (%) 1.53 9.14 × 10 1 3.66 1.41 1.72 1.37 4.58 × 10 1 4.42 × 10 1 2.61 × 10 1
Table 8. ANOVA analysis of fuzzy controllers without overshoot.
Table 8. ANOVA analysis of fuzzy controllers without overshoot.
Sum_sqdfFPR(>F)
C(features)9.5358593.011.0742660.000038
Residual9.18488232.0
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content.

Share and Cite

MDPI and ACS Style

Montes Rivera, M.; Olvera-Gonzalez, E.; Escalante-Garcia, N. UPAFuzzySystems: A Python Library for Control and Simulation with Fuzzy Inference Systems. Machines 2023, 11, 572. https://doi.org/10.3390/machines11050572

AMA Style

Montes Rivera M, Olvera-Gonzalez E, Escalante-Garcia N. UPAFuzzySystems: A Python Library for Control and Simulation with Fuzzy Inference Systems. Machines. 2023; 11(5):572. https://doi.org/10.3390/machines11050572

Chicago/Turabian Style

Montes Rivera, Martín, Ernesto Olvera-Gonzalez, and Nivia Escalante-Garcia. 2023. "UPAFuzzySystems: A Python Library for Control and Simulation with Fuzzy Inference Systems" Machines 11, no. 5: 572. https://doi.org/10.3390/machines11050572

Note that from the first issue of 2016, this journal uses article numbers instead of page numbers. See further details here.

Article Metrics

Back to TopTop