02/07/15

Dependencies:   mbed

Dependents:   TabbedGUIMbed

Committer:
aidanPJG
Date:
Thu Sep 03 15:32:01 2015 +0000
Revision:
71:60f06e7e50a3
Parent:
70:168d67695a65
tried to add date but it failed;

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 68:c6399471ea49 5 #include "Data.h"
aidanPJG 31:bea1117f14fb 6
aidanPJG 64:809bd19f4e7c 7 //*************defining methods used in this method
aidanPJG 71:60f06e7e50a3 8 void printArray(double array[20], double timesArray[20], int NoOfPins,double speedAvg[20], double timesTotal[20], string date);
aidanPJG 64:809bd19f4e7c 9 void data(int sensor_number, double time, double speed, double timesTotal, double speedAvg);
aidanPJG 71:60f06e7e50a3 10 void reset(int distance, int NoOfPins, string date);
aidanPJG 64:809bd19f4e7c 11
aidanPJG 61:e7f2a0e38360 12 //*********mbed pins*********************************
aidanPJG 61:e7f2a0e38360 13 extern Serial pc; //defines the communication between MBed and pc
aidanPJG 61:e7f2a0e38360 14 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 15 DigitalOut led4(LED4);
aidanPJG 31:bea1117f14fb 16
aidanPJG 64:809bd19f4e7c 17 //********c++ variables*******************************
aidanPJG 64:809bd19f4e7c 18
aidanPJG 56:bf08d9e50ccc 19 Timer t;
aidanPJG 64:809bd19f4e7c 20 double timeDiff= 0; //time between 2 sensors
aidanPJG 64:809bd19f4e7c 21 double speed = 0; //instspeed
aidanPJG 64:809bd19f4e7c 22 double avgtime= 0;;
aidanPJG 62:9de96bcfdb4b 23 double avgspeed = 0;
aidanPJG 62:9de96bcfdb4b 24 double avgdistance = 0;
aidanPJG 68:c6399471ea49 25 int bendCounter = 0;
aidanPJG 64:809bd19f4e7c 26 //arrays
aidanPJG 64:809bd19f4e7c 27 double times[20]; //array of the time the sensor is reached .(the size of #pins)
aidanPJG 64:809bd19f4e7c 28 double speeds[20]; //array of inst speeds
aidanPJG 64:809bd19f4e7c 29 double avgSpeeds[20]; //avg speeds
aidanPJG 64:809bd19f4e7c 30 double totalTimes[20]; //total runnning times
aidanPJG 64:809bd19f4e7c 31
aidanPJG 52:d21fc7266b66 32
aidanPJG 71:60f06e7e50a3 33 int calculate(double distance, int NoOfPins, string date) //run when the pipe is being bent around the former.
aidanPJG 61:e7f2a0e38360 34 {
aidanPJG 70:168d67695a65 35 while(true) //keep doing this
aidanPJG 70:168d67695a65 36 {
aidanPJG 61:e7f2a0e38360 37 int i = 0; //set the pin to 0, starting from the 0th ( first) pin
aidanPJG 61:e7f2a0e38360 38 while( i < NoOfPins) //while we have not reached the last pin
aidanPJG 25:bab86a12e9ad 39 {
aidanPJG 63:7d2bff227f65 40 while(!sensor[i]) //while the sensor is not broken hold it.
aidanPJG 63:7d2bff227f65 41 {
aidanPJG 70:168d67695a65 42 if(sensor[i + 1]) //bit of error bypassing - if the next sensor is hit
aidanPJG 63:7d2bff227f65 43 {
aidanPJG 63:7d2bff227f65 44 //printf("it skipped sensor % d", i);
aidanPJG 63:7d2bff227f65 45 led4 = 1; //alerts user that a sensor has been skipped.
aidanPJG 70:168d67695a65 46 i++; //keep going on as normal
aidanPJG 63:7d2bff227f65 47
aidanPJG 63:7d2bff227f65 48 }
aidanPJG 63:7d2bff227f65 49 }
aidanPJG 70:168d67695a65 50 t.start(); //starts the timer once, doesn't do anything next iteration
aidanPJG 70:168d67695a65 51
aidanPJG 70:168d67695a65 52 timeDiff = t.read(); //reads what the timer is currently at. Getting the time between each sensor
aidanPJG 63:7d2bff227f65 53 t.reset(); //starts the timer again from 0.
aidanPJG 70:168d67695a65 54 times[i] = timeDiff; //adds sensor times to array for logging. This must remain above the calculations for avgdistance to work.
aidanPJG 63:7d2bff227f65 55
aidanPJG 70:168d67695a65 56 if( i <1 ) //for the first iteration so it prints out 0 instead of getting inf by dividing 0 and getting nan symbols.
aidanPJG 63:7d2bff227f65 57 {
aidanPJG 63:7d2bff227f65 58 printf("0\n");
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("1\n");
aidanPJG 63:7d2bff227f65 63 i++;
aidanPJG 63:7d2bff227f65 64 }
aidanPJG 70:168d67695a65 65 else //otherwise if it is not the first one do this
aidanPJG 63:7d2bff227f65 66 {
aidanPJG 70:168d67695a65 67 pc.printf(" timediff: %lf\n", timeDiff); //displays the timediff between each sensor at the top of the GUI
aidanPJG 63:7d2bff227f65 68
aidanPJG 70:168d67695a65 69 //*****************Inst Speed *************************
aidanPJG 70:168d67695a65 70 speed = 60* (distance / timeDiff); //instaneous speed between each sensor. Multiplied by 60 to get meters per minute
aidanPJG 70:168d67695a65 71 //*******average speed*******************************
aidanPJG 70:168d67695a65 72 avgdistance = distance * i; //total distance = distance between each sensor multiplied by number of sensors
aidanPJG 63:7d2bff227f65 73 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 70:168d67695a65 74 //gets the total time it has taken. Adds up all the inst speeds. (Could start another timer and read it without resets but I don't think there is a beneficial difference)
aidanPJG 70:168d67695a65 75 pc.printf("avgtime: %f \n", avgtime); //print the average time for the top bit for checking/testing
aidanPJG 63:7d2bff227f65 76 avgspeed = 60*(avgdistance / avgtime); //multiplied by 60 to get meters per minute
aidanPJG 63:7d2bff227f65 77
aidanPJG 70:168d67695a65 78 //******send to GUI****************************************
aidanPJG 63:7d2bff227f65 79 pc.printf("%f\n", speed); //inst speed printed
aidanPJG 63:7d2bff227f65 80 pc.printf("%f\n", avgspeed); //avg speed printed
aidanPJG 63:7d2bff227f65 81 pc.printf("%d \n",i+1); //which sensor it is on printed
aidanPJG 70:168d67695a65 82 //*********add to array*************************************
aidanPJG 70:168d67695a65 83 speeds[i] = speed; //save inst speeds.
aidanPJG 70:168d67695a65 84 avgSpeeds[i] = avgspeed; //save avg speeds
aidanPJG 70:168d67695a65 85 totalTimes[i] = avgtime; //save total times
aidanPJG 70:168d67695a65 86 i++; //move onto next sensor
aidanPJG 63:7d2bff227f65 87 }
aidanPJG 63:7d2bff227f65 88 }
aidanPJG 68:c6399471ea49 89
aidanPJG 71:60f06e7e50a3 90 printArray(speeds,times,NoOfPins,avgSpeeds,totalTimes, date); //write info to Mbed xl
aidanPJG 71:60f06e7e50a3 91 reset(distance, NoOfPins, date); //reset after all sensors on former have been used. Prepare for it to start from the start again.
aidanPJG 68:c6399471ea49 92 }
aidanPJG 68:c6399471ea49 93 }
aidanPJG 69:74bffa1d3f7f 94
aidanPJG 71:60f06e7e50a3 95 void reset(int distance, int NoOfPins, string date) //reset after all sensors on former have been used. Prepare for it to start from the start again.
aidanPJG 68:c6399471ea49 96 {
aidanPJG 68:c6399471ea49 97 while ( sensor[0]){
aidanPJG 68:c6399471ea49 98
aidanPJG 68:c6399471ea49 99 }
aidanPJG 70:168d67695a65 100 wait_ms(50); //give it some time
aidanPJG 70:168d67695a65 101 bendCounter++; //increment the bendcounter, used in the xl doc to record which bend is which
aidanPJG 70:168d67695a65 102 //*******resets c# display to null**************
aidanPJG 69:74bffa1d3f7f 103 pc.printf("\n" );
aidanPJG 69:74bffa1d3f7f 104 pc.printf("\n" );
aidanPJG 69:74bffa1d3f7f 105 pc.printf("\n" );
aidanPJG 69:74bffa1d3f7f 106 pc.printf("\n" );
aidanPJG 69:74bffa1d3f7f 107 pc.printf("%d\n", 0);
aidanPJG 70:168d67695a65 108
aidanPJG 70:168d67695a65 109 //*****8resets arrays to all 0 ************
aidanPJG 68:c6399471ea49 110 std::fill_n(times, 20 , 0); //empty array of times so averaging works
aidanPJG 68:c6399471ea49 111 std::fill_n(speeds, 20 , 0);
aidanPJG 68:c6399471ea49 112 std::fill_n(avgSpeeds, 20 , 0);
aidanPJG 68:c6399471ea49 113 std::fill_n(totalTimes, 20 , 0);
aidanPJG 68:c6399471ea49 114 t.reset(); //clock reset
aidanPJG 70:168d67695a65 115
aidanPJG 70:168d67695a65 116 //******start again************
aidanPJG 71:60f06e7e50a3 117 calculate(distance, NoOfPins, date); //be ready for another bend on same former.
aidanPJG 22:34bc2f2f5b09 118 }
aidanPJG 30:ab3a436e8968 119
aidanPJG 71:60f06e7e50a3 120 void printArray(double array[20], double timesArray[20], int NoOfPins, double speedAvg[20], double timesTotal[20], string date) //for printing arrays to xl doc on the mbed.
aidanPJG 38:da262dc4f90a 121 {
aidanPJG 71:60f06e7e50a3 122 initialise(bendCounter, date); //used to create the Datalog file header.Should happen once for every bend test. BendCounter is the iteration of the bend
aidanPJG 70:168d67695a65 123 for (int i = 0; i < NoOfPins ; i++) //for every sensor
aidanPJG 38:da262dc4f90a 124 {
aidanPJG 70:168d67695a65 125 data( i,times[i],speeds[i],timesTotal[i],speedAvg[i]); //printing to Datalog
aidanPJG 44:3b45ec49bc44 126 }
aidanPJG 38:da262dc4f90a 127 }