Utility class for controlling a servo system motor.

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