Driver for two motors library
Diff: Driver.cpp
- Revision:
- 0:eafb70ae80f7
- Child:
- 1:e417951c16d7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Driver.cpp Tue Jul 28 05:46:24 2020 +0000 @@ -0,0 +1,94 @@ +#include "mbed.h" +#include "Driver.h" + +driver::driver(PinName M1A, PinName M1B, PinName M2A, PinName M2B): + M1A(M1A), M1B(M1B), M2A(M2A), M2B(M2B){ + setPeriod_ms(1000); +} + +void driver::setPeriod_ms(int32_t period){ + _period = period; + M1A.period_ms(_period); + M1B.period_ms(_period); + M2A.period_ms(_period); + M2B.period_ms(_period); +} + +void driver::neutral(){ + M1A.write(0); + M1B.write(0); + M2A.write(0); + M2B.write(0); +} + +void driver::forward(float dutyCycle1, float dutyCycle2){ + _dutyCycle1 = dutyCycle1; + _dutyCycle2 = dutyCycle2; + if(_dutyCycle1 > 1.0f) + _dutyCycle1 = 1.0f; + if(_dutyCycle2 > 1.0f) + _dutyCycle2 = 1.0f; + if(_dutyCycle1 < 0) + _dutyCycle1 = 0.0f; + if(_dutyCycle2 < 0) + _dutyCycle2 = 0.0f; + + M1A.write(_dutyCycle1); + M1B.write(0); + M2A.write(_dutyCycle2); + M2B.write(0); +} + +void driver::backward(float dutyCycle1, float dutyCycle2){ + _dutyCycle1 = dutyCycle1; + _dutyCycle2 = dutyCycle2; + if(_dutyCycle1 > 1.0f) + _dutyCycle1 = 1.0f; + if(_dutyCycle2 > 1.0f) + _dutyCycle2 = 1.0f; + if(_dutyCycle1 < 0) + _dutyCycle1 = 0.0f; + if(_dutyCycle2 < 0) + _dutyCycle2 = 0.0f; + + M1A.write(0); + M1B.write(_dutyCycle1); + M2A.write(0); + M2B.write(_dutyCycle2); +} + +void driver::right(float dutyCycle1, float dutyCycle2){ + _dutyCycle1 = dutyCycle1; + _dutyCycle2 = dutyCycle2; + if(_dutyCycle1 > 1.0f) + _dutyCycle1 = 1.0f; + if(_dutyCycle2 > 1.0f) + _dutyCycle2 = 1.0f; + if(_dutyCycle1 < 0) + _dutyCycle1 = 0.0f; + if(_dutyCycle2 < 0) + _dutyCycle2 = 0.0f; + + M1A.write(0); + M1B.write(_dutyCycle1); + M2A.write(_dutyCycle2); + M2B.write(0); +} + +void driver::left(float dutyCycle1, float dutyCycle2){ + _dutyCycle1 = dutyCycle1; + _dutyCycle2 = dutyCycle2; + if(_dutyCycle1 > 1.0f) + _dutyCycle1 = 1.0f; + if(_dutyCycle2 > 1.0f) + _dutyCycle2 = 1.0f; + if(_dutyCycle1 < 0) + _dutyCycle1 = 0.0f; + if(_dutyCycle2 < 0) + _dutyCycle2 = 0.0f; + + M1A.write(_dutyCycle1); + M1B.write(0); + M2A.write(0); + M2B.write(_dutyCycle2); +} \ No newline at end of file