State machine
Dependencies: mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter
Diff: EMG_calibration.h
- Revision:
- 37:9a5da4463982
- Child:
- 39:f119ca6fc821
diff -r e5b3b01d128a -r 9a5da4463982 EMG_calibration.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EMG_calibration.h Thu Nov 01 14:19:33 2018 +0000 @@ -0,0 +1,122 @@ +#pragma once + +#include "mbed.h" +#include "MODSERIAL.h" +#include "Screen.h" +#include "EMGFilter.h" + +class EMG_calibration +{ +private: +Ticker calibration_timer; +Timeout adjustment_time; +Timeout calibration_bicep; // Timeout to get to relax state +Timeout end_of_calibration; // Timeout to get to final state +EMGFilter* EMGFilter ; +Screen* screen; + +//variables + volatile float EMG_signal1; + volatile float EMG_min_relax; + volatile float EMG_min_tense; + volatile float EMG_max_relax; + volatile float EMG_max_tense; + volatile float EMG_threshold; +// IN STATE MACHINE: +EMGFilter* emg_input; // Voor kalibratie van bicep 1 +//EMGFilter emg_input = EMGFilter(A1); // Voor kalibratie van bicep 2 + +EMG_calibration(Screen *new_screen,EMGFilter *new_emg_input){ + screen= new_screen; + emg_input = new_emg_input; + EMG_min_relax=1.000; + EMG_min_tense=1.000; + EMG_max_relax=0.000; + EMG_max_tense=0.000; + relax_calibration(); +} + +void tense_calibration() +{ + EMG_signal1 = emg_input.get_envelope_emg(); + if (EMG_max_tense < EMG_signal1) + { + EMG_max_tense=EMG_signal1; + } + if (EMG_min_tense > EMG_signal1) + { + EMG_min_tense=EMG_signal1; + } + screen.get_screen_handle()->setTextCursor(0, 0); + screen.get_screen_handle()->printf("Tense "); + screen.get_screen_handle()->printf("EMG maximum= %.6f",EMG_max_tense); + screen.get_screen_handle()->printf("EMG minimum= %.6f",EMG_min_tense); + screen.get_screen_handle()->printf(" ",EMG_min_tense); + screen.display(); +} + +void relax_calibration() +{ + EMG_signal1 = emg_input.get_envelope_emg(); + if (EMG_max_relax < EMG_signal1) + { + EMG_max_relax=EMG_signal1; + } + if (EMG_min_relax > EMG_signal1) + { + EMG_min_relax=EMG_signal1; + } + EMG_threshold=(((EMG_min_tense-EMG_max_relax)/2)+EMG_max_relax); + screen.get_screen_handle()->setTextCursor(0, 0); + screen.get_screen_handle()->printf("Relax "); + screen.get_screen_handle()->printf("EMG maximum= %.6f",EMG_max_relax); + screen.get_screen_handle()->printf("EMG minimum= %.6f",EMG_min_relax); + screen.get_screen_handle()->printf("threshold= %.6f",EMG_threshold); + screen.display(); +} + +void TheFinalCalibration() + { + calibration_bicep.detach(); + calibration_timer.detach(); + screen.get_screen_handle()->setTextCursor(0, 0); + screen.get_screen_handle()->printf("threshold= %.6f",EMG_threshold); + screen.get_screen_handle()->printf(" "); + screen.get_screen_handle()->printf(" "); + screen.get_screen_handle()->printf(" "); + screen.display(); + emg_input.set_threshold(EMG_threshold); + } + +void relax_measure() +{ + calibration_timer.detach(); + screen.get_screen_handle()->setTextCursor(0, 0); + screen.get_screen_handle()->printf("Relax muscle ",EMG_max_relax); + screen.get_screen_handle()->printf(" ",EMG_min_tense); + screen.get_screen_handle()->printf(" ",EMG_min_tense); + screen.get_screen_handle()->printf(" ",EMG_min_tense); + screen.display(); + adjustment_time.attach(callback(this, &EMG_calibration::start_relax),2.5f); +} +void start_relax(){ + calibration_timer.attach(&relax_calibration, 0.1); + end_of_calibration.attach(&relax_measure,5.0f); +} + +void tense_measure() +{ + calibration_timer.detach(); + screen.get_screen_handle()->setTextCursor(0, 0); + screen.get_screen_handle()->printf("Tense muscle ",EMG_max_relax); + screen.get_screen_handle()->printf(" ",EMG_min_tense); + screen.get_screen_handle()->printf(" ",EMG_min_tense); + screen.get_screen_handle()->printf(" ",EMG_min_tense); + screen.display(); + adjustment_time.attach(callback(this, &EMG_calibration::start_tense),2.5f); +} +void start_tense(){ + calibration_timer.attach(&tense_calibration,0.1); + calibration_bicep.attach(&TheFinalCalibration,5); +} +};