Aukie Hooglugt / Mbed 2 deprecated EMG_Project

Dependencies:   HIDScope MODSERIAL- mbed-dsp mbed

Committer:
Hooglugt
Date:
Tue Oct 07 10:13:12 2014 +0000
Revision:
32:aaf01b1bf05d
Parent:
31:d8eaf0ce8517
Child:
33:a78ec776dfd5
duidelijke beschrijvingen toegevoegd van wat er nog gedaan moet worden, vragen, opmerkingen, etc.

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