Team Design project 3 main file

Dependencies:   mbed Motor_Driver Sensors MMA8451Q LocalPositionSystem

Fork of TDP_main by Ivelin Kozarev

main.cpp

Committer:
orsharp
Date:
2015-03-06
Revision:
12:bb21b76b6375
Parent:
11:9e56d52485d1
Child:
14:3844d1dacece

File content as of revision 12:bb21b76b6375:

// TESTING REPO COMMIT

/*
****** MAIN PROGRAM ******



Please consider that although it is an embedded envrionment we are NOT creating a HARD-TIME real time system - delays can be dealt with


Sensors are mapped on the global variable sensorsCheckSum,
which multiplies the sensor number by itself to then decode,
 which sensors are off and which are on
ie. if sensor rightright - sensorChecksum = 1*1 = 1
    if rightright and rightcentre - sensorChecksum = 1*1 + 2*2 = 5
    ...
*/



#include "mbed.h"
#include "sensor_measure.h"
#include "Motor_Driver.h"
#include "shooter.h"
//#include "Sensors.h"

#define PWM_PERIOD_US 10000


//DigitalOut led(LED1);


//Serial pc(USBTX, USBRX);

//Timer MeasureTimer; //Timer used for measurement

//Motor_Driver motors(PTD4, PTA12, PTA4, PTA5, PTC9, PTC8, PWM_PERIOD_US);

typedef enum  mode {REGULAR,SQUARE} mode; //enumeration for different states
mode driveMode; //declaring the variable for the states
int sensorsCheckSum; //varibale used for sensors mapping access



void measureSensors () {
    sensorsCheckSum = 0; //zero it when first going into the routine
    int iterationNumber = NUMBER_SENSORS_REGULAR;
    if (driveMode == SQUARE) {
          iterationNumber += NUMBER_SENSORS_SQUARE;
    }
    for (int i = 0; i < iterationNumber;i++){
        if (measure(*sensorArray[i]) == 1) {//if sensor is white 
            sensorsCheckSum += (i+1)*(i+1);
        }
    }    
    //pc.printf("sensorsCheckSum is %i",sensorsCheckSum);
}

int main() {
    
    
    /*
    Motor_Driver motors(PTD4, PTA12, PTA4, PTA5, PTC9, PTC8, PWM_PERIOD_US);
    
    motors.setSpeed(1.0f);
    motors.forward();
    motors.start();
    
    wait(3);
    
    motors.stop()
    motors.reverse();
    wait(200ms);
    
    motors.start();
    */ 
 //   setup_counter(1000, 0);
  //  float frequency = measure_frequency(CHANNEL_1);
    
    driveMode = REGULAR; //initialise drivemode
    sensor_initialise(); // initialise sensor values
    wait(1); //give time to set up the system
    
    sensorTimer.start(); // start timer for sensors
    
    
    pc.printf("Start...");
    
    
    while (1) {
       
       if (driveMode == REGULAR) {
          measureSensors();
          switch (sensorsCheckSum) {
            case 1: //right right is 1*1 + 0 + 0+ 0+0 +0 
                pc.printf ("only Right is white \n");
                break;
            case 4 :   //0*0 + 2*2 + 0 + 0 + 0
                pc.printf ("only Left is white \n");
                break;
            case 5 :  //1*1 + 2*2 + 0 + 0 
                pc.printf ("both are white \n");
                break;
//          case 91: 
//            driveMode = SQUARE; //if all sensors are white you're in the square
//            break;
          default: //checksum is zero , all are black
            pc.printf ("BLACK BLACK");
            break;
          } 
              
        
           
        }
       else { //if (driveMode == SQUARE}
        //implement the square searching thing..
       
       }
       
       
    }
    
        
}





/*    TOBE deleted
double measure1 (){


    
    sensorTimer.reset();
    double freq,period = 0.0;
    int n =0; //number of samples
    int sensor_old = 0;
    int sensor_new  = 0;//variable to remember old sensor state
    double time_us = sensorTimer.read(); 
    while (n < NUMBER_SAMPLES){
        sensor_new = pin_right_right.read();
        if ( sensor_new== 1 && sensor_old == 0){ // detect on rising edge ,sensor_old
            n++;
        }
        sensor_old = sensor_new;
        
    }
    double time_us2 = sensorTimer.read();
   // pc.printf(" delta time is %f , time 2 is %f " , (time_us2 - time_us), time_us2); 
    period = time_us2/((double)NUMBER_SAMPLES); // Get time
    freq = (1/period);   // Convert period (in us) to frequency (Hz). Works up to 100kHz. 
   // pc.printf(" period is  %f  ", period);
    
    return freq;//(freq < sensor.black_value*2)? 1 : 0;
}
*/