Team Ascent / Mbed 2 deprecated FinalBluetoothFunction

Dependencies:   mbed

main.cpp

Committer:
Dbee16
Date:
2015-03-11
Revision:
1:c4af0bbe20b9
Parent:
0:f54028dab58f
Child:
2:054c41590f3f

File content as of revision 1:c4af0bbe20b9:


/*         what target am i at?
        wait for input from phone
         if input is 1 or 2 then return number to choose target choice function which then fires at target
         did i hit the target?
        wait for input from phone
        if input is yes then go back to maze navigation function
        if input is no then try again
 ********************************************************** */

#include "mbed.h"

Serial blue(PTC4,PTC3);
DigitalOut led(LED1);

// which target are we at?
int TargetChoice()
{

    blue.printf("Which target am I at?");           // ask user question
    char TargetIn = blue.getc();                    // assign user input to TargetIn
    int command=0, choice = 0;

    while(command==0) {                             // loop until a command has been given
        switch (TargetIn) {                         // input is either 1 or 2 in DECIMAL
            case 0x31:                              // if target 1
                choice=1;                           //
                command = 1;                        // exit while
                break;
            case 0x32:                              // if target 2
                choice=2;                            // return 2 to ball firing or call ball fire 2
                command = 1;                        // exit while
                break;
        }

    }
    return choice;                                  // return which target to fire at
}

// did we hit the target
int SuccessCheck()
{

    blue.printf("Did I hit the target?");           // ask user question
    char SuccessIn = blue.getc();                   // assign user input to SuccessIn
    int command=0, success=0;

    while(command==0) {                             // loop until a command has been given
        switch (SuccessIn) {                        // input either y (0x79) or n (0x6E)
            case 0x79:                              // if input is y
                success=1;                          // return 1 directs to go back to the maze and locate next target
                command = 1;
                break;

            case 0x6E:                              // if input is n
                success=0;                          // return 0 to go back to trying to hit the target
                command = 1;
                break;
        }

    }
    return success;
}

//main bluetooth operation function
void MazeBluetoothing()
{

    //  int bluetooth=0;  does this still initialise int bluetooth to 0? for the while loop?
    while(int bluetooth=0) {

        int target = TargetChoice();                                 // call target choosing function

        // if target = 1 then call function to fire at target 1, if target = 2 then call fire function 2
        switch (target) {
            case 1:
                //call target 1 function
                break;

            case 2:
                //call target 2 function
                break;
        }
        int success = SuccessCheck();

        // if success is 0, then reposition & go back to start of bluetoothing()
        switch (success) {
            case 0:
                // call repositioning functionthen break to go back to loop of bluetoothing
                break;

            case 1:
                bluetooth=1;
                break;

        }
    }
}


//motor select pins
DigitalOut motor_lf(PTE2);
DigitalOut motor_lb(PTE3);
DigitalOut motor_rf(PTE4);
DigitalOut motor_rb(PTE5);

//Frequency of Pulse Width Modulated signal in Hz
#define PWM_FREQ          1000

//PWM pin (Enable 1 and 2)
PwmOut motor_l (PTC2);
PwmOut motor_r (PTE29);

//LED to test
DigitalOut myled(LED_BLUE);

void set_direction( int direction, float speed, float angle)
{
    /* *******************Example Use*****************
                 set_direction("11", 0.5, 0);  //This would set the robot to go forward, at half speed, straight

                 set_directin( "11", 0.8, 0.2); //this would set robot to go forward, at 100% to the right and 60% to the left (turn right)

                 set_direction("00", 0.3, 0.7); //robot back, 100% rightmotor; 0%left (technically -40%, but 0% is min)

                 set_direction("11", 0.5, 0.5); // robot pivots forward, to the right, full speed (100% duty right ; 0% duty left)

                 set_direction("11", 0.5, -0.5); //robot pivots forward, to the left,  full speed  (0% duty right ; 100% duty left)

                 set_direction("11", 0.3, 0.5); //robot pivots forard, to the right , at 80% speed (80% right; 0% left)

                 set_direction("11", -0.3, 0.5); //robot pivotrs foward, to the right, AT 20% SPEED (20%, 0%)

                 set_direction("10", 0.5, 0);   //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%

                 set_direction("10", 0.5, 0.3); //robot spins to the right, 80% right forward ; 20% left backward

    */

    float r_result = (speed + angle); //where angle can be postive and negative and will TURN the robot
    float l_result = (speed - angle); //if turning left, you'd make the angle postive (right motor goes faster left motor goes slwoer)
    switch( direction ) {
        case 0x11: { //forward
            motor_r.write(  r_result  < 0 ? 0    :     r_result > 1 ? 1    :       r_result);
            //Look up ternary statements -- {condition ? value_if_true : value_if_false} This makes 0<result<1
            motor_l.write(  l_result < 0 ? 0     :     l_result > 1 ? 1    :       l_result);

            motor_rf=1;
            motor_rb=0;
            motor_lf=1;
            motor_lb=0;
        }
        case 0x00: { //backward
            motor_r.write( r_result  < 0 ? 0    :     r_result > 1 ? 1    :       r_result);
            motor_l.write( l_result  < 0 ? 0    :     l_result > 1 ? 1    :       l_result);

            motor_rf=0;
            motor_rb=1;
            motor_lf=0;
            motor_lb=1;
        }
        case 0x01: {  //spin left  --     Right forward, left backward
            motor_r.write( r_result  < 0 ? 0    :     r_result > 1 ? 1    :       r_result);
            motor_l.write( l_result  < 0 ? 0    :     l_result > 1 ? 1    :       l_result);

            motor_rf=1;
            motor_rb=0;
            motor_lf=0;
            motor_lb=1;
        }
        case 0x10: {    //spin right
            motor_r.write( r_result  < 0 ? 0    :     r_result > 1 ? 1    :       r_result);
            motor_l.write( l_result  < 0 ? 0    :     l_result > 1 ? 1    :       l_result);

            motor_rf=0;
            motor_rb=1;
            motor_lf=1;
            motor_lb=0;
        }
    }
}


void SpinFunct()
{
    //Set PWM frequency to 1000Hz
    motor_l.period( 1.0f / (float) PWM_FREQ);
    motor_r.period( 1.0f / (float) PWM_FREQ);
    //Initialise direction to nothing.
    motor_rf=0;
    motor_rb=0;
    motor_lf=0;
    motor_lb=0;
    led=1;
    set_direction(0x10, 0.5, 0.3); //robot spins to the right, 80% right forward ; 20% left backward
    wait(5);
    led=0;

}

void JiveFunct()
{

    //Set PWM frequency to 1000Hz
    motor_l.period( 1.0f / (float) PWM_FREQ);
    motor_r.period( 1.0f / (float) PWM_FREQ);
    //Initialise direction to nothing.
    motor_rf=0;
    motor_rb=0;
    motor_lf=0;
    motor_lb=0;

    //forward + right spin
    wait(2);
    myled = !myled;
    set_direction(0x11, 0.3, 0.5); //robot pivots forard, to the right , at 80% speed (80% right; 0% left)

    // back spin
    wait(2);
    myled = !myled;
    set_direction(0x00, 0.3, 0.7); //robot back, 100% rightmotor; 0%left (technically -40%, but 0% is min)

    //
    wait(2);
    myled = !myled;
    set_direction(0x01, 0.3, 0.4);   //robot spins to teh left, at half speed. Left forward 70% ; Right 0% (technically -30%, but 0% is min)

    wait(2);
    myled = !myled;
    set_direction(0x10, 0.5, 0);   //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%


    /*
     wait(2);
     led = !led;
     set_direction(0x11, 0.5, 0);  //This would set the robot to go forward, at half speed, straight

     wait(2);
     led = !led;
     set_direction(0x11, 0.8, 0.2); //this would set robot to go forward, at 100% to the right and 60% to the left (turn right)

     wait(2);
     led = !led;
     set_direction(0x11, 0.5, 0.5); // robot pivots forward, to the right, full speed (100% duty right ; 0% duty left)

     wait(2);
     led = !led;
     set_direction(0x11, 0.5, -0.5); //robot pivots forward, to the left,  full speed  (0% duty right ; 100% duty left)



     wait(2);
     led = !led;
     set_direction(0x11, -0.3, 0.5); //robot pivotrs foward, to the right, AT 20% SPEED (20%, 0%)

     wait(2);
     led = !led;
     set_direction(0x10, 0.5, 0);   //robot spins to teh right, at half speed. Left forward 50% ; Right forward 50%

     wait(2);
     led = !led;
     set_direction(0x10, 0.5, 0.3); //robot spins to the right, 80% right forward ; 20% left backward
     */

}


// will just make a question at the end to say quit? yes or no. it's possible to make a quitter function and test the input for quit
// at every point but it's too annoying to get it to work... so fuk dat

void RemoteControl()
{
    char dir[2]; /*speed=0, angle=0, time=0, */
    int quit=0;

    while(quit==0) {
        wait(1);
        //dir
        blue.printf("Direction?\n");
        while ( blue.readable()==0 ) {}               // loops until there is an input from the user

        for(int i=0; i<2; i++) {
            dir[i] = blue.getc();    //Store incoming response into array
        }
        blue.printf("%c dir\n", dir);

    }
}
//for(int i=0; i<4; i++) { quitarray[i]= blue.getc();  blue.printf("get %c", quitarray[i]); } //Store incoming response into array
//quit=Quitter(quitarray);

/*
//speed
blue.printf("Speed?");
while ( blue.readable() ) {}               // loops until there is an input from the user
    speed=blue.getc();
    quit=Quitter();


//angle
blue.printf("Angle?");
while (blue.readable()==0) {}               // loops until there is an input from the user
    angle=blue.getc();
    quit=Quitter();


//time
blue.printf("How long?");
while (blue.readable()==0) {}               // loops until there is an input from the user
    time=blue.getc();
    quit=Quitter();
*/
//set_direction(dir, speed, angle); //also find a way to do time too
//wait(time);


/*

send 11, 0.5, 0.2 in one string
for the floats, it will send (num)(point)(num) ef 0.3 = 30, 2E, 33
all in ASCII remember

so for:
11 0.5 0.2
it will send:
31 31 20 30 2e 35 20 30 2e 32

sending 313120302e3520302e32 would cause a stack overload

going to have to split the user into 3 different lines of tx

so it'd be simpler too but more difficult for the user to control.
might need to add in a "for how long for?" function that will only drive the motors for a short period of time

user would have to be knowledgeable of the set direction function  aka get daniel to do the demonstration

so then user sends: direction 11,10,01; speed 0.2,0.5,0.7; angle 0.2,0.4,0.8; time 1,2,3,4;

yes it is slow, but it allows for exact computation of distance, speed and angle

so 1 function with 4 different while loops

while user has not input a direction, loop again

one function : remote control
*/

int main()
{

    int spin[4] = {0x73, 0x70, 0x69, 0x6e};
    int jive[4] = {0x6a, 0x69, 0x76, 0x65};
    int ctrl[4] = {0x63, 0x74, 0x72, 0x6c};
    int maze[4] = {0x6d, 0x61, 0x7a, 0x65};
    unsigned char x[4];

    while (1) {
        blue.printf("What will I do?\n");           // ask user question
        while (blue.readable()==0) {}               // loops until there is an input from the user

        for(int i=0; i<4; i++) {
            x[i] = blue.getc();     //Store incoming response into array
        }

        int FindSpin=0, FindJive=0, FindCtrl=0, FindMaze=0;

        for(int s=0; s<4; s++) {
            if (x[s]==spin[s]) {
                FindSpin++;
            }
            if (x[s]==jive[s]) {
                FindJive++;
            }
            if (x[s]==ctrl[s]) {
                FindCtrl++;
            }
            if (x[s]==maze[s]) {
                FindMaze++;
            }
        }

        if (FindSpin==4) {
            SpinFunct();
        }
        if (FindJive==4) {
            JiveFunct();
        }
        if (FindCtrl==4) {
            RemoteControl() ;
        }
        if (FindMaze==4) {
            led=1;
            wait(4);
            led=0;
        }
    }
}










/*
int Quitter(char x[4]) {     int quit=0; char quitting[4]={0x71, 0x75, 0x69, 0x74};

    blue.printf ("works");
    for(int i=0; i<4; i++) {  x[i] = blue.getc();  } //Store incoming response into array
    for(int s=0; s<4; s++) {
        if (x[s]==quitting[s]) { quit++; }
    }
    if (quit==4) { quit=1;}
    return quit;
}
*/