This is my first 'big' mbed program so far, it is an implementation of fuzzy logic, the main.cpp is kind off a debugging program, to show how it works, I still have planned some enhancements, but any comment is well recieved, i\'m still learning What i'm looking forward in publishing this, is ways to improve the code, some general advices that may help write better code. Any comment post it here: http://mbed.org/users/Yo_Robot/notebook/fuzzy_logic/

Dependencies:   mbed

Committer:
Yo_Robot
Date:
Sun Mar 20 04:20:47 2011 +0000
Revision:
0:252f423535e5
v0.1 - still under test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yo_Robot 0:252f423535e5 1 #include "Fuzzy.h"
Yo_Robot 0:252f423535e5 2
Yo_Robot 0:252f423535e5 3 extern Serial pc;
Yo_Robot 0:252f423535e5 4
Yo_Robot 0:252f423535e5 5 void Fuzzy::setMember( float low1, float mid1, float low2, float mid2, float hi )
Yo_Robot 0:252f423535e5 6 {
Yo_Robot 0:252f423535e5 7 if( low1 < mid1 && mid1 < low2 && low2 < mid2 && mid2 < hi )
Yo_Robot 0:252f423535e5 8 {
Yo_Robot 0:252f423535e5 9 left.low_ = low1; center.low_ = mid1; right.low_ = low2;
Yo_Robot 0:252f423535e5 10 left.mid_ = mid1; center.mid_ = low2; right.mid_ = mid2;
Yo_Robot 0:252f423535e5 11 left.high_= low2; center.high_= mid2; right.high_= hi;
Yo_Robot 0:252f423535e5 12 }
Yo_Robot 0:252f423535e5 13 else
Yo_Robot 0:252f423535e5 14 pc.printf( "\nInvalid Limits Set them Again" );
Yo_Robot 0:252f423535e5 15 }
Yo_Robot 0:252f423535e5 16
Yo_Robot 0:252f423535e5 17 float Fuzzy::read( limitEnum element )
Yo_Robot 0:252f423535e5 18 {
Yo_Robot 0:252f423535e5 19 switch ( element )
Yo_Robot 0:252f423535e5 20 {
Yo_Robot 0:252f423535e5 21 case limit_low1:
Yo_Robot 0:252f423535e5 22 return left.low_;
Yo_Robot 0:252f423535e5 23
Yo_Robot 0:252f423535e5 24 case limit_mid1:
Yo_Robot 0:252f423535e5 25 return left.mid_;
Yo_Robot 0:252f423535e5 26
Yo_Robot 0:252f423535e5 27 case limit_low2:
Yo_Robot 0:252f423535e5 28 return left.high_;
Yo_Robot 0:252f423535e5 29
Yo_Robot 0:252f423535e5 30 case limit_mid2:
Yo_Robot 0:252f423535e5 31 return right.mid_;
Yo_Robot 0:252f423535e5 32
Yo_Robot 0:252f423535e5 33 case limit_high:
Yo_Robot 0:252f423535e5 34 return right.high_;
Yo_Robot 0:252f423535e5 35 }
Yo_Robot 0:252f423535e5 36 /* NO DEFAULT RETURN if requested an invalid number, stop the program */
Yo_Robot 0:252f423535e5 37 }
Yo_Robot 0:252f423535e5 38 float Fuzzy::getValue( float x, Fuzzy Output )
Yo_Robot 0:252f423535e5 39 {
Yo_Robot 0:252f423535e5 40 float hLow, hMid, hHigh;
Yo_Robot 0:252f423535e5 41
Yo_Robot 0:252f423535e5 42 hLow = fuzz( x, left ); /* negative = Member Structure Negative */
Yo_Robot 0:252f423535e5 43 hMid = fuzz( x, center );
Yo_Robot 0:252f423535e5 44 hHigh= fuzz( x, right );
Yo_Robot 0:252f423535e5 45
Yo_Robot 0:252f423535e5 46 calculateArea( hLow, hMid, hHigh, Output ); /* This Calcuates the areas and writes directly into the object */
Yo_Robot 0:252f423535e5 47
Yo_Robot 0:252f423535e5 48 return defuzz( Output ); /* returns the Output value */
Yo_Robot 0:252f423535e5 49
Yo_Robot 0:252f423535e5 50 /* So the program runs like this: When you create your Fuzzy Object and set the paramaters, you create
Yo_Robot 0:252f423535e5 51 * an structure that holds the limits of the three trianguls and another structures that holds the areas
Yo_Robot 0:252f423535e5 52 * of each of these trianguls according to a value given.
Yo_Robot 0:252f423535e5 53 * 'x' can be error (x = set point - sensor), this functions first calculates the height of the
Yo_Robot 0:252f423535e5 54 * triangul for all three trianguls, then passes these values (hLow, hMid, hHigh) to calculate the areas
Yo_Robot 0:252f423535e5 55 * and writes these areas in the object srtucture described above.
Yo_Robot 0:252f423535e5 56 * Finally to deffuz you only need an Output fuzzy object to set the limits desired to generate an output value
Yo_Robot 0:252f423535e5 57 * all other data necesary for the calculation is already whitin the structures of the object
Yo_Robot 0:252f423535e5 58 */
Yo_Robot 0:252f423535e5 59 }
Yo_Robot 0:252f423535e5 60
Yo_Robot 0:252f423535e5 61 float Fuzzy::getValue( float x, float y, Fuzzy memberX, Fuzzy memberY, Fuzzy Output )
Yo_Robot 0:252f423535e5 62 {
Yo_Robot 0:252f423535e5 63 float hLowX, hMidX, hHighX; /* Heights for Object X */
Yo_Robot 0:252f423535e5 64 float hLowY, hMidY, hHighY; /* Heights for Object Y */
Yo_Robot 0:252f423535e5 65 float hLowLow, hLowMid, hLowHigh;
Yo_Robot 0:252f423535e5 66 float hMidLow, hMidMid, hMidHigh;
Yo_Robot 0:252f423535e5 67 float hHighLow,hHighMid,hHighHigh;
Yo_Robot 0:252f423535e5 68 float hLow, hMid, hHigh;
Yo_Robot 0:252f423535e5 69
Yo_Robot 0:252f423535e5 70 hLowX = fuzz( x, memberX.left );
Yo_Robot 0:252f423535e5 71 hMidX = fuzz( x, memberX.center );
Yo_Robot 0:252f423535e5 72 hHighX= fuzz( x, memberX.right );
Yo_Robot 0:252f423535e5 73
Yo_Robot 0:252f423535e5 74 hLowY = fuzz( y, memberY.left );
Yo_Robot 0:252f423535e5 75 hMidY = fuzz( y, memberY.center );
Yo_Robot 0:252f423535e5 76 hHighY= fuzz( y, memberY.right );
Yo_Robot 0:252f423535e5 77
Yo_Robot 0:252f423535e5 78 hLowLow = min( hLowX, hLowY );
Yo_Robot 0:252f423535e5 79 hLowMid = min( hLowX, hMidY );
Yo_Robot 0:252f423535e5 80 hLowHigh= min( hLowX, hHighY );
Yo_Robot 0:252f423535e5 81
Yo_Robot 0:252f423535e5 82 hMidLow = min( hMidX, hLowY );
Yo_Robot 0:252f423535e5 83 hMidMid = min( hMidX, hMidY );
Yo_Robot 0:252f423535e5 84 hMidHigh= min( hMidX, hHighY );
Yo_Robot 0:252f423535e5 85
Yo_Robot 0:252f423535e5 86 hHighLow =min( hHighX, hLowY );
Yo_Robot 0:252f423535e5 87 hHighMid =min( hHighX, hMidY );
Yo_Robot 0:252f423535e5 88 hHighHigh=min( hHighX, hHighY);
Yo_Robot 0:252f423535e5 89
Yo_Robot 0:252f423535e5 90 hLow = max( hLowLow, hMidLow, hHighLow );
Yo_Robot 0:252f423535e5 91 hMid = max( hLowMid, hMidMid, hHighMid );
Yo_Robot 0:252f423535e5 92 hHigh= max( hLowHigh,hMidHigh,hHighHigh);
Yo_Robot 0:252f423535e5 93
Yo_Robot 0:252f423535e5 94 memberX.Area.left_ = hLow;
Yo_Robot 0:252f423535e5 95 memberX.Area.center_ = hMid;
Yo_Robot 0:252f423535e5 96 memberX.Area.right_ = hHigh;
Yo_Robot 0:252f423535e5 97 memberY.Area = memberX.Area;
Yo_Robot 0:252f423535e5 98
Yo_Robot 0:252f423535e5 99 return memberX.defuzz( Output );
Yo_Robot 0:252f423535e5 100 }
Yo_Robot 0:252f423535e5 101 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Yo_Robot 0:252f423535e5 102
Yo_Robot 0:252f423535e5 103 float Fuzzy::fuzz( float value, limitStr member )
Yo_Robot 0:252f423535e5 104 {
Yo_Robot 0:252f423535e5 105 float return_value; /* return value */
Yo_Robot 0:252f423535e5 106
Yo_Robot 0:252f423535e5 107 if( value > member.low_ && value < member.mid_ )
Yo_Robot 0:252f423535e5 108 { return_value = 1 + ( value - member.mid_ )/( member.mid_ - member.low_ ); }
Yo_Robot 0:252f423535e5 109
Yo_Robot 0:252f423535e5 110 else if( value > member.mid_ && value < member.high_ )
Yo_Robot 0:252f423535e5 111 { return_value = 1 - ( value - member.high_ )/( member.high_ - member.mid_ ); }
Yo_Robot 0:252f423535e5 112
Yo_Robot 0:252f423535e5 113 else
Yo_Robot 0:252f423535e5 114 return_value = 0;
Yo_Robot 0:252f423535e5 115
Yo_Robot 0:252f423535e5 116 return return_value;
Yo_Robot 0:252f423535e5 117 }
Yo_Robot 0:252f423535e5 118
Yo_Robot 0:252f423535e5 119 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Yo_Robot 0:252f423535e5 120 float Fuzzy::min( float x1, float x2 )
Yo_Robot 0:252f423535e5 121 {
Yo_Robot 0:252f423535e5 122 if (x1 >= x2)
Yo_Robot 0:252f423535e5 123 return x1;
Yo_Robot 0:252f423535e5 124 else
Yo_Robot 0:252f423535e5 125 return x2;
Yo_Robot 0:252f423535e5 126 }
Yo_Robot 0:252f423535e5 127
Yo_Robot 0:252f423535e5 128 float Fuzzy::max( float x1, float x2, float x3 )
Yo_Robot 0:252f423535e5 129 {
Yo_Robot 0:252f423535e5 130 if( x1 >= x2 && x1 >= x3 )
Yo_Robot 0:252f423535e5 131 return x1;
Yo_Robot 0:252f423535e5 132
Yo_Robot 0:252f423535e5 133 else if( x2 >= x1 && x2 >= x3)
Yo_Robot 0:252f423535e5 134 return x2;
Yo_Robot 0:252f423535e5 135
Yo_Robot 0:252f423535e5 136 else if( x3 >= x1 && x3 >= x2)
Yo_Robot 0:252f423535e5 137 return x3;
Yo_Robot 0:252f423535e5 138
Yo_Robot 0:252f423535e5 139 else
Yo_Robot 0:252f423535e5 140 return x2;
Yo_Robot 0:252f423535e5 141 }
Yo_Robot 0:252f423535e5 142 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Yo_Robot 0:252f423535e5 143
Yo_Robot 0:252f423535e5 144 void Fuzzy::calculateArea( float n, float z, float p , Fuzzy Output )
Yo_Robot 0:252f423535e5 145 {
Yo_Robot 0:252f423535e5 146 Area.left_ = ( n * ( Output.left.high_ - Output.left.low_ ) ) / 2.0 ;
Yo_Robot 0:252f423535e5 147 Area.right_ = ( p * ( Output.right.high_ - Output.right.low_ ) ) / 2.0 ;
Yo_Robot 0:252f423535e5 148 Area.center_ = ( z * ( Output.center.high_ - Output.center.low_ ) ) / 2.0 ;
Yo_Robot 0:252f423535e5 149 }
Yo_Robot 0:252f423535e5 150
Yo_Robot 0:252f423535e5 151 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Yo_Robot 0:252f423535e5 152
Yo_Robot 0:252f423535e5 153 float Fuzzy::defuzz( Fuzzy Output )
Yo_Robot 0:252f423535e5 154 {
Yo_Robot 0:252f423535e5 155 float num, den;
Yo_Robot 0:252f423535e5 156
Yo_Robot 0:252f423535e5 157 num = Area.left_ * Output.left.mid_ + Area.center_ * Output.center.mid_ + Area.right_ * Output.right.mid_;
Yo_Robot 0:252f423535e5 158
Yo_Robot 0:252f423535e5 159 den = Area.left_ + Area.right_ + Area.center_ ;
Yo_Robot 0:252f423535e5 160
Yo_Robot 0:252f423535e5 161 return ( num / den );
Yo_Robot 0:252f423535e5 162 }
Yo_Robot 0:252f423535e5 163
Yo_Robot 0:252f423535e5 164 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Yo_Robot 0:252f423535e5 165
Yo_Robot 0:252f423535e5 166 void Fuzzy:: printArt()
Yo_Robot 0:252f423535e5 167 {
Yo_Robot 0:252f423535e5 168 pc.printf( "\n| /\\ /\\ /\\ " );
Yo_Robot 0:252f423535e5 169 pc.printf( "\n| / \\ / \\ / \\ " );
Yo_Robot 0:252f423535e5 170 pc.printf( "\n| / \\ / \\ / \\ " );
Yo_Robot 0:252f423535e5 171 pc.printf( "\n| / \\/ \\/ \\ " );
Yo_Robot 0:252f423535e5 172 pc.printf( "\n| / /\\ /\\ \\ " );
Yo_Robot 0:252f423535e5 173 pc.printf( "\n| / / \\ / \\ \\ " );
Yo_Robot 0:252f423535e5 174 pc.printf( "\n| / / \\ / \\ \\ " );
Yo_Robot 0:252f423535e5 175 pc.printf( "\n| / / \\/ \\ \\ " );
Yo_Robot 0:252f423535e5 176 pc.printf( "\n--|-------|-------|-------|-------|- " );
Yo_Robot 0:252f423535e5 177 pc.printf( "\nlow1 mid1 low2 mid2 high \n" );
Yo_Robot 0:252f423535e5 178
Yo_Robot 0:252f423535e5 179 }
Yo_Robot 0:252f423535e5 180
Yo_Robot 0:252f423535e5 181
Yo_Robot 0:252f423535e5 182
Yo_Robot 0:252f423535e5 183
Yo_Robot 0:252f423535e5 184
Yo_Robot 0:252f423535e5 185
Yo_Robot 0:252f423535e5 186