motor controller with P velocity control

Dependencies:   HIDScope PID QEI mbed EMG

Fork of PID_VelocityExample by Aaron Berk

Revision:
4:be465e9a12cb
Parent:
3:4c93be3a9010
Child:
5:8ae6d935a16a
diff -r 4c93be3a9010 -r be465e9a12cb main.cpp
--- a/main.cpp	Thu Sep 24 11:25:06 2015 +0000
+++ b/main.cpp	Thu Sep 24 12:46:27 2015 +0000
@@ -3,6 +3,7 @@
 //****************************************************************************/
 #include "PID.h"
 #include "QEI.h"
+ #include "HIDScope.h"        // Require the HIDScope library
 
 //****************************************************************************/
 // Defines
@@ -16,6 +17,8 @@
 // Globals
 //****************************************************************************/
 Serial pc(USBTX, USBRX);
+HIDScope    scope(2);        // Instantize a 2-channel HIDScope object
+Ticker      scopeTimer;      // Instantize the timer for sending data to the PC 
 //--------
 // Motors 
 //--------
@@ -59,7 +62,7 @@
     leftMotor.period_us(50);
     leftMotor = 1.0;
     leftBrake = 0.0;
-    leftDirection = 0;
+    leftDirection = 1;
 
 }
 
@@ -73,7 +76,7 @@
 }
 
 int main() {
-
+    scopeTimer.attach_us(&scope, &HIDScope::send, 1e4);
     //Initialization.
     initializeMotors();
     initializePidControllers();
@@ -85,19 +88,23 @@
 
     //Set velocity set point.
     
-    leftMotor.period_ms(0.1);
+    
     
     //Run for 3 seconds.
-    while (endTimer.read() < 10){
-        measure = pot1.read()*10000;
+    while (endTimer.read() < 100){
+        measure = pot1.read()*10500;
         leftController.setSetPoint(measure);
         leftPulses = leftQei.getPulses();
         leftVelocity = (leftPulses - leftPrevPulses) / RATE;
         leftPrevPulses = leftPulses;
         
-        leftController.setProcessValue(leftVelocity);
+        scope.set(0, measure);
+        scope.set(1, leftVelocity);
+        
+        leftController.setProcessValue(-leftVelocity);
         leftPwmDuty = leftController.compute();
         leftMotor = leftPwmDuty;
+       
         pc.printf("leftpusles: %d, lefVelocity: %f, leftPwmDuty: %f \n\r",leftPulses,leftVelocity,leftPwmDuty);
 
         //fprintf(fp, "%f,%f\n", leftVelocity, goal);