Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope MODSERIAL- mbed-dsp mbed
Project_main.cpp@34:e166187e62ce, 2014-10-07 (annotated)
- Committer:
- Hooglugt
- Date:
- Tue Oct 07 12:34:32 2014 +0000
- Revision:
- 34:e166187e62ce
- Parent:
- 33:a78ec776dfd5
- Child:
- 35:c3182df00ec8
reset_looper "groen" gemaakt, script functioneerde niet meer goed - wss doordat triceps groter dan 0.8 was, wanneer dit niet bedoeling was (door bewegingartefacten)
Who changed what in which revision?
| User | Revision | Line number | New 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 | 33:a78ec776dfd5 | 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 | 34:e166187e62ce | 43 | |
| Hooglugt | 34:e166187e62ce | 44 | /* EMG-singaal van biceps en triceps worden hier gefilterd*/ |
| Hooglugt | 34:e166187e62ce | 45 | |
| Hooglugt | 29:7523e4a8e000 | 46 | scope.set(0,emg_bivalue); |
| Hooglugt | 29:7523e4a8e000 | 47 | scope.set(1,emg_bifloat.read()); |
| Hooglugt | 29:7523e4a8e000 | 48 | scope.set(2,emg_trivalue); |
| Hooglugt | 29:7523e4a8e000 | 49 | scope.set(3,emg_trifloat.read()); |
| Hooglugt | 23:8d9a623dd713 | 50 | scope.send(); |
| Hooglugt | 23:8d9a623dd713 | 51 | } |
| Hooglugt | 23:8d9a623dd713 | 52 | |
| Hooglugt | 34:e166187e62ce | 53 | /* |
| Hooglugt | 32:aaf01b1bf05d | 54 | void resetlooper() // VRAAG: wat gebeurt er wanneer en resetlooper en looper tegelijkertijd gecalled worden?! |
| Hooglugt | 31:d8eaf0ce8517 | 55 | { |
| Hooglugt | 30:5e5098b0cca6 | 56 | if(emg_trifloat.read()>0.8 && direction != 0) { //dit is alleen mogelijk wanneer directionchoice is gemaakt |
| Hooglugt | 30:5e5098b0cca6 | 57 | direction = 0; |
| Hooglugt | 30:5e5098b0cca6 | 58 | force = 0; // WEGHALEN, wanneer in uiteindelijke script na force keuzen niet meer gereset kan worden (voor nu wel handig) |
| Hooglugt | 31:d8eaf0ce8517 | 59 | pc.printf("reset "); |
| Hooglugt | 31:d8eaf0ce8517 | 60 | } |
| Hooglugt | 30:5e5098b0cca6 | 61 | } |
| Hooglugt | 34:e166187e62ce | 62 | CONSTANTE RESETS DOOR BEWEGINGSARTEFACTEN*/ |
| Hooglugt | 31:d8eaf0ce8517 | 63 | |
| Hooglugt | 23:8d9a623dd713 | 64 | int main() |
| Hooglugt | 23:8d9a623dd713 | 65 | { |
| Hooglugt | 25:ec41b972b250 | 66 | pc.baud(115200); //baudrate instellen |
| Hooglugt | 29:7523e4a8e000 | 67 | emg_bifloat.period_ms(2); //sets period for the PWM to the emgfloat PTD4 |
| Hooglugt | 29:7523e4a8e000 | 68 | emg_trifloat.period_ms(2); |
| Hooglugt | 25:ec41b972b250 | 69 | log_timer.attach(looper, 0.001); // The looper() function will be called every 0.001 seconds (with the ticker object) |
| Hooglugt | 34:e166187e62ce | 70 | // reset_timer.attach(resetlooper, 0.1); // |
| Hooglugt | 32:aaf01b1bf05d | 71 | |
| Hooglugt | 25:ec41b972b250 | 72 | goto directionchoice; // goes to first while(1) for the deciding the direction |
| Hooglugt | 26:9b43d9cb1fb2 | 73 | |
| Hooglugt | 23:8d9a623dd713 | 74 | while(1) { //Loop keuze DIRECTION |
| Hooglugt | 25:ec41b972b250 | 75 | directionchoice: |
| Hooglugt | 23:8d9a623dd713 | 76 | for(int i=1; i<4; i++) { |
| Hooglugt | 23:8d9a623dd713 | 77 | if(i==1) { //red |
| Hooglugt | 23:8d9a623dd713 | 78 | red=0; |
| Hooglugt | 23:8d9a623dd713 | 79 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 80 | blue=1; |
| Hooglugt | 26:9b43d9cb1fb2 | 81 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
| Hooglugt | 29:7523e4a8e000 | 82 | if(emg_bifloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 25:ec41b972b250 | 83 | direction = 1; |
| Hooglugt | 29:7523e4a8e000 | 84 | red=1; |
| Hooglugt | 29:7523e4a8e000 | 85 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 86 | blue = 0; |
| Hooglugt | 26:9b43d9cb1fb2 | 87 | pc.printf("links "); |
| Hooglugt | 25:ec41b972b250 | 88 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie |
| Hooglugt | 25:ec41b972b250 | 89 | goto forcechoice; // goes to second while(1) for the deciding the force |
| Hooglugt | 23:8d9a623dd713 | 90 | } else { |
| Hooglugt | 23:8d9a623dd713 | 91 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 92 | } |
| Hooglugt | 23:8d9a623dd713 | 93 | } |
| Hooglugt | 23:8d9a623dd713 | 94 | } |
| Hooglugt | 23:8d9a623dd713 | 95 | if(i==2) { //green |
| Hooglugt | 23:8d9a623dd713 | 96 | red =1; |
| Hooglugt | 23:8d9a623dd713 | 97 | green=0; |
| Hooglugt | 23:8d9a623dd713 | 98 | blue=1; |
| Hooglugt | 26:9b43d9cb1fb2 | 99 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
| Hooglugt | 29:7523e4a8e000 | 100 | if(emg_bifloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 25:ec41b972b250 | 101 | direction = 2; |
| Hooglugt | 29:7523e4a8e000 | 102 | red=0; |
| Hooglugt | 29:7523e4a8e000 | 103 | green = 1; |
| Hooglugt | 24:c6073b9efd5b | 104 | blue = 0; |
| Hooglugt | 26:9b43d9cb1fb2 | 105 | pc.printf("mid "); |
| Hooglugt | 25:ec41b972b250 | 106 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 107 | goto forcechoice; |
| Hooglugt | 23:8d9a623dd713 | 108 | } else { |
| Hooglugt | 23:8d9a623dd713 | 109 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 110 | } |
| Hooglugt | 23:8d9a623dd713 | 111 | } |
| Hooglugt | 23:8d9a623dd713 | 112 | } |
| Hooglugt | 23:8d9a623dd713 | 113 | if(i==3) { //blue |
| Hooglugt | 23:8d9a623dd713 | 114 | red=1; |
| Hooglugt | 23:8d9a623dd713 | 115 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 116 | blue=0; |
| Hooglugt | 26:9b43d9cb1fb2 | 117 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
| Hooglugt | 29:7523e4a8e000 | 118 | if(emg_bifloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 119 | direction = 3; |
| Hooglugt | 29:7523e4a8e000 | 120 | red=0; |
| Hooglugt | 29:7523e4a8e000 | 121 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 122 | blue = 1; |
| Hooglugt | 26:9b43d9cb1fb2 | 123 | pc.printf("rechts "); |
| Hooglugt | 25:ec41b972b250 | 124 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 125 | goto forcechoice; |
| Hooglugt | 23:8d9a623dd713 | 126 | } else { |
| Hooglugt | 23:8d9a623dd713 | 127 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 128 | } |
| Hooglugt | 23:8d9a623dd713 | 129 | } |
| Hooglugt | 23:8d9a623dd713 | 130 | } |
| Hooglugt | 25:ec41b972b250 | 131 | } |
| Hooglugt | 23:8d9a623dd713 | 132 | } |
| Hooglugt | 23:8d9a623dd713 | 133 | while(1) { //Loop keuze FORCE |
| Hooglugt | 25:ec41b972b250 | 134 | forcechoice: |
| Hooglugt | 23:8d9a623dd713 | 135 | for(int i=1; i<4; i++) { |
| Hooglugt | 23:8d9a623dd713 | 136 | if(i==1) { //red |
| Hooglugt | 23:8d9a623dd713 | 137 | red=0; |
| Hooglugt | 23:8d9a623dd713 | 138 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 139 | blue=1; |
| Hooglugt | 33:a78ec776dfd5 | 140 | if(direction==0){ //if statement die controleert of direction 0 is (dus of triceps gereset is) |
| Hooglugt | 33:a78ec776dfd5 | 141 | goto directionchoice; |
| Hooglugt | 33:a78ec776dfd5 | 142 | } |
| Hooglugt | 26:9b43d9cb1fb2 | 143 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
| Hooglugt | 29:7523e4a8e000 | 144 | if(emg_bifloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 145 | force = 1; |
| Hooglugt | 29:7523e4a8e000 | 146 | red=1; |
| Hooglugt | 29:7523e4a8e000 | 147 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 148 | blue = 0; |
| Hooglugt | 26:9b43d9cb1fb2 | 149 | pc.printf("zwak "); |
| Hooglugt | 25:ec41b972b250 | 150 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 151 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 152 | } else { |
| Hooglugt | 23:8d9a623dd713 | 153 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 154 | } |
| Hooglugt | 23:8d9a623dd713 | 155 | } |
| Hooglugt | 23:8d9a623dd713 | 156 | } |
| Hooglugt | 23:8d9a623dd713 | 157 | if(i==2) { //green |
| Hooglugt | 23:8d9a623dd713 | 158 | red =1; |
| Hooglugt | 23:8d9a623dd713 | 159 | green=0; |
| Hooglugt | 23:8d9a623dd713 | 160 | blue=1; |
| Hooglugt | 33:a78ec776dfd5 | 161 | if(direction==0){ //if statement die controleert of direction 0 is (dus of triceps gereset is) |
| Hooglugt | 33:a78ec776dfd5 | 162 | goto directionchoice; |
| Hooglugt | 33:a78ec776dfd5 | 163 | } |
| Hooglugt | 26:9b43d9cb1fb2 | 164 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
| Hooglugt | 29:7523e4a8e000 | 165 | if(emg_bifloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 166 | force = 2; |
| Hooglugt | 29:7523e4a8e000 | 167 | red=0; |
| Hooglugt | 29:7523e4a8e000 | 168 | green = 1; |
| Hooglugt | 24:c6073b9efd5b | 169 | blue = 0; |
| Hooglugt | 26:9b43d9cb1fb2 | 170 | pc.printf("normaal "); |
| Hooglugt | 25:ec41b972b250 | 171 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 172 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 173 | } else { |
| Hooglugt | 23:8d9a623dd713 | 174 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 175 | } |
| Hooglugt | 23:8d9a623dd713 | 176 | } |
| Hooglugt | 23:8d9a623dd713 | 177 | } |
| Hooglugt | 23:8d9a623dd713 | 178 | if(i==3) { //blue |
| Hooglugt | 23:8d9a623dd713 | 179 | red=1; |
| Hooglugt | 23:8d9a623dd713 | 180 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 181 | blue=0; |
| Hooglugt | 33:a78ec776dfd5 | 182 | if(direction==0){ //if statement die controleert of direction 0 is (dus of triceps gereset is) |
| Hooglugt | 33:a78ec776dfd5 | 183 | goto directionchoice; |
| Hooglugt | 33:a78ec776dfd5 | 184 | } |
| Hooglugt | 26:9b43d9cb1fb2 | 185 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
| Hooglugt | 29:7523e4a8e000 | 186 | if(emg_bifloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 187 | force = 3; |
| Hooglugt | 29:7523e4a8e000 | 188 | red=0; |
| Hooglugt | 29:7523e4a8e000 | 189 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 190 | blue = 1; |
| Hooglugt | 26:9b43d9cb1fb2 | 191 | pc.printf("sterk "); |
| Hooglugt | 25:ec41b972b250 | 192 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 193 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 194 | } else { |
| Hooglugt | 23:8d9a623dd713 | 195 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 196 | } |
| Hooglugt | 23:8d9a623dd713 | 197 | } |
| Hooglugt | 23:8d9a623dd713 | 198 | } |
| Hooglugt | 23:8d9a623dd713 | 199 | } |
| Hooglugt | 23:8d9a623dd713 | 200 | } |
| Hooglugt | 25:ec41b972b250 | 201 | choicesmade: |
| Hooglugt | 33:a78ec776dfd5 | 202 | |
| Hooglugt | 32:aaf01b1bf05d | 203 | /* Vanaf hier komt de aansturing van de motor (inclusief de controller)*/ |
| Hooglugt | 33:a78ec776dfd5 | 204 | |
| Hooglugt | 25:ec41b972b250 | 205 | if(direction == 1 && force == 1) { // links zwak |
| Hooglugt | 25:ec41b972b250 | 206 | pc.printf("links zwak"); |
| Hooglugt | 25:ec41b972b250 | 207 | } |
| Hooglugt | 25:ec41b972b250 | 208 | if(direction == 1 && force == 2) { // links normaal |
| Hooglugt | 25:ec41b972b250 | 209 | pc.printf("links normaal"); |
| Hooglugt | 25:ec41b972b250 | 210 | } |
| Hooglugt | 25:ec41b972b250 | 211 | if(direction == 1 && force == 3) { // links sterk |
| Hooglugt | 25:ec41b972b250 | 212 | pc.printf("links sterk"); |
| Hooglugt | 25:ec41b972b250 | 213 | } |
| Hooglugt | 25:ec41b972b250 | 214 | if(direction == 2 && force == 1) { // mid zwak |
| Hooglugt | 25:ec41b972b250 | 215 | pc.printf("mid zwak"); |
| Hooglugt | 25:ec41b972b250 | 216 | } |
| Hooglugt | 25:ec41b972b250 | 217 | if(direction == 2 && force == 2) { // mid normaal |
| Hooglugt | 25:ec41b972b250 | 218 | pc.printf("mid normaal"); |
| Hooglugt | 25:ec41b972b250 | 219 | } |
| Hooglugt | 25:ec41b972b250 | 220 | if(direction == 2 && force == 3) { // mid sterk |
| Hooglugt | 25:ec41b972b250 | 221 | pc.printf("mid sterk"); |
| Hooglugt | 25:ec41b972b250 | 222 | } |
| Hooglugt | 25:ec41b972b250 | 223 | if(direction == 3 && force == 1) { // rechts zwak |
| Hooglugt | 25:ec41b972b250 | 224 | pc.printf("rechts zwak"); |
| Hooglugt | 25:ec41b972b250 | 225 | } |
| Hooglugt | 25:ec41b972b250 | 226 | if(direction == 3 && force == 2) { // rechts normaal |
| Hooglugt | 25:ec41b972b250 | 227 | pc.printf("rechts normaal"); |
| Hooglugt | 25:ec41b972b250 | 228 | } |
| Hooglugt | 25:ec41b972b250 | 229 | if(direction == 3 && force == 3) { // rechts sterk |
| Hooglugt | 25:ec41b972b250 | 230 | pc.printf("rechts sterk"); |
| Hooglugt | 25:ec41b972b250 | 231 | } |
| Hooglugt | 33:a78ec776dfd5 | 232 | if(direction == 0 || force == 0) { // wanneer de triceps in de korte tijd is aangespannen nadat beide keuzes zijn gemaakt |
| Hooglugt | 32:aaf01b1bf05d | 233 | pc.printf("error"); |
| Hooglugt | 33:a78ec776dfd5 | 234 | // mogelijkheid om een goto te maken naar directionchoice (opzich wel handig) |
| Hooglugt | 32:aaf01b1bf05d | 235 | } |
| Hooglugt | 33:a78ec776dfd5 | 236 | |
| Hooglugt | 32:aaf01b1bf05d | 237 | red = 0; |
| Hooglugt | 32:aaf01b1bf05d | 238 | green = 0; |
| Hooglugt | 32:aaf01b1bf05d | 239 | blue = 0; // wit lampje |
| Hooglugt | 25:ec41b972b250 | 240 | } |