![](/media/cache/group/9e3cc099b3b04bca937a1cca1da81b19.jpg.50x50_q85.jpg)
De hele robot in 1 keer bam
Dependencies: mbed QEI Servo HIDScope biquadFilter MODSERIAL FastPWM
Diff: main.cpp
- Revision:
- 34:13fac02ef324
- Parent:
- 33:90404e64d844
- Child:
- 35:e82834e62e44
diff -r 90404e64d844 -r 13fac02ef324 main.cpp --- a/main.cpp Tue Oct 29 13:18:37 2019 +0000 +++ b/main.cpp Tue Oct 29 13:50:28 2019 +0000 @@ -49,9 +49,7 @@ double emg1; double emg1_env; double emg1_MVC; -double emg1_MVC_stdev; double emg1_rest; -double emg1_rest_stdev; double emg1_factor; double emg1_th; double emg1_out; @@ -62,9 +60,7 @@ double emg2; double emg2_env; double emg2_MVC; -double emg2_MVC_stdev; double emg2_rest; -double emg2_rest_stdev; double emg2_factor; double emg2_th; double emg2_out; @@ -75,9 +71,7 @@ double emg3; double emg3_env; double emg3_MVC; -double emg3_MVC_stdev; double emg3_rest; -double emg3_rest_stdev; double emg3_factor; double emg3_th; double emg3_out; @@ -143,7 +137,7 @@ int vect_n = vect.size(); for (int i = 0; i < vect_n; i++) { - if (vect[i] < curr_max) { curr_max = vect[i]; }; + if (vect[i] > curr_max) { curr_max = vect[i]; }; } return curr_max; } @@ -274,28 +268,15 @@ switch( emg_curr_state ) { case emg_cal_MVC: emg1_MVC = getMax(emg1_cal); // Store max value of MVC globally - // emg1_MVC_stdev = getStdev(emg1_cal, emg1_MVC); // Store MVC stdev globally - emg2_MVC = getMax(emg2_cal); // Store max value of MVC globally - // emg2_MVC_stdev = getStdev(emg2_cal, emg2_MVC); // Store MVC stdev globally - emg3_MVC = getMax(emg3_cal); // Store max value of MVC globally - // emg3_MVC_stdev = getStdev(emg3_cal, emg3_MVC); // Store MVC stdev globally - emg_MVC_cal_done = true; // To set up transition guard to operation mode break; case emg_cal_rest: emg1_rest = getMean(emg1_cal); // Store rest EMG globally - emg1_rest_stdev = getStdev(emg1_cal, emg1_rest); // Store rest stdev globally - emg2_rest = getMean(emg2_cal); // Store rest EMG globally - emg2_rest_stdev = getStdev(emg2_cal, emg2_rest); // Store MVC stdev globally - emg3_rest = getMean(emg3_cal); // Store rest EMG globally - emg3_rest_stdev = getStdev(emg3_cal, emg3_rest); // Store MVC stdev globally - - emg_rest_cal_done = true; // To set up transition guard to operation mode break; } @@ -363,13 +344,12 @@ // Do stuff until end condition is met // Set HIDScope outputs - scope.set(0, emg1 ); // One notch filtered EMG to check 50Hz disturbance + scope.set(0, emg1 ); scope.set(1, emg1_env ); //scope.set(2, emg2_env ); //scope.set(3, emg3_env ); scope.send(); - // State transition guard if ( timerCalibration.read() >= Tcal ) { // After interval Tcal the calibration step is finished sampleNow = false; // Disable signal sampling in sampleSignal() @@ -388,7 +368,7 @@ // Entry function if ( emg_state_changed == true ) { emg_state_changed = false; // Disable entry functions - double margin_percentage = 10; // Set up % margin for rest + double margin_percentage = 5; // Set up % margin for rest emg1_factor = 1 / emg1_MVC; // Factor to normalize MVC emg1_th = emg1_rest * emg1_factor + margin_percentage/100; // Set normalized rest threshold @@ -409,12 +389,12 @@ // Set normalized EMG output signal (CAN BE MOVED TO EXTERNAL FUNCTION BECAUSE IT IS REPEATED 3 TIMES) if ( emg1_norm < emg1_th ) { // If below threshold, emg_out = 0 (ignored) emg1_out = 0.0; - } else if ( emg1_norm > 1.0f ) { // If above MVC (due to filtering perhaps), emg_out = 1 (max value) + } else if ( emg1_norm > 1.0f ) { // If above MVC (e.g. due to filtering), emg_out = 1 (max value) emg1_out = 1.0; } else { // If in between threshold and MVC, scale EMG signal accordingly // Inputs may be in range [emg_th, 1] // Outputs are scaled to range [0, 1] - emg1_out = rescale(emg1_norm, emg1_th, 1, 0, 1); + emg1_out = rescale(emg1_norm, 0, 1, emg1_th, 1); } // Idem for emg2 @@ -423,7 +403,7 @@ } else if ( emg2_norm > 1.0f ) { emg2_out = 1.0; } else { - emg2_out = rescale(emg2_norm, emg2_th, 1, 0, 1); + emg2_out = rescale(emg2_norm, 0, 1, emg2_th, 1); } // Idem for emg3 @@ -432,7 +412,7 @@ } else if ( emg3_norm > 1.0f ) { emg3_out = 1.0; } else { - emg3_out = rescale(emg3_norm, emg3_th, 1, 0, 1); + emg3_out = rescale(emg3_norm, 0, 1, emg3_th, 1); } // Set HIDScope outputs @@ -514,21 +494,13 @@ } else { led_r = 0; // LED on } + emg_curr_state = emg_wait; // Start off in EMG Wait state - - tickGlobal.attach( &tickGlobalFunc, Ts ); - - + tickGlobal.attach( &tickGlobalFunc, Ts ); // Start global ticker while(true) { - - // Show that system is running - // led_g = !led_g; - // pc.printf("currentState: %i vectors size: %i %i %i\r\n", emg_curr_state, emg1_cal_size, emg2_cal_size, emg3_cal_size); pc.printf("emg_state: %i emg1_env: %f emg1_out: %f emg1_th: %f emg1_factor: %f\r\n", emg_curr_state, emg1_env, emg1_out, emg1_th, emg1_factor); pc.printf(" emg1_MVC: %f emg1_rest: %f \r\n", emg1_MVC, emg1_rest); - //pc.printf(" emg2_env: %f emg2_out: %f emg2_th: %f emg2_factor: %f\r\n", emg2_env, emg2_out, emg2_th, emg2_factor); - //pc.printf(" emg3_env: %f emg3_out: %f emg3_th: %f emg3_factor: %f\r\n", emg3_env, emg3_out, emg3_th, emg3_factor); wait(0.5f); } } \ No newline at end of file