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 22 20:22:57 2015 +0000
Parent:
81:4263d0ce34d3
Child:
83:067e07db027c
Commit message:
The Y-motor works in this way. X-motor control commented

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Jun 22 15:18:08 2015 +0000
+++ b/main.cpp	Mon Jun 22 20:22:57 2015 +0000
@@ -13,18 +13,22 @@
 //#include "HIDScope.h"
 
 #define P_Gain      0.99
-#define K_Gain      150     //Gain of the filtered EMG signal
-#define Damp        5       //Deceleration of the motor
-#define Mass        1       // Mass value
-#define dt          0.01    //Sample frequency
+#define K_Gain      150   //Gain of the filtered EMG signal
+#define Damp        5    //Deceleration of the motor
+#define Mass        1    // Mass value
+#define dt          0.01 //Sample frequency
+#define MAX_bi      0.40 //Can be used for normalisation of the EMG signal of the biceps
+#define MAX_tri     0.60
+#define MAX_pect    0.48
+#define MAX_delt    1.07
 #define EMG_tresh1   0.01
 #define EMG_tresh2   0.01
 #define EMG_tresh3   0.01
 #define EMG_tresh4   0.01
 #define H_Gain  3.5
-#define Pt_x    0.83
-#define Pt_y    0.25
-#define error_tresh 0.01
+#define Pt_x    0.50
+#define Pt_y    0.50
+#define error_tresh 0.03
 
 //Motor control
 DigitalOut Dirx(p21);
@@ -47,15 +51,15 @@
 DigitalOut MS3(p29);
 
 //EMG inputs
-AnalogIn emg1(p15);
-AnalogIn emg2(p16);
+AnalogIn emg1(p15); //EMG bordje bovenop, biceps
+AnalogIn emg2(p16); //triceps
 AnalogIn emg3(p17);
 AnalogIn emg4(p18);
 
 //HIDScope scope(4);
 //Ticker   scopeTimer;
 
-//lcd screen
+//lcd
 C12832_LCD lcd;
 
 //Variables for motor control
@@ -69,7 +73,7 @@
 arm_biquad_casd_df1_inst_f32 lowpass_triceps;
 arm_biquad_casd_df1_inst_f32 lowpass_pect;
 arm_biquad_casd_df1_inst_f32 lowpass_deltoid;
-//lowpass filter settings: Fc = 2 Hz, Fs = 500 Hz
+//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;
@@ -90,11 +94,11 @@
 
 //global variabels
 float filtered_biceps, filtered_triceps, filtered_pect, filtered_deltoid;
-float speed_old1, speed_old2;
-float acc1, acc2;
-float force1, force2;
-float speed1, speed2;
-float damping1, damping2;
+float speed_old1, speed_old2, speed_old3, speed_old4;
+float acc1, acc2, acc3, acc4;
+float force1, force2, force3, force4;
+float speed1, speed2, speed3, speed4;
+float damping1, damping2, damping3, damping4;
 float emg_x, emg_y;
 float cx = 0;
 float cy = 0;
@@ -144,6 +148,7 @@
 
 void looper_motory()
 {
+
     emg_y = (filtered_biceps - filtered_triceps);
     emg_y_abs = fabs(emg_y);
     force1 = emg_y_abs*K_Gain;
@@ -173,6 +178,7 @@
     } else {
         Enabley = 0;
     }
+
 }
 
 /*void looper_motorx()
@@ -213,86 +219,99 @@
 {
     // 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);
-    /*
-        MS1 = 1;
-        MS2 = 0;
-        MS3 = 0;
+/*
+    MS1 = 1;
+    MS2 = 0;
+    MS3 = 0;
+
+    Stepx.write(0.5); // Duty cycle of 50%
+    Stepy.write(0.5);
 
-        Stepx.write(0.5); // Duty cycle of 50%
-        Stepy.write(0.5);
+    Enablex = 1;
+    Enabley = 1;
+    wait(1);
+    lcd.printf("Start homing");
+    wait(2);
+    lcd.cls();
+    wait(1);
+    Enablex = 0;
+    Enabley = 0;
+    while(errorx > error_tresh || errory > error_tresh) {
 
-        Enablex = 1;
-        Enabley = 1;
-        wait(1);
-        lcd.printf("Start homing");
-        wait(2);
-        lcd.cls();
-        wait(1);
-        Enablex = 0;
-        Enabley = 0;
+        Ps_x = Posx.read();
+        Ps_y = Posy.read();
+        errorx = fabs(Pt_x - Ps_x);
+        errory = fabs(Ps_y - Pt_y);
+        lcd.printf("%.2f %.2f \n", errorx, errory);
+        
+        
+        if (Ps_x < 0.50 && errorx > error_tresh) {
+            Dirx = 0;
+            //errorx = Pt_x - Ps_x;
+            cx = errorx * H_Gain;
 
-        //Homing of the motor, so you start from the same position every time.
-        while(errorx > error_tresh || errory > error_tresh) {
-
-            Ps_x = Posx.read();
-            Ps_y = Posy.read();
-            errorx = fabs(Pt_x - Ps_x);
-            errory = fabs(Ps_y - Pt_y);
-            lcd.printf("%.2f %.2f \n", Stepx.read(), Stepy.read());
-
+            float hnew_step_freqx;
+            hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx);
+            hstep_freqx = hnew_step_freqx;
+            Stepx.period(1.0/hstep_freqx);
+            wait(0.01);
+        }
+        if (Ps_y > 0.50 && errory > error_tresh) {
+            Diry = 0;
+            //errory = Ps_y - Pt_y;
+            cy = errory * H_Gain;
 
-            if (Ps_x < Pt_x && errorx > error_tresh) {
-                Dirx = 0;
-                cx = errorx * H_Gain;
-                float hnew_step_freqx;
-                hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx);
-                hstep_freqx = hnew_step_freqx;
-                Stepx.period(1.0/hstep_freqx);
-                wait(0.01);
-            }
-            if (Ps_y > Pt_y && errory > error_tresh) {
-                Diry = 0;
-                cy = errory * H_Gain;
-                float hnew_step_freqy;
-                hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy);
-                hstep_freqy = hnew_step_freqy;
-                Stepy.period(1.0/hstep_freqy);
-                wait(0.01);
-            }
+            float hnew_step_freqy;
+            hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy);
+            hstep_freqy = hnew_step_freqy;
+            Stepy.period(1.0/hstep_freqy);
+            wait(0.01);
+        }
+
+        if (Ps_x > 0.50 && errorx > error_tresh) {
+            Dirx = 1;
+            //errorx = Pt_x - Ps_x;
+            cx = errorx * H_Gain;
+
+            float hnew_step_freqx;
+            hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx);
+            hstep_freqx = hnew_step_freqx;
+            Stepx.period(1.0/hstep_freqx);
+            wait(0.01);
+        }
+        if (Ps_y < 0.50 && errory > error_tresh) {
+            Diry = 1;
+            //errory = Ps_y - Pt_y;
+            cy = errory * H_Gain;
 
-            if (Ps_x > Pt_x && errorx > error_tresh) {
-                Dirx = 1;
-                cx = errorx * H_Gain;
-                float hnew_step_freqx;
-                hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx);
-                hstep_freqx = hnew_step_freqx;
-                Stepx.period(1.0/hstep_freqx);
-                wait(0.01);
-            }
-            if (Ps_y < Pt_y && errory > error_tresh) {
-                Diry = 1;
-                cy = errory * H_Gain;
-                float hnew_step_freqy;
-                hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy);
-                hstep_freqy = hnew_step_freqy;
-                Stepy.period(1.0/hstep_freqy);
-                wait(0.01);
-            }
+            float hnew_step_freqy;
+            hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy);
+            hstep_freqy = hnew_step_freqy;
+            Stepy.period(1.0/hstep_freqy);
+            wait(0.01);
         }
-        lcd.printf("Done");
-        wait(2);
-        lcd.cls();
-        wait(1);
-        Enablex = 1;
-        Enabley = 1;
-        wait(3);
-        lcd.printf("Start EMG Control");
-        wait(2);
-        lcd.cls();
-        wait(1);
-        Enablex = 0;
-        Enabley = 0;
-    */
+
+    }
+    lcd.printf("Done");
+    wait(2);
+    lcd.cls();
+    wait(1);
+    Enablex = 1;
+    Enabley = 1;
+    wait(3);
+    lcd.printf("Start EMG Control");
+    wait(2);
+    lcd.cls();
+    wait(1);
+    Enablex = 0;
+    Enabley = 0;
+*/
+    MS1 = 1;
+    MS2 = 0;
+    MS3 = 0;
+    Stepx.write(0.5); // Duty cycle of 50%
+    Stepy.write(0.5);
+
     Ticker emgtimer;    //biceps
     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);
@@ -307,7 +326,7 @@
     arm_biquad_cascade_df1_init_f32(&highnotch_deltoid, 2 , highnotch_const, highnotch_deltoid_states);
     emgtimer.attach(looper_emg, 0.01);
 
-   // Ticker looptimer1;
+    //Ticker looptimer1;
     //looptimer1.attach(looper_motorx, 0.01); //X-Spindle motor, why this freq?
 
     Ticker looptimer2;
@@ -315,11 +334,13 @@
 
     //Microstepping control, now configured as half stepping (MS1=1,MS2=0,MS3=0)
 
+
+
     while (1) {
-        
-        //pc.printf("x %.2f, y %.2f \n", Ps_y, emg_y);
-        //lcd.printf("%.2f %.2f %.2f %.2f  \n", speed1, step_freq1, speed2, step_freq2);
-        wait(0.01);
+
+
+        pc.printf("%.2f %.2f %.2f  \n", Posy.read(), emg_y, step_freq1); //Send signal values to the computer.
+        wait(0.01); 
 
     }
 }