Using HIDScope for P(I)D controller

Dependencies:   FastPWM HIDScope MODSERIAL QEI biquadFilter mbed

Fork of PES_tutorial_5 by BMT Module 9 Group 4

Revision:
14:29236a33b5e4
Parent:
13:0b51846cf9e3
Child:
15:c2cfab737a4c
--- a/main.cpp	Tue Oct 16 09:06:44 2018 +0000
+++ b/main.cpp	Fri Oct 19 09:02:56 2018 +0000
@@ -13,12 +13,13 @@
 
 //Tickers
 Ticker MeasureControl;
-/*Ticker MakeMotorRotate;*/
+Ticker print;
+/*Ticker MakeMotorRotate;*/ //Waar zal deze ticker voor dienen?
 
 //Global variables
 volatile double measuredPosition = 0.0;
 volatile double referencePosition = 0.0; 
-volatile double motorValue = 0.0;
+volatile double motorValue= 0.01;
 volatile double Kp = 0.0;
 
 //------------------------------------------------------------------------------
@@ -27,23 +28,23 @@
 double GetReferencePosition()
 {
    double potMeterIn = potMeter1.read();
-   referencePosition = 4.0*3.14*potMeterIn - 2.0*3.14 ; // Reference value y, scaled to -2 to 2 revolutions (or 0 to 100 pi)
+   referencePosition = 4.0*3.14*potMeterIn - 2.0*3.14 ; // Reference value y, scaled to -2 to 2 revolutions (or 0 to 100 pi) WAAROM?
    return referencePosition;
 }
 
 double GetMeasuredPosition()
 {
     double counts = Encoder.getPulses();
-    measuredPosition = ( counts / 4.0 * 64.0) * 6.28; // Rotational position in radians 
+    measuredPosition = ( counts / (8400)) * 6.28; // Rotational position in radians 
     return measuredPosition;  
 } 
 
-double FeedbackControl(double Error)
-{  
+double FeedbackControl(double Error)
+{  
     double Kp = 20.0*potMeter2.read(); // Scale 0 to 20
-    // Proportional part:  
+    // Proportional part: 
     double u_k = Kp * Error;
-    // Sum all parts and return it 
+    // Sum all parts and return it 
     return u_k; //motorValue
  }
  
@@ -53,7 +54,7 @@
     // bits for motor 1. Positive value makes motor rotating
     // clockwise. motorValues outside range are truncated to
     // within range
-    if (motorValue >=0) 
+    if (motorValue >=0)
     {
         motor1DirectionPin=1;
     }
@@ -61,7 +62,7 @@
     {
         motor1DirectionPin=0;
     }
-    if (fabs(motorValue)>1) 
+    if (fabs(motorValue)>1)
     {
         motor1MagnitudePin = 1;
     }
@@ -71,18 +72,25 @@
     }
 }
 //-----------------------------------------------------------------------------
-// Ticker
+// Tickers
 void MeasureAndControl(void)
 {
-    // This function determines the desired velocity, measures the   
+    // This function determines the desired velocity, measures the   
     // actual velocity, and controls the motor with 
     // a simple Feedback controller. Call this from a Ticker.
-    double referencePosition = GetReferencePosition();
-    double measuredPosition = GetMeasuredPosition();
-    double motorValue = FeedbackControl(referencePosition - measuredPosition);
+    referencePosition = GetReferencePosition();
+    measuredPosition = GetMeasuredPosition();
+    motorValue = FeedbackControl(referencePosition - measuredPosition);
     SetMotor1(motorValue);
 }
 
+void printen()
+{
+    pc.baud (115200);
+    pc.printf("Referenceposition %f \r\n", referencePosition);
+    pc.printf("Measured position %f \r\n", measuredPosition);
+    pc.printf("Motorvalue %f \r\n", motorValue);
+}
 //-----------------------------------------------------------------------------
 int main()
 {
@@ -90,7 +98,7 @@
     pc.baud(115200);
     motor1MagnitudePin.period_us(60.0); // 60 microseconds PWM period: 16.7 kHz.
     MeasureControl.attach(MeasureAndControl, 0.01);
-    /*MakeMotorRotate.attach(SetMotor1, 0.01);*/
+    print.attach(printen, 3);
     
     //Other initializations