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: MotorController.cpp
- Revision:
- 0:ec2f323f6388
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MotorController.cpp Tue Jan 05 14:41:35 2016 +0000
@@ -0,0 +1,54 @@
+#include "MotorController.h"
+
+MotorController::MotorController (HBridge &motor, AnalogIn &motorInput) :
+ motor ( motor ), motorInput ( motorInput ) {
+}
+
+void MotorController::start () {
+ this->motor.start();
+}
+
+void MotorController::stop () {
+ this->motor.stop();
+}
+
+void MotorController::turnLeft () {
+ this->motor.start();
+ this->motor.backward();
+}
+
+
+void MotorController::turnRight () {
+ this->motor.start();
+ this->motor.forward();
+}
+
+void MotorController::setPosition (float position) {
+ float current_position = this->getPosition();
+
+ if (current_position > position) {
+ this->turnLeft();
+
+ while (1) {
+ if (this->getPosition() <= position) {
+ this->stop();
+ break;
+ }
+ }
+ }
+ else {
+ this->turnRight();
+
+ while (1) {
+ if (this->getPosition() >= position) {
+ this->stop();
+ break;
+ }
+ }
+ }
+}
+
+
+float MotorController::getPosition () {
+ return this->motorInput.read() * 100.0f;
+}
\ No newline at end of file