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:
1:4d4c77589134
Parent:
0:a0435a630c5d
Child:
3:f8e70f639ed0
--- a/AP1017.cpp	Fri Apr 14 19:35:08 2017 +0000
+++ b/AP1017.cpp	Mon Apr 17 19:08:36 2017 +0000
@@ -3,21 +3,19 @@
 /******************** Constructors & Destructors ****************************/
 
 // Default constructor
-AP1017::AP1017(void) : motorOn(false), dutyCycle(0.50), pwmPeriod_us(200),
-                        motor(ENABLE_PIN), inA(INPUTA_PIN), inB(INPUTB_PIN)
+AP1017::AP1017(void) : motorOn(false), dutyCycle(0.0), freq_hz(0.0), 
+                        pulseWidth_us(0.0), pwmPeriod_us(0.0)
 {
-    freq_hz = 0.000001/pwmPeriod_us;            // Derive frequency
-    pulseWidth_us = dutyCycle * pwmPeriod_us;   // Derive pulse width
-    stopMotor();                                // Keep motor off
+    motor = NULL;
+    inA = NULL;
+    inB = NULL;
 }
 
 // Default destructor
 AP1017::~AP1017(void)
 {
     stopMotor();
-    
-    if(ap1017)
-        delete ap1017;
+    delete inA, inB, motor;
 }
 
 /*********************** Member Functions ***********************************/
@@ -29,25 +27,25 @@
         case DIRECTION_CW:
             if(motorOn == true && dir == DIRECTION_CCW)
                 return ERROR_MOTORON;
-            inA.write(1);
-            inB.write(0);
+            inA->write(1);
+            inB->write(0);
             motorOn = true;
             break;
         case DIRECTION_CCW:
             if(motorOn == true && dir == DIRECTION_CW)
                 return ERROR_MOTORON;
-            inA.write(0);
-            inB.write(1);
+            inA->write(0);
+            inB->write(1);
             motorOn = true;
             break;
         case DIRECTION_BRAKE:
-            inA.write(1);
-            inB.write(1);
+            inA->write(1);
+            inB->write(1);
             motorOn = true;
             break;
         case DIRECTION_COAST:
-            inA.write(0);
-            inB.write(0);
+            inA->write(0);
+            inB->write(0);
             motorOn = false;
             break;
         default:
@@ -65,7 +63,7 @@
         pulseWidth_us = dutyCycle * pwmPeriod_us; // Override pulse width
 
         if(motorOn == true){
-            motor.write(dutyCycle);          // If motor is on, keep it on
+            motor->write(dutyCycle);          // If motor is on, keep it on
         }
 
     }
@@ -104,7 +102,7 @@
     freq_hz = 1/(per * 0.000001);   // Calculate the frequency
 
     if(motorOn == true){                // If motor on, keep it on
-        motor.period_us(pwmPeriod_us);
+        motor->period_us(pwmPeriod_us);
     }
 
     return SUCCESS;
@@ -126,9 +124,9 @@
 
 AP1017::Status AP1017::startMotor(void)
 {
-    motor.period_us(pwmPeriod_us);          // Set the period
-    motor.pulsewidth_us(pulseWidth_us);     // Set the pulse width
-    motor.write(dutyCycle);                 // Set duty cycle, start motor
+    motor->period_us(pwmPeriod_us);          // Set the period
+    motor->pulsewidth_us(pulseWidth_us);     // Set the pulse width
+    motor->write(dutyCycle);                 // Set duty cycle, start motor
     motorOn = true;                         // Set ON flag
 
     return SUCCESS;
@@ -137,7 +135,7 @@
 
 AP1017::Status AP1017::stopMotor(void)
 {
-    motor.write(0.0);                       // turn motor off (duty cycle saved)
+    motor->write(0.0);                       // turn motor off (duty cycle saved)
     motorOn = false;                        // Set OFF flag
 
     return SUCCESS;