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 "mbed.h"
Yo_Robot 0:252f423535e5 2 #include "Fuzzy.h"
Yo_Robot 0:252f423535e5 3
Yo_Robot 0:252f423535e5 4 Serial pc ( USBTX,USBRX ); /* Global Variable for debugging purposes PC */
Yo_Robot 0:252f423535e5 5
Yo_Robot 0:252f423535e5 6 void printMenu();
Yo_Robot 0:252f423535e5 7 void setLimits( Fuzzy &Member );
Yo_Robot 0:252f423535e5 8 void SimpleFuzzy( Fuzzy &Input, Fuzzy &Output );
Yo_Robot 0:252f423535e5 9 void DoubleFuzzy( Fuzzy &MemberX, Fuzzy &MemberY, Fuzzy &Output );
Yo_Robot 0:252f423535e5 10
Yo_Robot 0:252f423535e5 11 int main()
Yo_Robot 0:252f423535e5 12 {
Yo_Robot 0:252f423535e5 13 char ch;
Yo_Robot 0:252f423535e5 14 Fuzzy Error;
Yo_Robot 0:252f423535e5 15 Fuzzy Dt;
Yo_Robot 0:252f423535e5 16 Fuzzy PWM;
Yo_Robot 0:252f423535e5 17 Error.printArt();
Yo_Robot 0:252f423535e5 18 printMenu();
Yo_Robot 0:252f423535e5 19 do
Yo_Robot 0:252f423535e5 20 {
Yo_Robot 0:252f423535e5 21 ch = pc.getc();
Yo_Robot 0:252f423535e5 22 pc.printf( "%c", ch );
Yo_Robot 0:252f423535e5 23 switch ( ch )
Yo_Robot 0:252f423535e5 24 {
Yo_Robot 0:252f423535e5 25 case '1':
Yo_Robot 0:252f423535e5 26 setLimits( Error );
Yo_Robot 0:252f423535e5 27 break;
Yo_Robot 0:252f423535e5 28
Yo_Robot 0:252f423535e5 29 case '2':
Yo_Robot 0:252f423535e5 30 setLimits( Dt );
Yo_Robot 0:252f423535e5 31 break;
Yo_Robot 0:252f423535e5 32
Yo_Robot 0:252f423535e5 33 case '3':
Yo_Robot 0:252f423535e5 34 setLimits( PWM );
Yo_Robot 0:252f423535e5 35 break;
Yo_Robot 0:252f423535e5 36
Yo_Robot 0:252f423535e5 37 case '4':
Yo_Robot 0:252f423535e5 38 SimpleFuzzy( Error, PWM );
Yo_Robot 0:252f423535e5 39 break;
Yo_Robot 0:252f423535e5 40
Yo_Robot 0:252f423535e5 41 case '5':
Yo_Robot 0:252f423535e5 42 DoubleFuzzy( Error, Dt, PWM );
Yo_Robot 0:252f423535e5 43 break;
Yo_Robot 0:252f423535e5 44
Yo_Robot 0:252f423535e5 45 case '6':
Yo_Robot 0:252f423535e5 46 pc.printf( "\n--------end-----------------" );
Yo_Robot 0:252f423535e5 47 break;
Yo_Robot 0:252f423535e5 48 }
Yo_Robot 0:252f423535e5 49 }while( ch != '6' );
Yo_Robot 0:252f423535e5 50 }
Yo_Robot 0:252f423535e5 51
Yo_Robot 0:252f423535e5 52
Yo_Robot 0:252f423535e5 53 void setLimits( Fuzzy &Member )
Yo_Robot 0:252f423535e5 54 {
Yo_Robot 0:252f423535e5 55 float low1, mid1, low2, mid2, hi;
Yo_Robot 0:252f423535e5 56
Yo_Robot 0:252f423535e5 57 pc.printf( "\nlow1 = " );
Yo_Robot 0:252f423535e5 58 pc.scanf( "%f", &low1 );
Yo_Robot 0:252f423535e5 59 pc.printf( "%f",low1 );
Yo_Robot 0:252f423535e5 60
Yo_Robot 0:252f423535e5 61 pc.printf( "\nmid1 = " );
Yo_Robot 0:252f423535e5 62 pc.scanf( "%f", &mid1 );
Yo_Robot 0:252f423535e5 63 pc.printf( "%f",mid1 );
Yo_Robot 0:252f423535e5 64
Yo_Robot 0:252f423535e5 65 pc.printf( "\nlow2 = " );
Yo_Robot 0:252f423535e5 66 pc.scanf( "%f", &low2 );
Yo_Robot 0:252f423535e5 67 pc.printf( "%f",low2 );
Yo_Robot 0:252f423535e5 68
Yo_Robot 0:252f423535e5 69 pc.printf( "\nmid2 = " );
Yo_Robot 0:252f423535e5 70 pc.scanf( "%f", &mid2 );
Yo_Robot 0:252f423535e5 71 pc.printf( "%f",mid2 );
Yo_Robot 0:252f423535e5 72
Yo_Robot 0:252f423535e5 73 pc.printf( "\nhigh = " );
Yo_Robot 0:252f423535e5 74 pc.scanf( "%f", &hi);
Yo_Robot 0:252f423535e5 75 pc.printf( "%f",hi );
Yo_Robot 0:252f423535e5 76
Yo_Robot 0:252f423535e5 77 Member.setMember( low1, mid1, low2, mid2, hi );
Yo_Robot 0:252f423535e5 78 }
Yo_Robot 0:252f423535e5 79
Yo_Robot 0:252f423535e5 80
Yo_Robot 0:252f423535e5 81 void SimpleFuzzy( Fuzzy &Input, Fuzzy &Output )
Yo_Robot 0:252f423535e5 82 {
Yo_Robot 0:252f423535e5 83 float value_in, value_out, read_input1, read_input2;
Yo_Robot 0:252f423535e5 84 do
Yo_Robot 0:252f423535e5 85 {
Yo_Robot 0:252f423535e5 86 Input.limit = limit_low1;
Yo_Robot 0:252f423535e5 87 read_input1 = Input.read( Input.limit );
Yo_Robot 0:252f423535e5 88
Yo_Robot 0:252f423535e5 89 Input.limit = limit_high;
Yo_Robot 0:252f423535e5 90 read_input2 = Input.read( Input.limit );
Yo_Robot 0:252f423535e5 91
Yo_Robot 0:252f423535e5 92 pc.printf( "\n Give a value between %f and %f : ", read_input1, read_input2 );
Yo_Robot 0:252f423535e5 93 pc.scanf( "%f", &value_in );
Yo_Robot 0:252f423535e5 94 pc.printf("%f", value_in);
Yo_Robot 0:252f423535e5 95
Yo_Robot 0:252f423535e5 96 }while ( !( value_in > read_input1 && value_in < read_input2 ) ); // Within Limits
Yo_Robot 0:252f423535e5 97
Yo_Robot 0:252f423535e5 98 value_out = Input.getValue( value_in, Output );
Yo_Robot 0:252f423535e5 99 pc.printf( "\nFor value %f, your output is %f.", value_in, value_out );
Yo_Robot 0:252f423535e5 100
Yo_Robot 0:252f423535e5 101 }
Yo_Robot 0:252f423535e5 102
Yo_Robot 0:252f423535e5 103 void DoubleFuzzy( Fuzzy &MemberX, Fuzzy &MemberY, Fuzzy &Output )
Yo_Robot 0:252f423535e5 104 {
Yo_Robot 0:252f423535e5 105 float valueX, valueY, valueOut, read_input1, read_input2;
Yo_Robot 0:252f423535e5 106
Yo_Robot 0:252f423535e5 107 do
Yo_Robot 0:252f423535e5 108 {
Yo_Robot 0:252f423535e5 109 MemberX.limit = limit_low1;
Yo_Robot 0:252f423535e5 110 read_input1 = MemberX.read( MemberX.limit );
Yo_Robot 0:252f423535e5 111
Yo_Robot 0:252f423535e5 112 MemberX.limit = limit_high;
Yo_Robot 0:252f423535e5 113 read_input2 = MemberX.read( MemberX.limit );
Yo_Robot 0:252f423535e5 114
Yo_Robot 0:252f423535e5 115 pc.printf( "\n Give a value between %f and %f : ", read_input1, read_input2 );
Yo_Robot 0:252f423535e5 116 pc.scanf( "%f", &valueX );
Yo_Robot 0:252f423535e5 117 pc.printf("%f", valueX);
Yo_Robot 0:252f423535e5 118
Yo_Robot 0:252f423535e5 119 }while ( !( valueX > read_input2 && valueX < read_input2 ) ); // Within Limits
Yo_Robot 0:252f423535e5 120
Yo_Robot 0:252f423535e5 121 do
Yo_Robot 0:252f423535e5 122 {
Yo_Robot 0:252f423535e5 123
Yo_Robot 0:252f423535e5 124 MemberY.limit = limit_low1;
Yo_Robot 0:252f423535e5 125 read_input1 = MemberY.read( MemberY.limit );
Yo_Robot 0:252f423535e5 126
Yo_Robot 0:252f423535e5 127 MemberY.limit = limit_high;
Yo_Robot 0:252f423535e5 128 read_input2 = MemberY.read( MemberY.limit );
Yo_Robot 0:252f423535e5 129
Yo_Robot 0:252f423535e5 130 pc.printf( "\n Give a value between %f and %f : ", read_input1, read_input2 );
Yo_Robot 0:252f423535e5 131 pc.scanf( "%f", &valueY );
Yo_Robot 0:252f423535e5 132 pc.printf("%f", valueY);
Yo_Robot 0:252f423535e5 133
Yo_Robot 0:252f423535e5 134 }while ( !( valueY > read_input1 && valueY < read_input2 ) ); // Within Limits
Yo_Robot 0:252f423535e5 135
Yo_Robot 0:252f423535e5 136 valueOut = MemberX.getValue( valueX, valueY, MemberX, MemberY, Output );
Yo_Robot 0:252f423535e5 137 pc.printf( "\nFor value %f and %f, your output is %f.", valueX, valueY, valueOut );
Yo_Robot 0:252f423535e5 138
Yo_Robot 0:252f423535e5 139 }
Yo_Robot 0:252f423535e5 140
Yo_Robot 0:252f423535e5 141 void printMenu()
Yo_Robot 0:252f423535e5 142 {
Yo_Robot 0:252f423535e5 143 pc.printf( "\n\n --== Fuzzy Logic ==--\n" );
Yo_Robot 0:252f423535e5 144 pc.printf( "\nSelect an option: " );
Yo_Robot 0:252f423535e5 145 pc.printf( "\n[1] Set Limits X" );
Yo_Robot 0:252f423535e5 146 pc.printf( "\n[2] Set Limits Y" );
Yo_Robot 0:252f423535e5 147 pc.printf( "\n[3] Set Limits Output" );
Yo_Robot 0:252f423535e5 148 pc.printf( "\n[4] One Argument Fuzzy" );
Yo_Robot 0:252f423535e5 149 pc.printf( "\n[5] Two Argument Fuzzy" );
Yo_Robot 0:252f423535e5 150 pc.printf( "\n[6] Exit\n" );
Yo_Robot 0:252f423535e5 151 }
Yo_Robot 0:252f423535e5 152