![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
02/07/15
Dependencies: mbed
Calculate.cpp@62:9de96bcfdb4b, 2015-08-04 (annotated)
- Committer:
- aidanPJG
- Date:
- Tue Aug 04 14:37:54 2015 +0000
- Revision:
- 62:9de96bcfdb4b
- Parent:
- 61:e7f2a0e38360
- Child:
- 63:7d2bff227f65
dont know;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aidanPJG | 22:34bc2f2f5b09 | 1 | #include "mbed.h" |
aidanPJG | 22:34bc2f2f5b09 | 2 | #include <time.h> |
aidanPJG | 22:34bc2f2f5b09 | 3 | #include <string> |
aidanPJG | 22:34bc2f2f5b09 | 4 | #include <iostream> |
aidanPJG | 39:3527578e78dd | 5 | #include "Distance.h" |
aidanPJG | 31:bea1117f14fb | 6 | |
aidanPJG | 61:e7f2a0e38360 | 7 | void printArray(double array[20], double timesArray[20], int NoOfPins); //defining the methods |
aidanPJG | 61:e7f2a0e38360 | 8 | void data(int sensor_number, double time, double speed); |
aidanPJG | 61:e7f2a0e38360 | 9 | DigitalOut led4(LED4); |
aidanPJG | 38:da262dc4f90a | 10 | |
aidanPJG | 61:e7f2a0e38360 | 11 | //*********mbed pins********************************* |
aidanPJG | 61:e7f2a0e38360 | 12 | extern Serial pc; //defines the communication between MBed and pc |
aidanPJG | 61:e7f2a0e38360 | 13 | DigitalIn sensor[20] = {p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p24} ; //array of sensors. |
aidanPJG | 31:bea1117f14fb | 14 | |
aidanPJG | 61:e7f2a0e38360 | 15 | //********c++ variables******************************* |
aidanPJG | 56:bf08d9e50ccc | 16 | Timer t; |
aidanPJG | 62:9de96bcfdb4b | 17 | double timeDiff= 0; //time between 2 sensors - not used right now |
aidanPJG | 62:9de96bcfdb4b | 18 | double speed = 0; |
aidanPJG | 57:f94834892570 | 19 | double times[20]; //array the size of #pins |
aidanPJG | 61:e7f2a0e38360 | 20 | double speeds[20]; //array of speeds |
aidanPJG | 62:9de96bcfdb4b | 21 | double avgtime= 0;; |
aidanPJG | 62:9de96bcfdb4b | 22 | double avgspeed = 0; |
aidanPJG | 62:9de96bcfdb4b | 23 | double avgdistance = 0; |
aidanPJG | 52:d21fc7266b66 | 24 | |
aidanPJG | 52:d21fc7266b66 | 25 | |
aidanPJG | 53:10f4f519ba69 | 26 | int calculate(double distance, int NoOfPins) |
aidanPJG | 61:e7f2a0e38360 | 27 | { |
aidanPJG | 61:e7f2a0e38360 | 28 | int i = 0; //set the pin to 0, starting from the 0th ( first) pin |
aidanPJG | 61:e7f2a0e38360 | 29 | while( i < NoOfPins) //while we have not reached the last pin |
aidanPJG | 25:bab86a12e9ad | 30 | { |
aidanPJG | 61:e7f2a0e38360 | 31 | while(!sensor[i]) //while the sensor is not broken hold it. |
aidanPJG | 61:e7f2a0e38360 | 32 | { |
aidanPJG | 61:e7f2a0e38360 | 33 | if(sensor[i + 1]) //bit of error bypassing |
aidanPJG | 61:e7f2a0e38360 | 34 | { |
aidanPJG | 61:e7f2a0e38360 | 35 | //printf("it skipped sensor % d", i); |
aidanPJG | 61:e7f2a0e38360 | 36 | led4 = 1; //alerts user that a sensor has been skipped. |
aidanPJG | 61:e7f2a0e38360 | 37 | i++; |
aidanPJG | 61:e7f2a0e38360 | 38 | |
aidanPJG | 61:e7f2a0e38360 | 39 | } |
aidanPJG | 61:e7f2a0e38360 | 40 | } |
aidanPJG | 62:9de96bcfdb4b | 41 | t.start(); //starts the timer once, doesn't do anything next iteration |
aidanPJG | 62:9de96bcfdb4b | 42 | timeDiff = t.read(); //reads what the timer is currently at |
aidanPJG | 62:9de96bcfdb4b | 43 | t.reset(); //starts the timer again from 0. |
aidanPJG | 62:9de96bcfdb4b | 44 | times[i] = timeDiff; //adds sensor times to array for logging. |
aidanPJG | 62:9de96bcfdb4b | 45 | |
aidanPJG | 61:e7f2a0e38360 | 46 | pc.printf(" timediff: %lf\n", timeDiff); //testing |
aidanPJG | 61:e7f2a0e38360 | 47 | |
aidanPJG | 61:e7f2a0e38360 | 48 | //to ensure it is not the first one |
aidanPJG | 61:e7f2a0e38360 | 49 | speed = 60* (distance / timeDiff); //multiplied by 60 to get meters per minute |
aidanPJG | 61:e7f2a0e38360 | 50 | avgdistance = distance * i; //total distance = distance between each sensor multiplied by number of sensors |
aidanPJG | 61:e7f2a0e38360 | 51 | // pc.printf("%f dist/s \n", avgdistance); //testing |
aidanPJG | 61:e7f2a0e38360 | 52 | |
aidanPJG | 61:e7f2a0e38360 | 53 | avgtime = times[1] + times[2] + times[3] + times[4] + times[5] + times[6] + times[7] + times[8] + times[9] +times[10] + times[11] + times[12]+times[13] + times[14] + times[15]+times[16] + times[17] + times[18]+ times[19] ; //total time sum of all times |
aidanPJG | 61:e7f2a0e38360 | 54 | //gets the total time it has taken. Could start another timer and only read it. |
aidanPJG | 61:e7f2a0e38360 | 55 | pc.printf("avgtime: %f \n", avgtime); //testing |
aidanPJG | 61:e7f2a0e38360 | 56 | avgspeed = 60*(avgdistance / avgtime); //multiplied by 60 to get meters per minute |
aidanPJG | 61:e7f2a0e38360 | 57 | |
aidanPJG | 61:e7f2a0e38360 | 58 | pc.printf("%f\n", speed); //inst speed printed |
aidanPJG | 61:e7f2a0e38360 | 59 | pc.printf("%f\n", avgspeed); //avg speed printed |
aidanPJG | 61:e7f2a0e38360 | 60 | pc.printf("%d \n",i+1); //which sensor it is on printed |
aidanPJG | 61:e7f2a0e38360 | 61 | speeds[i] = speed; //log speeds. |
aidanPJG | 61:e7f2a0e38360 | 62 | i++; //move onto next sensor |
aidanPJG | 30:ab3a436e8968 | 63 | } |
aidanPJG | 61:e7f2a0e38360 | 64 | printArray(speeds,times,NoOfPins); |
aidanPJG | 22:34bc2f2f5b09 | 65 | } |
aidanPJG | 30:ab3a436e8968 | 66 | |
aidanPJG | 61:e7f2a0e38360 | 67 | void printArray(double array[20], double timesArray[20], int NoOfPins) //for printing arrays to the terminal |
aidanPJG | 38:da262dc4f90a | 68 | { |
aidanPJG | 61:e7f2a0e38360 | 69 | /*prints to the terminal |
aidanPJG | 52:d21fc7266b66 | 70 | for (int i = 0; i < NoOfPins ; i++) //printing module for array |
aidanPJG | 38:da262dc4f90a | 71 | { |
aidanPJG | 57:f94834892570 | 72 | pc.printf(" Sensor : %d Time %lf Speed %f \n", i,times[i],speeds[i]); |
aidanPJG | 50:d794595c6868 | 73 | // pc.printf(" %d : %d \t", i,times[i]); |
aidanPJG | 61:e7f2a0e38360 | 74 | } */ |
aidanPJG | 52:d21fc7266b66 | 75 | for (int i = 0; i < NoOfPins ; i++) //printing to Datalog |
aidanPJG | 44:3b45ec49bc44 | 76 | { |
aidanPJG | 51:210353276e06 | 77 | data( i,times[i],speeds[i]); |
aidanPJG | 44:3b45ec49bc44 | 78 | } |
aidanPJG | 38:da262dc4f90a | 79 | |
aidanPJG | 38:da262dc4f90a | 80 | } |