A library for H-Bridge dual motor control

Committer:
Generic
Date:
Sun Oct 22 10:27:18 2017 +0000
Revision:
3:3332f633d674
Parent:
2:c0e11aaa32e7
Fixed pwm calculation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cornelius Bezuidenhout 1:b85eb4e982a9 1 #include "DualMotorController.hpp"
Cornelius Bezuidenhout 1:b85eb4e982a9 2
Cornelius Bezuidenhout 1:b85eb4e982a9 3 DualMotorController::DualMotorController(PinName pwmL, PinName dirL0, PinName dirL1, PinName pwmR, PinName dirR0, PinName dirR1, float frequency) :
Generic 2:c0e11aaa32e7 4 _pwmL(pwmL),
Cornelius Bezuidenhout 1:b85eb4e982a9 5 _pwmR(pwmR),
Generic 2:c0e11aaa32e7 6 _dirL0(dirL0,0),
Cornelius Bezuidenhout 1:b85eb4e982a9 7 _dirR0(dirR0,0),
Generic 2:c0e11aaa32e7 8 _dirL1(dirL1,0),
Cornelius Bezuidenhout 1:b85eb4e982a9 9 _dirR1(dirR1,0)
Cornelius Bezuidenhout 1:b85eb4e982a9 10 {
Cornelius Bezuidenhout 1:b85eb4e982a9 11 _pwmL.period(1.0f/frequency);
Cornelius Bezuidenhout 1:b85eb4e982a9 12 _pwmR.period(1.0f/frequency);
Cornelius Bezuidenhout 1:b85eb4e982a9 13
Cornelius Bezuidenhout 1:b85eb4e982a9 14 _speedL = 0.0f;
Cornelius Bezuidenhout 1:b85eb4e982a9 15 _speedR = 0.0f;
Cornelius Bezuidenhout 1:b85eb4e982a9 16 _inverted = false;
Cornelius Bezuidenhout 1:b85eb4e982a9 17 }
Cornelius Bezuidenhout 1:b85eb4e982a9 18
Generic 2:c0e11aaa32e7 19 void DualMotorController::SetLeft(float speed, bool forward) {
Cornelius Bezuidenhout 1:b85eb4e982a9 20 if( forward ) {
Cornelius Bezuidenhout 1:b85eb4e982a9 21 _dirL0.write(1);
Cornelius Bezuidenhout 1:b85eb4e982a9 22 _dirL1.write(0);
Cornelius Bezuidenhout 1:b85eb4e982a9 23 } else {
Cornelius Bezuidenhout 1:b85eb4e982a9 24 _dirL0.write(0);
Cornelius Bezuidenhout 1:b85eb4e982a9 25 _dirL1.write(1);
Cornelius Bezuidenhout 1:b85eb4e982a9 26 }
Cornelius Bezuidenhout 1:b85eb4e982a9 27
Generic 3:3332f633d674 28 _pwmL.write(speed/100.0f);
Cornelius Bezuidenhout 1:b85eb4e982a9 29 }
Cornelius Bezuidenhout 1:b85eb4e982a9 30
Generic 2:c0e11aaa32e7 31 void DualMotorController::SetRight(float speed, bool forward) {
Cornelius Bezuidenhout 1:b85eb4e982a9 32 if( forward ) {
Cornelius Bezuidenhout 1:b85eb4e982a9 33 _dirR0.write(1);
Cornelius Bezuidenhout 1:b85eb4e982a9 34 _dirR1.write(0);
Cornelius Bezuidenhout 1:b85eb4e982a9 35 } else {
Cornelius Bezuidenhout 1:b85eb4e982a9 36 _dirR0.write(0);
Cornelius Bezuidenhout 1:b85eb4e982a9 37 _dirR1.write(1);
Cornelius Bezuidenhout 1:b85eb4e982a9 38 }
Cornelius Bezuidenhout 1:b85eb4e982a9 39
Generic 3:3332f633d674 40 _pwmR.write(speed/100.0f);
Cornelius Bezuidenhout 1:b85eb4e982a9 41 }