EMG signalen simuleren met toetsen voor veranderende signalen

Dependencies:   MODSERIAL mbed

Committer:
willem_hoitzing
Date:
Fri Sep 23 12:27:06 2016 +0000
Revision:
0:65c093df6044
EMG

Who changed what in which revision?

UserRevisionLine numberNew contents of line
willem_hoitzing 0:65c093df6044 1 #include "mbed.h"
willem_hoitzing 0:65c093df6044 2 #include "MODSERIAL.h"
willem_hoitzing 0:65c093df6044 3
willem_hoitzing 0:65c093df6044 4 PwmOut pwm_M1(D6);
willem_hoitzing 0:65c093df6044 5 DigitalOut dir_M1(D7);
willem_hoitzing 0:65c093df6044 6 DigitalOut ledr(LED_RED);
willem_hoitzing 0:65c093df6044 7 DigitalOut ledg(LED_GREEN);
willem_hoitzing 0:65c093df6044 8 DigitalOut ledb(LED_BLUE);
willem_hoitzing 0:65c093df6044 9 Serial pc(USBTX, USBRX);
willem_hoitzing 0:65c093df6044 10 InterruptIn knop(SW2);
willem_hoitzing 0:65c093df6044 11 Ticker combiner;
willem_hoitzing 0:65c093df6044 12 Ticker updater;
willem_hoitzing 0:65c093df6044 13
willem_hoitzing 0:65c093df6044 14 int cw = 0;
willem_hoitzing 0:65c093df6044 15 int ccw = 1;
willem_hoitzing 0:65c093df6044 16
willem_hoitzing 0:65c093df6044 17 // PARAMETERS
willem_hoitzing 0:65c093df6044 18 int cal_time = 5; // Duur van kalibratie
willem_hoitzing 0:65c093df6044 19 int n_cal = 10; // Aantal metingen tijdens kalibratie
willem_hoitzing 0:65c093df6044 20 float threshold = 0.2;
willem_hoitzing 0:65c093df6044 21 float Fs = 300; // Sample-frequentie [Hz]: aantal updates per seconde
willem_hoitzing 0:65c093df6044 22
willem_hoitzing 0:65c093df6044 23 volatile float EMG_biceps = 0;
willem_hoitzing 0:65c093df6044 24 volatile float EMG_triceps = 0;
willem_hoitzing 0:65c093df6044 25 volatile float EMG_biceps_norm;
willem_hoitzing 0:65c093df6044 26 volatile float EMG_triceps_norm;
willem_hoitzing 0:65c093df6044 27 volatile float EMG_biceps_max;
willem_hoitzing 0:65c093df6044 28 volatile float EMG_triceps_max;
willem_hoitzing 0:65c093df6044 29 volatile float EMG_combined_signal;
willem_hoitzing 0:65c093df6044 30
willem_hoitzing 0:65c093df6044 31 void input_b()
willem_hoitzing 0:65c093df6044 32 {
willem_hoitzing 0:65c093df6044 33 EMG_biceps = EMG_biceps + 1;
willem_hoitzing 0:65c093df6044 34 pc.printf("EMG_biceps = %f \tEMG_triceps = %f", EMG_biceps, EMG_triceps);
willem_hoitzing 0:65c093df6044 35 }
willem_hoitzing 0:65c093df6044 36
willem_hoitzing 0:65c093df6044 37 void input_n()
willem_hoitzing 0:65c093df6044 38 {
willem_hoitzing 0:65c093df6044 39 EMG_biceps = EMG_biceps - 1;
willem_hoitzing 0:65c093df6044 40 pc.printf("EMG_biceps = %f \tEMG_triceps = %f", EMG_biceps, EMG_triceps);
willem_hoitzing 0:65c093df6044 41 }
willem_hoitzing 0:65c093df6044 42
willem_hoitzing 0:65c093df6044 43 void input_t()
willem_hoitzing 0:65c093df6044 44 {
willem_hoitzing 0:65c093df6044 45 EMG_triceps = EMG_triceps + 1;
willem_hoitzing 0:65c093df6044 46 pc.printf("EMG_biceps = %f \tEMG_triceps = %f", EMG_biceps, EMG_triceps);
willem_hoitzing 0:65c093df6044 47 }
willem_hoitzing 0:65c093df6044 48
willem_hoitzing 0:65c093df6044 49 void input_y()
willem_hoitzing 0:65c093df6044 50 {
willem_hoitzing 0:65c093df6044 51 EMG_triceps = EMG_triceps - 1;
willem_hoitzing 0:65c093df6044 52 pc.printf("EMG_biceps = %f \tEMG_triceps = %f", EMG_biceps, EMG_triceps);
willem_hoitzing 0:65c093df6044 53 }
willem_hoitzing 0:65c093df6044 54
willem_hoitzing 0:65c093df6044 55 void combine_EMG()
willem_hoitzing 0:65c093df6044 56 {
willem_hoitzing 0:65c093df6044 57 // EMG_biceps = EMG_biceps_raw.read();
willem_hoitzing 0:65c093df6044 58 if(EMG_biceps > EMG_biceps_max)
willem_hoitzing 0:65c093df6044 59 {
willem_hoitzing 0:65c093df6044 60 EMG_biceps_norm = 1;
willem_hoitzing 0:65c093df6044 61 }
willem_hoitzing 0:65c093df6044 62 else
willem_hoitzing 0:65c093df6044 63 {
willem_hoitzing 0:65c093df6044 64 EMG_biceps_norm = EMG_biceps/EMG_biceps_max; // scaled 0-1
willem_hoitzing 0:65c093df6044 65 }
willem_hoitzing 0:65c093df6044 66 // EMG_triceps = EMG_triceps_raw.read();
willem_hoitzing 0:65c093df6044 67 if(EMG_triceps > EMG_triceps_max)
willem_hoitzing 0:65c093df6044 68 {
willem_hoitzing 0:65c093df6044 69 EMG_triceps_norm = 1;
willem_hoitzing 0:65c093df6044 70 }
willem_hoitzing 0:65c093df6044 71 else
willem_hoitzing 0:65c093df6044 72 {
willem_hoitzing 0:65c093df6044 73 EMG_triceps_norm = EMG_triceps/EMG_triceps_max; // scaled 0-1
willem_hoitzing 0:65c093df6044 74 }
willem_hoitzing 0:65c093df6044 75 EMG_combined_signal = EMG_biceps_norm - EMG_triceps_norm;
willem_hoitzing 0:65c093df6044 76 }
willem_hoitzing 0:65c093df6044 77
willem_hoitzing 0:65c093df6044 78 void update_M1()
willem_hoitzing 0:65c093df6044 79 {
willem_hoitzing 0:65c093df6044 80 if(EMG_combined_signal < (-1*threshold))
willem_hoitzing 0:65c093df6044 81 {
willem_hoitzing 0:65c093df6044 82 dir_M1 = ccw;
willem_hoitzing 0:65c093df6044 83 pwm_M1 = (-1/(1-threshold) * EMG_combined_signal + threshold/(threshold-1));
willem_hoitzing 0:65c093df6044 84 }
willem_hoitzing 0:65c093df6044 85 else if(EMG_combined_signal > threshold)
willem_hoitzing 0:65c093df6044 86 {
willem_hoitzing 0:65c093df6044 87 dir_M1 = cw;
willem_hoitzing 0:65c093df6044 88 pwm_M1 = (1/(1-threshold) * EMG_combined_signal + threshold/(threshold-1));
willem_hoitzing 0:65c093df6044 89 }
willem_hoitzing 0:65c093df6044 90 else
willem_hoitzing 0:65c093df6044 91 {
willem_hoitzing 0:65c093df6044 92 pwm_M1 = 0;
willem_hoitzing 0:65c093df6044 93 }
willem_hoitzing 0:65c093df6044 94 }
willem_hoitzing 0:65c093df6044 95
willem_hoitzing 0:65c093df6044 96 void calibrate()
willem_hoitzing 0:65c093df6044 97 {
willem_hoitzing 0:65c093df6044 98 combiner.detach();
willem_hoitzing 0:65c093df6044 99 updater.detach();
willem_hoitzing 0:65c093df6044 100 pwm_M1 = 0;
willem_hoitzing 0:65c093df6044 101 pc.printf("Hold biceps at maximum contraction for %i seconds, starting in 3...", cal_time);
willem_hoitzing 0:65c093df6044 102 wait(1);
willem_hoitzing 0:65c093df6044 103 pc.printf(" 2...");
willem_hoitzing 0:65c093df6044 104 wait(1);
willem_hoitzing 0:65c093df6044 105 pc.printf(" 1...");
willem_hoitzing 0:65c093df6044 106 wait(1);
willem_hoitzing 0:65c093df6044 107 pc.printf("\r\nCalibrating...");
willem_hoitzing 0:65c093df6044 108
willem_hoitzing 0:65c093df6044 109 // for(int meting = 1;meting >= n_cal;meting++);
willem_hoitzing 0:65c093df6044 110 // {
willem_hoitzing 0:65c093df6044 111 // read
willem_hoitzing 0:65c093df6044 112 // total = total+read;
willem_hoitzing 0:65c093df6044 113 // wait(cal_time/n_cal);
willem_hoitzing 0:65c093df6044 114 // }
willem_hoitzing 0:65c093df6044 115 //
willem_hoitzing 0:65c093df6044 116 // EMG_max = total/n_cal;
willem_hoitzing 0:65c093df6044 117 EMG_biceps_max = 10;
willem_hoitzing 0:65c093df6044 118 EMG_triceps_max = 10;
willem_hoitzing 0:65c093df6044 119
willem_hoitzing 0:65c093df6044 120 combiner.attach(&combine_EMG, (1/Fs));
willem_hoitzing 0:65c093df6044 121 wait(1/(2*Fs));
willem_hoitzing 0:65c093df6044 122 updater.attach(&update_M1, (1/Fs));
willem_hoitzing 0:65c093df6044 123 }
willem_hoitzing 0:65c093df6044 124
willem_hoitzing 0:65c093df6044 125 char c;
willem_hoitzing 0:65c093df6044 126 int main()
willem_hoitzing 0:65c093df6044 127 {
willem_hoitzing 0:65c093df6044 128 pwm_M1 = 0;
willem_hoitzing 0:65c093df6044 129 dir_M1 = cw;
willem_hoitzing 0:65c093df6044 130 ledr = 1;
willem_hoitzing 0:65c093df6044 131 ledg = 1;
willem_hoitzing 0:65c093df6044 132 ledb = 1;
willem_hoitzing 0:65c093df6044 133 pc.baud(115200);
willem_hoitzing 0:65c093df6044 134 calibrate();
willem_hoitzing 0:65c093df6044 135 knop.fall(calibrate);
willem_hoitzing 0:65c093df6044 136 for(;;);
willem_hoitzing 0:65c093df6044 137 {
willem_hoitzing 0:65c093df6044 138 c = pc.getc();
willem_hoitzing 0:65c093df6044 139 wait(0.01);
willem_hoitzing 0:65c093df6044 140 switch(c)
willem_hoitzing 0:65c093df6044 141 {
willem_hoitzing 0:65c093df6044 142 case 'b':
willem_hoitzing 0:65c093df6044 143 input_b();
willem_hoitzing 0:65c093df6044 144 break;
willem_hoitzing 0:65c093df6044 145 case 'n':
willem_hoitzing 0:65c093df6044 146 input_n();
willem_hoitzing 0:65c093df6044 147 break;
willem_hoitzing 0:65c093df6044 148 case 't':
willem_hoitzing 0:65c093df6044 149 input_t();
willem_hoitzing 0:65c093df6044 150 break;
willem_hoitzing 0:65c093df6044 151 case 'y':
willem_hoitzing 0:65c093df6044 152 input_y();
willem_hoitzing 0:65c093df6044 153 break;
willem_hoitzing 0:65c093df6044 154 default:
willem_hoitzing 0:65c093df6044 155 pc.printf("\nUnknown command\n\r");
willem_hoitzing 0:65c093df6044 156 }
willem_hoitzing 0:65c093df6044 157 }
willem_hoitzing 0:65c093df6044 158 }