02/07/15

Dependencies:   mbed

Dependents:   TabbedGUIMbed

Committer:
aidanPJG
Date:
Wed Aug 05 08:44:12 2015 +0000
Revision:
64:809bd19f4e7c
Parent:
63:7d2bff227f65
Child:
68:c6399471ea49
added avgtime array and avgspeed array to xl

Who changed what in which revision?

UserRevisionLine numberNew 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 64:809bd19f4e7c 7 //*************defining methods used in this method
aidanPJG 64:809bd19f4e7c 8 void printArray(double array[20], double timesArray[20], int NoOfPins,double speedAvg[20], double timesTotal[20]);
aidanPJG 64:809bd19f4e7c 9 void data(int sensor_number, double time, double speed, double timesTotal, double speedAvg);
aidanPJG 64:809bd19f4e7c 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 64:809bd19f4e7c 14 DigitalOut led4(LED4);
aidanPJG 31:bea1117f14fb 15
aidanPJG 64:809bd19f4e7c 16 //********c++ variables*******************************
aidanPJG 64:809bd19f4e7c 17
aidanPJG 56:bf08d9e50ccc 18 Timer t;
aidanPJG 64:809bd19f4e7c 19 double timeDiff= 0; //time between 2 sensors
aidanPJG 64:809bd19f4e7c 20 double speed = 0; //instspeed
aidanPJG 64:809bd19f4e7c 21 double avgtime= 0;;
aidanPJG 62:9de96bcfdb4b 22 double avgspeed = 0;
aidanPJG 62:9de96bcfdb4b 23 double avgdistance = 0;
aidanPJG 64:809bd19f4e7c 24 //arrays
aidanPJG 64:809bd19f4e7c 25 double times[20]; //array of the time the sensor is reached .(the size of #pins)
aidanPJG 64:809bd19f4e7c 26 double speeds[20]; //array of inst speeds
aidanPJG 64:809bd19f4e7c 27 double avgSpeeds[20]; //avg speeds
aidanPJG 64:809bd19f4e7c 28 double totalTimes[20]; //total runnning times
aidanPJG 64:809bd19f4e7c 29
aidanPJG 52:d21fc7266b66 30
aidanPJG 52:d21fc7266b66 31
aidanPJG 53:10f4f519ba69 32 int calculate(double distance, int NoOfPins)
aidanPJG 61:e7f2a0e38360 33 {
aidanPJG 61:e7f2a0e38360 34 int i = 0; //set the pin to 0, starting from the 0th ( first) pin
aidanPJG 61:e7f2a0e38360 35 while( i < NoOfPins) //while we have not reached the last pin
aidanPJG 25:bab86a12e9ad 36 {
aidanPJG 63:7d2bff227f65 37 while(!sensor[i]) //while the sensor is not broken hold it.
aidanPJG 63:7d2bff227f65 38 {
aidanPJG 63:7d2bff227f65 39 if(sensor[i + 1]) //bit of error bypassing
aidanPJG 63:7d2bff227f65 40 {
aidanPJG 63:7d2bff227f65 41 //printf("it skipped sensor % d", i);
aidanPJG 63:7d2bff227f65 42 led4 = 1; //alerts user that a sensor has been skipped.
aidanPJG 63:7d2bff227f65 43 i++;
aidanPJG 63:7d2bff227f65 44
aidanPJG 63:7d2bff227f65 45 }
aidanPJG 63:7d2bff227f65 46 }
aidanPJG 63:7d2bff227f65 47 t.start(); //starts the timer once, doesn't do anything next iteration
aidanPJG 63:7d2bff227f65 48 timeDiff = t.read(); //reads what the timer is currently at
aidanPJG 63:7d2bff227f65 49 t.reset(); //starts the timer again from 0.
aidanPJG 64:809bd19f4e7c 50 times[i] = timeDiff; //adds sensor times to array for logging. this must remain above the calculations for avgdistance to work.
aidanPJG 63:7d2bff227f65 51
aidanPJG 63:7d2bff227f65 52 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 53 {
aidanPJG 63:7d2bff227f65 54 printf("0\n");
aidanPJG 63:7d2bff227f65 55 printf("0\n");
aidanPJG 63:7d2bff227f65 56 printf("0\n");
aidanPJG 63:7d2bff227f65 57 printf("0\n");
aidanPJG 63:7d2bff227f65 58 printf("1\n");
aidanPJG 63:7d2bff227f65 59 i++;
aidanPJG 63:7d2bff227f65 60 }
aidanPJG 63:7d2bff227f65 61 else
aidanPJG 63:7d2bff227f65 62 {
aidanPJG 61:e7f2a0e38360 63
aidanPJG 63:7d2bff227f65 64 pc.printf(" timediff: %lf\n", timeDiff); //testing
aidanPJG 63:7d2bff227f65 65
aidanPJG 63:7d2bff227f65 66 //to ensure it is not the first one
aidanPJG 63:7d2bff227f65 67 speed = 60* (distance / timeDiff); //multiplied by 60 to get meters per minute
aidanPJG 63:7d2bff227f65 68 //*******average speed
aidanPJG 63:7d2bff227f65 69 avgdistance = distance * i; //total distance = distance between each sensor multiplied by number of sensors
aidanPJG 63:7d2bff227f65 70 // pc.printf("%f dist/s \n", avgdistance); //testing
aidanPJG 63:7d2bff227f65 71 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 72 //gets the total time it has taken. Could start another timer and only read it.
aidanPJG 63:7d2bff227f65 73 pc.printf("avgtime: %f \n", avgtime); //testing
aidanPJG 63:7d2bff227f65 74 avgspeed = 60*(avgdistance / avgtime); //multiplied by 60 to get meters per minute
aidanPJG 63:7d2bff227f65 75
aidanPJG 63:7d2bff227f65 76 //******send to program.
aidanPJG 63:7d2bff227f65 77 pc.printf("%f\n", speed); //inst speed printed
aidanPJG 63:7d2bff227f65 78 pc.printf("%f\n", avgspeed); //avg speed printed
aidanPJG 63:7d2bff227f65 79 pc.printf("%d \n",i+1); //which sensor it is on printed
aidanPJG 63:7d2bff227f65 80 //*********add to array
aidanPJG 63:7d2bff227f65 81 speeds[i] = speed; //log speeds.
aidanPJG 63:7d2bff227f65 82 avgSpeeds[i] = avgspeed;
aidanPJG 63:7d2bff227f65 83 totalTimes[i] = avgtime;
aidanPJG 63:7d2bff227f65 84 i++; //move onto next sensor
aidanPJG 63:7d2bff227f65 85 }
aidanPJG 63:7d2bff227f65 86 }
aidanPJG 64:809bd19f4e7c 87 printArray(speeds,times,NoOfPins,avgSpeeds,totalTimes);
aidanPJG 22:34bc2f2f5b09 88 }
aidanPJG 30:ab3a436e8968 89
aidanPJG 64:809bd19f4e7c 90 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 91 {
aidanPJG 61:e7f2a0e38360 92 /*prints to the terminal
aidanPJG 52:d21fc7266b66 93 for (int i = 0; i < NoOfPins ; i++) //printing module for array
aidanPJG 38:da262dc4f90a 94 {
aidanPJG 57:f94834892570 95 pc.printf(" Sensor : %d Time %lf Speed %f \n", i,times[i],speeds[i]);
aidanPJG 50:d794595c6868 96 // pc.printf(" %d : %d \t", i,times[i]);
aidanPJG 61:e7f2a0e38360 97 } */
aidanPJG 52:d21fc7266b66 98 for (int i = 0; i < NoOfPins ; i++) //printing to Datalog
aidanPJG 44:3b45ec49bc44 99 {
aidanPJG 64:809bd19f4e7c 100 data( i,times[i],speeds[i],timesTotal[i],speedAvg[i]);
aidanPJG 44:3b45ec49bc44 101 }
aidanPJG 38:da262dc4f90a 102 }