Lol smth

Dependencies:   LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed

Fork of TDP_main_BartFork by Yelfie

main.cpp

Committer:
Reckstyle
Date:
2015-03-18
Revision:
17:de8b3111ddc5
Parent:
16:3649eb1a056d
Child:
18:d947ea4bab72
Child:
19:d277614084bc

File content as of revision 17:de8b3111ddc5:

// 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 HC06(PTE0,PTE1); //TX,RX
//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++){
        //pc.printf("%i  iteration%i ",i,iterationNumber);
        if (measure(sensorArray[i]) == 1) {//if sensor is white 
            sensorsCheckSum += (i+1)*(i+1);
        }
    }    
    //pc.printf("sensorsCheckSum is %i",sensorsCheckSum);
}

void printBluetooth() { //for debugging
    
    HC06.printf("LLU%i  LRU%i               rlu%i rru%i\n",sensorArray[7]->state,sensorArray[6]->state,sensorArray[1]->state,sensorArray[0]->state);
    HC06.printf("LLD%i  LRD%i               rld%i rrd%i\n\n",sensorArray[5]->state,sensorArray[4]->state,sensorArray[3]->state,sensorArray[2]->state);
    //HC06.printf("%i  %i               %i %i",sensorArray[NUMBER_SENSORS_REGULAR-3]->state,sensorArray[NUMBER_SENSORS_REGULAR-4]->state,sensorArray[3]->state,sensorArray[2]->state);
    //HC06.printf("%i      %i/n%i      %i,sensorArray[NUMBER_SENSORS_REGULAR]->state,sensorArray[NUMBER_SENSORS_REGULAR+1]->state,sensorArray[NUMBER_SENSORS_REGULAR+2]->state,sensorArray[NUMBER_SENSORS_REGULAR+3]->state);        
    //HC06.printf("%f                      %f",motor.getLeftSpeed(),motor.getRightSpeed());
    HC06.printf("sensorCheckSum%i\n\n",sensorsCheckSum);
}

int main() {
    
    
    
    Motor_Driver motors(PTA5, PTC9, PTC8,PTD4, PTA12, PTA4,  PWM_PERIOD_US);
    
  //  motors.setSpeed(0.1f);
//    motors.forward();
//    motors.start();
//    wait(2);
//    float x=0.1f;
//    while (1) {
//        motors.setLeftSpeed(x);
//        x = x+0.05;
//        wait(3);
//        }
//    motors.setLeftSpeed(0.1f);
//    wait(5);
//    motors.setLeftSpeed(0.2f);
//    motors.setRightSpeed (0.2f);
//    wait(3);
//    motors.setRightSpeed (0.1f);
//    wait(5);
//    motors.stop();
    
    //wait(1);
//    motors.reverse();
//    wait(5);
//    motors.stop();
//    motors.setSpeed(0.5f);
//    motors.start();
//    wait(5);
//    motors.stop();
//    wait(1);
//    motors.reverse();
    
   
    
    //motors.start();
    
 //   setup_counter(1000, 0);
  //  float frequency = measure_frequency(CHANNEL_1);
    
    driveMode = REGULAR; //initialise drivemoder
    sensor_initialise(); // initialise sensor values
    wait(1); //give time to set up the system
    
    sensorTimer.start(); // start timer for sensors
    
    HC06.baud(9600);
    HC06.printf("working..");
    motors.setSpeed(0.1f);
    motors.forward();
    motors.start();
    while(1){
        measureSensors();
        printBluetooth();
        if (sensorsCheckSum > 94){
            motors.setSpeed(0.1f);
            motors.setLeftSpeed(0.05f);
        }
        else if (sensorsCheckSum < 94) {
            motors.setSpeed(0.1f);
            motors.setRightSpeed(0.05f);
        }
        else {
            motors.setSpeed(0.1f);
        }
        wait(0.1);
    }
    
   
    //HC06.printf("AT");
    //HC06.printf("AT+PIN5555");
    

   // pc.printf("Start...");
    
    
//    while (1) {
//       
//       if (driveMode == REGULAR) {
//          measureSensors();
//          switch (sensorsCheckSum) {
//            case 74: //all right are white,else is good TURN RIGHT
//                break;
//            case 78: //RLD is black, all else good
//                break;
//            case 90: //RLU is black,all else good
//                break;
//            
//            case 94: //keep straight
//                pc.printf ("only Right is white \n");
//                break;
//            case 95 :   //RRU is white
//                pc.printf ("only Left is white \n");
//                break;
//            case 103 :  //RRD is white all else normal
//                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..
//       
//       }
//       
//       
//    }
//    
        
}