Dependencies:   Motor mbed

main.cpp

Committer:
marinacaroline
Date:
2016-03-10
Revision:
1:38c836c4bc0f
Parent:
0:19db2de32450

File content as of revision 1:38c836c4bc0f:

#include "mbed.h"                                         //includes a library of basic c commands
#include "Motor.h"

Serial pc(USBTX,USBRX);                                 //enables Serial connection between the mbed and teraterm
AnalogIn dale(p18);                                     //declares input
Motor RickyBobby(p25,p27,p28);                          //declares output to motor

int go,cnt,pwm;                                         //These two variables are integers
float number,counter,x,mesHeight,desHeight,err,Kp,dutyCyc,intErr,Ki;
int main()
{
    pc.baud(115200);                                    //sets baud rate

    while(1) {                                          //Infinite loop
        cnt=0;
        number=100;

        //these two lines communicate from the mbed to the teraterm window to collect
        //desired values

        pc.printf("Enter one to start, along with the desired height.\r\n");
        pc.scanf("%d,%f",&go,&desHeight);

        while(go==1) {                                  //after the loop has been initialized

            while(cnt<number)                           //this while loop reads the data from the sensor that corresponds to dale
                //and then prints each iteration into the teraterm window
            {
                x=dale.read();
                mesHeight = ((267.3918*x*x*x) + (-431.3231*x*x) + 236.6525*x + -22.2608);
                err = desHeight-mesHeight;
                Kp = 1.8;       
 
/*                
                //This is the Integral Controller
                intErr = err*cnt;
                dutyCyc = Kp*err + Ki*intErr;
                pwm = dutyCyc;
                RickyBobby.speed(dutyCyc);
                wait(.05);
*/

                //This is the Proportional Controller
               
                dutyCyc = (Kp*err);
                RickyBobby.speed(dutyCyc);
                wait(.05);
                pwm = dutyCyc;


                if (pwm > 1) {
                    pwm = 1;
                    //dutyCyc = 1;
                }

                else if (pwm < -1) {
                    pwm = -1;
                    //dutyCyc = -1;
                }


                //pc.printf("mesHeight = %f\r\n", mesHeight);

                //This is the Logic Controller
/*                if (err > 0) {              
                    RickyBobby.speed(1);
                    pwm = 1;
                    wait(.05);
                }
                else if (err < 0) {
                    RickyBobby.speed(-1);
                    pwm = -1;
                    wait(.05);
                }
                else {
                    RickyBobby.speed(0);
                    pwm = 0;
                }
*/
                cnt=cnt+1;
                wait(.05);
                pc.printf("%d,%f,%d\r\n",cnt,mesHeight,pwm);

            }
            RickyBobby.speed(0);
            go = 0;
        }
    }
}