**2.2 Fuzzy sets of controller variables**

There is a great variety of types of fuzzy sets, which can be used for the implementation of a fuzzy controller. **Figure 3** shows a triangular type of fuzzy set, which is used to define the fuzzy sets of the input and output variables of the controller. This type of fuzzy set uses the subtraction and division operations for its implementation in software and hardware.

**Figure 3.** *Triangular fuzzy set.*

*Methodology for the Implementation of a Fuzzy Controller on Arduino, MATLAB™… DOI: http://dx.doi.org/10.5772/intechopen.109760*

$$\mu(\mathbf{x}) = \begin{cases} 0 & \text{si } \mathbf{x} \le \mathbf{a} \\ \frac{\mathbf{x} - \mathbf{a}}{\mathbf{b} - \mathbf{a}} & \text{si } \mathbf{a} < \mathbf{x} \le \mathbf{b} \\ \frac{\mathbf{c} - \mathbf{x}}{\mathbf{c} - \mathbf{b}} & \text{si } \mathbf{b} < \mathbf{x} \le \mathbf{c} \\ 0 & \text{si } \mathbf{x} \ge \mathbf{c} \end{cases} \tag{1}$$

On the other hand, the accuracy of the process state measurement depends on the number of fuzzy sets of the input variables. Similarly, the accuracy of the control action to adjust the process depends on the number of fuzzy sets of the output variable. Therefore, a process analysis must be performed to determine the number of fuzzy sets for the input and output variables of the controller. In this case according to the approximation error through simulations using five and seven, the approximation error between five and seven was minimal. So, five fuzzy sets were defined for the variable "food" labeled Very Bad (VB), Bad (BD), Regular (RG), Good (GD), and Very Good (VG). Also, five fuzzy sets were defined for the variable "service" labeled Very Bad (VB), Bad (BD), Regular (RG), Good (GD), and Very Good (VG). Also, five fuzzy sets were defined for the output variable "tip" labeled Very Bad (VB), Bad (BD), Regular (RG), Good (GD), and Excellent (EX). The dimensions of the fuzzy sets depend on the importance of the control action; therefore, the length of the fuzzy sets can be different. Finally, **Figure 4** shows the dimensions of the fuzzy sets of the variables of the fuzzy controller.

#### **2.3 Fuzzification stage**

The first stage of the fuzzy controller is fuzzification, which is used to transform a real variable ("food" or "service") into a fuzzy variable through the membership functions. Fuzzification determines the fuzzy sets that indicate the state or condition of an input variable. For this, the membership function μ(x) must be determined using Eq. (1) [21–23]. The algorithm used to implement the fuzzification in the

**Figure 4.** *Fuzzy sets of the variables (a) food, (b) service, and (c) tip.*

MATLAB™ Script and on the Arduino UNO, Arduino DUE, and Nexys 4™ boards is described below.

Eq. (1) is used to determine the μ(x) of the input variables (food or service), and IF-THEN conditional statements are used to determine the fuzzy sets that define the condition of the input variables. FD and SV are the value of food and service, respectively. Setfd and Setsv store the fuzzy set that indicates the state of the input variables, Mf1 and Mf2 are the membership functions of the input variables, and a1, a2, b1, and b2 are dimensions of a fuzzy set.


#### **2.4 Inference stage**

The inference stage uses fuzzy rules, which represent the knowledge base of a fuzzy controller and determine the controllability of the process. Fuzzy rules relate the membership functions of the fuzzy sets that the input variables have. The result of a fuzzy rule is a fuzzy set contained in the output variable obtained using the Mamdani implication. The membership functions of the input variable fuzzy sets indicate the state or condition of the process, and the fuzzy set of the output variable indicates the control action for the process. Generally, fuzzy rules of the Mamdani type have the structure shown in the Eq. (2), which are the most used for the simulation and implementation of a fuzzy controller. The knowledge of a person can be used, or the simulation of the process can be carried out to determine the fuzzy rules. The inference stage determines the fuzzy sets that will be used in the defuzzification stage. Finally, **Table 1** shows the fuzzy rules that relate the membership values of the fuzzification stage used to determine the inference matrix and with it, the value of the tip [24, 25].


**Table 1.**

*Knowledge base or inference matrix of the fuzzy rules to determine the tip.*

*Methodology for the Implementation of a Fuzzy Controller on Arduino, MATLAB™… DOI: http://dx.doi.org/10.5772/intechopen.109760*

Where: x and y are the input variables, z is the output variable, A and B are fuzzy sets of the input variables, and C is a fuzzy set of the output variable.

The algorithm used to implement fuzzy rules in MATLAB™ Script and Arduino UNO, Arduino DUE, and Nexys 4™ boards is described below.

Eq. (2) is used to declare fuzzy rules. Setfd and Setsv were defined in the fuzzification stage, and Settp contains the result of the fuzzy rule (output fuzzy set).


Also, the inference stage must define a membership function μ(x) for the output fuzzy set or result of a fuzzy rule. Generally, the inference method of the Mamdani type (min-max) is used for the implementation of a fuzzy controller, which uses Eq. (3), which selects the membership function of the input variable with the minimum value. In this case, the IF-THEN control statement is used to determine the membership function with the minimum value, which reduces the computational load and allows the inference stage to be implemented on different platforms that cannot use the min (a, b) operation. Finally, there are fuzzy rules that have the same result (output fuzzy set), which implies that multiple membership functions can be associated with an output fuzzy set.

$$\mu\_{\mathbb{C}}(\mathbf{x}) = \min\{\mu\_{\mathbb{A}}(\mathbf{x}), \mu\_{\mathbb{B}}(\mathbf{x}), \dots, \mu\_{\mathbb{M}}(\mathbf{x})\}\tag{3}$$

where: μA(x), μB(x), … , μM(x) are the membership functions of the input variables, and μC(x) is the membership function for a fuzzy set of the output variable.

The algorithm used to implement the inference method in MATLAB™ Script and Arduino UNO, Arduino DUE, and Nexys 4™ boards is described below.


#### **2.5 Aggregation stage**

The aggregation stage is used to determine and obtain the membership functions μ(x) of the output fuzzy sets, which will be used in the defuzzification stage. As mentioned above, the inference stage can associate multiple membership functions to an output fuzzy set. Therefore, the aggregation stage uses Eq. (4) to select the highest value of the membership function of the aggregation stage. In this case, IF-THEN conditional statements were used to compare the multiple

membership functions and obtain the highest value of the membership function. This option reduces the computational load and allows the aggregation stage to be implemented on different platforms, which cannot use the *max* (a, b) operation [26, 27].

$$\mu\_{\rm C}(\mathbf{x}) = \max(\mu\_{\rm C1}(\mathbf{x}), \mu\_{\rm C2}(\mathbf{x}), \dots, \mu\_{\rm CM}(\mathbf{x})) \tag{4}$$

where: μC(x) is the membership function of output fuzzy set, which will be used for defuzzification, and μC1(x), μC2(x), … , μCM(x) are the membership functions defined for the output fuzzy set.

The algorithm used to implement the aggregation stage in the MATLAB ™ Script and on the Arduino UNO, Arduino DUE, and Nexys 4™ boards is described below.


### **2.6 Defuzzification stage**

The last stage of the fuzzy controller is defuzzification, which is used to determine a numerical value, which represents the output fuzzy sets. In this work, the defuzzification value is determined using Eq. (5), which represents the centroid method. The centroid method is used, since this method only requires the basic operations of addition, subtraction, multiplication, and division for its implementation in software or hardware. The defuzzification value represents the rigid value of the output controller, in this case, the value of the tip of the food establishment. Finally, the defuzzification value represents the control action that must be performed in a process [20, 28, 29].

$$\text{defuzification} = \left(\mathbf{x}\_1^\* \,\mu(\mathbf{x}\_1) + \mathbf{x}\_2^\* \,\mu(\mathbf{x}\_2) + \dots + \mathbf{x}\_n^\* \,\mu(\mathbf{x}\_n)\right) / \left(\mu(\mathbf{x}\_1) + \mu(\mathbf{x}\_2) + \dots + \mu(\mathbf{x}\_n)\right) \tag{5}$$

where: x1, x2, … , xn are values of the output variable ("tip"), which are found within the fuzzy sets obtained for defuzzification, and μ(x1), μ(x2), … , μ(xn) are the membership functions of x1, x2, … , xn.

The algorithm used to implement the defuzzification stage in the MATLAB Script and on the Arduino UNO, Arduino DUE, and Nexys 4™ boards is described below.

*Methodology for the Implementation of a Fuzzy Controller on Arduino, MATLAB™… DOI: http://dx.doi.org/10.5772/intechopen.109760*

**Figure 5.** *Defuzzification using the centroid method.*

**Figure 5** shows the fuzzy set used to show the defuzzification process. Eq. (5) is used to determine the defuzzification value. Mftip is the membership function defined in the aggregation stage, mfx is the membership function of x, num1 and den1 are the numerator and denominator of Eq. (5), respectively.


#### **2.7 Procedure for the implementation of a fuzzy controller in the process**

As mentioned above, a fuzzy controller is used to control some variables of a process; therefore, the controlled variable, the type of sensor to measure the controlled variable, and the type of actuator (DC motor, stepper motor, fan, heater, etc.) required to adjust the process must be defined. Additionally, the resolution of the sensor (8 bits, 10 bits, 12 bits, etc.), the sampling rate of the sensor, the sensor operating range, and the type of signal (the number of steps of a stepper motor, minimum and maximum speed of a fan, PWM signal, etc.) required to control the movement of the actuator must be determined to control a process using a fuzzy controller. In this case, the sensor operating range is used to define the characteristics of the input variables of the fuzzy controller and the working range of the actuator is used to define the characteristics of the output variable of the controller [30]. **Figure 6** shows a block diagram of a control system for a process, which uses a generic fuzzy controller. The controller uses the error e(t) and the derivative of the error d(e(t))/dt as input variables, the output variable is a control signal for the actuator (mvact). Finally, a fuzzy controller does not allow an overshoot to be generated in the system

#### **Figure 6.**

*Block diagram of the control of a process using a fuzzy controller.*

response like a classical controller. This is because a fuzzy controller relates the input variables to the output variable through fuzzy rules of the IF-THEN type. So the output response does not have oscillations in the output, and only the settling time and rise time of the system response are short.
