g

Dependencies:   mbed mbedWSEsbc

Committer:
matthewhall115
Date:
Sun Nov 05 19:25:12 2017 +0000
Revision:
0:c5257a56d3c7
nothing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
matthewhall115 0:c5257a56d3c7 1 #include "mbed.h"
matthewhall115 0:c5257a56d3c7 2 #include "mbedWSEsbc.h"
matthewhall115 0:c5257a56d3c7 3 #define PI (3.14159)
matthewhall115 0:c5257a56d3c7 4
matthewhall115 0:c5257a56d3c7 5 //Declare objects (if necessary)
matthewhall115 0:c5257a56d3c7 6 Ticker Controller; //declare Ticker object name "Controller"
matthewhall115 0:c5257a56d3c7 7
matthewhall115 0:c5257a56d3c7 8 //variables for data handling and storage
matthewhall115 0:c5257a56d3c7 9 float TotalTime; //Total run time
matthewhall115 0:c5257a56d3c7 10 float Time; //Current elapsed time
matthewhall115 0:c5257a56d3c7 11 float Ts=0.0083; //Control update period (seconds) (100Hz equivalent)
matthewhall115 0:c5257a56d3c7 12 float Tstrm=0.01; //Data streaming period (seconds) (50Hz equivalent)
matthewhall115 0:c5257a56d3c7 13 float usrDC; //user-specified duty cycle
matthewhall115 0:c5257a56d3c7 14 float speed;
matthewhall115 0:c5257a56d3c7 15 float ang;
matthewhall115 0:c5257a56d3c7 16 float angp;
matthewhall115 0:c5257a56d3c7 17 float dc;
matthewhall115 0:c5257a56d3c7 18 float enc1;
matthewhall115 0:c5257a56d3c7 19
matthewhall115 0:c5257a56d3c7 20
matthewhall115 0:c5257a56d3c7 21
matthewhall115 0:c5257a56d3c7 22
matthewhall115 0:c5257a56d3c7 23 //Function definition Prototypes (declaration)
matthewhall115 0:c5257a56d3c7 24 void ctrCode(); //declare that a separate (other than main) function named "ctrCode" exists
matthewhall115 0:c5257a56d3c7 25
matthewhall115 0:c5257a56d3c7 26 //main function
matthewhall115 0:c5257a56d3c7 27 int main ()
matthewhall115 0:c5257a56d3c7 28 {
matthewhall115 0:c5257a56d3c7 29 //Initialize mbed to access functionality of encoder, A/D, driver, etc. chipsets
matthewhall115 0:c5257a56d3c7 30 //Input is baud rate for PC communication
matthewhall115 0:c5257a56d3c7 31 mbedWSEsbcInit(115200); //also intializes timer oject t
matthewhall115 0:c5257a56d3c7 32 mot_en1.period(0.020); //sets PWM period to 0.02 seconds for best DC motor operation
matthewhall115 0:c5257a56d3c7 33
matthewhall115 0:c5257a56d3c7 34 while(1) {
matthewhall115 0:c5257a56d3c7 35
matthewhall115 0:c5257a56d3c7 36 //Scan serial port for user input to begin experiment
matthewhall115 0:c5257a56d3c7 37 pc.scanf("%f,%f",&TotalTime,&usrDC);
matthewhall115 0:c5257a56d3c7 38 //perform necessary functions to time the experiment
matthewhall115 0:c5257a56d3c7 39 Time=0.0; //reset time variable
matthewhall115 0:c5257a56d3c7 40 t.reset(); // reset time object
matthewhall115 0:c5257a56d3c7 41
matthewhall115 0:c5257a56d3c7 42 //Attached the ctrCode function to ticker oject specified with period Ts
matthewhall115 0:c5257a56d3c7 43 Controller.attach(&ctrCode,Ts);
matthewhall115 0:c5257a56d3c7 44
matthewhall115 0:c5257a56d3c7 45 t.start(); //start measuring elapsed time
matthewhall115 0:c5257a56d3c7 46
matthewhall115 0:c5257a56d3c7 47 //perform operations while the elapsed time is less than the desired total time
matthewhall115 0:c5257a56d3c7 48 while(Time <= TotalTime) {
matthewhall115 0:c5257a56d3c7 49
matthewhall115 0:c5257a56d3c7 50 //read current elapsed time
matthewhall115 0:c5257a56d3c7 51 Time= t.read();
matthewhall115 0:c5257a56d3c7 52
matthewhall115 0:c5257a56d3c7 53 //send data over serial port
matthewhall115 0:c5257a56d3c7 54 pc.printf("%f,%f,%f\n",Time,speed,usrDC);
matthewhall115 0:c5257a56d3c7 55
matthewhall115 0:c5257a56d3c7 56
matthewhall115 0:c5257a56d3c7 57 wait(Tstrm); //print data at approximately 50 Hz
matthewhall115 0:c5257a56d3c7 58
matthewhall115 0:c5257a56d3c7 59 } // end while(Time<=Ttime)
matthewhall115 0:c5257a56d3c7 60
matthewhall115 0:c5257a56d3c7 61 Controller.detach(); // detach ticker to turn off controller
matthewhall115 0:c5257a56d3c7 62 mot_control(1,0.0);//Turn motor off at end of experiment
matthewhall115 0:c5257a56d3c7 63
matthewhall115 0:c5257a56d3c7 64 }//end while(1)
matthewhall115 0:c5257a56d3c7 65 }// end main
matthewhall115 0:c5257a56d3c7 66
matthewhall115 0:c5257a56d3c7 67 //aditional function definitions
matthewhall115 0:c5257a56d3c7 68 void ctrCode() //fuction to attach to ticker
matthewhall115 0:c5257a56d3c7 69 {
matthewhall115 0:c5257a56d3c7 70 enc1=LS7366_read_counter(1);
matthewhall115 0:c5257a56d3c7 71 ang = 2*PI*enc1/6500;//convert to radians
matthewhall115 0:c5257a56d3c7 72 speed = (ang - angp)/Ts;//estimate speed
matthewhall115 0:c5257a56d3c7 73 angp = ang;//age variables
matthewhall115 0:c5257a56d3c7 74 dc = usrDC;//compute duty cycle
matthewhall115 0:c5257a56d3c7 75 mot_control(1,dc);//send duty cycle to moto
matthewhall115 0:c5257a56d3c7 76
matthewhall115 0:c5257a56d3c7 77 }