![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
02/07/15
Dependencies: mbed
Calculate.cpp@63:7d2bff227f65, 2015-08-05 (annotated)
- Committer:
- aidanPJG
- Date:
- Wed Aug 05 08:13:19 2015 +0000
- Revision:
- 63:7d2bff227f65
- Parent:
- 62:9de96bcfdb4b
- Child:
- 64:809bd19f4e7c
Before trying to add more data to print to excel
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 | 63:7d2bff227f65 | 21 | double avgSpeeds[20]; |
aidanPJG | 63:7d2bff227f65 | 22 | double totalTimes[20]; |
aidanPJG | 62:9de96bcfdb4b | 23 | double avgtime= 0;; |
aidanPJG | 62:9de96bcfdb4b | 24 | double avgspeed = 0; |
aidanPJG | 62:9de96bcfdb4b | 25 | double avgdistance = 0; |
aidanPJG | 52:d21fc7266b66 | 26 | |
aidanPJG | 52:d21fc7266b66 | 27 | |
aidanPJG | 53:10f4f519ba69 | 28 | int calculate(double distance, int NoOfPins) |
aidanPJG | 61:e7f2a0e38360 | 29 | { |
aidanPJG | 61:e7f2a0e38360 | 30 | int i = 0; //set the pin to 0, starting from the 0th ( first) pin |
aidanPJG | 61:e7f2a0e38360 | 31 | while( i < NoOfPins) //while we have not reached the last pin |
aidanPJG | 25:bab86a12e9ad | 32 | { |
aidanPJG | 63:7d2bff227f65 | 33 | while(!sensor[i]) //while the sensor is not broken hold it. |
aidanPJG | 63:7d2bff227f65 | 34 | { |
aidanPJG | 63:7d2bff227f65 | 35 | if(sensor[i + 1]) //bit of error bypassing |
aidanPJG | 63:7d2bff227f65 | 36 | { |
aidanPJG | 63:7d2bff227f65 | 37 | //printf("it skipped sensor % d", i); |
aidanPJG | 63:7d2bff227f65 | 38 | led4 = 1; //alerts user that a sensor has been skipped. |
aidanPJG | 63:7d2bff227f65 | 39 | i++; |
aidanPJG | 63:7d2bff227f65 | 40 | |
aidanPJG | 63:7d2bff227f65 | 41 | } |
aidanPJG | 63:7d2bff227f65 | 42 | } |
aidanPJG | 63:7d2bff227f65 | 43 | t.start(); //starts the timer once, doesn't do anything next iteration |
aidanPJG | 63:7d2bff227f65 | 44 | timeDiff = t.read(); //reads what the timer is currently at |
aidanPJG | 63:7d2bff227f65 | 45 | t.reset(); //starts the timer again from 0. |
aidanPJG | 63:7d2bff227f65 | 46 | times[i] = timeDiff; //adds sensor times to array for logging. |
aidanPJG | 63:7d2bff227f65 | 47 | |
aidanPJG | 63:7d2bff227f65 | 48 | if( i <1 ) //for the first one so it prints out 0 instead of getting inf from dividing by 0 and getting nan symbols. |
aidanPJG | 63:7d2bff227f65 | 49 | { |
aidanPJG | 63:7d2bff227f65 | 50 | printf("0\n"); |
aidanPJG | 63:7d2bff227f65 | 51 | printf("0\n"); |
aidanPJG | 63:7d2bff227f65 | 52 | printf("0\n"); |
aidanPJG | 63:7d2bff227f65 | 53 | printf("0\n"); |
aidanPJG | 63:7d2bff227f65 | 54 | printf("1\n"); |
aidanPJG | 63:7d2bff227f65 | 55 | i++; |
aidanPJG | 63:7d2bff227f65 | 56 | } |
aidanPJG | 63:7d2bff227f65 | 57 | else |
aidanPJG | 63:7d2bff227f65 | 58 | { |
aidanPJG | 61:e7f2a0e38360 | 59 | |
aidanPJG | 63:7d2bff227f65 | 60 | pc.printf(" timediff: %lf\n", timeDiff); //testing |
aidanPJG | 63:7d2bff227f65 | 61 | |
aidanPJG | 63:7d2bff227f65 | 62 | //to ensure it is not the first one |
aidanPJG | 63:7d2bff227f65 | 63 | speed = 60* (distance / timeDiff); //multiplied by 60 to get meters per minute |
aidanPJG | 63:7d2bff227f65 | 64 | //*******average speed |
aidanPJG | 63:7d2bff227f65 | 65 | avgdistance = distance * i; //total distance = distance between each sensor multiplied by number of sensors |
aidanPJG | 63:7d2bff227f65 | 66 | // pc.printf("%f dist/s \n", avgdistance); //testing |
aidanPJG | 63:7d2bff227f65 | 67 | 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 | 63:7d2bff227f65 | 68 | //gets the total time it has taken. Could start another timer and only read it. |
aidanPJG | 63:7d2bff227f65 | 69 | pc.printf("avgtime: %f \n", avgtime); //testing |
aidanPJG | 63:7d2bff227f65 | 70 | avgspeed = 60*(avgdistance / avgtime); //multiplied by 60 to get meters per minute |
aidanPJG | 63:7d2bff227f65 | 71 | |
aidanPJG | 63:7d2bff227f65 | 72 | //******send to program. |
aidanPJG | 63:7d2bff227f65 | 73 | pc.printf("%f\n", speed); //inst speed printed |
aidanPJG | 63:7d2bff227f65 | 74 | pc.printf("%f\n", avgspeed); //avg speed printed |
aidanPJG | 63:7d2bff227f65 | 75 | pc.printf("%d \n",i+1); //which sensor it is on printed |
aidanPJG | 63:7d2bff227f65 | 76 | //*********add to array |
aidanPJG | 63:7d2bff227f65 | 77 | speeds[i] = speed; //log speeds. |
aidanPJG | 63:7d2bff227f65 | 78 | avgSpeeds[i] = avgspeed; |
aidanPJG | 63:7d2bff227f65 | 79 | totalTimes[i] = avgtime; |
aidanPJG | 63:7d2bff227f65 | 80 | i++; //move onto next sensor |
aidanPJG | 63:7d2bff227f65 | 81 | } |
aidanPJG | 63:7d2bff227f65 | 82 | } |
aidanPJG | 63:7d2bff227f65 | 83 | printArray(speeds,times,NoOfPins); |
aidanPJG | 22:34bc2f2f5b09 | 84 | } |
aidanPJG | 30:ab3a436e8968 | 85 | |
aidanPJG | 61:e7f2a0e38360 | 86 | void printArray(double array[20], double timesArray[20], int NoOfPins) //for printing arrays to the terminal |
aidanPJG | 38:da262dc4f90a | 87 | { |
aidanPJG | 61:e7f2a0e38360 | 88 | /*prints to the terminal |
aidanPJG | 52:d21fc7266b66 | 89 | for (int i = 0; i < NoOfPins ; i++) //printing module for array |
aidanPJG | 38:da262dc4f90a | 90 | { |
aidanPJG | 57:f94834892570 | 91 | pc.printf(" Sensor : %d Time %lf Speed %f \n", i,times[i],speeds[i]); |
aidanPJG | 50:d794595c6868 | 92 | // pc.printf(" %d : %d \t", i,times[i]); |
aidanPJG | 61:e7f2a0e38360 | 93 | } */ |
aidanPJG | 52:d21fc7266b66 | 94 | for (int i = 0; i < NoOfPins ; i++) //printing to Datalog |
aidanPJG | 44:3b45ec49bc44 | 95 | { |
aidanPJG | 51:210353276e06 | 96 | data( i,times[i],speeds[i]); |
aidanPJG | 44:3b45ec49bc44 | 97 | } |
aidanPJG | 38:da262dc4f90a | 98 | } |