Willem Hoitzing
/
Motor_Aansturing
Diff: main.cpp
- Revision:
- 0:735b8118a0c9
- Child:
- 1:e1c1afc945f9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Sep 23 10:03:41 2016 +0000 @@ -0,0 +1,100 @@ +#include "mbed.h" +#include "MODSERIAL.h" + +PwmOut pwm_M1(D6); +DigitalOut dir_M1(D7); +//PwmOut pwm_M2(D5); +//DigitalOut dir_M2(D4); +MODSERIAL pc(USBTX, USBRX); +int cw = 0; +int ccw = 1; +char input; +volatile float float_pwm; +volatile bool bool_dir = false; + +void input_u() +{ + if(pwm_M1 <= 0.95) + { + pwm_M1 = pwm_M1 + 0.05; + float_pwm = float_pwm+0.05; + pc.printf("Speed is set to: %f\n\r", float_pwm); + } + else + { + pwm_M1 = (1); + float_pwm = 1; + pc.printf("\nSpeed is maximum\r\n"); + } +} + +void input_d() +{ + if(pwm_M1 >= 0.05) + { + pwm_M1 = (pwm_M1 - 0.05); + float_pwm = float_pwm-0.05; + pc.printf("Speed is set to: %f\n\r", float_pwm); + } + else + { + pwm_M1 = (0); + float_pwm = 0; + pc.printf("\nSpeed is minimum\r\n"); + } +} + +void input_c() +{ + float pwm_original = pwm_M1; + while(pwm_M1>0) + { + pwm_M1 = pwm_M1 - 0.1; + wait(0.1); + } + dir_M1 = !dir_M1; + while(pwm_M1 < pwm_original) + { + pwm_M1 = pwm_M1 + 0.1; + wait(0.1); + } + bool_dir = !bool_dir; + if(dir_M1 == cw) + { + pc.printf("\nDirection is set to: clockwise\r\n"); + + pc.printf("\nDirection: %B\r\n", bool_dir); + } + else + { + pc.printf("\nDirection is set to: counter-clockwise\r\n"); + + pc.printf("\nDirection: %B\r\n", bool_dir); + } +} + +int main() +{ + pc.baud(115200); + pwm_M1 = (0.5); + float_pwm = 0.5; + dir_M1 = cw; + for(;;){ + input = pc.getc(); + wait(0.01); + switch(input) + { + case 'u': + input_u(); + break; + case 'd': + input_d(); + break; + case 'c': + input_c(); + break; + default: + pc.printf("\nUnknown command\n\r"); + } + } +} \ No newline at end of file