now with PID controler XXXD

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of another_try_from_scratch_on_emg by Daniqe Kottelenberg

Revision:
30:a7bae1c036a3
Parent:
29:76b2cc33690c
Child:
31:9333d3f48c88
--- a/main.cpp	Fri Oct 28 08:53:21 2016 +0000
+++ b/main.cpp	Fri Oct 28 09:19:35 2016 +0000
@@ -17,14 +17,14 @@
 MODSERIAL pc(USBTX, USBRX);             //pc connection
 
 //motors
-DigitalOut richting_motor1(D4);
+DigitalOut richting_motor1(D4); 
 PwmOut pwm_motor1(D5);
 DigitalOut richting_motor2(D7);
 PwmOut pwm_motor2(D6);
 
 //digital out
 
-DigitalOut  led(LED_GREEN);         //led included to check where code is
+DigitalOut  led(LED_GREEN);                 //led included to check where code is
 
 //define variables
 //other
@@ -35,11 +35,14 @@
 double signal_right_arm;                    //signal right arm
 double max_biceps;                          //calibration maximum biceps
 double max_triceps;                         //calibration maximum triceps
-int n = 0;                                  //start van de teller wordt op nul gesteld, om te kunnen switchen
+int n = 0;                                  //start of counter is zero, to make switch work. It depens on even and odd. 
  
-double emg_biceps_right;
-double emg_triceps_right;
-double emg_biceps_left;
+double emg_biceps_right;                    //emg biceps signal in, arm 1
+double emg_triceps_right;                   //emg triceps signal in, arm 1
+double emg_biceps_left;                     //emg biceps signal in, arm 2
+
+float percentage_max_triceps=0.2;
+float percentage_max_biceps =0.3;
 
 //Biquads defined
 
@@ -127,8 +130,8 @@
         }
         
         //send signals  to scope
-        scope.set(0, emg_filtered_biceps_right);            //set emg signal to scope in channel 0 // change into raw signal! 
-        scope.set(1, emg_filtered_triceps_right);           // set emg signal to scope in channel 1// change into raw signal!
+        scope.set(0, emg_biceps_right);            //set emg signal to scope in channel 0 // change into raw signal! 
+        scope.set(1, emg_triceps_right);           // set emg signal to scope in channel 1// change into raw signal!
         scope.set(2, emg_filtered_biceps_left);             // set emg signal to scope in channel 2
         scope.set(3, onoffsignal_rightarm);                 // set emg signal to scope in channel 3
         scope.set(4, switch_signal_leftarm);
@@ -138,9 +141,7 @@
 
 //calibration function for biceps 
 void calibration_biceps(){
-            if(button_calibration_biceps==0)
-            {
-            for(int n =0; n<2000;n++)                                                  //read for 5000 samples as calibration
+        for(int n =0; n<2000;n++)                                                  //read for 2000 samples as calibration
                 {
         emg_biceps_right=emg_biceps_right_in.read();                            //read the emg value from the elektrodes
         double emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right);   
@@ -156,9 +157,32 @@
                 }
                 wait(0.001f); //to sample at same freq; 1000Hz
                 }
-            cut_off_value_biceps=0.2*max_biceps; 
-            pc.printf(" change of cv %f ",cut_off_value_biceps );   
-            }
+            cut_off_value_biceps=percentage_max_biceps*max_biceps; 
+            pc.printf(" change of cv biceps: %f ",cut_off_value_biceps );   
+            
+                }
+                
+                //calibration function for biceps 
+void calibration_triceps(){
+        for(int n =0; n<2000;n++)                                                  //read for 2000 samples as calibration
+                {
+        emg_biceps_right=emg_biceps_right_in.read();                            //read the emg value from the elektrodes
+        double emg_filtered_high_triceps_right= filterhigh_b1.step(emg_triceps_right);   
+        double emg_filtered_high_notch_1_triceps_right=filternotch1_b1.step(emg_filtered_high_triceps_right);
+        double emg_filtered_high_notch_1_2_triceps_right=filternotch2_b1.step(emg_filtered_high_notch_1_triceps_right);
+        double emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_2_triceps_right); //fabs because float
+        double emg_filtered_triceps_right=filterlow_b1.step(emg_abs_triceps_right);
+                                
+            if (emg_filtered_triceps_right > max_triceps)                    //determine what the highest reachable emg signal is
+                {
+            max_triceps = emg_filtered_triceps_right;
+            
+                }
+                wait(0.001f); //to sample at same freq; 1000Hz
+                }
+            cut_off_value_triceps=percentage_max_triceps*max_triceps; 
+            pc.printf(" change of cv triceps: %f ",cut_off_value_triceps );   
+            
                 }
 
 //program
@@ -167,8 +191,9 @@
 {  
 pc.baud(115200);
 sample_timer.attach(&filter, 0.001);        //continously execute the EMG reader and filter, it ensures that filter and sampling is executed every 1/frequency seconds
-switch_function.attach(&SwitchN,0.1);                       
-button_calibration_biceps.fall(&calibration_biceps,0.001); //to call calibration biceps, stop everything else
+switch_function.attach(&SwitchN,0.01);                       
+button_calibration_biceps.fall(&calibration_biceps); //to call calibration biceps, stop everything else
+button_calibration_triceps.fall(&calibration_triceps); //to call calibration triceps, stop everything else
 //endless loop
 
     while (true) {                        // zorgt er voor dat de code oneindig doorgelopen wordt