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

Dependencies:   C12832_lcd HIDScope mbed-dsp mbed

Files at this revision

API Documentation at this revision

Comitter:
jessekaiser
Date:
Tue Jun 16 09:29:43 2015 +0000
Parent:
57:0a278c60d28b
Child:
59:ba22a8c26dee
Commit message:
Eindconcept 7. Gain al in EMG verwerking.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Jun 15 10:37:57 2015 +0000
+++ b/main.cpp	Tue Jun 16 09:29:43 2015 +0000
@@ -12,7 +12,7 @@
 #include "arm_math.h"
 //#include "HIDScope.h"
 
-#define K_Gain      60    //Gain of the filtered EMG signal
+#define K_Gain      10    //Gain of the filtered EMG signal
 #define Damp        5       //Deceleration of the motor
 #define Mass        1       // Mass value
 #define dt          0.10   //Sample frequency
@@ -36,9 +36,9 @@
 
 //Position sensors
 AnalogIn Posx(p19);
-AnalogIn Posy(p20); 
-DigitalOut Enablex(p25); 
-DigitalOut Enabley(p26); 
+AnalogIn Posy(p20);
+DigitalOut Enablex(p25);
+DigitalOut Enabley(p26);
 
 //Microstepping
 DigitalOut MS1(p27);
@@ -90,8 +90,6 @@
 
 //global variabels
 float filtered_biceps, filtered_triceps, filtered_pect, filtered_deltoid;
-float norm_biceps, norm_triceps, norm_pect, norm_deltoid;
-float gain_biceps, gain_triceps, gain_pect, gain_deltoid;
 float speed_old1, speed_old2, speed_old3, speed_old4;
 float acc1, acc2, acc3, acc4;
 float force1, force2, force3, force4;
@@ -108,25 +106,25 @@
 
     //process emg biceps
     arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 );   //High pass and notch filter
-    filtered_biceps = fabs(filtered_biceps);                                                //Rectifier
+    filtered_biceps = fabs(filtered_biceps)*K_Gain;                                                //Rectifier, The Gain is already implemented.
     arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_biceps, &filtered_biceps, 1 );    //low pass filter
 
     //process emg triceps
     arm_biquad_cascade_df1_f32(&highnotch_triceps, &emg_value2_f32, &filtered_triceps, 1 );
-    filtered_triceps = fabs(filtered_triceps);
+    filtered_triceps = fabs(filtered_triceps)*K_Gain;
     arm_biquad_cascade_df1_f32(&lowpass_triceps, &filtered_triceps, &filtered_triceps, 1 );
 
     //process emg pectoralis major
     arm_biquad_cascade_df1_f32(&highnotch_pect, &emg_value3_f32, &filtered_pect, 1 );
-    filtered_pect = fabs(filtered_pect);
+    filtered_pect = fabs(filtered_pect)*K_Gain;
     arm_biquad_cascade_df1_f32(&lowpass_pect, &filtered_pect, &filtered_pect, 1 );
 
     //process emg deltoid
     arm_biquad_cascade_df1_f32(&highnotch_deltoid, &emg_value4_f32, &filtered_deltoid, 1 );
-    filtered_deltoid = fabs(filtered_deltoid);
+    filtered_deltoid = fabs(filtered_deltoid)*K_Gain;
     arm_biquad_cascade_df1_f32(&lowpass_deltoid, &filtered_deltoid, &filtered_deltoid, 1 );
 
-    /*send value to PC. 
+    /*send value to PC.
     scope.set(0,filtered_biceps); //Filtered EMG signal
     scope.set(1,filtered_triceps);
     scope.set(2,filtered_pect);
@@ -136,9 +134,7 @@
 void looper_motory()
 {
     //Forward
-    norm_biceps = (filtered_biceps/MAX_bi);
-    gain_biceps = K_Gain*norm_biceps;
-    force1 = gain_biceps;
+    force1 = (filtered_biceps/MAX_bi); //MAX_bi is used to normalize the signal
     force1 = force1 - damping1;
     acc1 = force1/Mass;
     speed1 = speed_old1 + (acc1 * dt);
@@ -147,16 +143,14 @@
     speed_old1 = speed1;
 
     //Achteruit triceps
-    norm_triceps = (filtered_triceps/MAX_tri); 
-    gain_triceps = K_Gain*norm_triceps;
-    force2 = gain_triceps;
+    force2 = (filtered_triceps/MAX_tri);
     force2 = force2 - damping2;
     acc2 = force2/Mass;
     speed2 = speed_old2 + (acc2 * dt);
     damping2 = speed2 * Damp;
     step_freq2 = (setpoint*speed2);
     speed_old2 = speed2;
-    
+
     if (filtered_biceps > filtered_triceps || Posy < 0.20) {
         Diry = 1;
         speed2 = 0.01;
@@ -172,11 +166,13 @@
     //Speed limit
     if (speed1 > 1) {
         speed1 = 1;
-        step_freq1 = setpoint;
+        Stepy.period(1.0/setpoint);
+        //step_freq1 = setpoint;
     }
     if (speed2 > 1) {
         speed2 = 1;
-        step_freq2 = setpoint;
+        Stepy.period(1.0/setpoint);
+        //step_freq2 = setpoint;
     }
     //EMG treshold
     if (filtered_biceps < EMG_tresh1 && filtered_triceps < EMG_tresh2) {
@@ -194,9 +190,7 @@
 void looper_motorx()
 {
     //To the left
-    norm_pect = (filtered_pect/MAX_pect);
-    gain_pect = K_Gain*norm_pect;
-    force3 = gain_pect;
+    force3 = (filtered_pect/MAX_pect);
     force3 = force3 - damping3;
     acc3 = force3/Mass;
     speed3 = speed_old3 + (acc3 * dt);
@@ -205,16 +199,14 @@
     speed_old3 = speed3;
 
     //To the right
-    norm_deltoid = (filtered_deltoid/MAX_delt);
-    gain_deltoid = K_Gain*norm_deltoid;
-    force4 = gain_deltoid;
+    force4 = (filtered_deltoid/MAX_delt);
     force4 = force4 - damping4;
     acc4 = force4/Mass;
     speed4 = speed_old4 + (acc4 * dt);
     damping4 = speed4 * Damp;
     step_freq4 = (setpoint*speed4);
     speed_old4 = speed4;
-    
+
     if (filtered_pect > filtered_deltoid || Posx < 0.30) {
         Dirx = 0;
         speed4 = 0.01;
@@ -230,11 +222,13 @@
     //Speed limit
     if (speed3 > 1) {
         speed3 = 1;
-        step_freq3 = setpoint;
+        Stepx.period(1.0/setpoint);
+        //step_freq3 = setpoint;
     }
     if (speed4 > 1) {
         speed4 = 1;
-        step_freq4 = setpoint;
+        Stepx.period(1.0/setpoint); 
+        //step_freq4 = setpoint;
     }
     //EMG treshold
     if (filtered_pect < EMG_tresh3 && filtered_deltoid < EMG_tresh4) {
@@ -251,7 +245,7 @@
 int main()
 {
     // Attach the HIDScope::send method from the scope object to the timer at 500Hz. Hier wordt de sample freq aangegeven.
-   // scopeTimer.attach_us(&scope, &HIDScope::send, 2e3);
+    // scopeTimer.attach_us(&scope, &HIDScope::send, 2e3);
 
     Ticker emgtimer;    //biceps
     arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states);
@@ -279,19 +273,19 @@
     MS3 = 0;
     Stepx.write(0.5); // Duty cycle of 50%
     Stepy.write(0.5);
-    
+
     //if (Posx < 0.50) {
-      //  Stepx.period(1.0/3000); //if the speed and distance is known. The motor can be brought to a home position.
-        //Need to know rpm to m/s.  
-        
+    //  Stepx.period(1.0/3000); //if the speed and distance is known. The motor can be brought to a home position.
+    //Need to know rpm to m/s.
+
 
     while (1) {
-        
+
         //lcd.printf("x %.2f, y %.2f \n", Posx.read(), Posy.read());
         lcd.printf("%.2f, %.2f %.2f %.2f \n", filtered_biceps, filtered_triceps, filtered_pect, filtered_deltoid); //Filtered EMG values
         //lcd.printf("1 %.0f, 2 %.0f, 3 %.0f, 4 %.0f \n", step_freq1, step_freq2, step_freq3, step_freq4); //step_freq value of every EMG sensor
-        //lcd.printf("%.2f, %.2f %.2f %.2f \n", gain_biceps, gain_triceps, gain_pect, gain_deltoid); 
-    //lcd.printf("%.2f, %.2f %.2f %.2f \n", norm_biceps, norm_triceps, norm_pect, norm_deltoid);
-       wait(0.01);
+        //lcd.printf("%.2f, %.2f %.2f %.2f \n", gain_biceps, gain_triceps, gain_pect, gain_deltoid);
+        //lcd.printf("%.2f, %.2f %.2f %.2f \n", norm_biceps, norm_triceps, norm_pect, norm_deltoid);
+        wait(0.01);
     }
 }