Team Ascent / Mbed 2 deprecated FinalBluetoothFunction

Dependencies:   mbed

main.cpp

Committer:
fraserhmbed
Date:
2015-03-26
Revision:
2:054c41590f3f
Parent:
1:c4af0bbe20b9

File content as of revision 2:054c41590f3f:


/*         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);


void manual_mode()
{
    while( blue.readable() )
        int direction = 0x11;
    unsigned char rem_dir; //remote_direction

    blue.printf("Thanks, calculating it now, you entered %c\n",rem_dir);
    if(   ( rem_dir == 'f')   ) {
        direction = 0x11;
        blue.printf("You wanted to go straight\n");
        switch (rem_dir) {

            case('f'):
                direction = 0x11;
                blue.printf("You wanted to go straight\n");
                break;
            case( 'b' )    :
                direction = 0x00;
                blue.printf("You wanted to go backwards\n");
                break;
            case( 'l' )  :

                direction = 0x01;
                blue.printf("You wanted to spin anti-clockwise\n");

                break;
            case ('r') :

                direction = 0x10;
                blue.printf("You wanted to spin clockwise\n");
                break;
            case ('s') :
                motor_r.write(0);
                motor_l.write(0);
                blue.printf("Stopping now.\n");
                break;
        }
        if (   (rem_dir == 'b' )     ) {

            direction = 0x00;
            blue.printf("You wanted to go backwards\n");
        }
        if (   (rem_dir == 'l' )   ) {

            direction = 0x01;
            blue.printf("You wanted to spin anti-clockwise\n");

        }
        if (   (rem_dir == 'r' )   )  {

            direction = 0x10;
            blue.printf("You wanted to spin clockwise\n");
        }
        if (rem_dir =='s') {

            motor_r.write(0);
            motor_l.write(0);
            blue.printf("STAAAAHP\n");
        } else {
            wait(0.1);
            wait(0.1);
            blue.printf("What duty left?\n");
            while ( blue.readable()==0 ) {}               // loops until there is an input from the user
            char l_speed = blue.getc();
            int l_result = l_speed - '0';
            int r_result = r_speed - '0';
            set_direction(direction, (float)l_result/10, (float)r_result/10);
        }

    }

}

}

}



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

    blue.printf("Which target am I at?");           // ask user question
    char TargetIn = blue.getc();                    // assign user input to TargetIn
    int command=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
                closetarget();                           //
                command = 1;                        // exit while
                break;
            case 0x32:                              // if target 2
                fartarget();                            // return 2 to ball firing or call ball fire 2
                command = 1;                        // exit while
                break;
        }
    }
}

// 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 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;

}

// cant send 10 in ascii or in hex as they
/*

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 ctrl[4] = {0x63, 0x74, 0x72, 0x6c};
    int maze[4] = {0x6d, 0x61, 0x7a, 0x65};
    unsigned char x[4];


//Set PWM frequency to 1000Hz
    motor_rb=0;
    motor_lf=0;
    motor_lb=0;

    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, FindCtrl=0, FindMaze=0;

        for(int s=0; s<4; s++) {
            if (x[s]==spin[s]) {
                FindSpin++;
            }

            if (x[s]==ctrl[s]) {
                FindCtrl++;
            }
            if (x[s]==maze[s]) {
                FindMaze++;
            }
        }

        if (FindSpin==4) {
            SpinFunct();
        }

        if (FindCtrl==4) {
            manual_mode() ;
        }

        if (FindMaze==4) {
            led=1;
            wait(4);
            led=0;
        }
    }
}