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

Dependencies:   C12832_lcd HIDScope mbed-dsp mbed

Revision:
29:83f005c637be
Parent:
28:593929bbdb98
Child:
30:0a8f849e0292
--- a/main.cpp	Tue May 19 15:16:50 2015 +0000
+++ b/main.cpp	Wed May 20 13:11:05 2015 +0000
@@ -21,9 +21,10 @@
 AnalogIn Pot1(p19);
 AnalogIn emg0(p17);
 HIDScope scope(2);
+Ticker   scopeTimer;
 
 //lcd
-C12832_LCD lcd;
+//C12832_LCD lcd;
 
 //Variables for motor control
 float setpoint = 6500; //Frequentie
@@ -34,12 +35,12 @@
 arm_biquad_casd_df1_inst_f32 lowpass_pot;
 arm_biquad_casd_df1_inst_f32 lowpass_step;
 
-//lowpass filter potmeter: Fcn = 0.1 Hz, Fs = 100 Hz
+//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, 
 //float lowpass_const[] = {0.000009825916403675327, 0.000019651832807350654, 0.000009825916403675327, 1.991114207740345, -0.9911535114059596};
-//lowpass filter potmeter: Fcn = 0.05 Hz, Fs = 500
-//float lowpass_const[] = {0.0055, 0.0111, 0.0055, 1.7786, -0.8008};
+//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};
 
 //EMG filter
 arm_biquad_casd_df1_inst_f32 lowpass_biceps;
@@ -54,14 +55,28 @@
 float lowpass_biceps_states[4];
 float highnotch_biceps_states[8];
 float lowpass_pot_states[4];
+float lowpass1_step_states[4];
 
 //global variabels
 float filtered_biceps;
+float filtered_average_bi;
 float filtered_pot;
 float filtered_average_pot;
 float filtered_step;
 float pot_value1_f32;
 
+void average_biceps(float filtered_biceps,float *average)
+{
+    static float total=0;
+    static float number=0;
+    total = total + filtered_biceps;
+    number = number + 1;
+    if ( number == 50) {
+        *average = total/50;
+        total = 0;
+        number = 0;
+    }
+}
 
 void looper_emg()
 {
@@ -69,6 +84,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)
@@ -76,13 +92,14 @@
 
     //process emg biceps
     arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 );
-    filtered_biceps = fabs(filtered_biceps);
+    filtered_biceps = fabs(filtered_biceps)*10;
     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,emg_value1);  //Raw EMG signal biceps
-    //scope.set(1,filtered_biceps); //Filtered signal
-   // scope.send();
+    scope.set(0,filtered_average_bi);  //Raw EMG signal biceps
+    scope.set(1,filtered_biceps); //Filtered signal
+    
 }
 void looper_pot()
 {
@@ -91,9 +108,7 @@
 
     //process input
     arm_biquad_cascade_df1_f32(&lowpass_pot, &pot_value1_f32, &filtered_pot, 1 );
-    scope.set(0, filtered_pot); 
-    scope.set(1, Pot1.read());
-    scope.send();
+     
 }
 
 
@@ -102,7 +117,7 @@
     float new_step_freq;
     new_step_freq = (setpoint*filtered_pot*2);
     step_freq = abs(new_step_freq); //Gives the PWM frequenty to the motor.
- 
+    //arm_biquad_cascade_df1_f32(&lowpass_step, &step_freq, &filtered_step, 1);
 
     if (step_freq < 850) {
         Enable = 1;
@@ -114,18 +129,21 @@
 }
 int main()
 {
-    Ticker log_timer;
+    // 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, 5e3); 
+    
+    /*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);
+    log_timer.attach(looper_pot, 0.01);*/
 
     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.005);
 
-    Ticker looptimer;
-    looptimer.attach(looper_motor, 0.01);
+    /*Ticker looptimer;
+    looptimer.attach(looper_motor, 0.01);*/
 
     MS1 = 1;
     MS2 = 0;
@@ -141,8 +159,8 @@
             Dir = 1;
         }
         Step.write(0.5);
-        lcd.printf("Freq %.0f Hz Filt %.4f \n", step_freq, filtered_pot); //snelheid meting op lcd
-        //pc.printf(" %.4f \n", filtered_pot);
+        //lcd.printf("Freq %.0f Hz Filt %.4f \n", step_freq, filtered_pot); //snelheid meting op lcd
+        //pc.printf(" %.4f \n", Pot1.read());
         //lcd.printf("filt %.3f raw %.3f \n", filtered_biceps, emg0.read());
         //pc.printf("Spd %.0f Hz p1 %.4f \n", step_freq, pot_value1_f32); //snelheid meting op lcd
         wait(0.01);