Biorobotics 7 / Mbed 2 deprecated State_Machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EMG_calibration.h Source File

EMG_calibration.h

00001 #pragma once
00002 
00003 #include "mbed.h"
00004 #include "MODSERIAL.h"
00005 #include "Screen.h"
00006 #include "EMGFilter.h"
00007 
00008 const double emg_calibration_time = 3.0;
00009 
00010 class EMG_calibration
00011 {
00012 private:
00013     Ticker      calibration_timer;
00014     Timeout     adjustment_time;
00015     Timeout     calibration_bicep;      // Timeout to get to relax state
00016     Timeout     end_of_calibration;     // Timeout to get to final state
00017     Screen*     screen;
00018 
00019     //variables
00020     volatile float emg_signal;
00021     volatile float EMG_min_relax;
00022     volatile float EMG_min_tense;
00023     volatile float EMG_max_relax;
00024     volatile float EMG_max_tense;
00025     volatile float EMG_threshold;
00026     // IN STATE MACHINE:
00027     EMGFilter *emg_input;         // Voor kalibratie van bicep 1
00028     //EMGFilter emg_input = EMGFilter(A1);         // Voor kalibratie van bicep 2
00029     
00030     volatile float calibration_complete;
00031 
00032 public:
00033     EMG_calibration(Screen *new_screen, EMGFilter *new_emg_input) {
00034         screen= new_screen;
00035         emg_input = new_emg_input;
00036         EMG_min_relax=1.000;
00037         EMG_min_tense=1.000;
00038         EMG_max_relax=0.000;
00039         EMG_max_tense=0.000;
00040         
00041         calibration_complete = false;
00042     }
00043     
00044     void start() {
00045         tense_measure();
00046     }
00047     
00048     bool is_calibrated() {
00049         return calibration_complete;
00050     }
00051 
00052 private:
00053     void tense_calibration() {
00054         emg_signal = emg_input->get_envelope_emg();
00055         if (EMG_max_tense < emg_signal) {
00056             EMG_max_tense=emg_signal;
00057         }
00058         if (EMG_min_tense > emg_signal) {
00059             EMG_min_tense=emg_signal;
00060         }
00061         screen->get_screen_handle()->setTextCursor(0, 0);
00062         screen->get_screen_handle()->printf("Tense                ");
00063         screen->get_screen_handle()->printf("EMG maximum= %.6f",EMG_max_tense);
00064         screen->get_screen_handle()->printf("EMG minimum= %.6f",EMG_min_tense);
00065         screen->get_screen_handle()->printf("                     ",EMG_min_tense);
00066         screen->display();
00067     }
00068 
00069     void relax_calibration() {
00070         emg_signal = emg_input->get_envelope_emg();
00071         if (EMG_max_relax < emg_signal) {
00072             EMG_max_relax=emg_signal;
00073         }
00074         if (EMG_min_relax > emg_signal) {
00075             EMG_min_relax=emg_signal;
00076         }
00077         EMG_threshold=(((EMG_min_tense-EMG_max_relax)/2)+EMG_max_relax);
00078         screen->get_screen_handle()->setTextCursor(0, 0);
00079         screen->get_screen_handle()->printf("Relax                ");
00080         screen->get_screen_handle()->printf("EMG maximum= %.6f",EMG_max_relax);
00081         screen->get_screen_handle()->printf("EMG minimum= %.6f",EMG_min_relax);
00082         screen->get_screen_handle()->printf("threshold= %.6f",EMG_threshold);
00083         screen->display();
00084     }
00085 
00086     void TheFinalCalibration() {
00087         calibration_bicep.detach();
00088         calibration_timer.detach();
00089         screen->get_screen_handle()->setTextCursor(0, 0);
00090         screen->get_screen_handle()->printf("Threshold= %.6f",EMG_threshold);
00091         screen->get_screen_handle()->printf("                     ");
00092         screen->get_screen_handle()->printf("                     ");
00093         screen->get_screen_handle()->printf("                     ");
00094         screen->display();
00095         emg_input->set_threshold(EMG_threshold);
00096         
00097         calibration_complete = true;
00098     }
00099 
00100     void relax_measure() {
00101         calibration_timer.detach();
00102         screen->get_screen_handle()->setTextCursor(0, 0);
00103         screen->get_screen_handle()->printf("Relax muscle         ",EMG_max_relax);
00104         screen->get_screen_handle()->printf("                     ",EMG_min_tense);
00105         screen->get_screen_handle()->printf("                     ",EMG_min_tense);
00106         screen->get_screen_handle()->printf("                     ",EMG_min_tense);
00107         screen->display();
00108         adjustment_time.attach(callback(this, &EMG_calibration::start_relax),2.5f);
00109     }
00110     void start_relax() {
00111         calibration_timer.attach(callback(this, &EMG_calibration::relax_calibration), 0.1);
00112         end_of_calibration.attach(callback(this, &EMG_calibration::TheFinalCalibration), emg_calibration_time);
00113     }
00114 
00115     void tense_measure() {
00116         calibration_timer.detach();
00117         screen->get_screen_handle()->setTextCursor(0, 0);
00118         screen->get_screen_handle()->printf("Tense muscle         ",EMG_max_relax);
00119         screen->get_screen_handle()->printf("                     ",EMG_min_tense);
00120         screen->get_screen_handle()->printf("                     ",EMG_min_tense);
00121         screen->get_screen_handle()->printf("                     ",EMG_min_tense);
00122         screen->display();
00123         adjustment_time.attach(callback(this, &EMG_calibration::start_tense),2.5f);
00124     }
00125     void start_tense() {
00126         calibration_timer.attach(callback(this, &EMG_calibration::tense_calibration),0.1);
00127         calibration_bicep.attach(callback(this, &EMG_calibration::relax_measure), emg_calibration_time);
00128     }
00129 };