Tony Lin
/
BX-car_s
share
Fork of BX-car_2 by
motor_api.cpp@10:9f0ce6ba7663, 2014-06-26 (annotated)
- Committer:
- TonyLin
- Date:
- Thu Jun 26 14:29:53 2014 +0000
- Revision:
- 10:9f0ce6ba7663
- Parent:
- 7:fd976e1ced33
ok;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
backman | 0:68c173249c01 | 1 | #include "mbed.h" |
backman | 0:68c173249c01 | 2 | #include "motor_api.h" |
backman | 0:68c173249c01 | 3 | #define init_v 10 // period in 1.35 ~170ms |
backman | 0:68c173249c01 | 4 | |
TonyLin | 10:9f0ce6ba7663 | 5 | BX_motor::BX_motor(char type){ |
TonyLin | 10:9f0ce6ba7663 | 6 | //need N level???? |
backman | 0:68c173249c01 | 7 | engine_enable = new DigitalOut(PTE21); |
TonyLin | 10:9f0ce6ba7663 | 8 | *engine_enable=1; |
backman | 0:68c173249c01 | 9 | Type=type; |
backman | 0:68c173249c01 | 10 | switch (type){ |
backman | 0:68c173249c01 | 11 | case 'A': |
TonyLin | 10:9f0ce6ba7663 | 12 | forward_A = new PwmOut(PTC3); |
TonyLin | 10:9f0ce6ba7663 | 13 | backward_A = new PwmOut(PTC4); |
backman | 0:68c173249c01 | 14 | |
backman | 0:68c173249c01 | 15 | forward_A->period_ms(init_v); |
backman | 0:68c173249c01 | 16 | backward_A->period_ms(init_v); |
TonyLin | 10:9f0ce6ba7663 | 17 | *forward_A=0.0f; |
TonyLin | 10:9f0ce6ba7663 | 18 | *backward_A=0.0f; |
TonyLin | 10:9f0ce6ba7663 | 19 | break; |
backman | 0:68c173249c01 | 20 | |
TonyLin | 10:9f0ce6ba7663 | 21 | case 'B': |
TonyLin | 10:9f0ce6ba7663 | 22 | forward_B = new PwmOut(PTE29); |
TonyLin | 10:9f0ce6ba7663 | 23 | backward_B = new PwmOut(PTE23); |
backman | 0:68c173249c01 | 24 | forward_B->period_ms(init_v); |
backman | 0:68c173249c01 | 25 | backward_B->period_ms(init_v); |
TonyLin | 10:9f0ce6ba7663 | 26 | *forward_B=0.0f; |
TonyLin | 10:9f0ce6ba7663 | 27 | *backward_B=0.0f; |
TonyLin | 10:9f0ce6ba7663 | 28 | break; |
backman | 0:68c173249c01 | 29 | } |
TonyLin | 10:9f0ce6ba7663 | 30 | } |
backman | 0:68c173249c01 | 31 | |
backman | 0:68c173249c01 | 32 | |
backman | 0:68c173249c01 | 33 | // level: -1.0 ~1.0 |
backman | 0:68c173249c01 | 34 | void BX_motor::rotate(float level){ |
backman | 0:68c173249c01 | 35 | |
backman | 0:68c173249c01 | 36 | Level=level; |
backman | 0:68c173249c01 | 37 | |
backman | 0:68c173249c01 | 38 | if(level >0){ |
backman | 0:68c173249c01 | 39 | switch(Type){ |
backman | 0:68c173249c01 | 40 | case 'A': |
backman | 0:68c173249c01 | 41 | *forward_A=level; |
TonyLin | 10:9f0ce6ba7663 | 42 | *backward_A=0.01; |
backman | 0:68c173249c01 | 43 | break; |
backman | 0:68c173249c01 | 44 | case 'B': |
backman | 0:68c173249c01 | 45 | *forward_B=level; |
TonyLin | 10:9f0ce6ba7663 | 46 | *backward_B=0.01; |
backman | 0:68c173249c01 | 47 | break; |
backman | 0:68c173249c01 | 48 | } |
TonyLin | 10:9f0ce6ba7663 | 49 | } |
TonyLin | 10:9f0ce6ba7663 | 50 | else if(level <0){ |
TonyLin | 10:9f0ce6ba7663 | 51 | level=-1.0*level; |
TonyLin | 10:9f0ce6ba7663 | 52 | switch(Type){ |
backman | 0:68c173249c01 | 53 | case 'A': |
TonyLin | 10:9f0ce6ba7663 | 54 | *forward_A=0.01; |
backman | 0:68c173249c01 | 55 | *backward_A=level; |
backman | 0:68c173249c01 | 56 | break; |
backman | 0:68c173249c01 | 57 | case 'B': |
TonyLin | 10:9f0ce6ba7663 | 58 | *forward_B=0.01; |
backman | 0:68c173249c01 | 59 | *backward_B=level; |
backman | 0:68c173249c01 | 60 | break; |
backman | 0:68c173249c01 | 61 | } |
TonyLin | 10:9f0ce6ba7663 | 62 | } |
backman | 0:68c173249c01 | 63 | else{ |
backman | 0:68c173249c01 | 64 | switch(Type){ |
backman | 0:68c173249c01 | 65 | |
backman | 0:68c173249c01 | 66 | case 'A': |
backman | 0:68c173249c01 | 67 | *forward_A=1.0f; |
backman | 0:68c173249c01 | 68 | *backward_A=1.0f; |
backman | 0:68c173249c01 | 69 | |
backman | 0:68c173249c01 | 70 | break; |
backman | 0:68c173249c01 | 71 | case 'B': |
TonyLin | 10:9f0ce6ba7663 | 72 | *forward_B=1.0f; |
backman | 0:68c173249c01 | 73 | *backward_B=1.0f; |
backman | 0:68c173249c01 | 74 | break; |
backman | 0:68c173249c01 | 75 | } |
TonyLin | 10:9f0ce6ba7663 | 76 | } |
backman | 0:68c173249c01 | 77 | } |
backman | 0:68c173249c01 | 78 | |
backman | 0:68c173249c01 | 79 | |
backman | 0:68c173249c01 | 80 |