A library for H-Bridge dual motor control
DualMotorController.cpp@2:c0e11aaa32e7, 2017-10-22 (annotated)
- Committer:
- Generic
- Date:
- Sun Oct 22 09:18:19 2017 +0000
- Revision:
- 2:c0e11aaa32e7
- Parent:
- 1:b85eb4e982a9
- Child:
- 3:3332f633d674
Changed boolean to bool. Fixed initialization order
Who changed what in which revision?
User | Revision | Line number | New 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 | |
Cornelius Bezuidenhout |
1:b85eb4e982a9 | 28 | _pwmL.write(speed); |
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 | |
Cornelius Bezuidenhout |
1:b85eb4e982a9 | 40 | _pwmR.write(speed); |
Cornelius Bezuidenhout |
1:b85eb4e982a9 | 41 | } |