Willem Hoitzing
/
Motor_Aansturing
main.cpp@0:735b8118a0c9, 2016-09-23 (annotated)
- Committer:
- willem_hoitzing
- Date:
- Fri Sep 23 10:03:41 2016 +0000
- Revision:
- 0:735b8118a0c9
- Child:
- 1:e1c1afc945f9
Aansturing motor met toetsenbord
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
willem_hoitzing | 0:735b8118a0c9 | 1 | #include "mbed.h" |
willem_hoitzing | 0:735b8118a0c9 | 2 | #include "MODSERIAL.h" |
willem_hoitzing | 0:735b8118a0c9 | 3 | |
willem_hoitzing | 0:735b8118a0c9 | 4 | PwmOut pwm_M1(D6); |
willem_hoitzing | 0:735b8118a0c9 | 5 | DigitalOut dir_M1(D7); |
willem_hoitzing | 0:735b8118a0c9 | 6 | //PwmOut pwm_M2(D5); |
willem_hoitzing | 0:735b8118a0c9 | 7 | //DigitalOut dir_M2(D4); |
willem_hoitzing | 0:735b8118a0c9 | 8 | MODSERIAL pc(USBTX, USBRX); |
willem_hoitzing | 0:735b8118a0c9 | 9 | int cw = 0; |
willem_hoitzing | 0:735b8118a0c9 | 10 | int ccw = 1; |
willem_hoitzing | 0:735b8118a0c9 | 11 | char input; |
willem_hoitzing | 0:735b8118a0c9 | 12 | volatile float float_pwm; |
willem_hoitzing | 0:735b8118a0c9 | 13 | volatile bool bool_dir = false; |
willem_hoitzing | 0:735b8118a0c9 | 14 | |
willem_hoitzing | 0:735b8118a0c9 | 15 | void input_u() |
willem_hoitzing | 0:735b8118a0c9 | 16 | { |
willem_hoitzing | 0:735b8118a0c9 | 17 | if(pwm_M1 <= 0.95) |
willem_hoitzing | 0:735b8118a0c9 | 18 | { |
willem_hoitzing | 0:735b8118a0c9 | 19 | pwm_M1 = pwm_M1 + 0.05; |
willem_hoitzing | 0:735b8118a0c9 | 20 | float_pwm = float_pwm+0.05; |
willem_hoitzing | 0:735b8118a0c9 | 21 | pc.printf("Speed is set to: %f\n\r", float_pwm); |
willem_hoitzing | 0:735b8118a0c9 | 22 | } |
willem_hoitzing | 0:735b8118a0c9 | 23 | else |
willem_hoitzing | 0:735b8118a0c9 | 24 | { |
willem_hoitzing | 0:735b8118a0c9 | 25 | pwm_M1 = (1); |
willem_hoitzing | 0:735b8118a0c9 | 26 | float_pwm = 1; |
willem_hoitzing | 0:735b8118a0c9 | 27 | pc.printf("\nSpeed is maximum\r\n"); |
willem_hoitzing | 0:735b8118a0c9 | 28 | } |
willem_hoitzing | 0:735b8118a0c9 | 29 | } |
willem_hoitzing | 0:735b8118a0c9 | 30 | |
willem_hoitzing | 0:735b8118a0c9 | 31 | void input_d() |
willem_hoitzing | 0:735b8118a0c9 | 32 | { |
willem_hoitzing | 0:735b8118a0c9 | 33 | if(pwm_M1 >= 0.05) |
willem_hoitzing | 0:735b8118a0c9 | 34 | { |
willem_hoitzing | 0:735b8118a0c9 | 35 | pwm_M1 = (pwm_M1 - 0.05); |
willem_hoitzing | 0:735b8118a0c9 | 36 | float_pwm = float_pwm-0.05; |
willem_hoitzing | 0:735b8118a0c9 | 37 | pc.printf("Speed is set to: %f\n\r", float_pwm); |
willem_hoitzing | 0:735b8118a0c9 | 38 | } |
willem_hoitzing | 0:735b8118a0c9 | 39 | else |
willem_hoitzing | 0:735b8118a0c9 | 40 | { |
willem_hoitzing | 0:735b8118a0c9 | 41 | pwm_M1 = (0); |
willem_hoitzing | 0:735b8118a0c9 | 42 | float_pwm = 0; |
willem_hoitzing | 0:735b8118a0c9 | 43 | pc.printf("\nSpeed is minimum\r\n"); |
willem_hoitzing | 0:735b8118a0c9 | 44 | } |
willem_hoitzing | 0:735b8118a0c9 | 45 | } |
willem_hoitzing | 0:735b8118a0c9 | 46 | |
willem_hoitzing | 0:735b8118a0c9 | 47 | void input_c() |
willem_hoitzing | 0:735b8118a0c9 | 48 | { |
willem_hoitzing | 0:735b8118a0c9 | 49 | float pwm_original = pwm_M1; |
willem_hoitzing | 0:735b8118a0c9 | 50 | while(pwm_M1>0) |
willem_hoitzing | 0:735b8118a0c9 | 51 | { |
willem_hoitzing | 0:735b8118a0c9 | 52 | pwm_M1 = pwm_M1 - 0.1; |
willem_hoitzing | 0:735b8118a0c9 | 53 | wait(0.1); |
willem_hoitzing | 0:735b8118a0c9 | 54 | } |
willem_hoitzing | 0:735b8118a0c9 | 55 | dir_M1 = !dir_M1; |
willem_hoitzing | 0:735b8118a0c9 | 56 | while(pwm_M1 < pwm_original) |
willem_hoitzing | 0:735b8118a0c9 | 57 | { |
willem_hoitzing | 0:735b8118a0c9 | 58 | pwm_M1 = pwm_M1 + 0.1; |
willem_hoitzing | 0:735b8118a0c9 | 59 | wait(0.1); |
willem_hoitzing | 0:735b8118a0c9 | 60 | } |
willem_hoitzing | 0:735b8118a0c9 | 61 | bool_dir = !bool_dir; |
willem_hoitzing | 0:735b8118a0c9 | 62 | if(dir_M1 == cw) |
willem_hoitzing | 0:735b8118a0c9 | 63 | { |
willem_hoitzing | 0:735b8118a0c9 | 64 | pc.printf("\nDirection is set to: clockwise\r\n"); |
willem_hoitzing | 0:735b8118a0c9 | 65 | |
willem_hoitzing | 0:735b8118a0c9 | 66 | pc.printf("\nDirection: %B\r\n", bool_dir); |
willem_hoitzing | 0:735b8118a0c9 | 67 | } |
willem_hoitzing | 0:735b8118a0c9 | 68 | else |
willem_hoitzing | 0:735b8118a0c9 | 69 | { |
willem_hoitzing | 0:735b8118a0c9 | 70 | pc.printf("\nDirection is set to: counter-clockwise\r\n"); |
willem_hoitzing | 0:735b8118a0c9 | 71 | |
willem_hoitzing | 0:735b8118a0c9 | 72 | pc.printf("\nDirection: %B\r\n", bool_dir); |
willem_hoitzing | 0:735b8118a0c9 | 73 | } |
willem_hoitzing | 0:735b8118a0c9 | 74 | } |
willem_hoitzing | 0:735b8118a0c9 | 75 | |
willem_hoitzing | 0:735b8118a0c9 | 76 | int main() |
willem_hoitzing | 0:735b8118a0c9 | 77 | { |
willem_hoitzing | 0:735b8118a0c9 | 78 | pc.baud(115200); |
willem_hoitzing | 0:735b8118a0c9 | 79 | pwm_M1 = (0.5); |
willem_hoitzing | 0:735b8118a0c9 | 80 | float_pwm = 0.5; |
willem_hoitzing | 0:735b8118a0c9 | 81 | dir_M1 = cw; |
willem_hoitzing | 0:735b8118a0c9 | 82 | for(;;){ |
willem_hoitzing | 0:735b8118a0c9 | 83 | input = pc.getc(); |
willem_hoitzing | 0:735b8118a0c9 | 84 | wait(0.01); |
willem_hoitzing | 0:735b8118a0c9 | 85 | switch(input) |
willem_hoitzing | 0:735b8118a0c9 | 86 | { |
willem_hoitzing | 0:735b8118a0c9 | 87 | case 'u': |
willem_hoitzing | 0:735b8118a0c9 | 88 | input_u(); |
willem_hoitzing | 0:735b8118a0c9 | 89 | break; |
willem_hoitzing | 0:735b8118a0c9 | 90 | case 'd': |
willem_hoitzing | 0:735b8118a0c9 | 91 | input_d(); |
willem_hoitzing | 0:735b8118a0c9 | 92 | break; |
willem_hoitzing | 0:735b8118a0c9 | 93 | case 'c': |
willem_hoitzing | 0:735b8118a0c9 | 94 | input_c(); |
willem_hoitzing | 0:735b8118a0c9 | 95 | break; |
willem_hoitzing | 0:735b8118a0c9 | 96 | default: |
willem_hoitzing | 0:735b8118a0c9 | 97 | pc.printf("\nUnknown command\n\r"); |
willem_hoitzing | 0:735b8118a0c9 | 98 | } |
willem_hoitzing | 0:735b8118a0c9 | 99 | } |
willem_hoitzing | 0:735b8118a0c9 | 100 | } |