working version but stripped of most unnecessary code like print statements

Dependencies:   HIDScope MODSERIAL biquadFilter mbed FastPWM QEI

Files at this revision

API Documentation at this revision

Comitter:
RiP
Date:
Fri Oct 21 13:32:25 2016 +0000
Parent:
26:91d48c0b722d
Child:
28:3b1b29193851
Commit message:
referentie signaal toegevoegd, maar hij verhoogt de referentie maar 1x

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Oct 20 13:24:39 2016 +0000
+++ b/main.cpp	Fri Oct 21 13:32:25 2016 +0000
@@ -11,6 +11,7 @@
 AnalogIn    emg2( A1 );
 AnalogIn    emg3( A2 );
 
+Ticker      pos_timer;
 Ticker      sample_timer;
 HIDScope    scope( 6 );
 MODSERIAL pc(USBTX, USBRX);
@@ -25,8 +26,9 @@
 double emg02;
 double emg12;
 double emg22;
-//double ref_x=0.000;
-//double ref_y=0.000;
+double ref_x=0.000;
+double ref_y=0.000;
+
 typedef enum { STATE_CALIBRATION, STATE_PAUZE, STATE_X, STATE_X_NEG, STATE_Y, STATE_Y_NEG, STATE_XY, STATE_XY_NEG } states;
 
 BiQuadChain bqc11;
@@ -46,7 +48,6 @@
 /*  low pass filter*/
 BiQuad bq131( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
 
-
 /*  BiQuads for filter emg2
     notch filter*/
 BiQuad bq211(0.9795,   -1.5849,    0.9795,    1.0000,   -1.5849,    0.9589);
@@ -68,6 +69,7 @@
 BiQuad bq331( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
 
 
+
 void sampleflag()
 {
     sampletimer=true;
@@ -151,10 +153,9 @@
             mystate = STATE_PAUZE;
         }
     }
-    
+
     if (buttonflag==true) {
         mystate = STATE_CALIBRATION;
-        newcase=true;
     }
 
     if (myoldstate==mystate) {
@@ -167,19 +168,20 @@
 
 void sample(states &mystate)
 {
+    states myoldstate=mystate;
 
     /* Read the emg signals and filter it*/
 
     scope.set(0, emg1.read());    //original signal
-    double emg02=bqc13.step(fabs(bqc11.step(emg1.read())));
+    emg02=bqc13.step(fabs(bqc11.step(emg1.read())));
     scope.set(1, emg02);
     /* Read the second emg signal and filter it*/
     scope.set(2, emg2.read());    //original signal
-    double emg12=bqc23.step(fabs(bqc21.step(emg2.read())));
+    emg12=bqc23.step(fabs(bqc21.step(emg2.read())));
     scope.set(3, emg12);
     /* Read the third emg signal and filter it*/
     scope.set(4, emg3.read());    //original signal
-    double emg22=bqc33.step(fabs(bqc31.step(emg3.read())));
+    emg22=bqc33.step(fabs(bqc31.step(emg3.read())));
     scope.set(5, emg22);
     //pc.printf("Hello World!\n");
     /*   Ensure that enough channels are available (HIDScope scope( 2 ))
@@ -216,9 +218,37 @@
         mystate = STATE_CALIBRATION;
     }
 
+    if (myoldstate==mystate) {
+        newcase=false;
+    } else {
+        newcase=true;
+    }
+
     sampletimer = false;
 }
 
+void change_x(int direction)
+{
+    ref_x=ref_x+0.1*direction;
+
+}
+
+void change_y(int direction)
+{
+    ref_y=ref_y+0.1*direction;
+
+}
+void change_xy(int direction)
+{
+    ref_x=ref_x+0.1*direction;
+    ref_y=ref_y+0.1*direction;
+}
+
+void my_pos()
+{
+    pc.printf("x_pos=%.4f\ty_pos=%.4f\n\r",ref_x,ref_y);
+
+}
 
 int main()
 {
@@ -237,6 +267,7 @@
     /*Attach the 'sample' function to the timer 'sample_timer'.
       this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
     */
+    pos_timer.attach(&my_pos, 1);
     sample_timer.attach(&sampleflag, samplefreq);
     button_calibrate.fall(&buttonflag_go);
 
@@ -246,10 +277,11 @@
             //sample(mystate);
             sample_button(mystate);
         }
+
         // switch, case
         switch (mystate) {
             case STATE_CALIBRATION : { // calibration
-                pc.printf("calibration\n");
+                pc.printf("calibration\n\r");
                 while (button_calibrate==0) {}
                 newcase=false;
                 buttonflag=false;
@@ -257,44 +289,50 @@
             }
             case STATE_X : // run
                 if (newcase==true) {
-                    pc.printf("X\n");
+                    pc.printf("X\n\r");
                     newcase=false;
+                    change_x(1);
                 }
 
                 break;
             case STATE_X_NEG : // run
                 if (newcase==true) {
-                    pc.printf("Xneg\n");
+                    pc.printf("Xneg\n\r");
                     newcase=false;
+                    change_x(-1);
                 }
                 break;
             case STATE_Y : // execute mode 1
                 if (newcase==true) {
-                    pc.printf("Y\n");
+                    pc.printf("Y\n\r");
                     newcase=false;
+                    change_y(1);
                 }
                 break;
             case STATE_Y_NEG : // execute mode 1
                 if (newcase==true) {
-                    pc.printf("Yneg\n");
+                    pc.printf("Yneg\n\r");
                     newcase=false;
+                    change_y(-1);
                 }
                 break;
             case STATE_XY : // execute mode 2
                 if (newcase==true) {
-                    pc.printf("XY\n");
+                    pc.printf("XY\n\r");
                     newcase=false;
+                    change_xy(1);
                 }
                 break;
             case STATE_XY_NEG : // execute mode 2
                 if (newcase==true) {
-                    pc.printf("XYneg\n");
+                    pc.printf("XYneg\n\r");
                     newcase=false;
+                    change_xy(-1);
                 }
                 break;
             case STATE_PAUZE : // default
                 if (newcase==true) {
-                    pc.printf("PAUZE\n");
+                    pc.printf("PAUZE\n\r");
                     newcase=false;
                 }
                 break;