Motor control in Ticker
Dependencies: FastPWM MODSERIAL mbed QEI
Fork of Motorcontrol by
Diff: main.cpp
- Revision:
- 2:ba344767412c
- Parent:
- 1:621c4bb5301d
- Child:
- 3:bfb7f197991f
- Child:
- 4:7f7974aec29d
--- a/main.cpp Fri Sep 28 09:05:37 2018 +0000 +++ b/main.cpp Fri Sep 28 09:13:19 2018 +0000 @@ -2,6 +2,8 @@ #include "FastPWM.h" #include "MODSERIAL.h" +Ticker Motorticker; + FastPWM motor1_pwm(D5); DigitalOut motor1_richting(D4); FastPWM motor2_pwm(D6); @@ -11,17 +13,8 @@ AnalogIn pot2(A0); MODSERIAL pc(USBTX, USBRX); - - -int main() +void Motor_control() { - int frequency_pwm = 16700; //16.7 kHz PWM - - - motor1_pwm.period(1.0/frequency_pwm); // T = 1/f - - while(true){ - pc.baud(115200); float AnalogVoltage1 = pot1.read()*2 - 1; @@ -30,27 +23,39 @@ //Motor1 - if (AnalogVoltage1 <= 0) { + if (AnalogVoltage1 <= 0) + { motor1_richting = 0; motor1_pwm.write(-AnalogVoltage1); //write Duty cycle - } - if (AnalogVoltage1 >= 0) { + } + + if (AnalogVoltage1 >= 0) + { motor1_richting = 1; motor1_pwm.write(AnalogVoltage1); //write Duty cycle - } + } //Motor 2 - if (AnalogVoltage2 <= 0) { + if (AnalogVoltage2 <= 0) + { motor2_richting = 0; motor2_pwm.write(-AnalogVoltage2); //write Duty cycle - } - if (AnalogVoltage2 >= 0) { + } + if (AnalogVoltage2 >= 0) + { motor2_richting = 1; motor2_pwm.write(AnalogVoltage2); //write Duty cycle - } - - - } +} + +int main() +{ + int frequency_pwm = 16700; //16.7 kHz PWM + + Motorticker.attach(Motor_control,0.1); + motor1_pwm.period(1.0/frequency_pwm); // T = 1/f + + while(true) + {} } \ No newline at end of file