Lol smth

Dependencies:   LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed

Fork of TDP_main_BartFork by Yelfie

main.cpp

Committer:
Bartas
Date:
2015-03-19
Revision:
18:d947ea4bab72
Parent:
17:de8b3111ddc5

File content as of revision 18:d947ea4bab72:

// 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 0: // all black, turn around by 180 degrees 
//                  break;
//            case 30: //all right are white, left all black >> turn right(move left wheel)
//                break;
//            case 46: //left 5 white, right only 3 black >> turn right
//                  break;
//            case 94: //normal startting position, half of right and half of left are white, (move right wheel)
//                break;
//            case 104: //right all white, left half white >> turn right 90 degrees
//                break;
//            case 154: //right 4 white, left only 6 black >> turn left
//                  break;
//            case 174: //left all white, right all black >> turn left (move right wheel)
//                pc.printf ("only Right is white \n");
//                break;
//            case 194 :   //left all white, right half white >> go straight, turn right if 194 goes to 204
//                pc.printf ("only Left is white \n");
//                break;
//            case 204 :  //all sensors are white
//                pc.printf ("both are white \n");
//                break;
//            
//
//          default: //checksum is zero , all are black
//            pc.printf ("BLACK BLACK");
//            break;
//          } 
//              
//        
//           
//        }
//       else { //if (driveMode == SQUARE}
//        //implement the square searching thing..
//       
//       }
//       
//       
//    }
//    
        
}