Team Ascent / Mbed 2 deprecated TDPCode1

Dependencies:   mbed

main.cpp

Committer:
Dbee16
Date:
2015-02-18
Revision:
1:2bab3a0bc3bc
Parent:
0:59d25bb7f825
Child:
2:1feae3cb6731

File content as of revision 1:2bab3a0bc3bc:

#include "mbed.h"

//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 led(LED_RED);

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

                 set_directin(1, 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(0, 0.3, 0.7); //robot back, 100% rightmotor; 0%left (technically -40%, but 0% is min)

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

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

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

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

    */
    switch(direction) {
        case '1': { //forward
            motor_r.write(speed + angle); //where angle can be postive and negative and will TURN the robot
            motor_l.write(speed - angle);
            //if turning left, you'd make the angle postive (right motor goes faster left motor goes slwoer)
            motor_rf=1;
            motor_rb=0;
            motor_lf=1;
            motor_lb=0;
        }
        case '0': { //backward
            motor_r.write(speed + angle); //where angle can be postive and negative and will TURN the robot
            motor_l.write(speed - angle);
            //if turning left, you'd make the angle postive (right motor goes faster left motor goes slwoer)
            motor_rf=0;
            motor_rb=1;
            motor_lf=0;
            motor_lb=1;
        }
    }
}

int main()
{

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



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

        wait(2);
        led = !led;
        set_direction(1, 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(0, 0.3, 0.7); //robot back, 100% rightmotor; 0%left (technically -40%, but 0% is min)

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

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

        wait(2);
        led = !led;
        set_direction(1, 0.3, 0.5); //robot pivots forard, to the right , at 80% speed (80% right; 0% left)

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


    /* working demo

    while(1) { //infinite loop
        //***********************************************
        myled = !myled;

        //Set duty cycle at 100% AKA Full Speed
        motor_r.write(1.0);
        motor_l.write(1.0);

        motor_rf=1;
        motor_rb=0;
        motor_lf=1;
        motor_lb=0; //go forwards
        wait(2);
        //**************************************
        myled = !myled;

        motor_r.write(0.75);
        motor_l.write(0.75);

        motor_rf=1;
        motor_rb=0;
        motor_lf=1;
        motor_lb=0; //go forwards
        //
        wait(2);

        myled = !myled;

        motor_r.write(0.5);
        motor_l.write(0.5);

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


        wait(2);
        myled = !myled;

        motor_r.write(0.30);
        motor_l.write(0.30);

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

        wait(2);

        myled = !myled;
        motor_r.write(1.0);
        motor_l.write(1.0);

        motor_rf=0;
        motor_rb=1;
        motor_lf=0;
        motor_lb=1; //go forwards
        //
        wait(2);



        myled = !myled;
        motor_r.write(0.5);
        motor_l.write(0.5);

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

        wait(2);
        myled = !myled;

        motor_rf=0;
        motor_rb=1;
        motor_lf=0;
        motor_lb=0; //reverse turn

        wait(2);

        myled = !myled;
        motor_r.write(0.5);
        motor_l.write(1.0);

        motor_rf=1;
        motor_rb=0;
        motor_lf=1;
        motor_lb=0; //go forwards
        //
        wait(4);
    }
        */


}