Vincent Angelino
/
ES305_Lab1
Good
main.cpp@0:bc003175d694, 2018-10-02 (annotated)
- Committer:
- Vincent9
- Date:
- Tue Oct 02 16:17:06 2018 +0000
- Revision:
- 0:bc003175d694
- Child:
- 1:68c99ab0c404
Yes
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 | 0:bc003175d694 | 19 | |
Vincent9 | 0:bc003175d694 | 20 | |
Vincent9 | 0:bc003175d694 | 21 | |
Vincent9 | 0:bc003175d694 | 22 | // Function definition Prototypes (declarations) |
Vincent9 | 0:bc003175d694 | 23 | void ctrCode(); // declare that a separate (other than main) function named "ctrCode" exists |
Vincent9 | 0:bc003175d694 | 24 | |
Vincent9 | 0:bc003175d694 | 25 | |
Vincent9 | 0:bc003175d694 | 26 | |
Vincent9 | 0:bc003175d694 | 27 | // Enter main function |
Vincent9 | 0:bc003175d694 | 28 | int main () |
Vincent9 | 0:bc003175d694 | 29 | { |
Vincent9 | 0:bc003175d694 | 30 | // Initializes mbed to access functionality of encoder, A/D, driver, etc. chipsets |
Vincent9 | 0:bc003175d694 | 31 | // Input is baud rate for PC communication |
Vincent9 | 0:bc003175d694 | 32 | mbedWSEsbcInit(115200); // also initializes timer object t |
Vincent9 | 0:bc003175d694 | 33 | mot_en1.period(0.020); // sets PWM period to 0.02 seconds for best DC motor operation |
Vincent9 | 0:bc003175d694 | 34 | |
Vincent9 | 0:bc003175d694 | 35 | while(1) { |
Vincent9 | 0:bc003175d694 | 36 | |
Vincent9 | 0:bc003175d694 | 37 | // Scan serial port for user input to begin experiment |
Vincent9 | 0:bc003175d694 | 38 | pc.scanf("%f,%f",&TotalTime,&DCAmp); |
Vincent9 | 0:bc003175d694 | 39 | |
Vincent9 | 0:bc003175d694 | 40 | // perform necessary functions to time the experiment |
Vincent9 | 0:bc003175d694 | 41 | Time = 0.0; // reset time variable |
Vincent9 | 0:bc003175d694 | 42 | t.reset(); // reset timer object |
Vincent9 | 0:bc003175d694 | 43 | |
Vincent9 | 0:bc003175d694 | 44 | // Attach the ctrCode function to ticker object specified with period Ts |
Vincent9 | 0:bc003175d694 | 45 | Controller.attach(&ctrCode,Ts); |
Vincent9 | 0:bc003175d694 | 46 | |
Vincent9 | 0:bc003175d694 | 47 | t.start(); // start measuring elapsed time |
Vincent9 | 0:bc003175d694 | 48 | |
Vincent9 | 0:bc003175d694 | 49 | // perform operations while the elapsed time is less than the desired total time |
Vincent9 | 0:bc003175d694 | 50 | while(Time <= TotalTime) { |
Vincent9 | 0:bc003175d694 | 51 | |
Vincent9 | 0:bc003175d694 | 52 | // send data over serial port |
Vincent9 | 0:bc003175d694 | 53 | pc.printf("%f,%f,%f\n",Time,speed,dc); |
Vincent9 | 0:bc003175d694 | 54 | |
Vincent9 | 0:bc003175d694 | 55 | |
Vincent9 | 0:bc003175d694 | 56 | wait(Tstrm); // print data at approximately 50 Hz |
Vincent9 | 0:bc003175d694 | 57 | |
Vincent9 | 0:bc003175d694 | 58 | |
Vincent9 | 0:bc003175d694 | 59 | |
Vincent9 | 0:bc003175d694 | 60 | |
Vincent9 | 0:bc003175d694 | 61 | } // end while(Time<=Ttime) |
Vincent9 | 0:bc003175d694 | 62 | |
Vincent9 | 0:bc003175d694 | 63 | Controller.detach(); // detach ticker to turn off controller |
Vincent9 | 0:bc003175d694 | 64 | // Turn motor off at end of experiment |
Vincent9 | 0:bc003175d694 | 65 | mot_control(1,0.0); |
Vincent9 | 0:bc003175d694 | 66 | |
Vincent9 | 0:bc003175d694 | 67 | }// end while(1) |
Vincent9 | 0:bc003175d694 | 68 | }// end main |
Vincent9 | 0:bc003175d694 | 69 | |
Vincent9 | 0:bc003175d694 | 70 | |
Vincent9 | 0:bc003175d694 | 71 | |
Vincent9 | 0:bc003175d694 | 72 | // Additional function definitions |
Vincent9 | 0:bc003175d694 | 73 | void ctrCode() // function to attach to ticker |
Vincent9 | 0:bc003175d694 | 74 | { |
Vincent9 | 0:bc003175d694 | 75 | |
Vincent9 | 0:bc003175d694 | 76 | myled = !myled; // toggle LED 2 to indicate control update |
Vincent9 | 0:bc003175d694 | 77 | |
Vincent9 | 0:bc003175d694 | 78 | // read current elapsed time |
Vincent9 | 0:bc003175d694 | 79 | Time = t.read(); |
Vincent9 | 0:bc003175d694 | 80 | |
Vincent9 | 0:bc003175d694 | 81 | // Read encoder |
Vincent9 | 0:bc003175d694 | 82 | enc1 = LS7366_read_counter(1); // input is the encoder channel |
Vincent9 | 0:bc003175d694 | 83 | |
Vincent9 | 0:bc003175d694 | 84 | // Convert from counts to radians |
Vincent9 | 0:bc003175d694 | 85 | ang = 2.0*PI*enc1/6500.0; |
Vincent9 | 0:bc003175d694 | 86 | |
Vincent9 | 0:bc003175d694 | 87 | // Estimate speed |
Vincent9 | 0:bc003175d694 | 88 | speed = (ang-angp)/Ts; |
Vincent9 | 0:bc003175d694 | 89 | |
Vincent9 | 0:bc003175d694 | 90 | // Age variables |
Vincent9 | 0:bc003175d694 | 91 | angp = ang; |
Vincent9 | 0:bc003175d694 | 92 | |
Vincent9 | 0:bc003175d694 | 93 | // compute duty cycle for motor (will be changed later!) |
Vincent9 | 0:bc003175d694 | 94 | //dc = DCAmp*sin(2.0*PI/5.0*Time); // user-specified duty cycle |
Vincent9 | 0:bc003175d694 | 95 | // Lab3 DC |
Vincent9 | 0:bc003175d694 | 96 | if(Time<0.1) { |
Vincent9 | 0:bc003175d694 | 97 | dc = 0.0; |
Vincent9 | 0:bc003175d694 | 98 | } else { |
Vincent9 | 0:bc003175d694 | 99 | dc = DCAmp; |
Vincent9 | 0:bc003175d694 | 100 | } |
Vincent9 | 0:bc003175d694 | 101 | |
Vincent9 | 0:bc003175d694 | 102 | // motor control |
Vincent9 | 0:bc003175d694 | 103 | mot_control(1,dc); // first input is the motor channel, second is duty cycle |
Vincent9 | 0:bc003175d694 | 104 | |
Vincent9 | 0:bc003175d694 | 105 | } // end ctrCode() |