Aukie Hooglugt / Mbed 2 deprecated EMG_Project

Dependencies:   HIDScope MODSERIAL- mbed-dsp mbed

Committer:
Hooglugt
Date:
Wed Oct 08 11:41:43 2014 +0000
Revision:
38:277ba1c0693c
Parent:
37:2d248e64b745
Child:
39:0328b1f16a5a
floats for emg_bi/trifloat instead of pin outputs to "create" floats

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hooglugt 23:8d9a623dd713 1 #include "mbed.h"
Hooglugt 23:8d9a623dd713 2 #include "MODSERIAL.h"
Hooglugt 23:8d9a623dd713 3 #include "HIDScope.h"
Hooglugt 29:7523e4a8e000 4
Hooglugt 38:277ba1c0693c 5 #define TIMEB4NEXTCHOICE 2 // 1s keuzelampje blijft aan
Hooglugt 38:277ba1c0693c 6 #define TIMEBETWEENBLINK 20 // 1s voor volgende blink
Hooglugt 23:8d9a623dd713 7
Hooglugt 23:8d9a623dd713 8 //Define objects
Hooglugt 29:7523e4a8e000 9 AnalogIn emg0(PTB1); //Analog input biceps
Hooglugt 38:277ba1c0693c 10 float emg_bifloat; //Float voor EMG-waarde biceps, VRAAG: waarom we hier een PwmOut voor gebruiken - kunnen we geen float gebruiken?
Hooglugt 38:277ba1c0693c 11
Hooglugt 29:7523e4a8e000 12 AnalogIn emg1(PTB2); //Analog input triceps
Hooglugt 38:277ba1c0693c 13 float emg_trifloat; //Float voor EMG-waarde triceps
Hooglugt 29:7523e4a8e000 14
Hooglugt 38:277ba1c0693c 15 HIDScope scope(4);
Hooglugt 38:277ba1c0693c 16
Hooglugt 38:277ba1c0693c 17 Ticker log_timer;
Hooglugt 38:277ba1c0693c 18 Ticker reset_timer;
Hooglugt 38:277ba1c0693c 19 MODSERIAL pc(USBTX,USBRX);
Hooglugt 29:7523e4a8e000 20
Hooglugt 29:7523e4a8e000 21 PwmOut red(LED_RED);
Hooglugt 23:8d9a623dd713 22 PwmOut green(LED_GREEN);
Hooglugt 23:8d9a623dd713 23 PwmOut blue(LED_BLUE);
Hooglugt 29:7523e4a8e000 24
Hooglugt 35:c3182df00ec8 25 int direction = 0;
Hooglugt 35:c3182df00ec8 26 int force = 0;
Hooglugt 23:8d9a623dd713 27
Hooglugt 38:277ba1c0693c 28
Hooglugt 23:8d9a623dd713 29
Hooglugt 29:7523e4a8e000 30 void looper() //nieuwe looper maken, die om een nog te bepalen tijdseenheid de emgtrifloat checked en anders nieuwe goto maakt naar vorige selectiekeuze
Hooglugt 23:8d9a623dd713 31 {
Hooglugt 29:7523e4a8e000 32 /*put raw emg value of biceps both in emg_bifloat and in emg_bivalue*/
Hooglugt 38:277ba1c0693c 33 emg_bifloat = emg0.read(); // read float value (0..1 = 0..3.3V) biceps
Hooglugt 29:7523e4a8e000 34 uint16_t emg_bivalue;
Hooglugt 29:7523e4a8e000 35 emg_bivalue = emg0.read_u16(); // read direct ADC result (0..4096 = 0..3.3V) biceps
Hooglugt 29:7523e4a8e000 36
Hooglugt 29:7523e4a8e000 37 /*put raw emg value of triceps both in emg_trifloat and in emg_trivalue*/
Hooglugt 38:277ba1c0693c 38 emg_trifloat = emg1.read(); // read float value (0..1 = 0..3.3V) triceps
Hooglugt 29:7523e4a8e000 39 uint16_t emg_trivalue;
Hooglugt 29:7523e4a8e000 40 emg_trivalue = emg1.read_u16(); // read direct ADC result (0..4096 = 0..3.3V) biceps
Hooglugt 29:7523e4a8e000 41
Hooglugt 31:d8eaf0ce8517 42 /*send value to PC. Line below is used to prevent buffer overrun */
Hooglugt 30:5e5098b0cca6 43 if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) { //VRAAG: praktisch nut hiervan? print emg value wanneer buffercount groter dan 30 is
Hooglugt 30:5e5098b0cca6 44 //pc.printf("%u\n",emg_bivalue);
Hooglugt 29:7523e4a8e000 45 }
Hooglugt 36:af949aaaba01 46
Hooglugt 34:e166187e62ce 47 /* EMG-singaal van biceps en triceps worden hier gefilterd*/
Hooglugt 36:af949aaaba01 48
Hooglugt 29:7523e4a8e000 49 scope.set(0,emg_bivalue);
Hooglugt 38:277ba1c0693c 50 scope.set(1,emg_bifloat);
Hooglugt 29:7523e4a8e000 51 scope.set(2,emg_trivalue);
Hooglugt 38:277ba1c0693c 52 scope.set(3,emg_trifloat);
Hooglugt 23:8d9a623dd713 53 scope.send();
Hooglugt 23:8d9a623dd713 54 }
Hooglugt 23:8d9a623dd713 55
Hooglugt 34:e166187e62ce 56 /*
Hooglugt 32:aaf01b1bf05d 57 void resetlooper() // VRAAG: wat gebeurt er wanneer en resetlooper en looper tegelijkertijd gecalled worden?!
Hooglugt 31:d8eaf0ce8517 58 {
Hooglugt 30:5e5098b0cca6 59 if(emg_trifloat.read()>0.8 && direction != 0) { //dit is alleen mogelijk wanneer directionchoice is gemaakt
Hooglugt 30:5e5098b0cca6 60 direction = 0;
Hooglugt 30:5e5098b0cca6 61 force = 0; // WEGHALEN, wanneer in uiteindelijke script na force keuzen niet meer gereset kan worden (voor nu wel handig)
Hooglugt 31:d8eaf0ce8517 62 pc.printf("reset ");
Hooglugt 31:d8eaf0ce8517 63 }
Hooglugt 30:5e5098b0cca6 64 }
Hooglugt 35:c3182df00ec8 65 CONSTANTE RESETS DOOR BEWEGINGSARTEFACTEN */
Hooglugt 31:d8eaf0ce8517 66
Hooglugt 23:8d9a623dd713 67 int main()
Hooglugt 23:8d9a623dd713 68 {
Hooglugt 25:ec41b972b250 69 pc.baud(115200); //baudrate instellen
Hooglugt 38:277ba1c0693c 70 // emg_bifloat.period_ms(2); //sets period for the PWM to the emgfloat PTD4
Hooglugt 38:277ba1c0693c 71 // emg_trifloat.period_ms(2);
Hooglugt 25:ec41b972b250 72 log_timer.attach(looper, 0.001); // The looper() function will be called every 0.001 seconds (with the ticker object)
Hooglugt 34:e166187e62ce 73 // reset_timer.attach(resetlooper, 0.1); //
Hooglugt 32:aaf01b1bf05d 74
Hooglugt 37:2d248e64b745 75 goto directionchoice; // goes to first while(1) for the deciding the direction
Hooglugt 37:2d248e64b745 76
Hooglugt 37:2d248e64b745 77 while(1) { //Loop keuze DIRECTION
Hooglugt 37:2d248e64b745 78 directionchoice:
Hooglugt 23:8d9a623dd713 79 for(int i=1; i<4; i++) {
Hooglugt 23:8d9a623dd713 80 if(i==1) { //red
Hooglugt 23:8d9a623dd713 81 red=0;
Hooglugt 23:8d9a623dd713 82 green=1;
Hooglugt 23:8d9a623dd713 83 blue=1;
Hooglugt 37:2d248e64b745 84 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 38:277ba1c0693c 85 if(emg_bifloat>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 25:ec41b972b250 86 direction = 1;
Hooglugt 37:2d248e64b745 87 red=1;
Hooglugt 37:2d248e64b745 88 green = 0;
Hooglugt 36:af949aaaba01 89 blue = 0;
Hooglugt 37:2d248e64b745 90 pc.printf("links ");
Hooglugt 37:2d248e64b745 91 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie
Hooglugt 37:2d248e64b745 92 goto forcechoice; // goes to second while(1) for the deciding the force
Hooglugt 23:8d9a623dd713 93 } else {
Hooglugt 23:8d9a623dd713 94 wait(0.1);
Hooglugt 23:8d9a623dd713 95 }
Hooglugt 23:8d9a623dd713 96 }
Hooglugt 23:8d9a623dd713 97 }
Hooglugt 23:8d9a623dd713 98 if(i==2) { //green
Hooglugt 23:8d9a623dd713 99 red =1;
Hooglugt 23:8d9a623dd713 100 green=0;
Hooglugt 23:8d9a623dd713 101 blue=1;
Hooglugt 37:2d248e64b745 102 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 38:277ba1c0693c 103 if(emg_bifloat>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 25:ec41b972b250 104 direction = 2;
Hooglugt 37:2d248e64b745 105 red=0;
Hooglugt 29:7523e4a8e000 106 green = 1;
Hooglugt 37:2d248e64b745 107 blue = 0;
Hooglugt 37:2d248e64b745 108 pc.printf("mid ");
Hooglugt 37:2d248e64b745 109 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie
Hooglugt 23:8d9a623dd713 110 goto forcechoice;
Hooglugt 23:8d9a623dd713 111 } else {
Hooglugt 23:8d9a623dd713 112 wait(0.1);
Hooglugt 23:8d9a623dd713 113 }
Hooglugt 23:8d9a623dd713 114 }
Hooglugt 23:8d9a623dd713 115 }
Hooglugt 23:8d9a623dd713 116 if(i==3) { //blue
Hooglugt 23:8d9a623dd713 117 red=1;
Hooglugt 23:8d9a623dd713 118 green=1;
Hooglugt 23:8d9a623dd713 119 blue=0;
Hooglugt 37:2d248e64b745 120 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 38:277ba1c0693c 121 if(emg_bifloat>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 23:8d9a623dd713 122 direction = 3;
Hooglugt 37:2d248e64b745 123 red=0;
Hooglugt 29:7523e4a8e000 124 green = 0;
Hooglugt 37:2d248e64b745 125 blue = 1;
Hooglugt 37:2d248e64b745 126 pc.printf("rechts ");
Hooglugt 37:2d248e64b745 127 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie
Hooglugt 23:8d9a623dd713 128 goto forcechoice;
Hooglugt 23:8d9a623dd713 129 } else {
Hooglugt 23:8d9a623dd713 130 wait(0.1);
Hooglugt 23:8d9a623dd713 131 }
Hooglugt 23:8d9a623dd713 132 }
Hooglugt 23:8d9a623dd713 133 }
Hooglugt 25:ec41b972b250 134 }
Hooglugt 23:8d9a623dd713 135 }
Hooglugt 37:2d248e64b745 136 while(1) { //Loop keuze FORCE
Hooglugt 25:ec41b972b250 137 forcechoice:
Hooglugt 37:2d248e64b745 138 for(int j=1; j<4; j++) {
Hooglugt 37:2d248e64b745 139 if(j==1) { //red
Hooglugt 37:2d248e64b745 140 red=0;
Hooglugt 37:2d248e64b745 141 green=1;
Hooglugt 37:2d248e64b745 142 blue=1;
Hooglugt 37:2d248e64b745 143 if(direction==0) { //if statement die controleert of direction 0 is (dus of triceps gereset is)
Hooglugt 37:2d248e64b745 144 goto directionchoice;
Hooglugt 37:2d248e64b745 145 }
Hooglugt 37:2d248e64b745 146 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 38:277ba1c0693c 147 if(emg_bifloat>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 37:2d248e64b745 148 force = 1;
Hooglugt 37:2d248e64b745 149 red=1;
Hooglugt 37:2d248e64b745 150 green = 0;
Hooglugt 37:2d248e64b745 151 blue = 0;
Hooglugt 37:2d248e64b745 152 pc.printf("zwak ");
Hooglugt 37:2d248e64b745 153 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie
Hooglugt 37:2d248e64b745 154 goto choicesmade;
Hooglugt 37:2d248e64b745 155 } else {
Hooglugt 37:2d248e64b745 156 wait(0.1);
Hooglugt 23:8d9a623dd713 157 }
Hooglugt 23:8d9a623dd713 158 }
Hooglugt 37:2d248e64b745 159 }
Hooglugt 37:2d248e64b745 160 if(j==2) { //green
Hooglugt 37:2d248e64b745 161 red =1;
Hooglugt 37:2d248e64b745 162 green=0;
Hooglugt 37:2d248e64b745 163 blue=1;
Hooglugt 37:2d248e64b745 164 if(direction==0) { //if statement die controleert of direction 0 is (dus of triceps gereset is)
Hooglugt 37:2d248e64b745 165 goto directionchoice;
Hooglugt 37:2d248e64b745 166 }
Hooglugt 37:2d248e64b745 167 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 38:277ba1c0693c 168 if(emg_bifloat>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 37:2d248e64b745 169 force = 2;
Hooglugt 37:2d248e64b745 170 red=0;
Hooglugt 37:2d248e64b745 171 green = 1;
Hooglugt 37:2d248e64b745 172 blue = 0;
Hooglugt 37:2d248e64b745 173 pc.printf("normaal ");
Hooglugt 37:2d248e64b745 174 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie
Hooglugt 37:2d248e64b745 175 goto choicesmade;
Hooglugt 37:2d248e64b745 176 } else {
Hooglugt 37:2d248e64b745 177 wait(0.1);
Hooglugt 23:8d9a623dd713 178 }
Hooglugt 23:8d9a623dd713 179 }
Hooglugt 37:2d248e64b745 180 }
Hooglugt 37:2d248e64b745 181 if(j==3) { //blue
Hooglugt 37:2d248e64b745 182 red=1;
Hooglugt 37:2d248e64b745 183 green=1;
Hooglugt 37:2d248e64b745 184 blue=0;
Hooglugt 37:2d248e64b745 185 if(direction==0) { //if statement die controleert of direction 0 is (dus of triceps gereset is)
Hooglugt 37:2d248e64b745 186 goto directionchoice;
Hooglugt 37:2d248e64b745 187 }
Hooglugt 37:2d248e64b745 188 for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
Hooglugt 38:277ba1c0693c 189 if(emg_bifloat>0.8) { //0.8 klopt niet als grenswaarde. #nofilter
Hooglugt 37:2d248e64b745 190 force = 3;
Hooglugt 37:2d248e64b745 191 red=0;
Hooglugt 37:2d248e64b745 192 green = 0;
Hooglugt 37:2d248e64b745 193 blue = 1;
Hooglugt 37:2d248e64b745 194 pc.printf("sterk ");
Hooglugt 37:2d248e64b745 195 wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie
Hooglugt 37:2d248e64b745 196 goto choicesmade;
Hooglugt 37:2d248e64b745 197 } else {
Hooglugt 37:2d248e64b745 198 wait(0.1);
Hooglugt 23:8d9a623dd713 199 }
Hooglugt 23:8d9a623dd713 200 }
Hooglugt 23:8d9a623dd713 201 }
Hooglugt 23:8d9a623dd713 202 }
Hooglugt 37:2d248e64b745 203 }
Hooglugt 37:2d248e64b745 204 choicesmade:
Hooglugt 33:a78ec776dfd5 205
Hooglugt 37:2d248e64b745 206 /* Vanaf hier komt de aansturing van de motor (inclusief de controller)*/
Hooglugt 37:2d248e64b745 207
Hooglugt 33:a78ec776dfd5 208
Hooglugt 25:ec41b972b250 209 if(direction == 1 && force == 1) { // links zwak
Hooglugt 25:ec41b972b250 210 pc.printf("links zwak");
Hooglugt 25:ec41b972b250 211 }
Hooglugt 25:ec41b972b250 212 if(direction == 1 && force == 2) { // links normaal
Hooglugt 25:ec41b972b250 213 pc.printf("links normaal");
Hooglugt 25:ec41b972b250 214 }
Hooglugt 25:ec41b972b250 215 if(direction == 1 && force == 3) { // links sterk
Hooglugt 25:ec41b972b250 216 pc.printf("links sterk");
Hooglugt 25:ec41b972b250 217 }
Hooglugt 25:ec41b972b250 218 if(direction == 2 && force == 1) { // mid zwak
Hooglugt 25:ec41b972b250 219 pc.printf("mid zwak");
Hooglugt 25:ec41b972b250 220 }
Hooglugt 25:ec41b972b250 221 if(direction == 2 && force == 2) { // mid normaal
Hooglugt 25:ec41b972b250 222 pc.printf("mid normaal");
Hooglugt 25:ec41b972b250 223 }
Hooglugt 25:ec41b972b250 224 if(direction == 2 && force == 3) { // mid sterk
Hooglugt 25:ec41b972b250 225 pc.printf("mid sterk");
Hooglugt 25:ec41b972b250 226 }
Hooglugt 25:ec41b972b250 227 if(direction == 3 && force == 1) { // rechts zwak
Hooglugt 25:ec41b972b250 228 pc.printf("rechts zwak");
Hooglugt 25:ec41b972b250 229 }
Hooglugt 25:ec41b972b250 230 if(direction == 3 && force == 2) { // rechts normaal
Hooglugt 25:ec41b972b250 231 pc.printf("rechts normaal");
Hooglugt 25:ec41b972b250 232 }
Hooglugt 25:ec41b972b250 233 if(direction == 3 && force == 3) { // rechts sterk
Hooglugt 25:ec41b972b250 234 pc.printf("rechts sterk");
Hooglugt 25:ec41b972b250 235 }
Hooglugt 33:a78ec776dfd5 236 if(direction == 0 || force == 0) { // wanneer de triceps in de korte tijd is aangespannen nadat beide keuzes zijn gemaakt
Hooglugt 32:aaf01b1bf05d 237 pc.printf("error");
Hooglugt 33:a78ec776dfd5 238 // mogelijkheid om een goto te maken naar directionchoice (opzich wel handig)
Hooglugt 32:aaf01b1bf05d 239 }
Hooglugt 33:a78ec776dfd5 240
Hooglugt 32:aaf01b1bf05d 241 red = 0;
Hooglugt 32:aaf01b1bf05d 242 green = 0;
Hooglugt 37:2d248e64b745 243 blue = 0; // wit lampje
Hooglugt 25:ec41b972b250 244 }