Skip to content

HangX-Ma/fuzzy-logic-controller

Repository files navigation

A fuzzy logic controller

Realize the basic function of fuzzy logic controller. User can select Triangle, Trapezoid, Gaussian membership functions to do fuzzification. The inference process will select the highest weight rule, if exist some rules that points to the same output discourse. Centroid (COG) method is used to defuzzification.

You can use P-FC controller that combines proportional controller with fuzzy controller. This will enhance the system response time and accuracy when input error or error rate high.

NOTE: Fuzzy control can make control more concise, but also make it slow. I use forward feedback in demo to increase the control speed!

Fuzzy logic controller design, HangX-Ma
Fuzzy logic controller design, HangX-Ma

Environment

  • Python3.11, matplotlib-3.7.1
  • Eigen-3.4.0
  • matplotlib-cpp
  • C++17, CMake 3.23

Prerequisite

  • Install spdlog development components.

    sudo apt-get install libspdlog-dev
  • Install Eigen3 to acquire linear algebra calculation ability.

    wget -O Eigen.zip https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.zip
    unzip Eigen.zip # it has unzipped into the library called eigen-3.4.0
    cd eigen-3.4.0
    cmake -B build
    sudo cmake --build build --target install # install Eigen3

Schedule

Usage

Compile the project after you download this repository. You can find the output executable file in build/bin.

git clone https://github.com/HangX-Ma/fuzzy-logic-controller.git
cd fuzzy-logic-controller
cmake -B build -S .
cmake --build build
# run the demo!
./build/bin/fuzzy_demo

You can also include this repository and compile it as a shared or static library. libfuzzy.a or libfuzzy.so will be found in build folder.

This repository only provide three membership functions: Triangle, Trapezoid, Gaussian. You need to provide your configuration parameters by yourself.

Membership function: Triangle, HangX-Ma Membership function: Trapezoid, HangX-Ma Membership function: Gaussian, HangX-Ma
Membership function: Triangle, HangX-Ma Membership function: Trapezoid, HangX-Ma Membership function: Gaussian, HangX-Ma

You can annotate these definitions in CMakeLists.txt to switch off/on those relative functions.

# CMakeLists.txt
add_definitions(
    "-DFC_USE_DOUBLE"
    "-DFC_USE_MATPLOTLIB"
)

Demo

Fuzzy control surface, HangX-Ma
Fuzzy control surface, HangX-Ma

[2024-01-16 22:18:41.579] [info] Fuzzy logic controller info:
[2024-01-16 22:18:41.579] [info] => discourse  e: [-60.000, 60.000], min-max[-3.00, 3.00]
[2024-01-16 22:18:41.579] [info] => discourse ec: [-30.000, 30.000], min-max[-3.00, 3.00]
[2024-01-16 22:18:41.579] [info] => discourse  u: [-15.000, 15.000], min-max[-3.00, 3.00]
[2024-01-16 22:18:41.579] [info] => error quantifying factor             [Ke]: 0.0500
[2024-01-16 22:18:41.579] [info] => derivative error quantifying factor [Kec]: 0.1000
[2024-01-16 22:18:41.579] [info] => output scaling factor                [Ku]: 5.0000
[2024-01-16 22:18:41.579] [info] => proportional controller              [Kp]: 2.0000

Fuzzy Controller

FC Demo - Constant: Target and Actual, HangX-Ma FC Demo - Constant: Error and Derivative Error, HangX-Ma
FC Demo - Constant: Target and Actual, HangX-Ma FC Demo - Constant: Error and Derivative Error, HangX-Ma
FC Demo - Sine: Target and Actual, HangX-Ma FC Demo - Sine: Error and Derivative Error, HangX-Ma
FC Demo - Sine: Target and Actual, HangX-Ma FC Demo - Sine: Error and Derivative Error, HangX-Ma

Proportional + Fuzzy Controller

P-FC Demo - Constant: Target and Actual, HangX-Ma P-FC Demo - Constant: Error and Derivative Error, HangX-Ma
P-FC Demo - Constant: Target and Actual, HangX-Ma P-FC Demo - Constant: Error and Derivative Error, HangX-Ma
P-FC Demo - Sine: Target and Actual, HangX-Ma P-FC Demo - Sine: Error and Derivative Error, HangX-Ma
P-FC Demo - Sine: Target and Actual, HangX-Ma P-FC Demo - Sine: Error and Derivative Error, HangX-Ma

Fuzzy Logic Control Basic Concept

Fuzzy logic controller is composed of the following four elements:

  1. A rule-base (a set of If-Then rules), which contains a fuzzy logic quantification of the expert’s linguistic description of how to achieve good control.
  2. An inference mechanism (also called an “inference engine” or “fuzzy inference” module), which emulates the expert’s decision making in interpreting and applying knowledge about how best to control the plant.
  3. A fuzzification interface, which converts controller inputs into information that the inference mechanism can easily use to activate and apply rules.
  4. A defuzzification interface, which converts the conclusions of the inference mechanism into actual inputs for the process.
Fuzzy controller architecture, HangX-Ma
Fuzzy controller architecture, HangX-Ma

Reference

License

MIT License