Pipeline Technology Centre / Mbed 2 deprecated ConsolTest

Dependencies:   mbed

Dependents:   TabbedGUIMbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Calculate.cpp Source File

Calculate.cpp

00001 #include "mbed.h"
00002 #include <time.h>
00003 #include <string>
00004 #include <iostream>
00005 #include "Data.h"
00006 
00007 //*************defining methods used in this method
00008 void printArray(double array[20], double timesArray[20], int NoOfPins,double speedAvg[20], double timesTotal[20], string date);                                     
00009 void data(int sensor_number, double time, double speed, double timesTotal, double speedAvg);  
00010 void reset(int distance, int NoOfPins, string date);
00011                                                 
00012  //*********mbed pins*********************************
00013 extern Serial pc;                                                                   //defines the communication between MBed and pc       
00014 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.  
00015 DigitalOut led4(LED4); 
00016 
00017 //********c++ variables*******************************
00018     
00019     Timer t;
00020     double timeDiff= 0;                          //time between 2 sensors  
00021     double speed = 0;                           //instspeed
00022     double avgtime= 0;;                                 
00023     double avgspeed = 0;
00024     double avgdistance = 0;
00025     int bendCounter = 0;
00026     //arrays
00027     double times[20];                                //array of the time the sensor is reached .(the size of #pins)
00028     double speeds[20];                                //array of inst speeds 
00029     double avgSpeeds[20];                             //avg speeds
00030     double totalTimes[20];                             //total runnning times 
00031    
00032 
00033 int calculate(double distance, int NoOfPins, string date)                  //run when the pipe is being bent around the former.
00034 {     
00035 while(true)                                                       //keep doing this
00036 {   
00037     int i = 0;                                          //set the pin to 0, starting from the 0th ( first) pin
00038     while( i < NoOfPins)                                //while we have not reached the last pin
00039     {
00040                 while(!sensor[i])                                       //while the sensor is not broken hold it. 
00041                 {
00042                     if(sensor[i + 1])                                       //bit of error bypassing - if the next sensor is hit
00043                      {
00044                         //printf("it skipped sensor % d", i);
00045                         led4 = 1;                                           //alerts user that a sensor has been skipped. 
00046                         i++;                                                //keep going on as normal
00047                         
00048                     }                                                        
00049                 }
00050                 t.start();                                                           //starts the timer once, doesn't do anything next iteration
00051                                                                                
00052                 timeDiff =  t.read();                                                       //reads what the timer is currently at. Getting the time between each sensor
00053                 t.reset();                                                                 //starts the timer again from 0.
00054                 times[i] = timeDiff;                                                 //adds sensor times to array for logging.  This must remain above the calculations for avgdistance to work.
00055                 
00056                 if( i <1 )                                                        //for the first iteration so it prints out 0 instead of getting inf by dividing 0 and getting nan symbols.
00057                 {
00058                      printf("0\n");
00059                      printf("0\n");
00060                      printf("0\n");
00061                      printf("0\n");
00062                      printf("1\n");
00063                      i++;   
00064                 }
00065                 else                                                            //otherwise if it is not the first one do this
00066                 {
00067                             pc.printf(" timediff: %lf\n", timeDiff);                            //displays the timediff between each sensor at the top of the GUI
00068                                    
00069                                    //*****************Inst Speed *************************
00070                                      speed = 60* (distance / timeDiff);                //instaneous speed between each sensor.  Multiplied by 60 to get meters per minute
00071                                      //*******average speed*******************************
00072                                      avgdistance = distance * i;                        //total distance = distance between each sensor multiplied by number of sensors                                    
00073                                         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
00074                                         //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)
00075                                      pc.printf("avgtime: %f \n", avgtime);                  //print the average time for the top bit for checking/testing
00076                                     avgspeed = 60*(avgdistance / avgtime);                  //multiplied by 60 to get meters per minute
00077                                     
00078                                     //******send to GUI****************************************
00079                                      pc.printf("%f\n", speed);            //inst speed printed  
00080                                      pc.printf("%f\n", avgspeed);        //avg speed printed
00081                                      pc.printf("%d \n",i+1);                //which sensor it is on printed
00082                                      //*********add to array*************************************
00083                                      speeds[i] = speed;                     //save inst speeds.
00084                                      avgSpeeds[i] = avgspeed;               //save avg speeds 
00085                                      totalTimes[i] = avgtime;               //save total times
00086                                        i++;                           //move onto next sensor                                   
00087              } 
00088         }
00089         
00090          printArray(speeds,times,NoOfPins,avgSpeeds,totalTimes, date);            //write info to Mbed xl
00091         reset(distance, NoOfPins, date);                                          //reset after all sensors on former have been used. Prepare for it to start from the start again.
00092 }
00093 }
00094 
00095 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.     
00096 {
00097     while ( sensor[0]){
00098         
00099         }
00100         wait_ms(50);                                                        //give it some time
00101         bendCounter++;                                                      //increment the bendcounter, used in the xl doc to record which bend is which
00102         //*******resets c# display to null**************           
00103          pc.printf("\n" );
00104          pc.printf("\n" );
00105          pc.printf("\n" );
00106          pc.printf("\n" );
00107          pc.printf("%d\n", 0);
00108          
00109         //*****8resets arrays to all 0 ************    
00110          std::fill_n(times, 20 , 0);        //empty array of times so averaging works
00111          std::fill_n(speeds, 20 , 0);
00112          std::fill_n(avgSpeeds, 20 , 0);
00113          std::fill_n(totalTimes, 20 , 0);
00114          t.reset(); //clock reset
00115          
00116          //******start again************
00117         calculate(distance, NoOfPins, date);  //be ready for another bend on same former.
00118 }
00119 
00120 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.
00121       {
00122           initialise(bendCounter, date);                                 //used to create the Datalog file header.Should happen once for every bend test. BendCounter is the iteration of the bend
00123          for (int i = 0; i < NoOfPins ; i++)                                 //for every sensor                         
00124         {
00125              data( i,times[i],speeds[i],timesTotal[i],speedAvg[i]);            //printing to Datalog    
00126          }
00127       }