Control up to two motors using filtered EMG signals and a PID controller

Dependencies:   FastPWM HIDScope MODSERIAL QEI Matrix biquadFilter controller errorFetch mbed motorConfig refGen MatrixMath inverseKinematics

Fork of Minor_test_serial by First Last

Revision:
16:27430afe663e
Parent:
15:b76b8cff4d8f
Child:
17:616ce7bc1f96
--- a/main.cpp	Thu Sep 21 11:00:55 2017 +0000
+++ b/main.cpp	Thu Sep 21 11:20:03 2017 +0000
@@ -9,6 +9,7 @@
 
 QEI Encoder(D12,D13,NC,64, QEI::X4_ENCODING);
 MODSERIAL pc(USBTX, USBRX);
+HIDScope scope(2);
 
 // Defining outputs
 DigitalOut motor1_direction(D4);
@@ -17,23 +18,37 @@
 // Defining inputs
 InterruptIn sw2(SW2);
 InterruptIn sw3(SW3);
+InterruptIn button1(D2);
 AnalogIn pot(A0);
 
-// Enabling motors
-bool motorsOn = true;
+// PWM settings
+float pwmPeriod = 1.0/5000.0;
+int frequency_pwm = 10000; //10kHz PWM
 
+volatile float x;
+volatile float x_prev =0; 
+volatile float y; // filtered 'output' of ReadAnalogInAndFilter 
+volatile float ledBrightness = 0.00;
 
-float pwmPeriod = 1.0/5000.0;
 
 Ticker encoderTicker;
 volatile int counts = 0;
 volatile float revs = 0.00;
 
+
+
 void readEncoder(){
     counts = Encoder.getPulses();
     revs = counts/64.0f;
-    pc.printf("%i pulses \r\n", counts);
-    pc.printf("%f revolutions \r\n", revs);
+    
+    // Displaying revs in HIDscope
+    x = revs;   // Capture data
+    scope.set(0, x);   // store data in first element of scope memory
+    y = (x_prev + x)/2.0;   // averaging filter
+    scope.set(1, y);  // store data in second element of scope memory
+    x_prev = x; // Prepare for next round
+    
+    scope.send(); // send what's in scope memory to PC
     }
     
 void killSwitch(){
@@ -51,16 +66,15 @@
     motor1_direction = !motor1_direction;
     }
 
-int frequency_pwm = 10000; //10kHz PWM
-
 int main()
     {
     motor1_direction = false;
     motor1_pwm.period(pwmPeriod);//T=1/f 
     sw2.fall(&killSwitch);
     sw3.fall(&turnMotorsOn);
+    button1.rise(&M1switchDirection);
     pc.baud(115200);
-    encoderTicker.attach(readEncoder, 1.0);
+    encoderTicker.attach(readEncoder, 0.1);
       
     pc.printf("Encoder ticker attached and baudrate set");