02/07/15

Dependencies:   mbed

Dependents:   TabbedGUIMbed

Revision:
70:168d67695a65
Parent:
69:74bffa1d3f7f
Child:
71:60f06e7e50a3
--- a/Calculate.cpp	Thu Aug 27 14:32:20 2015 +0000
+++ b/Calculate.cpp	Thu Sep 03 15:10:51 2015 +0000
@@ -30,30 +30,30 @@
     double totalTimes[20];                             //total runnning times 
    
 
-int calculate(double distance, int NoOfPins)
+int calculate(double distance, int NoOfPins)                  //run when the pipe is being bent around the former.
 {     
-while(true){
-   
+while(true)                                                       //keep doing this
+{   
     int i = 0;                                          //set the pin to 0, starting from the 0th ( first) pin
     while( i < NoOfPins)                                //while we have not reached the last pin
     {
                 while(!sensor[i])                                       //while the sensor is not broken hold it. 
                 {
-                    if(sensor[i + 1])                                       //bit of error bypassing
+                    if(sensor[i + 1])                                       //bit of error bypassing - if the next sensor is hit
                      {
                         //printf("it skipped sensor % d", i);
                         led4 = 1;                                           //alerts user that a sensor has been skipped. 
-                        i++; 
+                        i++;                                                //keep going on as normal
                         
                     }                                                        
                 }
-                t.start();
-                                                                                //starts the timer once, doesn't do anything next iteration
-                timeDiff =  t.read();                                                       //reads what the timer is currently at
+                t.start();                                                           //starts the timer once, doesn't do anything next iteration
+                                                                               
+                timeDiff =  t.read();                                                       //reads what the timer is currently at. Getting the time between each sensor
                 t.reset();                                                                 //starts the timer again from 0.
-                times[i] = timeDiff;                                                 //adds sensor times to array for logging.  this must remain above the calculations for avgdistance to work.
+                times[i] = timeDiff;                                                 //adds sensor times to array for logging.  This must remain above the calculations for avgdistance to work.
                 
-                if( i <1 )          //for the first one so it prints out 0 instead of getting inf from dividing by 0 and getting nan symbols.
+                if( i <1 )                                                        //for the first iteration so it prints out 0 instead of getting inf by dividing 0 and getting nan symbols.
                 {
                      printf("0\n");
                      printf("0\n");
@@ -62,72 +62,66 @@
                      printf("1\n");
                      i++;   
                 }
-                else
+                else                                                            //otherwise if it is not the first one do this
                 {
-                    
-                           pc.printf(" timediff: %lf\n", timeDiff); //testing
+                            pc.printf(" timediff: %lf\n", timeDiff);                            //displays the timediff between each sensor at the top of the GUI
                                    
-                                                                                  //to ensure it is not the first one
-                                     speed = 60* (distance / timeDiff);                //multiplied by 60 to get meters per minute
-                                     //*******average speed
-                                     avgdistance = distance * i;                        //total distance = distance between each sensor multiplied by number of sensors
-                                     // pc.printf("%f dist/s \n", avgdistance);   //testing  
+                                   //*****************Inst Speed *************************
+                                     speed = 60* (distance / timeDiff);                //instaneous speed between each sensor.  Multiplied by 60 to get meters per minute
+                                     //*******average speed*******************************
+                                     avgdistance = distance * i;                        //total distance = distance between each sensor multiplied by number of sensors                                    
                                         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
-                                        //gets the total time it has taken. Could start another timer and only read it.
-                                     pc.printf("avgtime: %f \n", avgtime);  //testing
+                                        //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)
+                                     pc.printf("avgtime: %f \n", avgtime);                  //print the average time for the top bit for checking/testing
                                     avgspeed = 60*(avgdistance / avgtime);                  //multiplied by 60 to get meters per minute
                                     
-                                    //******send to program.
+                                    //******send to GUI****************************************
                                      pc.printf("%f\n", speed);            //inst speed printed  
                                      pc.printf("%f\n", avgspeed);        //avg speed printed
                                      pc.printf("%d \n",i+1);                //which sensor it is on printed
-                                     //*********add to array
-                                     speeds[i] = speed;                     //log speeds.
-                                     avgSpeeds[i] = avgspeed;
-                                     totalTimes[i] = avgtime;
-                                       i++;                           //move onto next sensor
-                                       
+                                     //*********add to array*************************************
+                                     speeds[i] = speed;                     //save inst speeds.
+                                     avgSpeeds[i] = avgspeed;               //save avg speeds 
+                                     totalTimes[i] = avgtime;               //save total times
+                                       i++;                           //move onto next sensor                                   
              } 
         }
         
-         printArray(speeds,times,NoOfPins,avgSpeeds,totalTimes);
-        reset(distance, NoOfPins);    
+         printArray(speeds,times,NoOfPins,avgSpeeds,totalTimes);            //write info to Mbed xl
+        reset(distance, NoOfPins);                                          //reset after all sensors on former have been used. Prepare for it to start from the start again.
 }
 }
 
-void reset(int distance, int NoOfPins)
+void reset(int distance, int NoOfPins)                                  //reset after all sensors on former have been used. Prepare for it to start from the start again.     
 {
     while ( sensor[0]){
         
         }
-        wait_ms(50);
-        bendCounter++;
-        //resets c# display to all 0's
+        wait_ms(50);                                                        //give it some time
+        bendCounter++;                                                      //increment the bendcounter, used in the xl doc to record which bend is which
+        //*******resets c# display to null**************           
          pc.printf("\n" );
          pc.printf("\n" );
          pc.printf("\n" );
          pc.printf("\n" );
          pc.printf("%d\n", 0);
-        //resets arrays to all 0     
+         
+        //*****8resets arrays to all 0 ************    
          std::fill_n(times, 20 , 0);        //empty array of times so averaging works
          std::fill_n(speeds, 20 , 0);
          std::fill_n(avgSpeeds, 20 , 0);
          std::fill_n(totalTimes, 20 , 0);
          t.reset(); //clock reset
-        calculate(distance, NoOfPins);
+         
+         //******start again************
+        calculate(distance, NoOfPins);  //be ready for another bend on same former.
 }
 
-void printArray(double array[20], double timesArray[20], int NoOfPins, double speedAvg[20], double timesTotal[20])              //for printing arrays to the terminal
+void printArray(double array[20], double timesArray[20], int NoOfPins, double speedAvg[20], double timesTotal[20])              //for printing arrays to xl doc on the mbed.
       {
-        /*prints to the terminal
-        for (int i = 0; i < NoOfPins ; i++)                                     //printing module for array                              
+          initialise(bendCounter);                                 //used to create the Datalog file header.Should happen once for every bend test. BendCounter is the iteration of the bend
+         for (int i = 0; i < NoOfPins ; i++)                                 //for every sensor                         
         {
-             pc.printf(" Sensor : %d    Time %lf    Speed %f  \n", i,times[i],speeds[i]); 
-          //   pc.printf(" %d : %d  \t", i,times[i]); 
-         } */
-          initialise(bendCounter);                                 //used to create the Datalog file should happen once for every bend test
-         for (int i = 0; i < NoOfPins ; i++)                                     //printing to Datalog                            
-        {
-             data( i,times[i],speeds[i],timesTotal[i],speedAvg[i]); 
+             data( i,times[i],speeds[i],timesTotal[i],speedAvg[i]);            //printing to Datalog    
          }
       }