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

Dependencies:   C12832_lcd HIDScope mbed-dsp mbed

Revision:
30:0a8f849e0292
Parent:
29:83f005c637be
Child:
31:372ff8d49430
--- a/main.cpp	Wed May 20 13:11:05 2015 +0000
+++ b/main.cpp	Thu May 21 10:15:37 2015 +0000
@@ -24,7 +24,7 @@
 Ticker   scopeTimer;
 
 //lcd
-//C12832_LCD lcd;
+C12832_LCD lcd;
 
 //Variables for motor control
 float setpoint = 6500; //Frequentie
@@ -64,6 +64,8 @@
 float filtered_average_pot;
 float filtered_step;
 float pot_value1_f32;
+float filt_avg_bi_old;
+float speed_old; 
 
 void average_biceps(float filtered_biceps,float *average)
 {
@@ -115,24 +117,28 @@
 void looper_motor()
 {
     float new_step_freq;
-    new_step_freq = (setpoint*filtered_pot*2);
+    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.
-    //arm_biquad_cascade_df1_f32(&lowpass_step, &step_freq, &filtered_step, 1);
+    speed_old = speed;
+    filt_avg_bi_old = filtered_average_bi;
 
-    if (step_freq < 850) {
+    if (step_freq < 500 || step_freq > 8000) {
         Enable = 1;
     } else {
         Enable = 0;
     }
-    Step.period(1.0/(100 + step_freq));
+    Step.period(1.0/(100 + step_freq)); //Step_freq is het aantal Hz. 
 
 }
 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, 5e3); 
+    scopeTimer.attach_us(&scope, &HIDScope::send, 2e3); 
     
-    /*Ticker log_timer;
+   /* 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);*/
@@ -140,10 +146,10 @@
     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);
+    emgtimer.attach(looper_emg, 0.002);
 
-    /*Ticker looptimer;
-    looptimer.attach(looper_motor, 0.01);*/
+    Ticker looptimer;
+    looptimer.attach(looper_motor, 0.002);
 
     MS1 = 1;
     MS2 = 0;
@@ -153,13 +159,7 @@
 
     while (1) {
 
-        if (filtered_pot < 0) { //Directie controle.
-            Dir = 0;
-        } else {
-            Dir = 1;
-        }
-        Step.write(0.5);
-        //lcd.printf("Freq %.0f Hz Filt %.4f \n", step_freq, filtered_pot); //snelheid meting op lcd
+        lcd.printf("Freq %.0f Hz \n", step_freq); //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