With some practice, it’s possible to balance a broom upright in our hand, relying largely on intuition rather than accurate measurements. Yet, the dynamics behind this seemingly simple act are more intricate than they appear. From an engineering perspective, this balancing act is analogous to the inverted pendulum, a classic problem in control theory.
The inverted pendulum is a system consisting of a tall rod, like our broom, but this time, it’s attached to a horizontally moving cart. The objective? Keep the rod upright, compensating for any disturbances that might knock it off balance. This control problem is complex due to the numerous degrees of freedom involved, but still straightforward enough to understand and simulate.
While mathematical solutions exist to keep the pendulum vertically upright, they require precise knowledge of the system’s parameters. Yet when we balance a broom, humans are clearly not relying on a mathematical model that depends on highly accurate measurements of angles and velocities. This suggests that for the inverted pendulum problem, it’s possible to design a successful controller without depending on precise measurements.
What if we wanted to introduce an element of that human intuition into our control systems? Enter fuzzy logic—a method that resembles human reasoning by working with concepts that aren’t strictly true or false but lie somewhere in between. For example, instead of processing exact numerical values, a fuzzy logic system might consider values like “almost upright” or “slightly tilted.” This approach offers flexibility, especially when precise data is unavailable or when a system needs to adjust to unforeseen challenges.
In the early 2000s, the number and variety of fuzzy logic applications increased significantly. They ranged from consumer products such as cameras, camcorders, washing machines, and microwave ovens to industrial process control, medical instrumentation, decision-support systems, and portfolio selection. Unlike standard logic, where variables can only take two values—True or False—fuzzy logic describes things in a vaguer form. Its variables can range between 0 and 1, characterizing a variable’s membership to a particular value. For instance, describing weather as “0.72 sunny and 0.18 cloudy” provides more information than simply stating “the weather today is sunny but not cloudy.” With fuzzy variables, one can construct operations known as fuzzy rules. These rules resemble human thinking and are structured as “If … then…” However, translating these rules into a soft mathematical form suitable for machine processing is essential.
Figure 1: Forces in the free-body diagram
Returning to our inverted pendulum on a cart problem, this system can be seen in Figure 1. Intuitively, the control force \(F\) should be determined by the magnitudes of the input variables \(\phi\) and \(\dot{\phi}\), which measure the angle of the pendulum from the upright position and its angular velocity, respectively. Using fuzzy logic, the relationship between these variables becomes linguistic, a much weaker form than exact measurements. Leveraging these linguistic variables, we can establish rules that determine the control \(F\) using common sense knowledge. These rules can look like such as “If \(\phi\) is very small, then \(F\) should be small,” or “If the pendulum is balanced, then hold very still, meaning do not apply any force.”
Fuzzy logic
Now let us look in more detail at the design of a fuzzy controller. In our case, the controller receives its input as the pair \((\phi,\dot{\phi})\). We define this input space as \(X \times Y\), where intervals \(X\) and \(Y\) represent the degrees for \(\phi\) and degrees per second for \(\dot{\phi}\), respectively. The output, or control space for \(F\), falls within an interval that signifies the force in Newtons.
The linguistic labels representing this data are modeled as fuzzy subsets of the spaces \(X\), \(Y\), and \(F\). These subsets are mapped using their respective membership functions. Common linguistic labels might include: negative big (NB), negative medium (NM), negative small (NS), positive small (PS), positive medium (PM), and positive big (PB).
Looking at these “If . . . then. . .” rules, we ask ourselves questions like the following: Is “If. . . then. . .” an implication operator in logic? How can one model linguistic labels like “small,” “medium,” and “large”? Given a finite number of “If. . . then. . .” rules, how can we handle all possible numerical inputs that will be measured by machine sensors, in order to produce actual control actions? The answers to all these basic questions lie in fuzzy logic theory. The term “fuzzy control” refers to the science of building fuzzy controllers based on the mathematics of fuzzy logic.
When we look at the “If… then…” rules, it raises several questions about their foundational logic. Does the “If… then…” structure represent a logical implication? How do we understand labels like “small”, “medium”, or “large” in this setting? And with a fixed number of “If… then…” rules, how does the system handle varied numerical inputs from sensors and then produce specific control actions? These questions can be answered by exploring fuzzy logic theory. Put simply, “fuzzy control” is about designing controllers using the mathematics of fuzzy logic. We’ll discuss these concepts further in the following sections.
One of the distinct advantages of fuzzy logic is its capacity to process a linguistic variable—a variable characterized by words rather than numbers. Interestingly, such rule-based systems have profound roots in classical AI, such as decision trees. Unlike conventional hard computing, soft computing is designed to navigate the inherent imprecision of the sensor measurements.
As an overview, let’s break down how the fuzzy decision process works step by step. Initially, a distinct set of input data is collected and transformed into a fuzzy set using fuzzy linguistic variables, terms, and membership functions – a process termed “fuzzification”. Following this, an inference phase takes place, using the set of “If… then…” rules. Finally, in the “defuzzification” step, the fuzzy output is converted back to a clear, distinct output through the use of membership functions. The full fuzzy logic process is illustrated in Figure 2.
Figure 2: Fuzzy Logic System
Linguistic Variables
Linguistic variables are either the input or output variables of the system. Their values are words or sentences from natural language, not numerical values. Typically, a linguistic variable is broken down into a set of linguistic terms. For example, in the context of an inverted pendulum, let the angle \(\phi\) be the linguistic variable symbolizing the angle formed with the upper vertical position. To qualify the angle, we naturally use linguistic terms such as “slightly slanted” and “falling over”. Using this intuition, we can define a mapping functions, such as \(\phi(t)=\) {negative big (NB) angle, negative medium (NM) angle, negative small (NS) angle, positive small (PS) angle, positivev medium (PM) angle, and positive big (PB) angle}. This can be the set of decomposition for the linguistic variable angle. Each member of this decomposition is called a linguistic term and can cover a portion of the overall values of the angle.
Membership Functions
Membership functions are used in the fuzzification and defuzzification steps of the fuzzy logic system, to map the non-fuzzy input values to fuzzy linguistic terms and vice versa. A membership function quantifies a linguistic term. For instance, Figure [fig:Membership-funtions-of-angle] shows the membership functions for the linguistic terms of the angle variable. An important characteristic of fuzzy logic is that a numerical value does not have to be fuzzified using only one membership function. In other words, a value can belong to multiple sets at the same time. For example, according to Figure [fig:Membership-funtions-of-angle], an angle value can be considered as “negative small” and “negative medium” at the same time, with different degrees of memberships.
Fuzzy Rules
Within a fuzzy logic system, a rule base is constructed to control the output variable. A fuzzy rule is a simple IF-THEN rule with a condition and a conclusion.
Fuzzy Set Operations
The evaluations of the fuzzy rules and the combination of the results of the individual rules are performed using fuzzy set operations. The operations on fuzzy sets are different from the operations on non-fuzzy sets. Let \(\mu_A\) and \(\mu_B\) represent the membership functions for fuzzy sets \(A\) and \(B\). Unlike in conventional Boolean algebra we can have different definitions for the logic operator. The table below lists potential fuzzy operations for OR and AND operators:
OR (Union)
AND (Intersection)
MAX
\(Max\{\mu_A(x), \mu_B(x)\}\)
MIN
\(Min\{\mu_A(x), \mu_B(x)\}\)
ASUM
\(\mu_A(x) + \mu_B(x) - \mu_A(x) \mu_B(x)\)
PROD
\(\mu_A(x) \mu_B(x)\)
BSUM
\(Min\{1, \mu_A(x) + \mu_B(x)\}\)
BDIF
\(Max\{0, \mu_A(x) + \mu_B(x) - 1\}\)
After evaluating the result of each rule, these results need to be merged to obtain a final result. This process is called inference. There are different methods that can combine the outcomes of individual rules. The table below contains possible accumulation methods that are used to combine the results of individual rules. The MAX operator is typically used for accumulation.
The final step is defuzzification. Following the inference step, we are left with a fuzzy value. This value must be transformed aka defuzzified, to yield our control output. Defuzzification is carried out based on the membership function of the output variable. The result of the inference step is illustrated on Figure 3, where the shaded areas collectively represent the fuzzy outcome. Our objective is to extract a definitive value from this fuzzy representation. This is denoted by a dot in the figure. The defuzzification process is depicted in Figure 3. Several algorithms exist for defuzzification, where the most commonly used algorithms are listed in this table:
In the fuzzy control system designed for this application, the primary inputs are the angle \(\phi\) and angular speed \(\dot\phi\). These values represent the current state of the system and serve as the basis for our fuzzy logic evaluations.
To make these numerical inputs compatible with the fuzzy control system, they need to be fuzzified. Fuzzification is the process of assigning a degree of membership to each value in a set. For our system, the membership functions used are Gaussian in nature and are defined as: \(f(x)=e^\frac{-(x-a)^2}{2\sigma^2}\)
The fuzzified values are then categorized into six linguistic variables, represented as: - NB (Negative Big) - NM (Negative Medium) - NS (Negative Small) - PS (Positive Small) - PM (Positive Medium) - PB (Positive Big)
One might wonder, that if we have access to accurate measurements, which is the case in this simulation since we are running a simulation, why would we intentionally get rid of accurate measurements? The reason is that this is just a toy example for fuzzy control logic, and even though we have accurate measurements, we are not going to take advantage of this fact.
Figure 4: Membership functions of angle
Figure 5: Membership functions of angular speed
2. Rule Base Construction
Using the these linguistic variables, we construct a comprehensive rule base, as outlined in the table below:
\(\phi\textbackslash\dot{\phi}\)
NB
NM
NS
PS
PM
PB
NB
NB
NB
NB
NM
NS
PS
NM
NB
NB
NM
NS
PS
PS
NS
NB
NM
NS
PS
PS
PM
PS
NM
NS
NS
PS
PM
PB
PM
NS
NS
PS
PM
PB
PB
PB
NS
PS
PM
PB
PB
PB
This rule table should be read the following way:
• \(\textbf{\textit{IF angle is NB and angular speed is NB then the control force is NB.}}\)
• \(\textbf{\textit{IF angle is NB and angular speed is NM then the control force is NB.}}\)
\(\vdots\)
• \(\textbf{\textit{IF angle is PB and angular speed is PB then the control force is PB.}}\)
3. Defuzzification
After establishing the membership values for each control force label (NB, NM, NS, PS, PM, PB), we proceed to the defuzzification step. This is crucial as the fuzzy logic system produces outputs in the form of a range of values, which needs to be translated into a distinct scalar output to implement in real-world systems.
For this purpose, the Centre of Gravity for Singletons operation is employed. This algorithm computes a crisp value, representing the control force to be applied to the cart. In simpler terms, based on the fuzzy outputs and the associated membership values, it calculates a single, unambiguous control force that best represents the desired action.
Putting it all together
Now that we defined the system, we can write our lil fuzzy logic system:
Englert, Brunó B. (Jul 2023). Balancing the Inverted Pendulum: An Introduction to Fuzzy Logic Control. https://englert.ai/posts/002_fuzzy_control/englert_ai_2_fuzzy_control.html.
Or
@article{englert2023fuzzy, title ="Balancing the Inverted Pendulum: An Introduction to Fuzzy Logic Control", author ="Englert, Brunó B.", journal ="englert.ai", year ="2023", month ="Jul", url ="https://englert.ai/posts/002_fuzzy_control/englert_ai_2_fuzzy_control.html"}