![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Lab 6 code to read DC motor and ouput time, speed, and duty cycle
main.cpp@0:a047182efa15, 2016-11-07 (annotated)
- Committer:
- m170594
- Date:
- Mon Nov 07 01:52:08 2016 +0000
- Revision:
- 0:a047182efa15
ES305 Lab 6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
m170594 | 0:a047182efa15 | 1 | /************************************************************************************* |
m170594 | 0:a047182efa15 | 2 | Program Name: ES305 Laboratory Experiment 2, Part 1 - mbed serial setup and streaming |
m170594 | 0:a047182efa15 | 3 | Description: A basic code to read an analog channel and print the data to the serial port |
m170594 | 0:a047182efa15 | 4 | |
m170594 | 0:a047182efa15 | 5 | Date: 8/9/2016 |
m170594 | 0:a047182efa15 | 6 | **************************************************************************************/ |
m170594 | 0:a047182efa15 | 7 | |
m170594 | 0:a047182efa15 | 8 | // Include necessary libraries |
m170594 | 0:a047182efa15 | 9 | #include "mbed.h" |
m170594 | 0:a047182efa15 | 10 | #include "mbedWSEsbc.h" |
m170594 | 0:a047182efa15 | 11 | #define PI (3.14159) |
m170594 | 0:a047182efa15 | 12 | |
m170594 | 0:a047182efa15 | 13 | |
m170594 | 0:a047182efa15 | 14 | // Declare necessary objects |
m170594 | 0:a047182efa15 | 15 | DigitalOut myled(LED1); |
m170594 | 0:a047182efa15 | 16 | |
m170594 | 0:a047182efa15 | 17 | |
m170594 | 0:a047182efa15 | 18 | // variables for data handling and storage |
m170594 | 0:a047182efa15 | 19 | float Ts; // Sampling period 1/Ts Hz |
m170594 | 0:a047182efa15 | 20 | float TotalTime; // Total run time |
m170594 | 0:a047182efa15 | 21 | float ang,angp,speed; // variables for approximating speed from encoder measurements |
m170594 | 0:a047182efa15 | 22 | float dc; // duty cycle applied to motor |
m170594 | 0:a047182efa15 | 23 | long enc1; // encoder variable |
m170594 | 0:a047182efa15 | 24 | float Time; // elapsed time |
m170594 | 0:a047182efa15 | 25 | float dt; // desired loop duration |
m170594 | 0:a047182efa15 | 26 | float DC; // duty cycle specified by user |
m170594 | 0:a047182efa15 | 27 | |
m170594 | 0:a047182efa15 | 28 | |
m170594 | 0:a047182efa15 | 29 | |
m170594 | 0:a047182efa15 | 30 | int main () |
m170594 | 0:a047182efa15 | 31 | { |
m170594 | 0:a047182efa15 | 32 | // Initializes mbed to access functionality of encoder, A/D, driver, etc. chipsets |
m170594 | 0:a047182efa15 | 33 | // Input is baud rate for PC communication |
m170594 | 0:a047182efa15 | 34 | mbedWSEsbcInit(115200); // also initializes timer object t |
m170594 | 0:a047182efa15 | 35 | mot_en1.period(0.020); // sets PWM period to 0.02 seconds |
m170594 | 0:a047182efa15 | 36 | |
m170594 | 0:a047182efa15 | 37 | while(1) { |
m170594 | 0:a047182efa15 | 38 | |
m170594 | 0:a047182efa15 | 39 | // Wait for user input to begin loop |
m170594 | 0:a047182efa15 | 40 | pc.scanf("%f,%f,%f",&TotalTime,&Ts,&DC); |
m170594 | 0:a047182efa15 | 41 | |
m170594 | 0:a047182efa15 | 42 | Time = 0.0; // reset time variable |
m170594 | 0:a047182efa15 | 43 | t.reset(); // reset timer object |
m170594 | 0:a047182efa15 | 44 | |
m170594 | 0:a047182efa15 | 45 | |
m170594 | 0:a047182efa15 | 46 | |
m170594 | 0:a047182efa15 | 47 | angp = 0; // initialize previous angle variable to zero |
m170594 | 0:a047182efa15 | 48 | |
m170594 | 0:a047182efa15 | 49 | myled = 1; // turn on light to indicate it's sampling |
m170594 | 0:a047182efa15 | 50 | |
m170594 | 0:a047182efa15 | 51 | while(Time <= TotalTime) { |
m170594 | 0:a047182efa15 | 52 | t.start(); // start measuring comp time |
m170594 | 0:a047182efa15 | 53 | |
m170594 | 0:a047182efa15 | 54 | // Read encoder |
m170594 | 0:a047182efa15 | 55 | enc1 = LS7366_read_counter(1); // input is the encoder channel |
m170594 | 0:a047182efa15 | 56 | pc.printf("counts = %f\n\r",enc1); |
m170594 | 0:a047182efa15 | 57 | |
m170594 | 0:a047182efa15 | 58 | // Convert from counts to radians |
m170594 | 0:a047182efa15 | 59 | ang = 2*PI*enc1/6500.0; |
m170594 | 0:a047182efa15 | 60 | |
m170594 | 0:a047182efa15 | 61 | // Estimate speed |
m170594 | 0:a047182efa15 | 62 | speed = (ang-angp)/Ts; |
m170594 | 0:a047182efa15 | 63 | |
m170594 | 0:a047182efa15 | 64 | // Age variables |
m170594 | 0:a047182efa15 | 65 | angp = ang; |
m170594 | 0:a047182efa15 | 66 | |
m170594 | 0:a047182efa15 | 67 | // compute duty cycle for motor using proportional control scheme |
m170594 | 0:a047182efa15 | 68 | if(Time<0.1) { |
m170594 | 0:a047182efa15 | 69 | dc = 0.0; |
m170594 | 0:a047182efa15 | 70 | } else { |
m170594 | 0:a047182efa15 | 71 | dc = DC; |
m170594 | 0:a047182efa15 | 72 | } |
m170594 | 0:a047182efa15 | 73 | |
m170594 | 0:a047182efa15 | 74 | // motor control |
m170594 | 0:a047182efa15 | 75 | mot_control(1,dc); // first input is the motor channel, second is duty cycle |
m170594 | 0:a047182efa15 | 76 | |
m170594 | 0:a047182efa15 | 77 | |
m170594 | 0:a047182efa15 | 78 | pc.printf("%f,%f,%f\n\r",Time,speed,dc); |
m170594 | 0:a047182efa15 | 79 | |
m170594 | 0:a047182efa15 | 80 | |
m170594 | 0:a047182efa15 | 81 | Time = Time + Ts; // update time |
m170594 | 0:a047182efa15 | 82 | |
m170594 | 0:a047182efa15 | 83 | // wait to achieve sampling rate |
m170594 | 0:a047182efa15 | 84 | dt = Ts - t.read(); |
m170594 | 0:a047182efa15 | 85 | if(dt<0.0) { |
m170594 | 0:a047182efa15 | 86 | dt = 0.0; |
m170594 | 0:a047182efa15 | 87 | } |
m170594 | 0:a047182efa15 | 88 | wait(dt); |
m170594 | 0:a047182efa15 | 89 | |
m170594 | 0:a047182efa15 | 90 | t.reset(); |
m170594 | 0:a047182efa15 | 91 | |
m170594 | 0:a047182efa15 | 92 | |
m170594 | 0:a047182efa15 | 93 | } // end while(Time<=Ttime) |
m170594 | 0:a047182efa15 | 94 | mot_control(1,0.0); // turn motor off at end of test |
m170594 | 0:a047182efa15 | 95 | myled = 0; // turn off light to indicate it has finished |
m170594 | 0:a047182efa15 | 96 | |
m170594 | 0:a047182efa15 | 97 | }// end while(repeat==1) |
m170594 | 0:a047182efa15 | 98 | }// end main |