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:
20:e00e41e3cda8
Parent:
19:1353ba4d94db
Child:
21:5f88e09d6ab8
diff -r 1353ba4d94db -r e00e41e3cda8 main.cpp
--- a/main.cpp	Mon Oct 22 08:54:04 2018 +0000
+++ b/main.cpp	Sat Oct 27 15:55:00 2018 +0000
@@ -2,6 +2,7 @@
 #include "FastPWM.h"    
 #include "MODSERIAL.h"
 #include "QEI.h"
+#include "HIDScope.h"
 #include <math.h>
 MODSERIAL pc(USBTX, USBRX);
 DigitalOut motor1DirectionPin(D7);
@@ -9,11 +10,14 @@
 AnalogIn potMeter1(A4);
 AnalogIn potMeter2(A5);
 InterruptIn button2(D3);
+DigitalOut led(LED1);
 QEI Encoder (D12, D13, NC, 64, QEI::X4_ENCODING);
+HIDScope scope(1);
 
 //Tickers
 Ticker MeasureControl;
 Ticker print;
+Ticker SenttoHidscope;
 
 //Global variables
 volatile double measuredPosition = 0.0;
@@ -23,15 +27,18 @@
 volatile double Ki = 1.0; //dit moeten we bepalen met een plot bijvoorbeeld
 volatile double Kd = 0.0;
 volatile double Ts = 0.01;
+volatile double measuredVelocity = 0.0;
+volatile double tickertime = 0.001;
 
 //------------------------------------------------------------------------------
 // Functions
-double Filters()
+/*double EMGFilters()
 {
     static BiQuad Notchfilter(1.0000, -1.6180, 1.0000, 1.0000, -1.5687, 0.9391);
     static BiQuad HighPassFilter (1.0000, -2.0000, 1.0000, 1.0000, -1.8268, 0.8407);
     static BiQuad LowPassFilter (1.0000, 2.0000, 1.0000, 1.0000, 0.3172, 0.1894);
-}
+}*/
+
 double GetReferencePosition()
 {
    double potMeterIn = potMeter1.read();
@@ -102,7 +109,16 @@
     SetMotor1(motorValue);
 }
 
-void printen()
+void hidscope()
+{
+    double OldmeasuredPosition = measuredPosition;
+    double measuredVelocity = (measuredPosition - OldmeasuredPosition) / tickertime;
+    scope.set(0, measuredVelocity); // set the encoder pulses in channel 0
+    scope.send();
+    led = !led;
+}
+
+/*void printen()
 {
     pc.baud (115200);
     pc.printf("Referenceposition %f \r\n", referencePosition);
@@ -111,7 +127,7 @@
     pc.printf("Proportional gain %f \r\n", Kp);
     pc.printf("Integral gain %f \r\n", Ki);
     pc.printf("Derivative gain %f \r\n", Kd);
-}
+}*/
 //-----------------------------------------------------------------------------
 int main()
 {
@@ -119,7 +135,8 @@
     pc.baud(115200);
     motor1MagnitudePin.period_us(60.0); // 60 microseconds PWM period: 16.7 kHz.
     MeasureControl.attach(MeasureAndControl, 0.01);
-    print.attach(printen, 3);
+    SenttoHidscope.attach(hidscope, 0.02);
+    /*print.attach(printen, 3);*/
     
     //Other initializations