![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
non
main.cpp@0:2c721947a97e, 2017-11-05 (annotated)
- Committer:
- matthewhall115
- Date:
- Sun Nov 05 19:27:33 2017 +0000
- Revision:
- 0:2c721947a97e
none
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
matthewhall115 | 0:2c721947a97e | 1 | #include "mbed.h" |
matthewhall115 | 0:2c721947a97e | 2 | #include "mbedWSEsbc.h" |
matthewhall115 | 0:2c721947a97e | 3 | #define PI (3.14159) |
matthewhall115 | 0:2c721947a97e | 4 | |
matthewhall115 | 0:2c721947a97e | 5 | |
matthewhall115 | 0:2c721947a97e | 6 | // Declare objects (if necessary) |
matthewhall115 | 0:2c721947a97e | 7 | Ticker Controller; // declare Ticker object named "Controller" |
matthewhall115 | 0:2c721947a97e | 8 | DigitalOut myled(LED1); // LED, flash lights for debugging |
matthewhall115 | 0:2c721947a97e | 9 | |
matthewhall115 | 0:2c721947a97e | 10 | |
matthewhall115 | 0:2c721947a97e | 11 | // variables for data handling and storage |
matthewhall115 | 0:2c721947a97e | 12 | float TotalTime; // Total run time |
matthewhall115 | 0:2c721947a97e | 13 | float Time; // Current elapsed time |
matthewhall115 | 0:2c721947a97e | 14 | float Ts = 0.0083; // Control update period (seconds) (100 Hz equivalent) |
matthewhall115 | 0:2c721947a97e | 15 | float Tstrm = 0.01; // Data streaming period (seconds) (50 Hz equivalent) |
matthewhall115 | 0:2c721947a97e | 16 | float usrDC; // uaser-specified duty cycle |
matthewhall115 | 0:2c721947a97e | 17 | float ang,angp,speed; // variables for approximating speed from encoder measurements |
matthewhall115 | 0:2c721947a97e | 18 | float dc; // duty cycle applied to motor |
matthewhall115 | 0:2c721947a97e | 19 | long enc1; // encoder variable |
matthewhall115 | 0:2c721947a97e | 20 | float lowDC; |
matthewhall115 | 0:2c721947a97e | 21 | |
matthewhall115 | 0:2c721947a97e | 22 | |
matthewhall115 | 0:2c721947a97e | 23 | // Function definition Prototypes (declarations) |
matthewhall115 | 0:2c721947a97e | 24 | void ctrCode(); // declare that a separate (other than main) function named "ctrCode" exists |
matthewhall115 | 0:2c721947a97e | 25 | void twoStepCode(); //Two Step input |
matthewhall115 | 0:2c721947a97e | 26 | |
matthewhall115 | 0:2c721947a97e | 27 | |
matthewhall115 | 0:2c721947a97e | 28 | // Enter main function |
matthewhall115 | 0:2c721947a97e | 29 | int main () |
matthewhall115 | 0:2c721947a97e | 30 | { |
matthewhall115 | 0:2c721947a97e | 31 | // Initializes mbed to access functionality of encoder, A/D, driver, etc. chipsets |
matthewhall115 | 0:2c721947a97e | 32 | // Input is baud rate for PC communication |
matthewhall115 | 0:2c721947a97e | 33 | mbedWSEsbcInit(115200); // also initializes timer object t |
matthewhall115 | 0:2c721947a97e | 34 | mot_en1.period(0.020); // sets PWM period to 0.02 seconds for best DC motor operation |
matthewhall115 | 0:2c721947a97e | 35 | |
matthewhall115 | 0:2c721947a97e | 36 | while(1) { |
matthewhall115 | 0:2c721947a97e | 37 | |
matthewhall115 | 0:2c721947a97e | 38 | // Scan serial port for user input to begin experiment |
matthewhall115 | 0:2c721947a97e | 39 | pc.scanf("%f,%f",&TotalTime,&lowDC); |
matthewhall115 | 0:2c721947a97e | 40 | |
matthewhall115 | 0:2c721947a97e | 41 | // perform necessary functions to time the experiment |
matthewhall115 | 0:2c721947a97e | 42 | Time = 0.0; // reset time variable |
matthewhall115 | 0:2c721947a97e | 43 | t.reset(); // reset timer object |
matthewhall115 | 0:2c721947a97e | 44 | |
matthewhall115 | 0:2c721947a97e | 45 | // Attach the ctrCode function to ticker object specified with period Ts |
matthewhall115 | 0:2c721947a97e | 46 | Controller.attach(&twoStepCode,Ts); |
matthewhall115 | 0:2c721947a97e | 47 | |
matthewhall115 | 0:2c721947a97e | 48 | t.start(); // start measuring elapsed time |
matthewhall115 | 0:2c721947a97e | 49 | |
matthewhall115 | 0:2c721947a97e | 50 | // perform operations while the elapsed time is less than the desired total time |
matthewhall115 | 0:2c721947a97e | 51 | while(Time <= TotalTime) { |
matthewhall115 | 0:2c721947a97e | 52 | |
matthewhall115 | 0:2c721947a97e | 53 | |
matthewhall115 | 0:2c721947a97e | 54 | |
matthewhall115 | 0:2c721947a97e | 55 | // read current elapsed time |
matthewhall115 | 0:2c721947a97e | 56 | Time = t.read(); |
matthewhall115 | 0:2c721947a97e | 57 | |
matthewhall115 | 0:2c721947a97e | 58 | |
matthewhall115 | 0:2c721947a97e | 59 | // send data over serial port |
matthewhall115 | 0:2c721947a97e | 60 | pc.printf("%f,%f,%f\n",Time,speed,dc); |
matthewhall115 | 0:2c721947a97e | 61 | |
matthewhall115 | 0:2c721947a97e | 62 | |
matthewhall115 | 0:2c721947a97e | 63 | wait(Tstrm); // print data at approximately 50 Hz |
matthewhall115 | 0:2c721947a97e | 64 | |
matthewhall115 | 0:2c721947a97e | 65 | |
matthewhall115 | 0:2c721947a97e | 66 | |
matthewhall115 | 0:2c721947a97e | 67 | |
matthewhall115 | 0:2c721947a97e | 68 | } // end while(Time<=Ttime) |
matthewhall115 | 0:2c721947a97e | 69 | |
matthewhall115 | 0:2c721947a97e | 70 | Controller.detach(); // detach ticker to turn off controller |
matthewhall115 | 0:2c721947a97e | 71 | // Turn motor off at end of experiment |
matthewhall115 | 0:2c721947a97e | 72 | mot_control(1,0.0); |
matthewhall115 | 0:2c721947a97e | 73 | |
matthewhall115 | 0:2c721947a97e | 74 | }// end while(1) |
matthewhall115 | 0:2c721947a97e | 75 | }// end main |
matthewhall115 | 0:2c721947a97e | 76 | |
matthewhall115 | 0:2c721947a97e | 77 | |
matthewhall115 | 0:2c721947a97e | 78 | |
matthewhall115 | 0:2c721947a97e | 79 | // Additional function definitions |
matthewhall115 | 0:2c721947a97e | 80 | void ctrCode() // function to attach to ticker |
matthewhall115 | 0:2c721947a97e | 81 | { |
matthewhall115 | 0:2c721947a97e | 82 | |
matthewhall115 | 0:2c721947a97e | 83 | myled = !myled; // toggle LED 2 to indicate control update |
matthewhall115 | 0:2c721947a97e | 84 | |
matthewhall115 | 0:2c721947a97e | 85 | // Read encoder |
matthewhall115 | 0:2c721947a97e | 86 | enc1 = LS7366_read_counter(1); // input is the encoder channel |
matthewhall115 | 0:2c721947a97e | 87 | |
matthewhall115 | 0:2c721947a97e | 88 | // Convert from counts to radians |
matthewhall115 | 0:2c721947a97e | 89 | ang = 2.0*PI*enc1/6400.0; |
matthewhall115 | 0:2c721947a97e | 90 | |
matthewhall115 | 0:2c721947a97e | 91 | // Estimate speed |
matthewhall115 | 0:2c721947a97e | 92 | speed = (ang-angp)/Ts; |
matthewhall115 | 0:2c721947a97e | 93 | |
matthewhall115 | 0:2c721947a97e | 94 | // Age variables |
matthewhall115 | 0:2c721947a97e | 95 | angp = ang; |
matthewhall115 | 0:2c721947a97e | 96 | |
matthewhall115 | 0:2c721947a97e | 97 | // compute duty cycle for motor (will be changed later!) |
matthewhall115 | 0:2c721947a97e | 98 | dc = usrDC; // user-specified duty cycle |
matthewhall115 | 0:2c721947a97e | 99 | |
matthewhall115 | 0:2c721947a97e | 100 | // motor control |
matthewhall115 | 0:2c721947a97e | 101 | mot_control(1,dc); // first input is the motor channel, second is duty cycle |
matthewhall115 | 0:2c721947a97e | 102 | |
matthewhall115 | 0:2c721947a97e | 103 | } // end ctrCode() |
matthewhall115 | 0:2c721947a97e | 104 | |
matthewhall115 | 0:2c721947a97e | 105 | |
matthewhall115 | 0:2c721947a97e | 106 | |
matthewhall115 | 0:2c721947a97e | 107 | |
matthewhall115 | 0:2c721947a97e | 108 | // Additional function definitions |
matthewhall115 | 0:2c721947a97e | 109 | void twoStepCode() // function to attach to ticker |
matthewhall115 | 0:2c721947a97e | 110 | { |
matthewhall115 | 0:2c721947a97e | 111 | myled = !myled; // toggle LED 2 to indicate control update |
matthewhall115 | 0:2c721947a97e | 112 | |
matthewhall115 | 0:2c721947a97e | 113 | // Read encoder |
matthewhall115 | 0:2c721947a97e | 114 | enc1 = LS7366_read_counter(1); // input is the encoder channel |
matthewhall115 | 0:2c721947a97e | 115 | |
matthewhall115 | 0:2c721947a97e | 116 | // Convert from counts to radians |
matthewhall115 | 0:2c721947a97e | 117 | ang = 2.0*PI*enc1/6400.0; |
matthewhall115 | 0:2c721947a97e | 118 | |
matthewhall115 | 0:2c721947a97e | 119 | // Estimate speed |
matthewhall115 | 0:2c721947a97e | 120 | speed = (ang-angp)/Ts; |
matthewhall115 | 0:2c721947a97e | 121 | |
matthewhall115 | 0:2c721947a97e | 122 | // Age variables |
matthewhall115 | 0:2c721947a97e | 123 | angp = ang; |
matthewhall115 | 0:2c721947a97e | 124 | |
matthewhall115 | 0:2c721947a97e | 125 | // compute duty cycle for motor over two step inputs |
matthewhall115 | 0:2c721947a97e | 126 | if(Time<0.1) { |
matthewhall115 | 0:2c721947a97e | 127 | dc = 0.0; |
matthewhall115 | 0:2c721947a97e | 128 | } else if(Time<0.55) { |
matthewhall115 | 0:2c721947a97e | 129 | dc = lowDC; // low duty cycle |
matthewhall115 | 0:2c721947a97e | 130 | } else { |
matthewhall115 | 0:2c721947a97e | 131 | dc = 0.10; |
matthewhall115 | 0:2c721947a97e | 132 | } |
matthewhall115 | 0:2c721947a97e | 133 | // motor control |
matthewhall115 | 0:2c721947a97e | 134 | mot_control(1,dc); // first input is the motor channel, second is duty cycle |
matthewhall115 | 0:2c721947a97e | 135 | } // end twoStepCode() |
matthewhall115 | 0:2c721947a97e | 136 | |
matthewhall115 | 0:2c721947a97e | 137 |