Program Listing for File fuzzy_pd_rule_table.hpp

Return to documentation for file (src/tap/algorithms/fuzzy_pd_rule_table.hpp)

/*
 * Copyright (c) 2022 Advanced Robotics at the University of Washington <robomstr@uw.edu>
 *
 * This file is part of Taproot.
 *
 * Taproot is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Taproot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Taproot.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef TAPROOT_FUZZY_PD_RULE_TABLE_HPP_
#define TAPROOT_FUZZY_PD_RULE_TABLE_HPP_

#include <array>

#include "fuzzy_rule_table.hpp"

namespace tap::algorithms
{
class FuzzyPDRuleTable : public FuzzyRuleTableInterface<2>
{
public:
    enum FuzzyMembers
    {
        N = 0,
        Z,
        P,
        NUM_FUZZY_MEMBERS,
    };

    FuzzyPDRuleTable() : kpArray{}, kdArray{} {}

    FuzzyPDRuleTable(
        const std::array<float, NUM_FUZZY_MEMBERS> &kpParams,
        const std::array<float, NUM_FUZZY_MEMBERS> &kdParams)
        : kpArray(kpParams),
          kdArray(kdParams)
    {
    }

    modm::Matrix<float, 2, 1> performFuzzyUpdate(float e, float d) override;

    inline modm::Matrix<float, 2, 1> getFuzzyGains() const override { return fuzzyGains; }

private:
    void performFuzzification(float e, float d);

    void updateFuzzyMatrix();

    void performDefuzzification();

    static inline void performSingleValueFuzzification(
        const float value,
        std::array<float, NUM_FUZZY_MEMBERS> &fuzzificationMemberValues)
    {
        if (value < 0)
        {
            // value < 0 means the positive triangle function is 0 and the negative and zero members
            // should be updated

            // The value should be mapped from [-1, 0] to [1, 0] (i.e. if -1, the negative member
            // value should be 1)
            fuzzificationMemberValues[N] = std::min(-value, 1.0f);
            // The value should be mapped from [-1, 0] to [0, 1] (i.e. if the value is 0, the zero
            // member value should be 1)
            fuzzificationMemberValues[Z] = std::max(value + 1.0f, 0.0f);
            fuzzificationMemberValues[P] = 0;
        }
        else
        {
            // value > 0 means negative triangle function is 0 and positive and zero members should
            // be updated

            fuzzificationMemberValues[N] = 0;
            // The value should be mapped from [0, 1] to [1, 0] (i.e. if the value is 0, the zero
            // member value should be 1)
            fuzzificationMemberValues[Z] = std::max(1.0f - value, 0.0f);
            // The value should be mapped from [0, 1] to [0, 1] (direct mapping)
            fuzzificationMemberValues[P] = std::min(value, 1.0f);
        }
    }

private:
    std::array<float, NUM_FUZZY_MEMBERS> errorFuzzificationMemberValues = {};
    std::array<float, NUM_FUZZY_MEMBERS> derivativeFuzzificationMemberValues = {};
    std::array<std::array<float, NUM_FUZZY_MEMBERS>, NUM_FUZZY_MEMBERS> fuzzyMatrix = {};
    modm::Matrix<float, 2, 1> fuzzyGains = {};
    std::array<float, NUM_FUZZY_MEMBERS> kpArray;
    std::array<float, NUM_FUZZY_MEMBERS> kdArray;
};
}  // namespace tap::algorithms

#endif  // TAPROOT_FUZZY_PD_RULE_TABLE_HPP_