Willem Hoitzing
/
Motor_Aansturing
main.cpp@1:e1c1afc945f9, 2016-09-23 (annotated)
- Committer:
- willem_hoitzing
- Date:
- Fri Sep 23 10:13:25 2016 +0000
- Revision:
- 1:e1c1afc945f9
- Parent:
- 0:735b8118a0c9
2e motor toegevoegd
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 | 1:e1c1afc945f9 | 6 | PwmOut pwm_M2(D5); |
willem_hoitzing | 1:e1c1afc945f9 | 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 | 1:e1c1afc945f9 | 12 | volatile float float_pwm1; |
willem_hoitzing | 1:e1c1afc945f9 | 13 | volatile bool bool_dir1 = false; |
willem_hoitzing | 1:e1c1afc945f9 | 14 | volatile float float_pwm2; |
willem_hoitzing | 1:e1c1afc945f9 | 15 | volatile bool bool_dir2 = false; |
willem_hoitzing | 0:735b8118a0c9 | 16 | |
willem_hoitzing | 0:735b8118a0c9 | 17 | void input_u() |
willem_hoitzing | 0:735b8118a0c9 | 18 | { |
willem_hoitzing | 0:735b8118a0c9 | 19 | if(pwm_M1 <= 0.95) |
willem_hoitzing | 0:735b8118a0c9 | 20 | { |
willem_hoitzing | 0:735b8118a0c9 | 21 | pwm_M1 = pwm_M1 + 0.05; |
willem_hoitzing | 1:e1c1afc945f9 | 22 | float_pwm1 = float_pwm1+0.05; |
willem_hoitzing | 1:e1c1afc945f9 | 23 | pc.printf("Speed M1 is set to: %f\n\r", float_pwm1); |
willem_hoitzing | 0:735b8118a0c9 | 24 | } |
willem_hoitzing | 0:735b8118a0c9 | 25 | else |
willem_hoitzing | 0:735b8118a0c9 | 26 | { |
willem_hoitzing | 0:735b8118a0c9 | 27 | pwm_M1 = (1); |
willem_hoitzing | 1:e1c1afc945f9 | 28 | float_pwm1 = 1; |
willem_hoitzing | 1:e1c1afc945f9 | 29 | pc.printf("\nSpeed M1 is maximum\r\n"); |
willem_hoitzing | 1:e1c1afc945f9 | 30 | } |
willem_hoitzing | 1:e1c1afc945f9 | 31 | } |
willem_hoitzing | 1:e1c1afc945f9 | 32 | |
willem_hoitzing | 1:e1c1afc945f9 | 33 | void input_i() |
willem_hoitzing | 1:e1c1afc945f9 | 34 | { |
willem_hoitzing | 1:e1c1afc945f9 | 35 | if(pwm_M2 <= 0.95) |
willem_hoitzing | 1:e1c1afc945f9 | 36 | { |
willem_hoitzing | 1:e1c1afc945f9 | 37 | pwm_M2 = pwm_M2 + 0.05; |
willem_hoitzing | 1:e1c1afc945f9 | 38 | float_pwm2 = float_pwm2+0.05; |
willem_hoitzing | 1:e1c1afc945f9 | 39 | pc.printf("Speed M2 is set to: %f\n\r", float_pwm2); |
willem_hoitzing | 1:e1c1afc945f9 | 40 | } |
willem_hoitzing | 1:e1c1afc945f9 | 41 | else |
willem_hoitzing | 1:e1c1afc945f9 | 42 | { |
willem_hoitzing | 1:e1c1afc945f9 | 43 | pwm_M2 = (1); |
willem_hoitzing | 1:e1c1afc945f9 | 44 | float_pwm2 = 1; |
willem_hoitzing | 1:e1c1afc945f9 | 45 | pc.printf("\nSpeed M2 is maximum\r\n"); |
willem_hoitzing | 0:735b8118a0c9 | 46 | } |
willem_hoitzing | 0:735b8118a0c9 | 47 | } |
willem_hoitzing | 0:735b8118a0c9 | 48 | |
willem_hoitzing | 0:735b8118a0c9 | 49 | void input_d() |
willem_hoitzing | 0:735b8118a0c9 | 50 | { |
willem_hoitzing | 0:735b8118a0c9 | 51 | if(pwm_M1 >= 0.05) |
willem_hoitzing | 0:735b8118a0c9 | 52 | { |
willem_hoitzing | 0:735b8118a0c9 | 53 | pwm_M1 = (pwm_M1 - 0.05); |
willem_hoitzing | 1:e1c1afc945f9 | 54 | float_pwm1 = float_pwm1-0.05; |
willem_hoitzing | 1:e1c1afc945f9 | 55 | pc.printf("Speed M1 is set to: %f\n\r", float_pwm1); |
willem_hoitzing | 0:735b8118a0c9 | 56 | } |
willem_hoitzing | 0:735b8118a0c9 | 57 | else |
willem_hoitzing | 0:735b8118a0c9 | 58 | { |
willem_hoitzing | 0:735b8118a0c9 | 59 | pwm_M1 = (0); |
willem_hoitzing | 1:e1c1afc945f9 | 60 | float_pwm1 = 0; |
willem_hoitzing | 1:e1c1afc945f9 | 61 | pc.printf("\nSpeed M1 is minimum\r\n"); |
willem_hoitzing | 1:e1c1afc945f9 | 62 | } |
willem_hoitzing | 1:e1c1afc945f9 | 63 | } |
willem_hoitzing | 1:e1c1afc945f9 | 64 | |
willem_hoitzing | 1:e1c1afc945f9 | 65 | void input_f() |
willem_hoitzing | 1:e1c1afc945f9 | 66 | { |
willem_hoitzing | 1:e1c1afc945f9 | 67 | if(pwm_M2 >= 0.05) |
willem_hoitzing | 1:e1c1afc945f9 | 68 | { |
willem_hoitzing | 1:e1c1afc945f9 | 69 | pwm_M2 = (pwm_M2 - 0.05); |
willem_hoitzing | 1:e1c1afc945f9 | 70 | float_pwm2 = float_pwm2-0.05; |
willem_hoitzing | 1:e1c1afc945f9 | 71 | pc.printf("Speed M2 is set to: %f\n\r", float_pwm2); |
willem_hoitzing | 1:e1c1afc945f9 | 72 | } |
willem_hoitzing | 1:e1c1afc945f9 | 73 | else |
willem_hoitzing | 1:e1c1afc945f9 | 74 | { |
willem_hoitzing | 1:e1c1afc945f9 | 75 | pwm_M2 = (0); |
willem_hoitzing | 1:e1c1afc945f9 | 76 | float_pwm2 = 0; |
willem_hoitzing | 1:e1c1afc945f9 | 77 | pc.printf("\nSpeed M2 is minimum\r\n"); |
willem_hoitzing | 0:735b8118a0c9 | 78 | } |
willem_hoitzing | 0:735b8118a0c9 | 79 | } |
willem_hoitzing | 0:735b8118a0c9 | 80 | |
willem_hoitzing | 0:735b8118a0c9 | 81 | void input_c() |
willem_hoitzing | 0:735b8118a0c9 | 82 | { |
willem_hoitzing | 1:e1c1afc945f9 | 83 | float pwm_original1 = pwm_M1; |
willem_hoitzing | 0:735b8118a0c9 | 84 | while(pwm_M1>0) |
willem_hoitzing | 0:735b8118a0c9 | 85 | { |
willem_hoitzing | 0:735b8118a0c9 | 86 | pwm_M1 = pwm_M1 - 0.1; |
willem_hoitzing | 0:735b8118a0c9 | 87 | wait(0.1); |
willem_hoitzing | 0:735b8118a0c9 | 88 | } |
willem_hoitzing | 0:735b8118a0c9 | 89 | dir_M1 = !dir_M1; |
willem_hoitzing | 1:e1c1afc945f9 | 90 | while(pwm_M1 < pwm_original1) |
willem_hoitzing | 0:735b8118a0c9 | 91 | { |
willem_hoitzing | 0:735b8118a0c9 | 92 | pwm_M1 = pwm_M1 + 0.1; |
willem_hoitzing | 0:735b8118a0c9 | 93 | wait(0.1); |
willem_hoitzing | 0:735b8118a0c9 | 94 | } |
willem_hoitzing | 1:e1c1afc945f9 | 95 | bool_dir1 = !bool_dir1; |
willem_hoitzing | 0:735b8118a0c9 | 96 | if(dir_M1 == cw) |
willem_hoitzing | 0:735b8118a0c9 | 97 | { |
willem_hoitzing | 1:e1c1afc945f9 | 98 | pc.printf("\nDirection M1 is set to: clockwise\r\n"); |
willem_hoitzing | 0:735b8118a0c9 | 99 | |
willem_hoitzing | 1:e1c1afc945f9 | 100 | pc.printf("Direction M1: %B\r\n", bool_dir1); |
willem_hoitzing | 0:735b8118a0c9 | 101 | } |
willem_hoitzing | 0:735b8118a0c9 | 102 | else |
willem_hoitzing | 0:735b8118a0c9 | 103 | { |
willem_hoitzing | 1:e1c1afc945f9 | 104 | pc.printf("\nDirection M1 is set to: counter-clockwise\r\n"); |
willem_hoitzing | 0:735b8118a0c9 | 105 | |
willem_hoitzing | 1:e1c1afc945f9 | 106 | pc.printf("Direction M1: %B\r\n", bool_dir1); |
willem_hoitzing | 1:e1c1afc945f9 | 107 | } |
willem_hoitzing | 1:e1c1afc945f9 | 108 | } |
willem_hoitzing | 1:e1c1afc945f9 | 109 | |
willem_hoitzing | 1:e1c1afc945f9 | 110 | void input_v() |
willem_hoitzing | 1:e1c1afc945f9 | 111 | { |
willem_hoitzing | 1:e1c1afc945f9 | 112 | float pwm_original2 = pwm_M2; |
willem_hoitzing | 1:e1c1afc945f9 | 113 | while(pwm_M2>0) |
willem_hoitzing | 1:e1c1afc945f9 | 114 | { |
willem_hoitzing | 1:e1c1afc945f9 | 115 | pwm_M2 = pwm_M2 - 0.1; |
willem_hoitzing | 1:e1c1afc945f9 | 116 | wait(0.1); |
willem_hoitzing | 1:e1c1afc945f9 | 117 | } |
willem_hoitzing | 1:e1c1afc945f9 | 118 | dir_M2 = !dir_M2; |
willem_hoitzing | 1:e1c1afc945f9 | 119 | while(pwm_M2 < pwm_original2) |
willem_hoitzing | 1:e1c1afc945f9 | 120 | { |
willem_hoitzing | 1:e1c1afc945f9 | 121 | pwm_M2 = pwm_M2 + 0.1; |
willem_hoitzing | 1:e1c1afc945f9 | 122 | wait(0.1); |
willem_hoitzing | 1:e1c1afc945f9 | 123 | } |
willem_hoitzing | 1:e1c1afc945f9 | 124 | bool_dir2 = !bool_dir2; |
willem_hoitzing | 1:e1c1afc945f9 | 125 | if(dir_M2 == cw) |
willem_hoitzing | 1:e1c1afc945f9 | 126 | { |
willem_hoitzing | 1:e1c1afc945f9 | 127 | pc.printf("\nDirection M2 is set to: clockwise\r\n"); |
willem_hoitzing | 1:e1c1afc945f9 | 128 | |
willem_hoitzing | 1:e1c1afc945f9 | 129 | pc.printf("Direction M2: %B\r\n", bool_dir2); |
willem_hoitzing | 1:e1c1afc945f9 | 130 | } |
willem_hoitzing | 1:e1c1afc945f9 | 131 | else |
willem_hoitzing | 1:e1c1afc945f9 | 132 | { |
willem_hoitzing | 1:e1c1afc945f9 | 133 | pc.printf("\nDirection M2 is set to: counter-clockwise\r\n"); |
willem_hoitzing | 1:e1c1afc945f9 | 134 | |
willem_hoitzing | 1:e1c1afc945f9 | 135 | pc.printf("Direction M2: %B\r\n", bool_dir2); |
willem_hoitzing | 0:735b8118a0c9 | 136 | } |
willem_hoitzing | 0:735b8118a0c9 | 137 | } |
willem_hoitzing | 0:735b8118a0c9 | 138 | |
willem_hoitzing | 0:735b8118a0c9 | 139 | int main() |
willem_hoitzing | 0:735b8118a0c9 | 140 | { |
willem_hoitzing | 0:735b8118a0c9 | 141 | pc.baud(115200); |
willem_hoitzing | 1:e1c1afc945f9 | 142 | pwm_M1 = 0.5; |
willem_hoitzing | 1:e1c1afc945f9 | 143 | pwm_M2 = 0.5; |
willem_hoitzing | 1:e1c1afc945f9 | 144 | float_pwm1 = 0.5; |
willem_hoitzing | 1:e1c1afc945f9 | 145 | float_pwm2 = 0.5; |
willem_hoitzing | 0:735b8118a0c9 | 146 | dir_M1 = cw; |
willem_hoitzing | 1:e1c1afc945f9 | 147 | dir_M2 = cw; |
willem_hoitzing | 0:735b8118a0c9 | 148 | for(;;){ |
willem_hoitzing | 0:735b8118a0c9 | 149 | input = pc.getc(); |
willem_hoitzing | 0:735b8118a0c9 | 150 | wait(0.01); |
willem_hoitzing | 0:735b8118a0c9 | 151 | switch(input) |
willem_hoitzing | 0:735b8118a0c9 | 152 | { |
willem_hoitzing | 0:735b8118a0c9 | 153 | case 'u': |
willem_hoitzing | 0:735b8118a0c9 | 154 | input_u(); |
willem_hoitzing | 0:735b8118a0c9 | 155 | break; |
willem_hoitzing | 0:735b8118a0c9 | 156 | case 'd': |
willem_hoitzing | 0:735b8118a0c9 | 157 | input_d(); |
willem_hoitzing | 0:735b8118a0c9 | 158 | break; |
willem_hoitzing | 0:735b8118a0c9 | 159 | case 'c': |
willem_hoitzing | 0:735b8118a0c9 | 160 | input_c(); |
willem_hoitzing | 0:735b8118a0c9 | 161 | break; |
willem_hoitzing | 1:e1c1afc945f9 | 162 | case 'i': |
willem_hoitzing | 1:e1c1afc945f9 | 163 | input_i(); |
willem_hoitzing | 1:e1c1afc945f9 | 164 | break; |
willem_hoitzing | 1:e1c1afc945f9 | 165 | case 'f': |
willem_hoitzing | 1:e1c1afc945f9 | 166 | input_f(); |
willem_hoitzing | 1:e1c1afc945f9 | 167 | break; |
willem_hoitzing | 1:e1c1afc945f9 | 168 | case 'v': |
willem_hoitzing | 1:e1c1afc945f9 | 169 | input_v(); |
willem_hoitzing | 1:e1c1afc945f9 | 170 | break; |
willem_hoitzing | 0:735b8118a0c9 | 171 | default: |
willem_hoitzing | 0:735b8118a0c9 | 172 | pc.printf("\nUnknown command\n\r"); |
willem_hoitzing | 0:735b8118a0c9 | 173 | } |
willem_hoitzing | 0:735b8118a0c9 | 174 | } |
willem_hoitzing | 0:735b8118a0c9 | 175 | } |