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:
Mon Jun 01 11:25:19 2015 +0000
Parent:
37:3b9b18450612
Child:
39:191ae0d12bd6
Commit message:
Poging 1, twee emg signalen, 2 directies;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed May 27 14:55:50 2015 +0000
+++ b/main.cpp	Mon Jun 01 11:25:19 2015 +0000
@@ -7,7 +7,8 @@
 #define Damp        4       //Deceleration of the motor
 #define Mass        1       // Mass value
 #define dt          0.002   //Sample frequency
-#define MAX_emg     0.05      //Can be used for normalisation of the EMG signal
+#define MAX_bi      0.08      //Can be used for normalisation of the EMG signal of the biceps
+#define MAX_tri     0.06
 #define MIN_freq    900     //The motor turns off below this frequency
 
 //Motor control
@@ -27,8 +28,10 @@
 //Potmeter and EMG
 AnalogIn Pot1(p19);
 AnalogIn Pot2(p20);
-AnalogIn emg0(p17);
-HIDScope scope(1);
+
+AnalogIn emg1(p17); //EMG bordje bovenop, biceps
+AnalogIn emg2(p15); //triceps
+HIDScope scope(2);
 Ticker   scopeTimer;
 
 //lcd
@@ -40,22 +43,27 @@
 
 //EMG filter
 arm_biquad_casd_df1_inst_f32 lowpass_biceps;
-//lowpass filter settings biceps: Fc = 2 Hz, Fs = 500 Hz, Gain = -3 dB
+arm_biquad_casd_df1_inst_f32 lowpass_triceps;
+//lowpass filter settings: Fc = 2 Hz, Fs = 500 Hz, Gain = -3 dB
 float lowpass_const[] = {0.00015514839749793376, 0.00031029679499586753, 0.00015514839749793376, 1.9644602512795832, -0.9650808448695751};
 arm_biquad_casd_df1_inst_f32 highnotch_biceps;
+arm_biquad_casd_df1_inst_f32 highnotch_triceps;
 //highpass filter settings: Fc = 20 Hz, Fs = 500 Hz, notch Fc = 50, Fs = 500 Hz
 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
 float lowpass_biceps_states[4];
 float highnotch_biceps_states[8];
-float lowpass_pot_states[4];
+float lowpass_triceps_states[4];
+float highnotch_triceps_states[8];
 
 //global variabels
 float filtered_biceps;
+float filtered_triceps;
 float speed_old;
 float acc;
-float force;
+float force1;
+float force2;
 float speed;
 float D;
 
@@ -64,67 +72,91 @@
 
 
     float emg_value1_f32;
-    emg_value1_f32 = emg0.read();
+    emg_value1_f32 = emg1.read();
+
+    float emg_value2_f32;
+    emg_value2_f32 = emg2.read();
 
     //process emg biceps
     arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 );
     filtered_biceps = fabs(filtered_biceps);
     arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_biceps, &filtered_biceps, 1 );
 
+    //process emg triceps
+    arm_biquad_cascade_df1_f32(&highnotch_triceps, &emg_value2_f32, &filtered_triceps, 1 );
+    filtered_triceps = fabs(filtered_triceps);
+    arm_biquad_cascade_df1_f32(&lowpass_triceps, &filtered_triceps, &filtered_triceps, 1 );
 
     /*send value to PC. */
     scope.set(0,filtered_biceps); //Filtered EMG signal
+    scope.set(1,filtered_triceps);
 }
 
 void looper_motor()
 {
-    Dir = 0;
-    force = K_Gain*(filtered_biceps/MAX_emg);
-    force = force - D;
-    acc = force/Mass;
-    speed = speed_old + (acc * dt);
-    D = speed * Damp;
-     if (speed > 1) {
-        speed = 1;
-        step_freq = setpoint; 
+    
+        if (filtered_biceps > filtered_triceps) {
+            Dir = 0;
+            force2 = 0;
+            force1 = K_Gain*(filtered_biceps/MAX_bi);
+            force1 = force1 - D;
+            acc = force1/Mass;
+            speed = speed_old + (acc * dt);
+            D = speed * Damp;
+    
+        } else {
+            Dir = 1;
+            force1 = 0;
+            force2 = K_Gain*(filtered_triceps/MAX_tri);
+            force2 = force2 - D;
+            acc = force2/Mass;
+            speed = speed_old + (acc * dt);
+            D = speed * Damp;
+           
+            
         }
-        else {
-    step_freq = (setpoint*speed);
-    }
-    Step.period(1.0/step_freq);
-    speed_old = speed;
-   
-
-    if (step_freq < MIN_freq) {
-        Enable = 1; 
-    } else {
-        Enable = 0;
-    }
-}
+        //Speed limit
+        if (speed > 1) {
+            speed = 1;
+            step_freq = setpoint;
+        } else {
+            step_freq = (setpoint*speed);
+        }
+        Step.period(1.0/step_freq);
+        speed_old = speed;
+        
 
-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);
+        if (step_freq < MIN_freq) {
+            Enable = 1;
+        } else {
+            Enable = 0;
+        }
+    }
 
-    Ticker emgtimer;
-    arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states);
-    arm_biquad_cascade_df1_init_f32(&highnotch_biceps, 2 , highnotch_const, highnotch_biceps_states);
-    emgtimer.attach(looper_emg, 0.002);
-
-    Ticker looptimer;
-    looptimer.attach(looper_motor, 0.01); //Uitzoeken waarom deze frequentie!
+    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);
 
-    //Microstepping control
-    MS1 = 1;
-    MS2 = 0;
-    MS3 = 0;
-    Step.write(0.5); // Duty cycle van 50%
+        Ticker emgtimer;
+        arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states);
+        arm_biquad_cascade_df1_init_f32(&highnotch_biceps, 2 , highnotch_const, highnotch_biceps_states);
+        arm_biquad_cascade_df1_init_f32(&lowpass_triceps, 1 , lowpass_const, lowpass_triceps_states);
+        arm_biquad_cascade_df1_init_f32(&highnotch_triceps, 2 , highnotch_const, highnotch_triceps_states);
+        emgtimer.attach(looper_emg, 0.002);
+
+        Ticker looptimer;
+        looptimer.attach(looper_motor, 0.01); //Uitzoeken waarom deze frequentie!
 
-    while (1) {
+        //Microstepping control
+        MS1 = 1;
+        MS2 = 0;
+        MS3 = 0;
+        Step.write(0.5); // Duty cycle van 50%
 
-        lcd.printf("Freq %.0f Hz, %.2f \n", step_freq, speed); //snelheid meting op lcd
-        //pc.printf("%.3f \n", emg0.read());
-        wait(0.01);
+        while (1) {
+
+            lcd.printf("Freq %.0f Hz, %.2f \n", step_freq, speed); //snelheid meting op lcd
+            //pc.printf("%.3f \n", emg0.read());
+            wait(0.01);
+        }
     }
-}