mit
HBridge.cpp@5:e90c8b57811c, 2018-12-24 (annotated)
- Committer:
- coldplay
- Date:
- Mon Dec 24 03:47:49 2018 +0000
- Revision:
- 5:e90c8b57811c
- Parent:
- 4:5ae9f8b3a16f
mit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abuchan | 4:5ae9f8b3a16f | 1 | #include "HBridge.h" |
abuchan | 4:5ae9f8b3a16f | 2 | |
abuchan | 4:5ae9f8b3a16f | 3 | HBridge::HBridge(PinName pwm_pin, PinName min_1_pin, PinName min_2_pin, float pwm_period): pwm_pin_(pwm_pin), min_1_pin_(min_1_pin), min_2_pin_(min_2_pin) { |
abuchan | 4:5ae9f8b3a16f | 4 | pwm_pin_.period(pwm_period); |
abuchan | 4:5ae9f8b3a16f | 5 | } |
abuchan | 4:5ae9f8b3a16f | 6 | |
abuchan | 4:5ae9f8b3a16f | 7 | //Input of -1 means full reverse, 1 means full forward |
abuchan | 4:5ae9f8b3a16f | 8 | //An input of magnitude > 1 gets reduced to 1 (or -1) by the pwm.write function |
abuchan | 4:5ae9f8b3a16f | 9 | void HBridge::set_output(float output) { |
abuchan | 4:5ae9f8b3a16f | 10 | this->output = output; |
abuchan | 4:5ae9f8b3a16f | 11 | if (output > 0) { |
abuchan | 4:5ae9f8b3a16f | 12 | min_1_pin_.write(0); |
abuchan | 4:5ae9f8b3a16f | 13 | min_2_pin_.write(1); |
coldplay | 5:e90c8b57811c | 14 | pwm_pin_.write(0.5); |
abuchan | 4:5ae9f8b3a16f | 15 | } else if (output < 0) { |
abuchan | 4:5ae9f8b3a16f | 16 | min_1_pin_.write(1); |
abuchan | 4:5ae9f8b3a16f | 17 | min_2_pin_.write(0); |
coldplay | 5:e90c8b57811c | 18 | pwm_pin_.write(0.5); |
abuchan | 4:5ae9f8b3a16f | 19 | } else { |
abuchan | 4:5ae9f8b3a16f | 20 | min_1_pin_.write(0); |
abuchan | 4:5ae9f8b3a16f | 21 | min_2_pin_.write(0); |
abuchan | 4:5ae9f8b3a16f | 22 | pwm_pin_.write(0.0); |
abuchan | 4:5ae9f8b3a16f | 23 | } |
coldplay | 5:e90c8b57811c | 24 | } |
coldplay | 5:e90c8b57811c | 25 | |
coldplay | 5:e90c8b57811c | 26 | void HBridge::forward() |
coldplay | 5:e90c8b57811c | 27 | { |
coldplay | 5:e90c8b57811c | 28 | min_1_pin_.write(1); |
coldplay | 5:e90c8b57811c | 29 | min_2_pin_.write(0); |
coldplay | 5:e90c8b57811c | 30 | pwm_pin_.write(0.5); |
coldplay | 5:e90c8b57811c | 31 | } |
coldplay | 5:e90c8b57811c | 32 | void HBridge::back() |
coldplay | 5:e90c8b57811c | 33 | { |
coldplay | 5:e90c8b57811c | 34 | min_1_pin_.write(0); |
coldplay | 5:e90c8b57811c | 35 | min_2_pin_.write(1); |
coldplay | 5:e90c8b57811c | 36 | pwm_pin_.write(0.5); |
coldplay | 5:e90c8b57811c | 37 | |
coldplay | 5:e90c8b57811c | 38 | } |
coldplay | 5:e90c8b57811c | 39 | void HBridge::initialize() |
coldplay | 5:e90c8b57811c | 40 | { |
coldplay | 5:e90c8b57811c | 41 | min_1_pin_.write(0); |
coldplay | 5:e90c8b57811c | 42 | min_2_pin_.write(0); |
coldplay | 5:e90c8b57811c | 43 | pwm_pin_.write(0); |
coldplay | 5:e90c8b57811c | 44 | } |