Motor driver library for the AP1017.

/media/uploads/tkstreet/akm_name_logo.png

AKM Development Platform

AP1017 Motor Driver

Import libraryAP1017

Motor driver library for the AP1017.

Revision:
4:c36159701cde
Parent:
3:f8e70f639ed0
Child:
5:2da4339149a6
--- a/AP1017.cpp	Wed Apr 19 23:13:04 2017 +0000
+++ b/AP1017.cpp	Fri Apr 21 20:18:32 2017 +0000
@@ -1,4 +1,5 @@
 #include "AP1017.h"
+#include "debug.h"
 
 /******************** Constructors & Destructors ****************************/
 
@@ -19,31 +20,45 @@
 
 /*********************** Member Functions ***********************************/
 
-AP1017::Status AP1017::setDirection(Rotation dir)
+AP1017::Status AP1017::setDirection(AP1017::Rotation dir)
 {
     direction = dir;
-
+    
     switch(direction){
-        case DIRECTION_CW:
-            if(motorOn == true && dir == DIRECTION_CCW)
+        case DIRECTION_CW:                              // direction = 0x00
+            if(isMotorOn())
+            {
+                MSG("#AP1017.cpp: Error. Cannot change direction while motor is running.\r\n");
                 return ERROR_MOTORON;
-            inA->write(1);
-            inB->write(0);
+            }else
+            {
+                inA->write(1);
+                inB->write(0);
+                MSG("#AP1017.cpp: Direction: CCW\r\n");
+            }
             break;
-        case DIRECTION_CCW:
-            if(motorOn == true && dir == DIRECTION_CW)
+        case DIRECTION_CCW:                                 // direction = 0x01
+            if(isMotorOn())
+            {
+                MSG("#AP1017.cpp: Error. Cannot change direction while motor is running.\r\n");
                 return ERROR_MOTORON;
-            inA->write(0);
-            inB->write(1);
+            }else
+            {
+                inA->write(0);
+                inB->write(1);
+                MSG("#AP1017.cpp: Direction: CW\r\n");
+            }
             break;
-        case DIRECTION_BRAKE:
+        case DIRECTION_BRAKE:                              // direction = 0x03
             inA->write(1);
             inB->write(1);
+            MSG("#AP1017.cpp: Direction: Brake\r\n");
             break;
-        case DIRECTION_COAST:
+        case DIRECTION_COAST:                              // direction = 0x04
             inA->write(0);
             inB->write(0);
             motorOn = false;
+            MSG("#AP1017.cpp: Direction: Coast\r\n");
             break;
         default:
             return ERROR_DIRECTION;
@@ -61,11 +76,13 @@
 
 AP1017::Status AP1017::setSpeed(float dc)
 {
-    if((dutyCycle <= 100.0) && (dutyCycle >= 0.0)){
+    if((dc <= 100.0) && (dc >= 0.0)){
         dutyCycle = dc/100.0;
+        MSG("#AP1017.cpp: dutyCycle = %.1f%%\r\n", dc);
 
         if(motorOn == true){
             motor->write(dutyCycle);          // If motor is on, keep it on
+            MSG("#AP1017.cpp: Changed running motor speed.\r\n");
         }
     }
     else{
@@ -80,6 +97,7 @@
 
 float AP1017::getSpeed(void)
 {
+    MSG("#AP1017.cpp: Speed = %.1f%%\r\n", dutyCycle);
     return dutyCycle*100.0;
 }
 
@@ -87,6 +105,7 @@
 AP1017::Status AP1017::start(void)
 {
     motor->write(dutyCycle);                // Set duty cycle, start motor
+    MSG("#AP1017.cpp: Motor: ON\r\n");
     motorOn = true;                         // Set ON flag
 
     return SUCCESS;
@@ -96,6 +115,7 @@
 AP1017::Status AP1017::stop(void)
 {
     motor->write(0.0);                      // turn motor off (duty cycle saved)
+    MSG("#AP1017.cpp: Motor: OFF\r\n");
     motorOn = false;                        // Set OFF flag
 
     return SUCCESS;
@@ -104,11 +124,18 @@
 AP1017::Status  AP1017::brake(void)
 {
     setDirection(DIRECTION_BRAKE);
+    MSG("#AP1017.cpp: Motor: BRAKE\r\n");
     return SUCCESS;
 }
 
 AP1017::Status  AP1017::coast(void)
 {
     setDirection(DIRECTION_COAST);
+    MSG("#AP1017.cpp: Motor: COAST\r\n");
     return SUCCESS;
+}
+
+bool AP1017::isMotorOn(void)
+{
+    return motorOn;
 }
\ No newline at end of file