02/07/15

Dependencies:   mbed

Dependents:   TabbedGUIMbed

Committer:
aidanPJG
Date:
Tue Aug 04 10:24:39 2015 +0000
Revision:
61:e7f2a0e38360
Parent:
60:27404fc0dc71
Child:
62:9de96bcfdb4b
average speed (total time) working now

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 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 56:bf08d9e50ccc 17 double timeDiff; //time between 2 sensors - not used right now
aidanPJG 57:f94834892570 18 double speed;
aidanPJG 57:f94834892570 19 double times[20]; //array the size of #pins
aidanPJG 61:e7f2a0e38360 20 double speeds[20]; //array of speeds
aidanPJG 61:e7f2a0e38360 21 double avgtime;
aidanPJG 61:e7f2a0e38360 22 double avgspeed;
aidanPJG 61:e7f2a0e38360 23 double avgdistance;
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 60:27404fc0dc71 41 t.start(); //starts the timer once, doesn't do anything next iteration
aidanPJG 57:f94834892570 42 timeDiff = t.read(); //reads what the timer is currently at
aidanPJG 57:f94834892570 43 t.reset(); //starts the timer again from 0.
aidanPJG 57:f94834892570 44 times[i] = timeDiff; //adds sensor times to array for logging.
aidanPJG 61:e7f2a0e38360 45
aidanPJG 61:e7f2a0e38360 46 pc.printf(" timediff: %lf\n", timeDiff); //testing
aidanPJG 61:e7f2a0e38360 47
aidanPJG 61:e7f2a0e38360 48
aidanPJG 61:e7f2a0e38360 49 //to ensure it is not the first one
aidanPJG 61:e7f2a0e38360 50 speed = 60* (distance / timeDiff); //multiplied by 60 to get meters per minute
aidanPJG 61:e7f2a0e38360 51 avgdistance = distance * i; //total distance = distance between each sensor multiplied by number of sensors
aidanPJG 61:e7f2a0e38360 52 // pc.printf("%f dist/s \n", avgdistance); //testing
aidanPJG 61:e7f2a0e38360 53
aidanPJG 61:e7f2a0e38360 54 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 55 //gets the total time it has taken. Could start another timer and only read it.
aidanPJG 61:e7f2a0e38360 56 pc.printf("avgtime: %f \n", avgtime); //testing
aidanPJG 61:e7f2a0e38360 57 avgspeed = 60*(avgdistance / avgtime); //multiplied by 60 to get meters per minute
aidanPJG 61:e7f2a0e38360 58
aidanPJG 61:e7f2a0e38360 59 pc.printf("%f\n", speed); //inst speed printed
aidanPJG 61:e7f2a0e38360 60 pc.printf("%f\n", avgspeed); //avg speed printed
aidanPJG 61:e7f2a0e38360 61 pc.printf("%d \n",i+1); //which sensor it is on printed
aidanPJG 61:e7f2a0e38360 62 speeds[i] = speed; //log speeds.
aidanPJG 61:e7f2a0e38360 63 i++; //move onto next sensor
aidanPJG 30:ab3a436e8968 64 }
aidanPJG 61:e7f2a0e38360 65 printArray(speeds,times,NoOfPins);
aidanPJG 22:34bc2f2f5b09 66 }
aidanPJG 30:ab3a436e8968 67
aidanPJG 61:e7f2a0e38360 68 void printArray(double array[20], double timesArray[20], int NoOfPins) //for printing arrays to the terminal
aidanPJG 38:da262dc4f90a 69 {
aidanPJG 61:e7f2a0e38360 70 /*prints to the terminal
aidanPJG 52:d21fc7266b66 71 for (int i = 0; i < NoOfPins ; i++) //printing module for array
aidanPJG 38:da262dc4f90a 72 {
aidanPJG 57:f94834892570 73 pc.printf(" Sensor : %d Time %lf Speed %f \n", i,times[i],speeds[i]);
aidanPJG 50:d794595c6868 74 // pc.printf(" %d : %d \t", i,times[i]);
aidanPJG 61:e7f2a0e38360 75 } */
aidanPJG 52:d21fc7266b66 76 for (int i = 0; i < NoOfPins ; i++) //printing to Datalog
aidanPJG 44:3b45ec49bc44 77 {
aidanPJG 51:210353276e06 78 data( i,times[i],speeds[i]);
aidanPJG 44:3b45ec49bc44 79 }
aidanPJG 38:da262dc4f90a 80
aidanPJG 38:da262dc4f90a 81 }