Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope biquadFilter mbed
Diff: main.cpp
- Revision:
- 2:13fa37643b8a
- Parent:
- 1:7fb4a74d33ff
--- a/main.cpp Mon Oct 24 18:34:06 2016 +0000
+++ b/main.cpp Mon Oct 24 22:08:52 2016 +0000
@@ -24,15 +24,15 @@
//Ticker
Ticker emgSampleTicker;
-//Timeout to change state after 5 seconds
-Timeout change_state;
-
-//Timeout to change state after 15 seconds
-Timeout change_state2;
-
//LED
DigitalOut led(LED_RED);
+//Button
+InterruptIn button(SW2);
+
+//Time led blink
+Timeout blink_timer;
+
//Emg input
AnalogIn emg0( A0 );
AnalogIn emg1( A1 );
@@ -70,15 +70,9 @@
go_emgSample = true;
}
-void calibrate() {
- state = STATE_CALIBRATION;
- led.write(0);
-}
+void blink();
-void run() {
- state = STATE_RUN;
- led.write(1);
-}
+void calibrate();
void EMG_filter();
@@ -86,10 +80,9 @@
//combine biquads in biquad chains for notch/high- low-pass filters
notch_50.add( &bq1 ).add( &bq2 ).add( &bq3 );
high_pass.add( &bq4 ).add( &bq5 );
+ button.mode(PullUp);
+ button.rise(&calibrate);
led.write(1);
-
- change_state.attach( &calibrate,5);
- change_state2.attach( &run,15);
emgSampleTicker.attach( &emgSample, 0.002);
while( true ){
if(go_emgSample == true){
@@ -137,43 +130,43 @@
RMS2 = sqrt(SumRMS2/n);
//Calculating min value and max value of emg signal
- if(state == STATE_CALIBRATION)
- {
- if (start_calibration == 0) {
- min_emg[0] = RMS0;
- max_emg[0] = RMS0;
- min_emg[1] = RMS1;
- max_emg[1] = RMS1;
- min_emg[2] = RMS2;
- max_emg[2] = RMS2;
- start_calibration++;
- }
- else {
- //finding min and max of emg0
- if (RMS0 < min_emg[0]) {
- min_emg[0] = RMS0;
- }
- else if (RMS0 > max_emg[0]) {
- max_emg[0] = RMS0;
- }
-
- //finding min and max of emg1
- if (RMS1 < min_emg[1]) {
- min_emg[1] = RMS1;
- }
- else if (RMS1 > max_emg[1]) {
- max_emg[1] = RMS1;
- }
-
- //finding min and max of emg2
- if (RMS2 < min_emg[2]) {
- min_emg[2] = RMS2;
- }
- else if (RMS2 > max_emg[2]) {
- max_emg[2] = RMS2;
- }
- }
- }
+ //if(state == STATE_CALIBRATION)
+ //{
+ // if (start_calibration == 0) {
+ // min_emg[0] = RMS0;
+ // max_emg[0] = RMS0;
+ // min_emg[1] = RMS1;
+ // max_emg[1] = RMS1;
+ // min_emg[2] = RMS2;
+ // max_emg[2] = RMS2;
+ // start_calibration++;
+ // }
+ // else {
+ // //finding min and max of emg0
+ // if (RMS0 < min_emg[0]) {
+ // min_emg[0] = RMS0;
+ // }
+ // else if (RMS0 > max_emg[0]) {
+ // max_emg[0] = RMS0;
+ // }
+ //
+ // //finding min and max of emg1
+ // if (RMS1 < min_emg[1]) {
+ // min_emg[1] = RMS1;
+ // }
+ // else if (RMS1 > max_emg[1]) {
+ // max_emg[1] = RMS1;
+ // }
+ //
+ // //finding min and max of emg2
+ // if (RMS2 < min_emg[2]) {
+ // min_emg[2] = RMS2;
+ // }
+ // else if (RMS2 > max_emg[2]) {
+ // max_emg[2] = RMS2;
+ // }
+ // }
+ //}
//calculating input_forces for controller
input_force0 = (RMS0 - min_emg[0])/(max_emg[0]-min_emg[0]);
@@ -191,3 +184,37 @@
}
}
+void calibrate() {
+ state = STATE_CALIBRATION;
+ switch(start_calibration) {
+ case 0 :
+ break;
+ case 1 :
+ min_emg[0] = RMS0;
+ break;
+ case 2 :
+ max_emg[0] = RMS0;
+ break;
+ case 3 :
+ min_emg[1] = RMS1;
+ break;
+ case 4:
+ max_emg[1] = RMS1;
+ break;
+ case 5:
+ min_emg[2] = RMS2;
+ break;
+ case 6:
+ max_emg[2] = RMS2;
+ break;
+ }
+ if (start_calibration < 7){
+ led.write(0);
+ blink_timer.attach(&blink,0.5);
+ start_calibration++;
+ }
+}
+
+void blink() {
+ led.write(1);
+}