State Machine, bezig met mooimaken
Dependencies: Encoder HIDScope MODSERIAL biquadFilter mbed
Fork of vanEMGnaarMOTORPauline_States_nacht by
main.cpp@10:518a8617c86e, 2017-11-01 (annotated)
- Committer:
- paulineoonk
- Date:
- Wed Nov 01 14:58:20 2017 +0000
- Revision:
- 10:518a8617c86e
- Parent:
- 9:285499f48cdd
- Child:
- 11:b46a4c48c08f
biquad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Miriam | 0:d5fb29bc0847 | 1 | //libaries |
Miriam | 0:d5fb29bc0847 | 2 | #include "mbed.h" |
Miriam | 0:d5fb29bc0847 | 3 | #include "BiQuad.h" |
Miriam | 0:d5fb29bc0847 | 4 | #include "HIDScope.h" |
Miriam | 0:d5fb29bc0847 | 5 | #include "encoder.h" |
Miriam | 0:d5fb29bc0847 | 6 | #include "MODSERIAL.h" |
Miriam | 0:d5fb29bc0847 | 7 | |
paulineoonk | 3:36e706d6b3d2 | 8 | //globalvariables Motor |
paulineoonk | 3:36e706d6b3d2 | 9 | Ticker Treecko; //We make a awesome ticker for our control system |
paulineoonk | 8:c4ec359af35d | 10 | Ticker printer; |
paulineoonk | 8:c4ec359af35d | 11 | //PwmOut M1E(D6); //Biorobotics Motor 1 PWM control of the speed |
paulineoonk | 8:c4ec359af35d | 12 | //DigitalOut M1D(D7); //Biorobotics Motor 1 diraction control |
Miriam | 0:d5fb29bc0847 | 13 | |
paulineoonk | 8:c4ec359af35d | 14 | //Encoder motor1(D13,D12,true); |
paulineoonk | 3:36e706d6b3d2 | 15 | MODSERIAL pc(USBTX,USBRX); |
paulineoonk | 3:36e706d6b3d2 | 16 | |
paulineoonk | 8:c4ec359af35d | 17 | //double PwmPeriod = 1.0/5000.0; //set up of PWM periode (5000 Hz, want 5000 periodes in 1 seconde) |
paulineoonk | 3:36e706d6b3d2 | 18 | const double Ts = 0.1; // tickettijd/ sample time |
paulineoonk | 8:c4ec359af35d | 19 | //double e_prev = 0; |
paulineoonk | 8:c4ec359af35d | 20 | //double e_int = 0; |
paulineoonk | 6:e0e5da2c068f | 21 | double tijdstap = 0.002; |
paulineoonk | 8:c4ec359af35d | 22 | volatile double LBF; |
paulineoonk | 8:c4ec359af35d | 23 | volatile double RBF; |
paulineoonk | 8:c4ec359af35d | 24 | volatile double LTF; |
paulineoonk | 8:c4ec359af35d | 25 | volatile double RTF; |
Miriam | 0:d5fb29bc0847 | 26 | |
paulineoonk | 7:05c71a859d27 | 27 | //buttons en leds voor calibration |
paulineoonk | 7:05c71a859d27 | 28 | DigitalIn button1(PTA4); |
paulineoonk | 9:285499f48cdd | 29 | |
paulineoonk | 8:c4ec359af35d | 30 | DigitalOut ledred(LED_RED); |
paulineoonk | 7:05c71a859d27 | 31 | DigitalOut ledblue(LED_BLUE); |
paulineoonk | 9:285499f48cdd | 32 | DigitalOut ledgreen(LED_GREEN); |
paulineoonk | 8:c4ec359af35d | 33 | |
paulineoonk | 10:518a8617c86e | 34 | //MVC for calibration |
paulineoonk | 10:518a8617c86e | 35 | double MVCLB = 0; double MVCRB = 0; double MVCLT = 0; double MVCRT = 0; |
paulineoonk | 10:518a8617c86e | 36 | //MEAN for calibration - rest |
paulineoonk | 10:518a8617c86e | 37 | double RESTMEANLB = 0; double RESTMEANRB =0; double RESTMEANLT = 0; double RESTMEANRT = 0; |
paulineoonk | 9:285499f48cdd | 38 | |
paulineoonk | 10:518a8617c86e | 39 | double emgMEANSUBLB;double emgMEANSUBRB ;double emgMEANSUBLT ;double emgMEANSUBRT ; |
paulineoonk | 10:518a8617c86e | 40 | double emgSUMLB;double emgSUMRB;double emgSUMLT;double emgSUMRT; |
paulineoonk | 10:518a8617c86e | 41 | |
paulineoonk | 8:c4ec359af35d | 42 | |
paulineoonk | 7:05c71a859d27 | 43 | bool caldone = false; |
paulineoonk | 8:c4ec359af35d | 44 | int CalibrationSample = 1000; //How long will we calibrate? Timersampletime*Calibrationsample |
paulineoonk | 8:c4ec359af35d | 45 | |
paulineoonk | 9:285499f48cdd | 46 | int Timescalibration = 0; |
paulineoonk | 10:518a8617c86e | 47 | int TimescalibrationREST = 0; |
paulineoonk | 10:518a8617c86e | 48 | |
Miriam | 0:d5fb29bc0847 | 49 | |
paulineoonk | 9:285499f48cdd | 50 | // Biquad filters voor Left Bicep (LB) |
paulineoonk | 9:285499f48cdd | 51 | // Biquad filters van respectievelijk Notch, High-pass en Low-pass filter |
paulineoonk | 9:285499f48cdd | 52 | BiQuad N1LB( 8.63271e-01, -1.39680e+00, 8.63271e-01, -1.39680e+00, 7.26543e-01 ); |
paulineoonk | 9:285499f48cdd | 53 | BiQuadChain NFLB; |
paulineoonk | 9:285499f48cdd | 54 | BiQuad HP1LB( 9.63001e-01, -9.62990e-01, 0.00000e+00, -9.62994e-01, 0.00000e+00 ); |
paulineoonk | 9:285499f48cdd | 55 | BiQuad HP2LB( 1.00000e+00, -2.00001e+00, 1.00001e+00, -1.96161e+00, 9.63007e-01 ); |
paulineoonk | 9:285499f48cdd | 56 | BiQuadChain HPFLB; |
paulineoonk | 9:285499f48cdd | 57 | BiQuad LP1LB( 2.56971e-06, 2.56968e-06, 0.00000e+00, -9.72729e-01, 0.00000e+00 ); |
paulineoonk | 9:285499f48cdd | 58 | BiQuad LP2LB( 1.00000e+00, 2.00001e+00, 1.00001e+00, -1.97198e+00, 9.72734e-01 ); |
paulineoonk | 9:285499f48cdd | 59 | BiQuadChain LPFLB; |
paulineoonk | 9:285499f48cdd | 60 | |
paulineoonk | 9:285499f48cdd | 61 | // Biquad filters voor Right Bicep (RB) |
Miriam | 0:d5fb29bc0847 | 62 | // Biquad filters van respectievelijk Notch, High-pass en Low-pass filter |
paulineoonk | 9:285499f48cdd | 63 | BiQuad N1RB( 8.63271e-01, -1.39680e+00, 8.63271e-01, -1.39680e+00, 7.26543e-01 ); |
paulineoonk | 9:285499f48cdd | 64 | BiQuadChain NFRB; |
paulineoonk | 9:285499f48cdd | 65 | BiQuad HP1RB( 9.63001e-01, -9.62990e-01, 0.00000e+00, -9.62994e-01, 0.00000e+00 ); |
paulineoonk | 9:285499f48cdd | 66 | BiQuad HP2RB( 1.00000e+00, -2.00001e+00, 1.00001e+00, -1.96161e+00, 9.63007e-01 ); |
paulineoonk | 9:285499f48cdd | 67 | BiQuadChain HPFRB; |
paulineoonk | 9:285499f48cdd | 68 | BiQuad LP1RB( 2.56971e-06, 2.56968e-06, 0.00000e+00, -9.72729e-01, 0.00000e+00 ); |
paulineoonk | 9:285499f48cdd | 69 | BiQuad LP2RB( 1.00000e+00, 2.00001e+00, 1.00001e+00, -1.97198e+00, 9.72734e-01 ); |
paulineoonk | 9:285499f48cdd | 70 | BiQuadChain LPFRB; |
paulineoonk | 9:285499f48cdd | 71 | |
paulineoonk | 9:285499f48cdd | 72 | // Biquad filters voor Left Tricep (LT) |
paulineoonk | 9:285499f48cdd | 73 | // Biquad filters van respectievelijk Notch, High-pass en Low-pass filter |
paulineoonk | 9:285499f48cdd | 74 | BiQuad N1LT( 8.63271e-01, -1.39680e+00, 8.63271e-01, -1.39680e+00, 7.26543e-01 ); |
paulineoonk | 9:285499f48cdd | 75 | BiQuadChain NFLT; |
paulineoonk | 9:285499f48cdd | 76 | BiQuad HP1LT( 9.63001e-01, -9.62990e-01, 0.00000e+00, -9.62994e-01, 0.00000e+00 ); |
paulineoonk | 9:285499f48cdd | 77 | BiQuad HP2LT( 1.00000e+00, -2.00001e+00, 1.00001e+00, -1.96161e+00, 9.63007e-01 ); |
paulineoonk | 9:285499f48cdd | 78 | BiQuadChain HPFLT; |
paulineoonk | 9:285499f48cdd | 79 | BiQuad LP1LT( 2.56971e-06, 2.56968e-06, 0.00000e+00, -9.72729e-01, 0.00000e+00 ); |
paulineoonk | 9:285499f48cdd | 80 | BiQuad LP2LT( 1.00000e+00, 2.00001e+00, 1.00001e+00, -1.97198e+00, 9.72734e-01 ); |
paulineoonk | 9:285499f48cdd | 81 | BiQuadChain LPFLT; |
paulineoonk | 9:285499f48cdd | 82 | |
paulineoonk | 9:285499f48cdd | 83 | // Biquad filters voor Left Tricep (RT) |
paulineoonk | 9:285499f48cdd | 84 | // Biquad filters van respectievelijk Notch, High-pass en Low-pass filter |
paulineoonk | 9:285499f48cdd | 85 | BiQuad N1RT( 8.63271e-01, -1.39680e+00, 8.63271e-01, -1.39680e+00, 7.26543e-01 ); |
paulineoonk | 9:285499f48cdd | 86 | BiQuadChain NFRT; |
paulineoonk | 9:285499f48cdd | 87 | BiQuad HP1RT( 9.63001e-01, -9.62990e-01, 0.00000e+00, -9.62994e-01, 0.00000e+00 ); |
paulineoonk | 9:285499f48cdd | 88 | BiQuad HP2RT( 1.00000e+00, -2.00001e+00, 1.00001e+00, -1.96161e+00, 9.63007e-01 ); |
paulineoonk | 9:285499f48cdd | 89 | BiQuadChain HPFRT; |
paulineoonk | 9:285499f48cdd | 90 | BiQuad LP1RT( 2.56971e-06, 2.56968e-06, 0.00000e+00, -9.72729e-01, 0.00000e+00 ); |
paulineoonk | 9:285499f48cdd | 91 | BiQuad LP2RT( 1.00000e+00, 2.00001e+00, 1.00001e+00, -1.97198e+00, 9.72734e-01 ); |
paulineoonk | 9:285499f48cdd | 92 | BiQuadChain LPFRT; |
paulineoonk | 9:285499f48cdd | 93 | |
paulineoonk | 9:285499f48cdd | 94 | |
paulineoonk | 7:05c71a859d27 | 95 | |
paulineoonk | 7:05c71a859d27 | 96 | Timer looptime; //moetuiteindelijk weg |
paulineoonk | 6:e0e5da2c068f | 97 | |
paulineoonk | 7:05c71a859d27 | 98 | //filters |
paulineoonk | 8:c4ec359af35d | 99 | double emgNotchLB; |
paulineoonk | 8:c4ec359af35d | 100 | double emgHPLB; |
paulineoonk | 8:c4ec359af35d | 101 | double emgAbsHPLB; |
paulineoonk | 8:c4ec359af35d | 102 | double emgLPLB; |
paulineoonk | 8:c4ec359af35d | 103 | |
paulineoonk | 8:c4ec359af35d | 104 | double emgNotchRB; |
paulineoonk | 8:c4ec359af35d | 105 | double emgHPRB; |
paulineoonk | 8:c4ec359af35d | 106 | double emgAbsHPRB; |
paulineoonk | 8:c4ec359af35d | 107 | double emgLPRB; |
paulineoonk | 7:05c71a859d27 | 108 | |
paulineoonk | 8:c4ec359af35d | 109 | double emgNotchLT; |
paulineoonk | 8:c4ec359af35d | 110 | double emgHPLT; |
paulineoonk | 8:c4ec359af35d | 111 | double emgAbsHPLT; |
paulineoonk | 8:c4ec359af35d | 112 | double emgLPLT; |
paulineoonk | 7:05c71a859d27 | 113 | |
paulineoonk | 8:c4ec359af35d | 114 | double emgNotchRT; |
paulineoonk | 8:c4ec359af35d | 115 | double emgHPRT; |
paulineoonk | 8:c4ec359af35d | 116 | double emgAbsHPRT; |
paulineoonk | 8:c4ec359af35d | 117 | double emgLPRT; |
Miriam | 0:d5fb29bc0847 | 118 | |
paulineoonk | 3:36e706d6b3d2 | 119 | double f = 500; // frequency |
paulineoonk | 3:36e706d6b3d2 | 120 | double dt = 1/f; // sample frequency |
paulineoonk | 8:c4ec359af35d | 121 | |
paulineoonk | 8:c4ec359af35d | 122 | AnalogIn emgLB(A0); // EMG lezen |
paulineoonk | 8:c4ec359af35d | 123 | AnalogIn emgRB(A1); |
paulineoonk | 8:c4ec359af35d | 124 | AnalogIn emgLT(A2); |
paulineoonk | 8:c4ec359af35d | 125 | AnalogIn emgRT(A3); |
Miriam | 0:d5fb29bc0847 | 126 | |
paulineoonk | 9:285499f48cdd | 127 | //float MVCLB = 0.3; |
paulineoonk | 9:285499f48cdd | 128 | //float MVCRB = 0.3; |
paulineoonk | 9:285499f48cdd | 129 | //float MVCLT = 0.3; |
paulineoonk | 9:285499f48cdd | 130 | //float MVCRT = 0.3; |
paulineoonk | 8:c4ec359af35d | 131 | |
paulineoonk | 8:c4ec359af35d | 132 | void Filteren() |
Miriam | 0:d5fb29bc0847 | 133 | { |
paulineoonk | 6:e0e5da2c068f | 134 | looptime.reset(); |
paulineoonk | 6:e0e5da2c068f | 135 | looptime.start(); |
paulineoonk | 8:c4ec359af35d | 136 | |
paulineoonk | 8:c4ec359af35d | 137 | //EMG 1 |
paulineoonk | 8:c4ec359af35d | 138 | |
paulineoonk | 10:518a8617c86e | 139 | emgNotchLB = NFLB.step(emgLB.read() ); // Notch filter |
paulineoonk | 10:518a8617c86e | 140 | emgHPLB = HPFLB.step(emgNotchLB); // High-pass filter: also normalises around 0. |
paulineoonk | 8:c4ec359af35d | 141 | emgAbsHPLB = abs(emgHPLB); // Take absolute value |
paulineoonk | 10:518a8617c86e | 142 | emgLPLB = LPFLB.step(emgAbsHPLB); // Low-pass filter: creates envelope |
paulineoonk | 10:518a8617c86e | 143 | emgMEANSUBLB = emgLPLB - RESTMEANLB; //substract the restmean value |
paulineoonk | 10:518a8617c86e | 144 | LBF = emgLPLB/MVCLB; // Scale to maximum signal: useful for motor. LBF should now be between 0-1. |
paulineoonk | 9:285499f48cdd | 145 | |
paulineoonk | 10:518a8617c86e | 146 | emgNotchRB = NFRB.step(emgRB.read()); // Notch filter |
paulineoonk | 10:518a8617c86e | 147 | emgHPRB = HPFRB.step(emgNotchRB); // High-pass filter: also normalises around 0. |
paulineoonk | 8:c4ec359af35d | 148 | emgAbsHPRB = abs(emgHPRB); // Take absolute value |
paulineoonk | 10:518a8617c86e | 149 | emgLPRB = LPFRB.step(emgAbsHPRB); // Low-pass filter: creates envelope |
paulineoonk | 10:518a8617c86e | 150 | emgMEANSUBLB = emgLPLB - RESTMEANLB; |
paulineoonk | 9:285499f48cdd | 151 | RBF = emgLPRB/MVCRB; // Scale to maximum signal: useful for motor |
paulineoonk | 8:c4ec359af35d | 152 | |
paulineoonk | 10:518a8617c86e | 153 | emgNotchLT = NFLT.step(emgLT.read() ); // Notch filter |
paulineoonk | 10:518a8617c86e | 154 | emgHPLT = HPFLT.step(emgNotchLT); // High-pass filter: also normalises around 0. |
paulineoonk | 8:c4ec359af35d | 155 | emgAbsHPLT = abs(emgHPLT); // Take absolute value |
paulineoonk | 10:518a8617c86e | 156 | emgLPLT = LPFLT.step(emgAbsHPLT); // Low-pass filter: creates envelope |
paulineoonk | 10:518a8617c86e | 157 | emgMEANSUBLT = emgLPLT - RESTMEANLT; //substract the restmean value |
paulineoonk | 9:285499f48cdd | 158 | LTF = emgLPLT/MVCLT; // Scale to maximum signal: useful for motor |
paulineoonk | 8:c4ec359af35d | 159 | |
paulineoonk | 10:518a8617c86e | 160 | emgNotchRT = NFRT.step(emgRT.read() ); // Notch filter |
paulineoonk | 10:518a8617c86e | 161 | emgHPRT = HPFRT.step(emgNotchRT); // High-pass filter: also normalises around 0. |
paulineoonk | 8:c4ec359af35d | 162 | emgAbsHPRT = abs(emgHPRT); // Take absolute value |
paulineoonk | 10:518a8617c86e | 163 | emgLPRT = LPFRT.step(emgAbsHPRT); // Low-pass filter: creates envelope |
paulineoonk | 10:518a8617c86e | 164 | emgMEANSUBRT = emgLPRT - RESTMEANRT; //substract the restmean value |
paulineoonk | 9:285499f48cdd | 165 | RTF = emgLPRT/MVCRT; // Scale to maximum signal: useful for motor |
paulineoonk | 8:c4ec359af35d | 166 | |
paulineoonk | 8:c4ec359af35d | 167 | //if (emgFiltered >1) |
paulineoonk | 8:c4ec359af35d | 168 | //{ |
paulineoonk | 8:c4ec359af35d | 169 | // emgFiltered=1.00; |
paulineoonk | 8:c4ec359af35d | 170 | //} |
paulineoonk | 8:c4ec359af35d | 171 | //pc.printf("emgreadLB = %f , emgFiltered = %f, maxi = %f, loop = %f \r\n, emgreadRB = %f , emgFiltered = %f, maxi = %f \r\n, emgreadLT = %f , emgFiltered = %f, maxi = %f \r\n, emgreadRT = %f , emgFiltered = %f, maxi = %f \r\n",emgLB.read(), LBF, maxiLB,looptime.read(),emgRB.read(), RBF, maxiRB,emgLT.read(), LTF, maxiLT, emgRT.read(), RTF, maxiRT); |
paulineoonk | 6:e0e5da2c068f | 172 | //int maxwaarde = 4096; // = 64x64 |
paulineoonk | 6:e0e5da2c068f | 173 | //double refP = emgFiltered*maxwaarde; |
paulineoonk | 6:e0e5da2c068f | 174 | //return refP; // value between 0 and 4096 |
paulineoonk | 9:285499f48cdd | 175 | |
Miriam | 0:d5fb29bc0847 | 176 | } |
paulineoonk | 9:285499f48cdd | 177 | |
paulineoonk | 10:518a8617c86e | 178 | void CalibrationEMG() |
paulineoonk | 7:05c71a859d27 | 179 | { |
paulineoonk | 7:05c71a859d27 | 180 | Timescalibration++; |
paulineoonk | 9:285499f48cdd | 181 | |
paulineoonk | 10:518a8617c86e | 182 | if(Timescalibration<2000) |
paulineoonk | 9:285499f48cdd | 183 | { |
paulineoonk | 10:518a8617c86e | 184 | |
paulineoonk | 10:518a8617c86e | 185 | emgNotchLB = NFLB.step(emgLB.read() ); |
paulineoonk | 10:518a8617c86e | 186 | emgHPLB = HPFLB.step(emgNotchLB); |
paulineoonk | 10:518a8617c86e | 187 | emgAbsHPLB = abs(emgHPLB); |
paulineoonk | 10:518a8617c86e | 188 | emgLPLB = LPFLB.step(emgAbsHPLB); |
paulineoonk | 10:518a8617c86e | 189 | emgSUMLB += emgLPLB; //SUM all rest values LB |
paulineoonk | 10:518a8617c86e | 190 | |
paulineoonk | 10:518a8617c86e | 191 | emgNotchRB = NFRB.step(emgRB.read()); |
paulineoonk | 10:518a8617c86e | 192 | emgHPRB = HPFRB.step(emgNotchRB); |
paulineoonk | 10:518a8617c86e | 193 | emgAbsHPRB = abs(emgHPRB); |
paulineoonk | 10:518a8617c86e | 194 | emgLPRB = LPFRB.step(emgAbsHPRB); |
paulineoonk | 10:518a8617c86e | 195 | emgSUMRB += emgLPRB; //SUM all rest values RB |
paulineoonk | 10:518a8617c86e | 196 | |
paulineoonk | 10:518a8617c86e | 197 | emgNotchLT = NFLT.step(emgLT.read() ); |
paulineoonk | 10:518a8617c86e | 198 | emgHPLT = HPFLT.step(emgNotchLT); |
paulineoonk | 10:518a8617c86e | 199 | emgAbsHPLT = abs(emgHPLT); |
paulineoonk | 10:518a8617c86e | 200 | emgLPLT = LPFLT.step(emgAbsHPLT); |
paulineoonk | 10:518a8617c86e | 201 | emgSUMLT += emgLPLT; //SUM all rest values LT |
paulineoonk | 10:518a8617c86e | 202 | |
paulineoonk | 10:518a8617c86e | 203 | emgNotchRT = NFRT.step(emgRT.read() ); |
paulineoonk | 10:518a8617c86e | 204 | emgHPRT = HPFRT.step(emgNotchRT); |
paulineoonk | 10:518a8617c86e | 205 | emgAbsHPRT = abs(emgHPRT); |
paulineoonk | 10:518a8617c86e | 206 | emgLPRT = LPFRT.step(emgAbsHPRT); |
paulineoonk | 10:518a8617c86e | 207 | emgSUMRT += emgLPRT; //SUM all rest values RT |
paulineoonk | 10:518a8617c86e | 208 | } |
paulineoonk | 10:518a8617c86e | 209 | if(Timescalibration==1999) |
paulineoonk | 10:518a8617c86e | 210 | { |
paulineoonk | 10:518a8617c86e | 211 | RESTMEANLB = emgSUMLB/Timescalibration; //determine the mean rest value |
paulineoonk | 10:518a8617c86e | 212 | RESTMEANRB = emgSUMRB/Timescalibration; //determine the mean rest value |
paulineoonk | 10:518a8617c86e | 213 | RESTMEANRT = emgSUMRT/Timescalibration; //determine the mean rest value |
paulineoonk | 10:518a8617c86e | 214 | RESTMEANLT = emgSUMLT/Timescalibration; //determine the mean rest value |
paulineoonk | 10:518a8617c86e | 215 | } |
paulineoonk | 10:518a8617c86e | 216 | if(Timescalibration>2000 && Timescalibration<6000) |
paulineoonk | 10:518a8617c86e | 217 | { |
paulineoonk | 10:518a8617c86e | 218 | emgNotchLB = NFLB.step(emgLB.read() ); |
paulineoonk | 10:518a8617c86e | 219 | emgHPLB = HPFLB.step(emgNotchLB); |
paulineoonk | 10:518a8617c86e | 220 | emgAbsHPLB = abs(emgHPLB); |
paulineoonk | 10:518a8617c86e | 221 | emgLPLB = LPFLB.step(emgAbsHPLB); |
paulineoonk | 9:285499f48cdd | 222 | double emgfinalLB = emgLPLB; |
paulineoonk | 9:285499f48cdd | 223 | if (emgfinalLB > MVCLB) |
paulineoonk | 9:285499f48cdd | 224 | { //determine what the highest reachable emg signal is |
paulineoonk | 9:285499f48cdd | 225 | MVCLB = emgfinalLB; |
paulineoonk | 9:285499f48cdd | 226 | } |
paulineoonk | 9:285499f48cdd | 227 | } |
paulineoonk | 9:285499f48cdd | 228 | |
paulineoonk | 10:518a8617c86e | 229 | if(Timescalibration>6000 && Timescalibration<10000) |
paulineoonk | 9:285499f48cdd | 230 | { |
paulineoonk | 10:518a8617c86e | 231 | emgNotchRB = NFRB.step(emgRB.read()); |
paulineoonk | 10:518a8617c86e | 232 | emgHPRB = HPFRB.step(emgNotchRB); |
paulineoonk | 10:518a8617c86e | 233 | emgAbsHPRB = abs(emgHPRB); |
paulineoonk | 10:518a8617c86e | 234 | emgLPRB = LPFRB.step(emgAbsHPRB); |
paulineoonk | 9:285499f48cdd | 235 | double emgfinalRB = emgLPRB; |
paulineoonk | 9:285499f48cdd | 236 | if (emgfinalRB > MVCRB) |
paulineoonk | 7:05c71a859d27 | 237 | { //determine what the highest reachable emg signal is |
paulineoonk | 9:285499f48cdd | 238 | MVCRB = emgfinalRB; |
paulineoonk | 9:285499f48cdd | 239 | } |
paulineoonk | 9:285499f48cdd | 240 | } |
paulineoonk | 9:285499f48cdd | 241 | |
paulineoonk | 10:518a8617c86e | 242 | if(Timescalibration>10000 && Timescalibration<14000) |
paulineoonk | 9:285499f48cdd | 243 | { |
paulineoonk | 10:518a8617c86e | 244 | emgNotchLT = NFLT.step(emgLT.read() ); |
paulineoonk | 10:518a8617c86e | 245 | emgHPLT = HPFLT.step(emgNotchLT); |
paulineoonk | 10:518a8617c86e | 246 | emgAbsHPLT = abs(emgHPLT); |
paulineoonk | 10:518a8617c86e | 247 | emgLPLT = LPFLT.step(emgAbsHPLT); |
paulineoonk | 9:285499f48cdd | 248 | double emgfinalLT = emgLPLT; |
paulineoonk | 9:285499f48cdd | 249 | if (emgfinalLT > MVCLT) |
paulineoonk | 9:285499f48cdd | 250 | { //determine what the highest reachable emg signal is |
paulineoonk | 9:285499f48cdd | 251 | MVCLT = emgfinalLT; |
paulineoonk | 7:05c71a859d27 | 252 | } |
paulineoonk | 9:285499f48cdd | 253 | } |
paulineoonk | 9:285499f48cdd | 254 | |
paulineoonk | 10:518a8617c86e | 255 | if(Timescalibration>14000 && Timescalibration<18000) |
paulineoonk | 9:285499f48cdd | 256 | { |
paulineoonk | 10:518a8617c86e | 257 | emgNotchRT = NFRT.step(emgRT.read() ); |
paulineoonk | 10:518a8617c86e | 258 | emgHPRT = HPFRT.step(emgNotchRT); |
paulineoonk | 10:518a8617c86e | 259 | emgAbsHPRT = abs(emgHPRT); |
paulineoonk | 10:518a8617c86e | 260 | emgLPRT = LPFRT.step(emgAbsHPRT); |
paulineoonk | 9:285499f48cdd | 261 | double emgfinalRT = emgLPRT; |
paulineoonk | 9:285499f48cdd | 262 | if (emgfinalRT > MVCRT) |
paulineoonk | 9:285499f48cdd | 263 | { //determine what the highest reachable emg signal is |
paulineoonk | 9:285499f48cdd | 264 | MVCRT = emgfinalRT; |
paulineoonk | 9:285499f48cdd | 265 | } |
paulineoonk | 9:285499f48cdd | 266 | } |
paulineoonk | 9:285499f48cdd | 267 | |
paulineoonk | 10:518a8617c86e | 268 | if(Timescalibration>18000) |
paulineoonk | 7:05c71a859d27 | 269 | { |
paulineoonk | 7:05c71a859d27 | 270 | caldone=true; |
paulineoonk | 7:05c71a859d27 | 271 | } |
paulineoonk | 9:285499f48cdd | 272 | // pc.printf("maxi waarde = %f emgfinal = %f \r\n",maxi,emgfinal); |
paulineoonk | 9:285499f48cdd | 273 | //} |
paulineoonk | 7:05c71a859d27 | 274 | //PAS ALS DEZE TRUE IS, MOET DE MOTOR PAS BEWEGEN!!! |
paulineoonk | 7:05c71a859d27 | 275 | //return maxi; |
paulineoonk | 7:05c71a859d27 | 276 | } |
paulineoonk | 7:05c71a859d27 | 277 | |
paulineoonk | 9:285499f48cdd | 278 | |
paulineoonk | 7:05c71a859d27 | 279 | |
paulineoonk | 6:e0e5da2c068f | 280 | /* |
paulineoonk | 3:36e706d6b3d2 | 281 | double Encoder () |
paulineoonk | 3:36e706d6b3d2 | 282 | { |
paulineoonk | 3:36e706d6b3d2 | 283 | double Huidigepositie = motor1.getPosition (); |
paulineoonk | 3:36e706d6b3d2 | 284 | return Huidigepositie; // huidige positie = current position |
paulineoonk | 3:36e706d6b3d2 | 285 | } |
paulineoonk | 6:e0e5da2c068f | 286 | |
paulineoonk | 3:36e706d6b3d2 | 287 | double FeedBackControl(double error, double &e_prev, double &e_int) // schaalt de snelheid naar de snelheid zodat onze chip het begrijpt (is nog niet in werking) |
Miriam | 0:d5fb29bc0847 | 288 | { |
paulineoonk | 3:36e706d6b3d2 | 289 | double kp = 0.001; // has jet to be scaled |
paulineoonk | 3:36e706d6b3d2 | 290 | double Proportional= kp*error; |
Miriam | 0:d5fb29bc0847 | 291 | |
paulineoonk | 3:36e706d6b3d2 | 292 | double kd = 0.0004; // has jet to be scaled |
paulineoonk | 3:36e706d6b3d2 | 293 | double VelocityError = (error - e_prev)/Ts; |
paulineoonk | 3:36e706d6b3d2 | 294 | double Derivative = kd*VelocityError; |
Miriam | 0:d5fb29bc0847 | 295 | e_prev = error; |
Miriam | 0:d5fb29bc0847 | 296 | |
paulineoonk | 3:36e706d6b3d2 | 297 | double ki = 0.00005; // has jet to be scaled |
Miriam | 0:d5fb29bc0847 | 298 | e_int = e_int+Ts*error; |
paulineoonk | 3:36e706d6b3d2 | 299 | double Integrator = ki*e_int; |
Miriam | 0:d5fb29bc0847 | 300 | |
Miriam | 0:d5fb29bc0847 | 301 | |
paulineoonk | 3:36e706d6b3d2 | 302 | double motorValue = Proportional + Integrator + Derivative; |
Miriam | 0:d5fb29bc0847 | 303 | return motorValue; |
Miriam | 0:d5fb29bc0847 | 304 | } |
paulineoonk | 6:e0e5da2c068f | 305 | |
paulineoonk | 3:36e706d6b3d2 | 306 | void SetMotor1(double motorValue) |
Miriam | 0:d5fb29bc0847 | 307 | { |
Miriam | 0:d5fb29bc0847 | 308 | if (motorValue >= 0) |
Miriam | 0:d5fb29bc0847 | 309 | { |
Miriam | 0:d5fb29bc0847 | 310 | M1D = 0; |
Miriam | 0:d5fb29bc0847 | 311 | } |
Miriam | 0:d5fb29bc0847 | 312 | else |
Miriam | 0:d5fb29bc0847 | 313 | { |
Miriam | 0:d5fb29bc0847 | 314 | M1D = 1; |
Miriam | 0:d5fb29bc0847 | 315 | } |
Miriam | 0:d5fb29bc0847 | 316 | |
Miriam | 0:d5fb29bc0847 | 317 | if (fabs(motorValue) > 1) |
Miriam | 0:d5fb29bc0847 | 318 | { |
Miriam | 0:d5fb29bc0847 | 319 | M1E = 1; //de snelheid wordt teruggeschaald naar 8.4 rad/s (maximale snelheid, dus waarde 1) |
Miriam | 0:d5fb29bc0847 | 320 | } |
Miriam | 0:d5fb29bc0847 | 321 | else |
Miriam | 0:d5fb29bc0847 | 322 | { |
Miriam | 0:d5fb29bc0847 | 323 | M1E = fabs(motorValue); //de absolute snelheid wordt bepaald, de motor staat uit bij een waarde 0 |
Miriam | 0:d5fb29bc0847 | 324 | } |
Miriam | 0:d5fb29bc0847 | 325 | } |
paulineoonk | 7:05c71a859d27 | 326 | */ |
paulineoonk | 3:36e706d6b3d2 | 327 | void MeasureAndControl () |
Miriam | 0:d5fb29bc0847 | 328 | { |
paulineoonk | 3:36e706d6b3d2 | 329 | // hier the control of the control system |
paulineoonk | 7:05c71a859d27 | 330 | |
paulineoonk | 9:285499f48cdd | 331 | if(caldone==false) |
paulineoonk | 9:285499f48cdd | 332 | { |
paulineoonk | 9:285499f48cdd | 333 | if(button1.read()==false) |
paulineoonk | 9:285499f48cdd | 334 | { |
paulineoonk | 10:518a8617c86e | 335 | CalibrationEMG(); |
paulineoonk | 9:285499f48cdd | 336 | } |
paulineoonk | 9:285499f48cdd | 337 | } |
paulineoonk | 9:285499f48cdd | 338 | if (caldone==true) |
paulineoonk | 7:05c71a859d27 | 339 | |
paulineoonk | 9:285499f48cdd | 340 | { |
paulineoonk | 8:c4ec359af35d | 341 | Filteren(); |
paulineoonk | 7:05c71a859d27 | 342 | //rest |
paulineoonk | 9:285499f48cdd | 343 | } |
paulineoonk | 7:05c71a859d27 | 344 | |
paulineoonk | 4:5607088ef6f5 | 345 | //double Huidigepositie = Encoder(); |
paulineoonk | 4:5607088ef6f5 | 346 | //double error = (refP - Huidigepositie);// make an error |
paulineoonk | 4:5607088ef6f5 | 347 | //double motorValue = FeedBackControl(error, e_prev, e_int); |
paulineoonk | 7:05c71a859d27 | 348 | //double motorValue = refP; |
paulineoonk | 7:05c71a859d27 | 349 | //SetMotor1(motorValue); |
Miriam | 0:d5fb29bc0847 | 350 | } |
Miriam | 0:d5fb29bc0847 | 351 | |
paulineoonk | 8:c4ec359af35d | 352 | void Tickerfunctie() |
paulineoonk | 8:c4ec359af35d | 353 | { |
paulineoonk | 10:518a8617c86e | 354 | pc.printf("emgreadRB = %f , emgFiltered = %f, maxi = %f meanrest = %f\r\n",emgRB.read(), RBF, MVCRB, RESTMEANLB); |
paulineoonk | 10:518a8617c86e | 355 | pc.printf("emgreadLB = %f , emgFiltered = %f, maxi = %f, loop = %f meanrest = %f \r\n",emgLB.read(), LBF, MVCLB,RESTMEANRB); |
paulineoonk | 10:518a8617c86e | 356 | pc.printf("emgreadRT = %f , emgFilteredRT = %f, maxiRT = %f meanrest = %f \r\n",emgRT.read(), RTF, MVCRT,RESTMEANRT); |
paulineoonk | 10:518a8617c86e | 357 | pc.printf("emgreadLT = %f , emgFilteredLT = %f, maxiLT = %f meanrest = %f \r\n",emgLT.read(), LTF, MVCLT,RESTMEANLT); |
paulineoonk | 8:c4ec359af35d | 358 | } |
paulineoonk | 7:05c71a859d27 | 359 | |
Miriam | 0:d5fb29bc0847 | 360 | int main() |
Miriam | 0:d5fb29bc0847 | 361 | { |
paulineoonk | 7:05c71a859d27 | 362 | //voor EMG filteren |
paulineoonk | 10:518a8617c86e | 363 | //Left Bicep |
paulineoonk | 9:285499f48cdd | 364 | NFLB.add( &N1LB ); |
paulineoonk | 10:518a8617c86e | 365 | HPFLB.add( &HP1LB ).add( &HP2LB ); |
paulineoonk | 9:285499f48cdd | 366 | LPFLB.add( &LP1LB ).add( &LP2LB ); |
paulineoonk | 9:285499f48cdd | 367 | |
paulineoonk | 10:518a8617c86e | 368 | //Right Bicep |
paulineoonk | 10:518a8617c86e | 369 | NFRB.add( &N1RB ); |
paulineoonk | 10:518a8617c86e | 370 | HPFRB.add( &HP1RB ).add( &HP2RB ); |
paulineoonk | 10:518a8617c86e | 371 | LPFRB.add( &LP1RB ).add( &LP2RB ); |
paulineoonk | 9:285499f48cdd | 372 | |
paulineoonk | 10:518a8617c86e | 373 | //Left Tricep |
paulineoonk | 10:518a8617c86e | 374 | NFLT.add( &N1LT ); |
paulineoonk | 10:518a8617c86e | 375 | HPFLT.add( &HP1LT ).add( &HP2LT ); |
paulineoonk | 10:518a8617c86e | 376 | LPFLT.add( &LP1LT ).add( &LP2LT ); |
paulineoonk | 10:518a8617c86e | 377 | |
paulineoonk | 10:518a8617c86e | 378 | //Right Tricep |
paulineoonk | 10:518a8617c86e | 379 | NFRT.add( &N1RT ); |
paulineoonk | 10:518a8617c86e | 380 | HPFRT.add( &HP1RT ).add( &HP2RT ); |
paulineoonk | 10:518a8617c86e | 381 | LPFRT.add( &LP1RT ).add( &LP2RT ); |
paulineoonk | 3:36e706d6b3d2 | 382 | |
paulineoonk | 7:05c71a859d27 | 383 | //voor serial |
paulineoonk | 7:05c71a859d27 | 384 | pc.baud(115200); |
paulineoonk | 7:05c71a859d27 | 385 | |
paulineoonk | 7:05c71a859d27 | 386 | //motor |
paulineoonk | 8:c4ec359af35d | 387 | // M1E.period(PwmPeriod); //set PWMposition at 5000hz |
paulineoonk | 7:05c71a859d27 | 388 | //Ticker |
paulineoonk | 7:05c71a859d27 | 389 | Treecko.attach(MeasureAndControl, tijdstap); //Elke 1 seconde zorgt de ticker voor het runnen en uitlezen van de verschillende |
paulineoonk | 3:36e706d6b3d2 | 390 | //functies en analoge signalen. Veranderingen worden elke 1 seconde doorgevoerd. |
paulineoonk | 9:285499f48cdd | 391 | printer.attach(Tickerfunctie,0.4); |
paulineoonk | 3:36e706d6b3d2 | 392 | while(true) |
Miriam | 0:d5fb29bc0847 | 393 | { |
paulineoonk | 3:36e706d6b3d2 | 394 | } |
paulineoonk | 3:36e706d6b3d2 | 395 | |
paulineoonk | 3:36e706d6b3d2 | 396 | |
paulineoonk | 3:36e706d6b3d2 | 397 | } |