4 directional EMG control of the XY table. Made during my bachelor end assignment.

Dependencies:   C12832_lcd HIDScope mbed-dsp mbed

Revision:
31:372ff8d49430
Parent:
30:0a8f849e0292
Child:
32:46b18f465600
--- a/main.cpp	Thu May 21 10:15:37 2015 +0000
+++ b/main.cpp	Thu May 21 15:38:12 2015 +0000
@@ -3,6 +3,11 @@
 #include "arm_math.h"
 #include "HIDScope.h"
 
+#define P_Gain 0.995
+#define tres_bi 0.05    //Biceps emg treshold
+#define Mass 1          // Mass value
+#define dt 0.002        //Sample frequency
+
 //Motor control
 DigitalOut Dir(p21);
 PwmOut Step(p22);
@@ -19,6 +24,7 @@
 
 //Potmeter and EMG
 AnalogIn Pot1(p19);
+AnalogIn Pot2(p20);
 AnalogIn emg0(p17);
 HIDScope scope(2);
 Ticker   scopeTimer;
@@ -37,7 +43,7 @@
 
 //lowpass filter settings: Fc = 1 Hz, Fs = 100 Hz, Gain = -3 dB onepole-lp
 float lowpass_const[] = {0.0201, 0.0402 , 0.0201, 1.5610, -0.6414};
-//Lowpass filter potmeter: Fc = 0.5 Hz, Fs = 500 Hz, 
+//Lowpass filter potmeter: Fc = 0.5 Hz, Fs = 500 Hz,
 //float lowpass_const[] = {0.000009825916403675327, 0.000019651832807350654, 0.000009825916403675327, 1.991114207740345, -0.9911535114059596};
 //lowpass for step_freq: Fc = 2 Hz, Fs = 100, Gain = 6 dB
 //float lowpass1_const[] = {0.007820199259120319, 0.015640398518240638, 0.007820199259120319, 1.7347238224240125, -0.7660046194604936};
@@ -47,8 +53,8 @@
 //lowpass filter settings biceps: Fc = 2 Hz, Fs = 500 Hz, Gain = -3 dB
 float lowpass2_const[] = {0.00015514839749793376, 0.00031029679499586753, 0.00015514839749793376, 1.9644602512795832, -0.9650808448695751};
 arm_biquad_casd_df1_inst_f32 highnotch_biceps;
-//highpass filter settings: Fc = 10 Hz, Fs = 500 Hz, Gain = -3 dB, notch Fc = 50, Fs =500Hz, Gain = -3 dB
-float highnotch_const[] = {0.9149684297741606, -1.8299368595483212, 0.9149684297741606, 1.8226935021735358, -0.8371802169231065 ,0.7063988100714527, -1.1429772843080923, 0.7063988100714527, 1.1429772843080923, -0.41279762014290533};
+//highpass filter settings: Fc = 20 Hz, Fs = 500 Hz, Gain = -3 dB, notch Fc = 50, Fs =500Hz, Gain = -3 dB
+float highnotch_const[] = {0.8370879899975344, -1.6741759799950688, 0.8370879899975344, 1.6474576182593796, -0.7008943417307579, 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, 1.1429772843080923, -0.41279762014290533};
 
 
 //state values
@@ -65,7 +71,14 @@
 float filtered_step;
 float pot_value1_f32;
 float filt_avg_bi_old;
-float speed_old; 
+float speed_old;
+float acc;
+float force;
+float spd;
+float spd_old;
+float D = 0;
+float Damp;
+float K_Gain;
 
 void average_biceps(float filtered_biceps,float *average)
 {
@@ -86,7 +99,7 @@
     volatile uint16_t emg_value1;
 
     float emg_value1_f32;
- 
+
 
     /*put raw emg value both in red and in emg_value*/
     emg_value1 = emg0.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V)
@@ -94,31 +107,31 @@
 
     //process emg biceps
     arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 );
-    filtered_biceps = fabs(filtered_biceps)*10;
+    filtered_biceps = fabs(filtered_biceps);
     arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_biceps, &filtered_biceps, 1 );
     average_biceps(filtered_biceps,&filtered_average_bi);
 
     /*send value to PC. */
     scope.set(0,filtered_average_bi);  //Raw EMG signal biceps
     scope.set(1,filtered_biceps); //Filtered signal
-    
+
 }
-void looper_pot()
+/*void looper_pot()
 {
 
     pot_value1_f32 = Pot1.read() - 0.500;
 
     //process input
     arm_biquad_cascade_df1_f32(&lowpass_pot, &pot_value1_f32, &filtered_pot, 1 );
-     
-}
+
+}*/
 
 
-void looper_motor()
+/*void looper_motor()
 {
     float new_step_freq;
     float speed;
-    
+
     speed = 0.02*filtered_average_bi + 0.02*filt_avg_bi_old + 0.96*speed_old; //Value between 0 and 1
     new_step_freq = (setpoint*speed);
     step_freq = abs(new_step_freq); //Gives the PWM frequenty to the motor.
@@ -130,18 +143,58 @@
     } else {
         Enable = 0;
     }
-    Step.period(1.0/(100 + step_freq)); //Step_freq is het aantal Hz. 
+    Step.period(1.0/(100 + step_freq)); //Step_freq is het aantal Hz.
+
+}*/
+//Motor accelereren met EMG treshold
+/*void looper_motor()
+{
+    float new_step_freq;
+    Dir = 0;
+
+    if (filtered_average_bi > tres_bi) {
+        Enable = 0;
+        new_step_freq = ((1-P)*setpoint) + (P*step_freq);
+        step_freq = new_step_freq;
+        Step.period(1.0/step_freq);
+    } else {
+        Enable = 1;
+        step_freq = 1;
+    }
+
+}*/
 
+void looper_motor()
+{
+    Dir = 0;
+    K_Gain = 20*Pot2.read();
+    force = K_Gain*filtered_biceps;
+    force = force - D;
+    acc = force/Mass;
+    spd = spd_old + (acc * dt);
+    Damp = 2*Pot1.read();
+    D = spd * Damp;
+    step_freq = (setpoint*spd);
+    Step.period(1.0/step_freq);
+    spd_old = spd;
+
+    if (step_freq < 800) {
+        Enable = 1;
+    } else {
+        Enable = 0;
+    }
 }
+
+
 int main()
 {
-    // Attach the HIDScope::send method from the scope object to the timer at 50Hz. Hier wordt de sample freq aangegeven. 
-    scopeTimer.attach_us(&scope, &HIDScope::send, 2e3); 
-    
-   /* Ticker log_timer;
-    //set up filters. Use external array for constants
-    arm_biquad_cascade_df1_init_f32(&lowpass_pot, 1 , lowpass_const, lowpass_pot_states);
-    log_timer.attach(looper_pot, 0.01);*/
+    // Attach the HIDScope::send method from the scope object to the timer at 50Hz. Hier wordt de sample freq aangegeven.
+    scopeTimer.attach_us(&scope, &HIDScope::send, 2e3);
+
+    /* Ticker log_timer;
+     //set up filters. Use external array for constants
+     arm_biquad_cascade_df1_init_f32(&lowpass_pot, 1 , lowpass_const, lowpass_pot_states);
+     log_timer.attach(looper_pot, 0.01);*/
 
     Ticker emgtimer;
     arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states);
@@ -149,7 +202,7 @@
     emgtimer.attach(looper_emg, 0.002);
 
     Ticker looptimer;
-    looptimer.attach(looper_motor, 0.002);
+    looptimer.attach(looper_motor, 0.01);
 
     MS1 = 1;
     MS2 = 0;