Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: StepperDriver.cpp
- Revision:
- 0:12be56dc6182
- Child:
- 1:9888802e71b9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/StepperDriver.cpp Thu Dec 01 13:35:45 2016 +0000
@@ -0,0 +1,52 @@
+#include "mbed.h"
+#include "StepperDriver.h"
+
+StepperDriver::StepperDriver(PinName outputPin, PinName directionPin) : output(outputPin), direction(directionPin) {
+ currentPosition = previousPosition = desiredPosition = 0;
+ maxPosition = 500;
+ minPosition = 0;
+ homePosition = (maxPosition - minPosition) / 2;
+}
+
+void StepperDriver::setPosition(uint32_t position) {
+ if (position >= minPosition && position <= maxPosition) {
+ desiredPosition = position;
+ }
+ if (desiredPosition != currentPosition && !isTickerAttached) {
+ attachTicker();
+ }
+}
+
+void StepperDriver::update() {
+ if (desiredPosition > currentPosition) {
+ direction = 1;
+ currentPosition++;
+ generateImpulse();
+ } else if (desiredPosition < currentPosition) {
+ direction = 0;
+ currentPosition--;
+ generateImpulse();
+ } else {
+ detachTicker();
+ }
+ previousPosition = currentPosition;
+}
+
+void StepperDriver::attachTicker() {
+ ticker.attach(this, &StepperDriver::update, 4e-3);
+ isTickerAttached = true;
+}
+
+void StepperDriver::detachTicker() {
+ ticker.detach();
+ isTickerAttached = false;
+}
+
+void StepperDriver::generateImpulse() {
+ output = 1;
+ timeout.attach(this, &StepperDriver::turnOutputOff, 100e-6);
+}
+
+void StepperDriver::turnOutputOff() {
+ output = 0;
+}
\ No newline at end of file