![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
02/07/15
Dependencies: mbed
Calculate.cpp@68:c6399471ea49, 2015-08-26 (annotated)
- Committer:
- aidanPJG
- Date:
- Wed Aug 26 16:08:09 2015 +0000
- Revision:
- 68:c6399471ea49
- Parent:
- 64:809bd19f4e7c
- Child:
- 69:74bffa1d3f7f
working with multiple iteration on the same side. Keeps going without user interaction
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 | 68:c6399471ea49 | 6 | #include "Data.h" |
aidanPJG | 31:bea1117f14fb | 7 | |
aidanPJG | 64:809bd19f4e7c | 8 | //*************defining methods used in this method |
aidanPJG | 64:809bd19f4e7c | 9 | void printArray(double array[20], double timesArray[20], int NoOfPins,double speedAvg[20], double timesTotal[20]); |
aidanPJG | 64:809bd19f4e7c | 10 | void data(int sensor_number, double time, double speed, double timesTotal, double speedAvg); |
aidanPJG | 68:c6399471ea49 | 11 | void reset(int distance, int NoOfPins); |
aidanPJG | 64:809bd19f4e7c | 12 | |
aidanPJG | 61:e7f2a0e38360 | 13 | //*********mbed pins********************************* |
aidanPJG | 61:e7f2a0e38360 | 14 | extern Serial pc; //defines the communication between MBed and pc |
aidanPJG | 61:e7f2a0e38360 | 15 | 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 | 64:809bd19f4e7c | 16 | DigitalOut led4(LED4); |
aidanPJG | 31:bea1117f14fb | 17 | |
aidanPJG | 64:809bd19f4e7c | 18 | //********c++ variables******************************* |
aidanPJG | 64:809bd19f4e7c | 19 | |
aidanPJG | 56:bf08d9e50ccc | 20 | Timer t; |
aidanPJG | 64:809bd19f4e7c | 21 | double timeDiff= 0; //time between 2 sensors |
aidanPJG | 64:809bd19f4e7c | 22 | double speed = 0; //instspeed |
aidanPJG | 64:809bd19f4e7c | 23 | double avgtime= 0;; |
aidanPJG | 62:9de96bcfdb4b | 24 | double avgspeed = 0; |
aidanPJG | 62:9de96bcfdb4b | 25 | double avgdistance = 0; |
aidanPJG | 68:c6399471ea49 | 26 | int bendCounter = 0; |
aidanPJG | 64:809bd19f4e7c | 27 | //arrays |
aidanPJG | 64:809bd19f4e7c | 28 | double times[20]; //array of the time the sensor is reached .(the size of #pins) |
aidanPJG | 64:809bd19f4e7c | 29 | double speeds[20]; //array of inst speeds |
aidanPJG | 64:809bd19f4e7c | 30 | double avgSpeeds[20]; //avg speeds |
aidanPJG | 64:809bd19f4e7c | 31 | double totalTimes[20]; //total runnning times |
aidanPJG | 64:809bd19f4e7c | 32 | |
aidanPJG | 52:d21fc7266b66 | 33 | |
aidanPJG | 53:10f4f519ba69 | 34 | int calculate(double distance, int NoOfPins) |
aidanPJG | 61:e7f2a0e38360 | 35 | { |
aidanPJG | 68:c6399471ea49 | 36 | while(true){ |
aidanPJG | 68:c6399471ea49 | 37 | |
aidanPJG | 61:e7f2a0e38360 | 38 | int i = 0; //set the pin to 0, starting from the 0th ( first) pin |
aidanPJG | 61:e7f2a0e38360 | 39 | while( i < NoOfPins) //while we have not reached the last pin |
aidanPJG | 25:bab86a12e9ad | 40 | { |
aidanPJG | 63:7d2bff227f65 | 41 | while(!sensor[i]) //while the sensor is not broken hold it. |
aidanPJG | 63:7d2bff227f65 | 42 | { |
aidanPJG | 63:7d2bff227f65 | 43 | if(sensor[i + 1]) //bit of error bypassing |
aidanPJG | 63:7d2bff227f65 | 44 | { |
aidanPJG | 63:7d2bff227f65 | 45 | //printf("it skipped sensor % d", i); |
aidanPJG | 63:7d2bff227f65 | 46 | led4 = 1; //alerts user that a sensor has been skipped. |
aidanPJG | 63:7d2bff227f65 | 47 | i++; |
aidanPJG | 63:7d2bff227f65 | 48 | |
aidanPJG | 63:7d2bff227f65 | 49 | } |
aidanPJG | 63:7d2bff227f65 | 50 | } |
aidanPJG | 68:c6399471ea49 | 51 | t.start(); |
aidanPJG | 68:c6399471ea49 | 52 | //starts the timer once, doesn't do anything next iteration |
aidanPJG | 63:7d2bff227f65 | 53 | timeDiff = t.read(); //reads what the timer is currently at |
aidanPJG | 63:7d2bff227f65 | 54 | t.reset(); //starts the timer again from 0. |
aidanPJG | 64:809bd19f4e7c | 55 | times[i] = timeDiff; //adds sensor times to array for logging. this must remain above the calculations for avgdistance to work. |
aidanPJG | 63:7d2bff227f65 | 56 | |
aidanPJG | 63:7d2bff227f65 | 57 | 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 | 58 | { |
aidanPJG | 63:7d2bff227f65 | 59 | printf("0\n"); |
aidanPJG | 63:7d2bff227f65 | 60 | printf("0\n"); |
aidanPJG | 63:7d2bff227f65 | 61 | printf("0\n"); |
aidanPJG | 63:7d2bff227f65 | 62 | printf("0\n"); |
aidanPJG | 63:7d2bff227f65 | 63 | printf("1\n"); |
aidanPJG | 63:7d2bff227f65 | 64 | i++; |
aidanPJG | 63:7d2bff227f65 | 65 | } |
aidanPJG | 63:7d2bff227f65 | 66 | else |
aidanPJG | 63:7d2bff227f65 | 67 | { |
aidanPJG | 61:e7f2a0e38360 | 68 | |
aidanPJG | 63:7d2bff227f65 | 69 | pc.printf(" timediff: %lf\n", timeDiff); //testing |
aidanPJG | 63:7d2bff227f65 | 70 | |
aidanPJG | 63:7d2bff227f65 | 71 | //to ensure it is not the first one |
aidanPJG | 63:7d2bff227f65 | 72 | speed = 60* (distance / timeDiff); //multiplied by 60 to get meters per minute |
aidanPJG | 63:7d2bff227f65 | 73 | //*******average speed |
aidanPJG | 63:7d2bff227f65 | 74 | avgdistance = distance * i; //total distance = distance between each sensor multiplied by number of sensors |
aidanPJG | 63:7d2bff227f65 | 75 | // pc.printf("%f dist/s \n", avgdistance); //testing |
aidanPJG | 63:7d2bff227f65 | 76 | 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 | 77 | //gets the total time it has taken. Could start another timer and only read it. |
aidanPJG | 63:7d2bff227f65 | 78 | pc.printf("avgtime: %f \n", avgtime); //testing |
aidanPJG | 63:7d2bff227f65 | 79 | avgspeed = 60*(avgdistance / avgtime); //multiplied by 60 to get meters per minute |
aidanPJG | 63:7d2bff227f65 | 80 | |
aidanPJG | 63:7d2bff227f65 | 81 | //******send to program. |
aidanPJG | 63:7d2bff227f65 | 82 | pc.printf("%f\n", speed); //inst speed printed |
aidanPJG | 63:7d2bff227f65 | 83 | pc.printf("%f\n", avgspeed); //avg speed printed |
aidanPJG | 63:7d2bff227f65 | 84 | pc.printf("%d \n",i+1); //which sensor it is on printed |
aidanPJG | 63:7d2bff227f65 | 85 | //*********add to array |
aidanPJG | 63:7d2bff227f65 | 86 | speeds[i] = speed; //log speeds. |
aidanPJG | 63:7d2bff227f65 | 87 | avgSpeeds[i] = avgspeed; |
aidanPJG | 63:7d2bff227f65 | 88 | totalTimes[i] = avgtime; |
aidanPJG | 63:7d2bff227f65 | 89 | i++; //move onto next sensor |
aidanPJG | 63:7d2bff227f65 | 90 | } |
aidanPJG | 63:7d2bff227f65 | 91 | } |
aidanPJG | 68:c6399471ea49 | 92 | |
aidanPJG | 64:809bd19f4e7c | 93 | printArray(speeds,times,NoOfPins,avgSpeeds,totalTimes); |
aidanPJG | 68:c6399471ea49 | 94 | reset(distance, NoOfPins); |
aidanPJG | 68:c6399471ea49 | 95 | } |
aidanPJG | 68:c6399471ea49 | 96 | } |
aidanPJG | 68:c6399471ea49 | 97 | void reset(int distance, int NoOfPins) |
aidanPJG | 68:c6399471ea49 | 98 | { |
aidanPJG | 68:c6399471ea49 | 99 | while ( sensor[0]){ |
aidanPJG | 68:c6399471ea49 | 100 | |
aidanPJG | 68:c6399471ea49 | 101 | } |
aidanPJG | 68:c6399471ea49 | 102 | wait_ms(50); |
aidanPJG | 68:c6399471ea49 | 103 | bendCounter++; |
aidanPJG | 68:c6399471ea49 | 104 | //resets |
aidanPJG | 68:c6399471ea49 | 105 | std::fill_n(times, 20 , 0); //empty array of times so averaging works |
aidanPJG | 68:c6399471ea49 | 106 | std::fill_n(speeds, 20 , 0); |
aidanPJG | 68:c6399471ea49 | 107 | std::fill_n(avgSpeeds, 20 , 0); |
aidanPJG | 68:c6399471ea49 | 108 | std::fill_n(totalTimes, 20 , 0); |
aidanPJG | 68:c6399471ea49 | 109 | t.reset(); //clock reset |
aidanPJG | 68:c6399471ea49 | 110 | calculate(distance, NoOfPins); |
aidanPJG | 22:34bc2f2f5b09 | 111 | } |
aidanPJG | 30:ab3a436e8968 | 112 | |
aidanPJG | 64:809bd19f4e7c | 113 | void printArray(double array[20], double timesArray[20], int NoOfPins, double speedAvg[20], double timesTotal[20]) //for printing arrays to the terminal |
aidanPJG | 38:da262dc4f90a | 114 | { |
aidanPJG | 61:e7f2a0e38360 | 115 | /*prints to the terminal |
aidanPJG | 52:d21fc7266b66 | 116 | for (int i = 0; i < NoOfPins ; i++) //printing module for array |
aidanPJG | 38:da262dc4f90a | 117 | { |
aidanPJG | 57:f94834892570 | 118 | pc.printf(" Sensor : %d Time %lf Speed %f \n", i,times[i],speeds[i]); |
aidanPJG | 50:d794595c6868 | 119 | // pc.printf(" %d : %d \t", i,times[i]); |
aidanPJG | 61:e7f2a0e38360 | 120 | } */ |
aidanPJG | 68:c6399471ea49 | 121 | initialise(bendCounter); //used to create the Datalog file should happen once for every bend test |
aidanPJG | 52:d21fc7266b66 | 122 | for (int i = 0; i < NoOfPins ; i++) //printing to Datalog |
aidanPJG | 44:3b45ec49bc44 | 123 | { |
aidanPJG | 64:809bd19f4e7c | 124 | data( i,times[i],speeds[i],timesTotal[i],speedAvg[i]); |
aidanPJG | 44:3b45ec49bc44 | 125 | } |
aidanPJG | 38:da262dc4f90a | 126 | } |