g

Dependencies:   mbed mbedWSEsbc

main.cpp

Committer:
matthewhall115
Date:
2017-11-05
Revision:
0:c5257a56d3c7

File content as of revision 0:c5257a56d3c7:

#include "mbed.h"
#include "mbedWSEsbc.h"
#define PI (3.14159)

//Declare objects (if necessary)
Ticker Controller; //declare Ticker object name "Controller"

//variables for data handling and storage
float TotalTime;   //Total run time
float Time;   //Current elapsed time
float Ts=0.0083; //Control update period (seconds) (100Hz equivalent)
float Tstrm=0.01; //Data streaming period (seconds) (50Hz equivalent)
float usrDC;  //user-specified duty cycle
float speed;
float ang;
float angp;
float dc;
float enc1;




//Function definition Prototypes (declaration)
void ctrCode(); //declare that a separate (other than main) function named "ctrCode" exists

//main function
int main ()
{
    //Initialize mbed to access functionality of encoder, A/D, driver, etc. chipsets
    //Input is baud rate for PC communication
    mbedWSEsbcInit(115200); //also intializes timer oject t
    mot_en1.period(0.020); //sets PWM period to 0.02 seconds for best DC motor operation

    while(1) {

        //Scan serial port for user input to begin experiment
        pc.scanf("%f,%f",&TotalTime,&usrDC);
        //perform necessary functions to time the experiment
        Time=0.0; //reset time variable
        t.reset(); // reset time object

//Attached the ctrCode function to ticker oject specified with period Ts
        Controller.attach(&ctrCode,Ts);

        t.start(); //start measuring elapsed time

        //perform operations while the elapsed time is less than the desired total time
        while(Time <= TotalTime) {

            //read current elapsed time
            Time= t.read();

            //send data over serial port
            pc.printf("%f,%f,%f\n",Time,speed,usrDC);


            wait(Tstrm); //print data at approximately 50 Hz

        } // end while(Time<=Ttime)

        Controller.detach(); // detach ticker to turn off controller
        mot_control(1,0.0);//Turn motor off at end of experiment

    }//end while(1)
}// end main

//aditional function definitions
void ctrCode() //fuction to attach to ticker
{
    enc1=LS7366_read_counter(1);
    ang = 2*PI*enc1/6500;//convert to radians
    speed = (ang - angp)/Ts;//estimate speed
    angp = ang;//age variables
    dc = usrDC;//compute duty cycle
    mot_control(1,dc);//send duty cycle to moto

}