Utility class for controlling a servo system motor.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MotorController.cpp Source File

MotorController.cpp

00001 #include "MotorController.h"
00002 
00003 MotorController::MotorController (HBridge &motor, AnalogIn &motorInput) :
00004     motor ( motor ), motorInput ( motorInput ) {
00005 }
00006 
00007 void MotorController::start () {
00008     this->motor.start();
00009 }
00010 
00011 void MotorController::stop () {
00012     this->motor.stop();
00013 }
00014 
00015 void MotorController::turnLeft () {
00016     this->motor.start();
00017     this->motor.backward();
00018 }
00019 
00020 
00021 void MotorController::turnRight () {
00022     this->motor.start();
00023     this->motor.forward();
00024 }
00025 
00026 void MotorController::setPosition (float position) {
00027     float current_position = this->getPosition();
00028     
00029     if (current_position > position) {
00030         this->turnLeft();
00031         
00032         while (1) {
00033             if (this->getPosition() <= position) {
00034                 this->stop();
00035                 break;
00036             }
00037         }
00038     }
00039     else {
00040         this->turnRight();
00041         
00042         while (1) {
00043             if (this->getPosition() >= position) {
00044                 this->stop();
00045                 break;
00046             }
00047         }
00048     }
00049 }
00050 
00051 
00052 float MotorController::getPosition () {
00053     return this->motorInput.read() * 100.0f;
00054 }