![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
a
main.cpp@0:980f29af9da6, 2018-11-14 (annotated)
- Committer:
- Vincent9
- Date:
- Wed Nov 14 05:55:46 2018 +0000
- Revision:
- 0:980f29af9da6
- Child:
- 1:da8fb81386ab
A
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent9 | 0:980f29af9da6 | 1 | #include "mbed.h" |
Vincent9 | 0:980f29af9da6 | 2 | #include "mbedWSEsbc.h" |
Vincent9 | 0:980f29af9da6 | 3 | #define PI (3.14159) |
Vincent9 | 0:980f29af9da6 | 4 | |
Vincent9 | 0:980f29af9da6 | 5 | float trt; // Total run time |
Vincent9 | 0:980f29af9da6 | 6 | float cet; // Current elapsed time |
Vincent9 | 0:980f29af9da6 | 7 | float updateperiod = 0.0083; // Control update period (seconds) (120 Hz equivalent) |
Vincent9 | 0:980f29af9da6 | 8 | float dsp = 0.01; // Data streaming period (seconds) (100 Hz equivalent) |
Vincent9 | 0:980f29af9da6 | 9 | //float ang,angp,speed; // variables for approximating speed from encoder measurements |
Vincent9 | 0:980f29af9da6 | 10 | //float DCAmp; // duty cycle applied to motor |
Vincent9 | 0:980f29af9da6 | 11 | float dc; // duty cycle |
Vincent9 | 0:980f29af9da6 | 12 | //long enc1; // encoder variable |
Vincent9 | 0:980f29af9da6 | 13 | float parameter; |
Vincent9 | 0:980f29af9da6 | 14 | float speed; |
Vincent9 | 0:980f29af9da6 | 15 | float DCAmp; |
Vincent9 | 0:980f29af9da6 | 16 | float measurement; |
Vincent9 | 0:980f29af9da6 | 17 | float rad; |
Vincent9 | 0:980f29af9da6 | 18 | float rad_old; |
Vincent9 | 0:980f29af9da6 | 19 | |
Vincent9 | 0:980f29af9da6 | 20 | Ticker Controller; |
Vincent9 | 0:980f29af9da6 | 21 | |
Vincent9 | 0:980f29af9da6 | 22 | void ctrCode (); |
Vincent9 | 0:980f29af9da6 | 23 | void StepCode(); |
Vincent9 | 0:980f29af9da6 | 24 | |
Vincent9 | 0:980f29af9da6 | 25 | int main() { |
Vincent9 | 0:980f29af9da6 | 26 | mbedWSEsbcInit(115200); |
Vincent9 | 0:980f29af9da6 | 27 | mot_en1.period(.020); |
Vincent9 | 0:980f29af9da6 | 28 | |
Vincent9 | 0:980f29af9da6 | 29 | while(1) { |
Vincent9 | 0:980f29af9da6 | 30 | pc.scanf("%f,%f",&trt,&DCAmp); |
Vincent9 | 0:980f29af9da6 | 31 | cet = 0; |
Vincent9 | 0:980f29af9da6 | 32 | t.reset(); |
Vincent9 | 0:980f29af9da6 | 33 | Controller.attach(&StepCode,updateperiod); |
Vincent9 | 0:980f29af9da6 | 34 | t.start(); |
Vincent9 | 0:980f29af9da6 | 35 | while (cet <= trt){ |
Vincent9 | 0:980f29af9da6 | 36 | pc.printf("%f,%f,%f\n",cet,speed,dc); |
Vincent9 | 0:980f29af9da6 | 37 | wait(dsp); |
Vincent9 | 0:980f29af9da6 | 38 | |
Vincent9 | 0:980f29af9da6 | 39 | } |
Vincent9 | 0:980f29af9da6 | 40 | Controller.detach(); |
Vincent9 | 0:980f29af9da6 | 41 | mot_control(1,0.0); |
Vincent9 | 0:980f29af9da6 | 42 | } |
Vincent9 | 0:980f29af9da6 | 43 | } |
Vincent9 | 0:980f29af9da6 | 44 | void ctrCode() { |
Vincent9 | 0:980f29af9da6 | 45 | cet=t.read(); |
Vincent9 | 0:980f29af9da6 | 46 | measurement= LS7366_read_counter(1); |
Vincent9 | 0:980f29af9da6 | 47 | rad = (2.0*PI)/6500*measurement; |
Vincent9 | 0:980f29af9da6 | 48 | speed = (rad - rad_old)/updateperiod; |
Vincent9 | 0:980f29af9da6 | 49 | rad_old = rad; |
Vincent9 | 0:980f29af9da6 | 50 | |
Vincent9 | 0:980f29af9da6 | 51 | dc = SineAmp*sin(2.0*PI*cet/5.0); |
Vincent9 | 0:980f29af9da6 | 52 | mot_control(1,dc); |
Vincent9 | 0:980f29af9da6 | 53 | } |
Vincent9 | 0:980f29af9da6 | 54 | |
Vincent9 | 0:980f29af9da6 | 55 | void StepCode() { |
Vincent9 | 0:980f29af9da6 | 56 | cet=t.read(); |
Vincent9 | 0:980f29af9da6 | 57 | measurement = LS7366_read_counter(1); |
Vincent9 | 0:980f29af9da6 | 58 | rad = (2.0*PI)/6500*measurement; |
Vincent9 | 0:980f29af9da6 | 59 | speed = (rad - rad_old)/updateperiod; |
Vincent9 | 0:980f29af9da6 | 60 | rad_old = rad; |
Vincent9 | 0:980f29af9da6 | 61 | |
Vincent9 | 0:980f29af9da6 | 62 | if(cet<0.1){ |
Vincent9 | 0:980f29af9da6 | 63 | dc = 0.0; |
Vincent9 | 0:980f29af9da6 | 64 | } else{ |
Vincent9 | 0:980f29af9da6 | 65 | dc = SineAmp; |
Vincent9 | 0:980f29af9da6 | 66 | } |
Vincent9 | 0:980f29af9da6 | 67 | mot_control(1,dc); |
Vincent9 | 0:980f29af9da6 | 68 | } |
Vincent9 | 0:980f29af9da6 | 69 | |
Vincent9 | 0:980f29af9da6 | 70 |