02/07/15

Dependencies:   mbed

Dependents:   TabbedGUIMbed

Calculate.cpp

Committer:
aidanPJG
Date:
2015-08-05
Revision:
63:7d2bff227f65
Parent:
62:9de96bcfdb4b
Child:
64:809bd19f4e7c

File content as of revision 63:7d2bff227f65:

#include "mbed.h"
#include <time.h>
#include <string>
#include <iostream>
#include "Distance.h"

void printArray(double array[20], double timesArray[20], int NoOfPins);                                     //defining the methods
void data(int sensor_number, double time, double speed);  
DigitalOut led4(LED4);                                                 

 //*********mbed pins*********************************
extern Serial pc;                                                                   //defines the communication between MBed and pc       
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.  

//********c++ variables*******************************   
    Timer t;
    double timeDiff= 0;                          //time between 2 sensors - not used right now   
    double speed = 0;
    double times[20];                                //array the size of #pins
    double speeds[20];                                //array of speeds 
    double avgSpeeds[20];
    double totalTimes[20];
    double avgtime= 0;;    
    double avgspeed = 0;
    double avgdistance = 0;

      
int calculate(double distance, int NoOfPins)
{     
    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
                     {
                        //printf("it skipped sensor % d", i);
                        led4 = 1;                                           //alerts user that a sensor has been skipped. 
                        i++; 
                        
                    }                                                        
                }
                t.start();                                                                //starts the timer once, doesn't do anything next iteration
                timeDiff =  t.read();                                                       //reads what the timer is currently at
                t.reset();                                                                 //starts the timer again from 0.
                times[i] = timeDiff;                                                 //adds sensor times to array for logging.
                
                if( i <1 )          //for the first one so it prints out 0 instead of getting inf from dividing by 0 and getting nan symbols.
                {
                     printf("0\n");
                     printf("0\n");
                     printf("0\n");
                     printf("0\n");
                     printf("1\n");
                     i++;   
                }
                else
                {
                    
                           pc.printf(" timediff: %lf\n", timeDiff); //testing
                                   
                                                                                  //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  
                                        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
                                    avgspeed = 60*(avgdistance / avgtime);                  //multiplied by 60 to get meters per minute
                                    
                                    //******send to program.
                                     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
             } 
        }
         printArray(speeds,times,NoOfPins);
}

void printArray(double array[20], double timesArray[20], int NoOfPins)              //for printing arrays to the terminal
      {
        /*prints to the terminal
        for (int i = 0; i < NoOfPins ; i++)                                     //printing module for array                              
        {
             pc.printf(" Sensor : %d    Time %lf    Speed %f  \n", i,times[i],speeds[i]); 
          //   pc.printf(" %d : %d  \t", i,times[i]); 
         } */
         for (int i = 0; i < NoOfPins ; i++)                                     //printing to Datalog                            
        {
             data( i,times[i],speeds[i]); 
         }
      }