Test

Dependencies:   SRF02 SRF05 mbed

Fork of Prosjekt_2 by HIOF Programmering

Committer:
leonardene
Date:
Fri Apr 01 12:47:28 2016 +0000
Revision:
8:63f1033aaea2
Parent:
7:112b745b2b9e
Child:
9:4b38e90b8f10
Resultat etter arbeid p? fredag, hovedsaklig triangulering

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leonardene 0:bf27900bdc22 1 #include "mbed.h"
leonardene 1:f3c6c8f735a5 2 #include "SRF05.h"
leonardene 7:112b745b2b9e 3 #include "SRF02.h"
leonardene 1:f3c6c8f735a5 4
leonardene 1:f3c6c8f735a5 5 Serial pc(USBTX, USBRX);
leonardene 1:f3c6c8f735a5 6 Serial bt(p13, p14);
leonardene 0:bf27900bdc22 7
leonardene 1:f3c6c8f735a5 8 DigitalOut led (LED1);
leonardene 1:f3c6c8f735a5 9 DigitalIn imp (p12);
leonardene 7:112b745b2b9e 10 SRF05 srf05(p7, p8);
leonardene 8:63f1033aaea2 11 SRF02 srf02(p9, p10);
leonardene 1:f3c6c8f735a5 12 DigitalOut bar[] = {NC, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30};
leonardene 1:f3c6c8f735a5 13
leonardene 2:d8543bd496a0 14 void meny();
leonardene 4:690abfca1f4d 15 char input();
leonardene 4:690abfca1f4d 16 void avstand();
leonardene 1:f3c6c8f735a5 17 void detektor(float);
leonardene 1:f3c6c8f735a5 18 void hastighet(float);
leonardene 7:112b745b2b9e 19 void triangulering();
leonardene 0:bf27900bdc22 20
leonardene 1:f3c6c8f735a5 21 int main()
leonardene 1:f3c6c8f735a5 22 {
leonardene 2:d8543bd496a0 23 while(1) {
leonardene 2:d8543bd496a0 24 meny();
leonardene 2:d8543bd496a0 25 }
leonardene 2:d8543bd496a0 26 }
leonardene 2:d8543bd496a0 27
leonardene 5:7209741a08bc 28 /* Void meny() er en valg meny. Her kan en bestemme hvilke handlinger som skal utføres. Det er tre muligheter å velge imellom: avstandsmåling, deteksjon og farts måling. */
leonardene 5:7209741a08bc 29
leonardene 2:d8543bd496a0 30 void meny()
leonardene 2:d8543bd496a0 31 {
leonardene 1:f3c6c8f735a5 32 float y;
leonardene 0:bf27900bdc22 33 while(1) {
leonardene 7:112b745b2b9e 34 pc.printf("\nMenu\n1)\tMeassure Distance\n2)\tMotion Detection\n3)\tMeassure Velocity\n4)\tTriangulation\n");
leonardene 7:112b745b2b9e 35 bt.printf("\nMenu\n1)\tMeassure Distance\n2)\tMotion Detection\n3)\tMeassure Velocity\n4)\tTriangulation\n");
leonardene 4:690abfca1f4d 36 while (pc.readable() == 0 && bt.readable() == 0) {}
leonardene 4:690abfca1f4d 37 switch (input()) {
leonardene 1:f3c6c8f735a5 38 case('1'): {
leonardene 4:690abfca1f4d 39 pc.printf("\nPress 'y' to meassure distance\nPress 'n' to return to the menu\nThe bargraph will light up according to the distance\n1 LED equals 40cm\n");
leonardene 4:690abfca1f4d 40 bt.printf("\nPress 'y' to meassure distance\nPress 'n' to return to the menu\nThe bargraph will light up according to the distance\n1 LED equals 40cm\n");
leonardene 4:690abfca1f4d 41 avstand();
leonardene 1:f3c6c8f735a5 42 break;
leonardene 1:f3c6c8f735a5 43 }
leonardene 1:f3c6c8f735a5 44 case('2'): {
leonardene 4:690abfca1f4d 45 pc.printf("\nLights will flash when a motion is detected\nPress 'n' to return to the menu\n");
leonardene 4:690abfca1f4d 46 bt.printf("\nLights will flash when a motion is detected\nPress 'n' to return to the menu\n");
leonardene 7:112b745b2b9e 47 y = srf05.read();
leonardene 2:d8543bd496a0 48 detektor(y);
leonardene 1:f3c6c8f735a5 49 break;
leonardene 1:f3c6c8f735a5 50 }
leonardene 1:f3c6c8f735a5 51 case('3'): {
leonardene 4:690abfca1f4d 52 pc.printf("\nThis feature meassures the velocity of any movement directed directly towards or away from the Mbed\nPress 'n' to return to the menu\n");
leonardene 4:690abfca1f4d 53 bt.printf("\nThis feature meassures the velocity of any movement directed directly towards or away from the Mbed\nPress 'n' to return to the menu\n");
leonardene 7:112b745b2b9e 54 y = srf05.read();
leonardene 1:f3c6c8f735a5 55 hastighet(y);
leonardene 1:f3c6c8f735a5 56 break;
leonardene 2:d8543bd496a0 57 }
leonardene 7:112b745b2b9e 58 case('4'): {
leonardene 8:63f1033aaea2 59 pc.printf("\nThis feature triangulation an object and forwards the positon within a 2D-table\nEach 'O' represents 10square centimeters.\nMeassurements start from 50cm away for SRF02 and 140 CM away from SRF05\n");
leonardene 8:63f1033aaea2 60 bt.printf("\nThis feature triangulation an object and forwards the positon within a 2D-table\nEach 'O' represents 10square centimeters.\nMeassurements start from 50cm away for SRF02 and 140 CM away from SRF05\n");
leonardene 8:63f1033aaea2 61 triangulering();
leonardene 7:112b745b2b9e 62 break;
leonardene 8:63f1033aaea2 63
leonardene 7:112b745b2b9e 64 }
leonardene 1:f3c6c8f735a5 65 default:
leonardene 6:5fee13e621e0 66 pc.printf("\nERROR: Invalid value\n");
leonardene 6:5fee13e621e0 67 bt.printf("\nERROR: Invalid value\n");
leonardene 1:f3c6c8f735a5 68 }
leonardene 0:bf27900bdc22 69 }
leonardene 4:690abfca1f4d 70 }
leonardene 5:7209741a08bc 71
leonardene 5:7209741a08bc 72 /* Char Input() er en funksjon som avgjør om input kommer fra bluetooth eller pc. Deretter returnerer den input verdien. */
leonardene 5:7209741a08bc 73
leonardene 4:690abfca1f4d 74 char input()
leonardene 4:690abfca1f4d 75 {
leonardene 4:690abfca1f4d 76 if(pc.readable())
leonardene 4:690abfca1f4d 77 return getchar();
leonardene 4:690abfca1f4d 78 else if (bt.readable())
leonardene 4:690abfca1f4d 79 return bt.getc();
leonardene 4:690abfca1f4d 80 else
leonardene 4:690abfca1f4d 81 return 1;
leonardene 0:bf27900bdc22 82 }
leonardene 1:f3c6c8f735a5 83
leonardene 5:7209741a08bc 84 /* Void avstand() er en funksjon som måler avstand til en gjenstand innenfor rekkevidden til ultralyd sensoren. Avstanden sendes til pc & bluetooth i cm. */
leonardene 5:7209741a08bc 85
leonardene 4:690abfca1f4d 86 void avstand()
leonardene 4:690abfca1f4d 87 {
leonardene 4:690abfca1f4d 88 char a;
leonardene 4:690abfca1f4d 89 float y;
leonardene 4:690abfca1f4d 90 int n = 0;
leonardene 1:f3c6c8f735a5 91
leonardene 2:d8543bd496a0 92 while (1) {
leonardene 4:690abfca1f4d 93 while (pc.readable() == 0 && bt.readable() == 0) {}
leonardene 4:690abfca1f4d 94 a = input();
leonardene 4:690abfca1f4d 95 if (a == 'y') {
leonardene 7:112b745b2b9e 96 y = srf05.read();
leonardene 5:7209741a08bc 97 if (y > 350) {
leonardene 5:7209741a08bc 98 y = 350;
leonardene 5:7209741a08bc 99 pc.printf("\nDetection area is exceeded (350cm)\n");
leonardene 5:7209741a08bc 100 bt.printf("\nDetection area is exceeded (350cm)\n");
leonardene 5:7209741a08bc 101 } else {
leonardene 5:7209741a08bc 102 pc.printf("\nMeassurement (%d):\t%.2fcm\n",n, y);
leonardene 5:7209741a08bc 103 bt.printf("\nMeassurement (%d):\t%.2fcm\n",n, y);
leonardene 4:690abfca1f4d 104 }
leonardene 5:7209741a08bc 105 for(int x = 1; x <= 10; x++) {
leonardene 5:7209741a08bc 106 (x*35 < y) ? bar[x] = 1 : bar[x] = 0;
leonardene 5:7209741a08bc 107 }
leonardene 4:690abfca1f4d 108 n++;
leonardene 4:690abfca1f4d 109 wait_ms (50);
leonardene 4:690abfca1f4d 110 } else if(a == 'n')
leonardene 4:690abfca1f4d 111 break;
leonardene 6:5fee13e621e0 112 else {
leonardene 6:5fee13e621e0 113 pc.printf("\nInvalid input\n");
leonardene 6:5fee13e621e0 114 bt.printf("\nInvalid input\n");
leonardene 6:5fee13e621e0 115 }
leonardene 1:f3c6c8f735a5 116 }
leonardene 4:690abfca1f4d 117 for(int x = 1; x <= 10; x++)
leonardene 4:690abfca1f4d 118 bar[x] = 0;
leonardene 1:f3c6c8f735a5 119 }
leonardene 1:f3c6c8f735a5 120
leonardene 5:7209741a08bc 121 /* Funksjonen Void detektor() er en funksjon som detekterer bevegelse. LED blinker ved deteksjon. */
leonardene 5:7209741a08bc 122
leonardene 1:f3c6c8f735a5 123 void detektor(float y)
leonardene 1:f3c6c8f735a5 124 {
leonardene 4:690abfca1f4d 125 int b = 0;
leonardene 2:d8543bd496a0 126 while (1) {
leonardene 7:112b745b2b9e 127 if (y+2 < srf05.read() || y-2 > srf05.read()) {
leonardene 4:690abfca1f4d 128 led = !led;
leonardene 4:690abfca1f4d 129 if (b == 0) {
leonardene 4:690abfca1f4d 130 pc.printf("\nMotion Detected\n");
leonardene 4:690abfca1f4d 131 bt.printf("\nMotion Detected\n");
leonardene 4:690abfca1f4d 132 b = 1;
leonardene 4:690abfca1f4d 133 }
leonardene 4:690abfca1f4d 134 } else
leonardene 4:690abfca1f4d 135 led = 0;
leonardene 4:690abfca1f4d 136 if(input() == 'n') break;
leonardene 2:d8543bd496a0 137 wait_ms (50);
leonardene 2:d8543bd496a0 138 }
leonardene 1:f3c6c8f735a5 139 }
leonardene 1:f3c6c8f735a5 140
leonardene 5:7209741a08bc 141 /* Funksjonen Void Hastighet() måler hastigheten til et objekt foran ultralyd sensoren. Hastigheten skrives til pc & bluetooth i km/t. */
leonardene 5:7209741a08bc 142
leonardene 1:f3c6c8f735a5 143 void hastighet (float y)
leonardene 1:f3c6c8f735a5 144 {
leonardene 3:1eb0ad70f562 145 Timer t;
leonardene 4:690abfca1f4d 146 float start, stopp;
leonardene 1:f3c6c8f735a5 147 while(1) {
leonardene 4:690abfca1f4d 148 t.start();
leonardene 7:112b745b2b9e 149 start = srf05.read();
leonardene 7:112b745b2b9e 150 while (t.read() < 0.25 && srf05.read() < 10) {}
leonardene 4:690abfca1f4d 151 t.stop();
leonardene 7:112b745b2b9e 152 stopp = srf05.read();
leonardene 6:5fee13e621e0 153 if (0.05 < (start-stopp)/(25) && start < 350 && stopp < 350) {
leonardene 6:5fee13e621e0 154 pc.printf("\nVelocity is = %0.2f km/t", (start-stopp)*3.6/25);
leonardene 6:5fee13e621e0 155 bt.printf("\nVelocity is = %0.2f km/t", (start-stopp)*3.6/25);
leonardene 6:5fee13e621e0 156 }
leonardene 4:690abfca1f4d 157 t.reset();
leonardene 5:7209741a08bc 158 if(input() == 'n') break;
leonardene 1:f3c6c8f735a5 159 }
leonardene 7:112b745b2b9e 160 }
leonardene 7:112b745b2b9e 161
leonardene 8:63f1033aaea2 162 /*Funksjonen triangulering 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.*/
leonardene 8:63f1033aaea2 163
leonardene 7:112b745b2b9e 164 void triangulering ()
leonardene 7:112b745b2b9e 165 {
leonardene 8:63f1033aaea2 166 char _2DplanX[12] = {'|','O','O','O','O','O','O','O','O','O','O','|'};
leonardene 8:63f1033aaea2 167 char _2Dplan[12] = {'|','O','O','O','O','O','O','O','O','O','O','|'};
leonardene 8:63f1033aaea2 168 int a, b, c;
leonardene 8:63f1033aaea2 169 float d = srf05.read();
leonardene 8:63f1033aaea2 170 b = srf02.getDistanceCm();
leonardene 8:63f1033aaea2 171 pc.printf("\nx = %d\ny = %f\n", b, d);
leonardene 8:63f1033aaea2 172 bt.printf("\nx = %d\ny = %f\n", b, d);
leonardene 8:63f1033aaea2 173 for (c = 1; c < 11; c++) {
leonardene 8:63f1033aaea2 174 if ((c*10+40) <= b && b < ((c+1)*10+40))
leonardene 8:63f1033aaea2 175 _2DplanX[c] = 'x';
leonardene 8:63f1033aaea2 176 else
leonardene 8:63f1033aaea2 177 _2DplanX[c] = 'O';
leonardene 8:63f1033aaea2 178 }
leonardene 8:63f1033aaea2 179 for (c = 190; c >= 150; c=c-10) {
leonardene 8:63f1033aaea2 180 if (c-10 <= d && d < c) {
leonardene 8:63f1033aaea2 181 for(a = 0; a < 12; a++)
leonardene 8:63f1033aaea2 182 pc.printf("%c",_2DplanX[a]);
leonardene 8:63f1033aaea2 183 bt.printf("%c",_2DplanX[a]);
leonardene 8:63f1033aaea2 184 } else {
leonardene 8:63f1033aaea2 185 for(a = 0; a < 12; a++)
leonardene 8:63f1033aaea2 186 pc.printf("%c",_2Dplan[a]);
leonardene 8:63f1033aaea2 187 bt.printf("%c",_2Dplan[a]);
leonardene 8:63f1033aaea2 188 }
leonardene 8:63f1033aaea2 189 printf("\n");
leonardene 8:63f1033aaea2 190 }
leonardene 5:7209741a08bc 191 }