Vincent Angelino
/
ES305_Lab1
Good
main.cpp@1:68c99ab0c404, 2018-11-14 (annotated)
- Committer:
- Vincent9
- Date:
- Wed Nov 14 05:57:48 2018 +0000
- Revision:
- 1:68c99ab0c404
- Parent:
- 0:bc003175d694
s
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent9 | 0:bc003175d694 | 1 | #include "mbed.h" |
Vincent9 | 0:bc003175d694 | 2 | #include "mbedWSEsbc.h" |
Vincent9 | 0:bc003175d694 | 3 | #define PI (3.14159) |
Vincent9 | 0:bc003175d694 | 4 | |
Vincent9 | 0:bc003175d694 | 5 | // Declare objects (if necessary) |
Vincent9 | 0:bc003175d694 | 6 | Ticker Controller; // declare Ticker object named "Controller" |
Vincent9 | 0:bc003175d694 | 7 | DigitalOut myled(LED1); // LED, flash lights for debugging |
Vincent9 | 0:bc003175d694 | 8 | |
Vincent9 | 0:bc003175d694 | 9 | |
Vincent9 | 0:bc003175d694 | 10 | // variables for data handling and storage |
Vincent9 | 0:bc003175d694 | 11 | float TotalTime; // Total run time |
Vincent9 | 0:bc003175d694 | 12 | float Time; // Current elapsed time |
Vincent9 | 0:bc003175d694 | 13 | float Ts = 0.0083; // Control update period (seconds) (120 Hz equivalent) |
Vincent9 | 0:bc003175d694 | 14 | float Tstrm = 0.01; // Data streaming period (seconds) (100 Hz equivalent) |
Vincent9 | 0:bc003175d694 | 15 | float ang,angp,speed; // variables for approximating speed from encoder measurements |
Vincent9 | 0:bc003175d694 | 16 | float DCAmp; // duty cycle applied to motor |
Vincent9 | 0:bc003175d694 | 17 | float dc; // duty cycle |
Vincent9 | 0:bc003175d694 | 18 | long enc1; // encoder variable |
Vincent9 | 1:68c99ab0c404 | 19 | float lowDC; |
Vincent9 | 0:bc003175d694 | 20 | |
Vincent9 | 0:bc003175d694 | 21 | |
Vincent9 | 0:bc003175d694 | 22 | // Function definition Prototypes (declarations) |
Vincent9 | 1:68c99ab0c404 | 23 | void twoStepCode(); // declare that a separate (other than main) function named "ctrCode" exists |
Vincent9 | 0:bc003175d694 | 24 | |
Vincent9 | 0:bc003175d694 | 25 | // Enter main function |
Vincent9 | 0:bc003175d694 | 26 | int main () |
Vincent9 | 0:bc003175d694 | 27 | { |
Vincent9 | 0:bc003175d694 | 28 | // Initializes mbed to access functionality of encoder, A/D, driver, etc. chipsets |
Vincent9 | 0:bc003175d694 | 29 | // Input is baud rate for PC communication |
Vincent9 | 0:bc003175d694 | 30 | mbedWSEsbcInit(115200); // also initializes timer object t |
Vincent9 | 0:bc003175d694 | 31 | mot_en1.period(0.020); // sets PWM period to 0.02 seconds for best DC motor operation |
Vincent9 | 0:bc003175d694 | 32 | |
Vincent9 | 0:bc003175d694 | 33 | while(1) { |
Vincent9 | 0:bc003175d694 | 34 | // Scan serial port for user input to begin experiment |
Vincent9 | 1:68c99ab0c404 | 35 | pc.scanf("%f,%f",&TotalTime,&lowDC); //&DCAmp,&lowDC); |
Vincent9 | 0:bc003175d694 | 36 | // perform necessary functions to time the experiment |
Vincent9 | 0:bc003175d694 | 37 | Time = 0.0; // reset time variable |
Vincent9 | 0:bc003175d694 | 38 | t.reset(); // reset timer object |
Vincent9 | 0:bc003175d694 | 39 | // Attach the ctrCode function to ticker object specified with period Ts |
Vincent9 | 1:68c99ab0c404 | 40 | Controller.attach(&twoStepCode,Ts); |
Vincent9 | 0:bc003175d694 | 41 | t.start(); // start measuring elapsed time |
Vincent9 | 0:bc003175d694 | 42 | // perform operations while the elapsed time is less than the desired total time |
Vincent9 | 0:bc003175d694 | 43 | while(Time <= TotalTime) { |
Vincent9 | 0:bc003175d694 | 44 | // send data over serial port |
Vincent9 | 0:bc003175d694 | 45 | pc.printf("%f,%f,%f\n",Time,speed,dc); |
Vincent9 | 0:bc003175d694 | 46 | wait(Tstrm); // print data at approximately 50 Hz |
Vincent9 | 0:bc003175d694 | 47 | } // end while(Time<=Ttime) |
Vincent9 | 0:bc003175d694 | 48 | Controller.detach(); // detach ticker to turn off controller |
Vincent9 | 0:bc003175d694 | 49 | // Turn motor off at end of experiment |
Vincent9 | 0:bc003175d694 | 50 | mot_control(1,0.0); |
Vincent9 | 0:bc003175d694 | 51 | }// end while(1) |
Vincent9 | 0:bc003175d694 | 52 | }// end main |
Vincent9 | 0:bc003175d694 | 53 | // Additional function definitions |
Vincent9 | 1:68c99ab0c404 | 54 | void twoStepCode() // function to attach to ticker |
Vincent9 | 0:bc003175d694 | 55 | { |
Vincent9 | 0:bc003175d694 | 56 | myled = !myled; // toggle LED 2 to indicate control update |
Vincent9 | 0:bc003175d694 | 57 | // read current elapsed time |
Vincent9 | 0:bc003175d694 | 58 | Time = t.read(); |
Vincent9 | 0:bc003175d694 | 59 | // Read encoder |
Vincent9 | 0:bc003175d694 | 60 | enc1 = LS7366_read_counter(1); // input is the encoder channel |
Vincent9 | 0:bc003175d694 | 61 | // Convert from counts to radians |
Vincent9 | 1:68c99ab0c404 | 62 | ang = 2.0*PI*enc1/6400.0; |
Vincent9 | 0:bc003175d694 | 63 | // Estimate speed |
Vincent9 | 0:bc003175d694 | 64 | speed = (ang-angp)/Ts; |
Vincent9 | 0:bc003175d694 | 65 | // Age variables |
Vincent9 | 0:bc003175d694 | 66 | angp = ang; |
Vincent9 | 0:bc003175d694 | 67 | // compute duty cycle for motor (will be changed later!) |
Vincent9 | 0:bc003175d694 | 68 | //dc = DCAmp*sin(2.0*PI/5.0*Time); // user-specified duty cycle |
Vincent9 | 0:bc003175d694 | 69 | // Lab3 DC |
Vincent9 | 0:bc003175d694 | 70 | if(Time<0.1) { |
Vincent9 | 0:bc003175d694 | 71 | dc = 0.0; |
Vincent9 | 1:68c99ab0c404 | 72 | } else if(Time<0.55) { |
Vincent9 | 1:68c99ab0c404 | 73 | dc = lowDC; |
Vincent9 | 0:bc003175d694 | 74 | } else { |
Vincent9 | 1:68c99ab0c404 | 75 | dc = 0.10; |
Vincent9 | 0:bc003175d694 | 76 | } |
Vincent9 | 0:bc003175d694 | 77 | // motor control |
Vincent9 | 0:bc003175d694 | 78 | mot_control(1,dc); // first input is the motor channel, second is duty cycle |
Vincent9 | 0:bc003175d694 | 79 | } // end ctrCode() |