Leo Bach / Mbed 2 deprecated Prosjekt_2

Dependencies:   SRF02 SRF05 mbed

Fork of Prosjekt_2 by HIOF Programmering

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SRF05.h"
00003 #include "SRF02.h"
00004 
00005 Serial pc(USBTX, USBRX);
00006 Serial bt(p13, p14);
00007 
00008 DigitalOut led (LED1);
00009 SRF05 srf05(p7, p8);
00010 SRF02 srf02(p9, p10);
00011 
00012 char input();
00013 void avstand();
00014 void detektor(float);
00015 void hastighet();
00016 void triangulering();
00017 
00018 int main()
00019 {
00020     float y;
00021     while(1) {
00022         char kom1 [] = {"\n\nMenu\n1)\tMeassure Distance\n2)\tMotion Detection\n3)\tMeassure Velocity\n4)\tPlacement\n"};
00023         pc.printf("%s", kom1);
00024         bt.printf("%s", kom1);
00025         while (pc.readable() == 0 && bt.readable() == 0) {}
00026         switch (input()) {
00027             case('1'): {
00028                 char kom2 [] = {"\nPress 'y' to meassure distance\nPress 'n' to return to the menu\nThe bargraph will light up according to how close the object is\n1 LED equals 35cm\n"};
00029                 pc.printf("%s", kom2);
00030                 bt.printf("%s", kom2);
00031                 avstand();
00032                 break;
00033             }
00034             case('2'): {
00035                 char kom3 [] = {"\nLights will flash when a motion is detected\nPress 'n' to return to the menu\n"};
00036                 pc.printf("%s", kom3);
00037                 bt.printf("%s", kom3);
00038                 y = srf05.read();
00039                 detektor(y);
00040                 break;
00041             }
00042             case('3'): {
00043                 char kom4 [] = {"\nThis feature meassures the velocity of any movement towards Ultra-Sonic Ranger SRF05\nPress 'n' to return to the menu\n"};
00044                 pc.printf("%s", kom4);
00045                 bt.printf("%s", kom4);
00046                 hastighet();
00047                 break;
00048             }
00049             case('4'): {
00050                 char kom5 [] = {"\nThis feature returns the positon of an object in a 2D-table\nEach 'O' represents 10square centimeters and 'X' represents the detected object\nPlace componentes according to the datasheet\n"};
00051                 pc.printf("%s", kom5);
00052                 bt.printf("%s", kom5);
00053                 triangulering();
00054                 break;
00055             }
00056             default:
00057                 char kom6 [] = {"\nERROR: Invalid value\n"};
00058                 pc.printf("%s", kom6);
00059                 bt.printf("%s", kom6);
00060         }
00061     }
00062 }
00063 
00064 /* Char Input() er en funksjon som avgjør om input kommer fra bluetooth eller pc. Deretter returnerer den input verdien. */
00065 
00066 char input()
00067 {
00068     if(pc.readable())
00069         return getchar();
00070     else if (bt.readable())
00071         return bt.getc();
00072     else
00073         return 1;
00074 }
00075 
00076 /* Void avstand() er en funksjon som måler avstand til en gjenstand innenfor rekkevidden til ultralyd sensoren. Avstanden sendes til pc & bluetooth i cm. */
00077 
00078 void avstand()
00079 {
00080     DigitalOut bar[] = {NC, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30};
00081     char a;
00082     float z, y;
00083     int n = 0;
00084 
00085     while (a != 'n') {
00086         while (pc.readable() == 0 && bt.readable() == 0) {}
00087         a = input();
00088         if (a == 'y') {
00089             y = srf05.read();
00090             if (y > 350) {
00091                 char kom1 [] = {"\nDetection area is exceeded (350cm)\n"};
00092                 pc.printf("%s", kom1);
00093                 bt.printf("%s", kom1);
00094                 y = z;
00095             } else {
00096                 z = y;
00097                 pc.printf("\nMeassurement (%d):\t%.2fcm\n",n, y);
00098                 bt.printf("\nMeassurement (%d):\t%.2fcm\n",n, y);
00099             }
00100             for(int x = 1; x <= 10; x++) {
00101                 (x*35 < y) ?   bar[x] = 0  :   bar[x] = 1;
00102             }
00103             n++;
00104             wait_ms (50);
00105         } else if (a != 'n') {
00106             char kom3 [] = {"\nInvalid input\n"};
00107             pc.printf("%s", kom3);
00108             bt.printf("%s", kom3);
00109         }
00110     }
00111     for(int x = 1; x <= 10; x++)
00112         bar[x] = 0;
00113 }
00114 
00115 /* Funksjonen Void detektor() er en funksjon som detekterer bevegelse. LED blinker ved deteksjon. */
00116 
00117 void detektor(float y)
00118 {
00119     int b = 0;
00120     while (input() != 'n') {
00121         if (y+2 < srf05.read() || y-2 > srf05.read()) {
00122             led = !led;
00123             if (b == 0) {
00124                 char kom1 [] = {"\nMotion Detected\n"};
00125                 pc.printf("%s", kom1);
00126                 bt.printf("%s", kom1);
00127 
00128                 b = 1;
00129             }
00130         } else
00131             b = led = 0;
00132         wait_ms (50);
00133     }
00134 }
00135 
00136 /* Funksjonen Void Hastighet() måler hastigheten til et objekt foran ultralyd sensoren. Hastigheten skrives til pc & bluetooth i km/t. */
00137 
00138 void hastighet ()
00139 {
00140     Timer t;
00141     float start, stopp, fart;
00142     while(input() != 'n') {
00143         t.start();
00144         start = srf05.read();
00145         while (t.read() < 0.25 && srf05.read() > 10) {}
00146         t.stop();
00147         stopp = srf05.read();
00148         fart = (start - stopp)*(3.6/(t.read()*100));
00149         if (stopp < 10) {
00150             char kom1 [] = {"\nERROR: Closer than 10cm from the Ultra Sonic Ranger."};
00151             pc.printf("%s", kom1);
00152             bt.printf("%s", kom1);
00153             wait_ms (250);
00154         }
00155         if (0.5 < fart && start < 350 && stopp < 350) {
00156             pc.printf("\nVelocity is = %0.2f km/t", fart);
00157             bt.printf("\nVelocity is = %0.2f km/t", fart);
00158         }
00159         t.reset();
00160     }
00161 }
00162 
00163 /*Funksjonen plasseringiplan måler avstanden til et objekt i målesonen, fra to forskjellige vinkler. Avstanden til objektet plasseres i et 2D-plan. Størrelsen på planet er 100cm langs x akse og 50cm langs y akse.*/
00164 
00165 void triangulering ()
00166 {
00167     char _2DplanX[12] = {'|','O','O','O','O','O','O','O','O','O','O','|'};
00168     char _2Dplan[12] = {'|','O','O','O','O','O','O','O','O','O','O','|'};
00169     double vinkel_b, vinkel_a, vinkel_x, vinkel_y, y_akse, x_akse, hyp, PI;
00170     int a, b, c, n, m;
00171     float d;
00172     PI = acos(-1.0);
00173     hyp = 305.0;
00174     char h;
00175     while (h != 'n') {
00176         while (pc.readable() == 0 && bt.readable() == 0) {}
00177         h = input();
00178         if (h == 'y') {
00179             d = 0.0;
00180             b = n = m = 0;
00181             while (d < 180.0 || d > 280.0 && n < 10) {
00182                 d = srf05.read();
00183                 n++;
00184                 wait_ms (100);
00185             }
00186             while (b < 150 || b > 250 && m < 10) {
00187                 b = srf02.getDistanceCm();
00188                 wait_ms (250);
00189                 m++;
00190             }
00191             if (n == 10 || m == 10) {
00192                 char kom1 []= {"\nERROR: No object detected within the bouandaries\n"};
00193                 pc.printf("%s", kom1);
00194                 bt.printf("%s", kom1);
00195             } else {
00196                 vinkel_b = acos((hyp*hyp+d*d-b*b)/(2.0*hyp*d));
00197                 vinkel_a = acos((hyp*hyp+b*b-d*d)/(2.0*hyp*b));
00198                 vinkel_y = PI-0.8076167287-vinkel_b;
00199                 if( vinkel_y > PI/2.0)
00200                     vinkel_y = 0.8076167287+vinkel_b;
00201                 y_akse = sin(vinkel_y)*d;
00202                 vinkel_x = PI-0.7631795981-vinkel_a;
00203                 if( vinkel_x > PI/2.0)
00204                     vinkel_x = 0.7631795981+vinkel_a;
00205                 x_akse = sin(vinkel_x)*b;
00206                 for (c = 1; c < 11; c++) {
00207                     if (((c-1)*10+150) <= x_akse && x_akse < (c*10+150))
00208                         _2DplanX[c] = 'X';
00209                     else
00210                         _2DplanX[c] = 'O';
00211                 }
00212                 for (c = 280; c >= 190; c=c-10) {
00213                     if (c-10 <= y_akse && y_akse < c) {
00214                         for(a = 0; a < 12; a++) {
00215                             pc.printf("%c",_2DplanX[a]);
00216                             bt.printf("%c",_2DplanX[a]);
00217                         }
00218                     } else {
00219                         for(a = 0; a < 12; a++) {
00220                             pc.printf("%c",_2Dplan[a]);
00221                             bt.printf("%c",_2Dplan[a]);
00222                         }
00223                     }
00224                     char kom2 []= {"\n"};
00225                     pc.printf("%s", kom2);
00226                     bt.printf("%s", kom2);
00227                 }
00228             }
00229         } else if (h != 'n') {
00230             char kom3 [] = {"\nInvalid input\n"};
00231             pc.printf("%s", kom3);
00232             bt.printf("%s", kom3);
00233         }
00234     }
00235 }