José Claudio / Mbed 2 deprecated QuadCopter-Sensor-Serial

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MembershipFunction.cpp Source File

MembershipFunction.cpp

00001 #include "MembershipFunction.h"
00002 
00003 MembershipFunction::MembershipFunction(mfType type, float a, float b, float c, float d, float u, float v)
00004 {
00005     this->type = type;
00006     
00007     this->a = a;
00008     this->b = b;
00009     this->c = c;
00010     this->d = d;
00011     
00012     this->u = u;
00013     this->v = v;
00014 }
00015 
00016 MembershipFunction::MembershipFunction(mfType type)
00017 {
00018     this->type = type;
00019     
00020     a = 0;
00021     b = 0;
00022     c = 0;
00023     d = 0;
00024     
00025     u = 0;
00026     v = 0;
00027 
00028     //MembershipFunction(type, 0, 0, 0, 0, 0, 0);
00029 }
00030 
00031 MembershipFunction::~MembershipFunction(void)
00032 {
00033 }
00034 
00035 float MembershipFunction::getValue(float x)
00036 {
00037     switch (type)
00038     {
00039         case TRIMF:
00040             return trimf(x);
00041         break;
00042 
00043         case TRAPMF:
00044             return trapmf(x);
00045         break;
00046     }
00047     
00048     return 0;
00049 }
00050 
00051 void MembershipFunction::setValues(float a, float b, float c)
00052 {
00053     this->a = a;
00054     this->b = b;
00055     this->c = c;
00056 }
00057 
00058 void MembershipFunction::setValues(float a, float b, float c, float d)
00059 {
00060     this->a = a;
00061     this->b = b;
00062     this->c = c;
00063     this->d = d;
00064 }
00065 
00066 void MembershipFunction::setValues(float u, float v)
00067 {
00068     this->u = u;
00069     this->v = v;
00070 }
00071 
00072 float MembershipFunction::trimf(float x)
00073 {
00074     if (x <= a || x > c)
00075         return 0;
00076 
00077     if (x > a && x <= b)
00078         return (x - a) / (b - a);
00079     
00080     if (x > b && x <= c)
00081         return (c - x) / (c - b);
00082         
00083     return 0;
00084 }
00085 
00086 float MembershipFunction::trapmf(float x)
00087 {
00088     if (x <= a || x > d)
00089         return 0;
00090 
00091     if (x > a && x <= b)
00092         return (x - a) / (b - a);
00093 
00094     if (x > b && x <= c)
00095         return 1;
00096     
00097     if (x > c && x <= d)
00098         return (d - x) / (d - c);
00099         
00100     return 0;
00101 }
00102 
00103 float MembershipFunction::centroid(float h)
00104 {
00105     float p1, p2, at, bt, ct;
00106 
00107     switch (type)
00108     {
00109         case TRIMF:
00110             p1 = a + (b - a) * h;
00111             p2 = c - (c - b) * h;
00112 
00113             at = p2 - p1;
00114             bt = c - a;
00115             ct = p1 - a;
00116 
00117             return a + (2*at*ct + at*at + ct*bt + at*bt + bt*bt) / (3 * (at + bt));
00118             //return p1 + (p2 - p1) / 2.0;
00119         break;
00120 
00121         case TRAPMF:
00122             p1 = a + (b - a) * h;
00123             p2 = d - (d - c) * h;
00124             return p1 + (p2 - p1) / 2.0;
00125         break;
00126     }
00127 
00128     //return a + (c - a) / 2.0;
00129     return 0;
00130 }