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@27:54167d54b0c5, 2014-10-03 (annotated)
- Committer:
- Hooglugt
- Date:
- Fri Oct 03 13:05:50 2014 +0000
- Revision:
- 27:54167d54b0c5
- Parent:
- 26:9b43d9cb1fb2
- Child:
- 28:f4b09acf78c9
meten van emg biceps en triceps toegevoegd, reset moet nog gedaan worden (in de 2e while(1) {} een if emg_trifloat>0.8 toevoegen en een goto naar de first loop
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 | 27:54167d54b0c5 | 4 | #define TIMEB4NEXTCHOICE 1 |
| Hooglugt | 27:54167d54b0c5 | 5 | #define TIMEBETWEENBLINK 10 |
| Hooglugt | 23:8d9a623dd713 | 6 | |
| Hooglugt | 23:8d9a623dd713 | 7 | //Define objects |
| Hooglugt | 27:54167d54b0c5 | 8 | AnalogIn emg0(PTB1); //Analog input biceps |
| Hooglugt | 27:54167d54b0c5 | 9 | AnalogIn emg1(PTB2); //Analog input triceps NO IDEA WELKE PTB |
| Hooglugt | 27:54167d54b0c5 | 10 | |
| Hooglugt | 27:54167d54b0c5 | 11 | PwmOut emg_bifloat(PTD4); //Float voor EMG-waarde biceps |
| Hooglugt | 27:54167d54b0c5 | 12 | PwmOut emg_trifloat(PTA4); //Float voor EMG-waarde triceps |
| Hooglugt | 27:54167d54b0c5 | 13 | |
| Hooglugt | 27:54167d54b0c5 | 14 | PwmOut red(LED_RED); //PWM output |
| Hooglugt | 23:8d9a623dd713 | 15 | PwmOut green(LED_GREEN); |
| Hooglugt | 23:8d9a623dd713 | 16 | PwmOut blue(LED_BLUE); |
| Hooglugt | 27:54167d54b0c5 | 17 | |
| Hooglugt | 23:8d9a623dd713 | 18 | int direction = 0; |
| Hooglugt | 23:8d9a623dd713 | 19 | int force = 0; |
| Hooglugt | 23:8d9a623dd713 | 20 | |
| Hooglugt | 23:8d9a623dd713 | 21 | Ticker log_timer; |
| Hooglugt | 23:8d9a623dd713 | 22 | MODSERIAL pc(USBTX,USBRX); |
| Hooglugt | 23:8d9a623dd713 | 23 | HIDScope scope(2); |
| Hooglugt | 23:8d9a623dd713 | 24 | |
| Hooglugt | 23:8d9a623dd713 | 25 | /** Looper function |
| Hooglugt | 23:8d9a623dd713 | 26 | * functions used for Ticker and Timeout should be of type void <name>(void) |
| Hooglugt | 23:8d9a623dd713 | 27 | * i.e. no input arguments, no output arguments. |
| Hooglugt | 23:8d9a623dd713 | 28 | * if you want to change a variable that you use in other places (for example in main) |
| Hooglugt | 23:8d9a623dd713 | 29 | * you will have to make that variable global in order to be able to reach it both from |
| Hooglugt | 23:8d9a623dd713 | 30 | * the function called at interrupt time, and in the main function. |
| Hooglugt | 23:8d9a623dd713 | 31 | * To make a variable global, define it under the includes. |
| Hooglugt | 23:8d9a623dd713 | 32 | * variables that are changed in the interrupt routine (written to) should be made |
| Hooglugt | 23:8d9a623dd713 | 33 | * 'volatile' to let the compiler know that those values may change outside the current context. |
| Hooglugt | 23:8d9a623dd713 | 34 | * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value" |
| Hooglugt | 23:8d9a623dd713 | 35 | * in the example below, the variable is not re-used in the main function, and is thus declared |
| Hooglugt | 23:8d9a623dd713 | 36 | * local in the looper function only. |
| Hooglugt | 23:8d9a623dd713 | 37 | **/ |
| Hooglugt | 23:8d9a623dd713 | 38 | void looper() |
| Hooglugt | 23:8d9a623dd713 | 39 | { |
| Hooglugt | 23:8d9a623dd713 | 40 | /*variable to store value in*/ |
| Hooglugt | 27:54167d54b0c5 | 41 | uint16_t emg_bivalue; |
| Hooglugt | 23:8d9a623dd713 | 42 | /*put raw emg value both in emgfloat and in emg_value*/ |
| Hooglugt | 27:54167d54b0c5 | 43 | emg_bifloat.write(emg0.read()); // read float value (0..1 = 0..3.3V) biceps |
| Hooglugt | 27:54167d54b0c5 | 44 | emg_bivalue = emg0.read_u16(); // read direct ADC result (0..4096 = 0..3.3V) biceps |
| Hooglugt | 27:54167d54b0c5 | 45 | |
| Hooglugt | 27:54167d54b0c5 | 46 | uint16_t emg_trivalue; |
| Hooglugt | 27:54167d54b0c5 | 47 | /*put raw emg value both in emgfloat and in emg_value*/ |
| Hooglugt | 27:54167d54b0c5 | 48 | emg_trifloat.write(emg1.read()); // read float value (0..1 = 0..3.3V) biceps |
| Hooglugt | 27:54167d54b0c5 | 49 | emg_trivalue = emg1.read_u16(); // read direct ADC result (0..4096 = 0..3.3V) biceps |
| Hooglugt | 27:54167d54b0c5 | 50 | |
| Hooglugt | 23:8d9a623dd713 | 51 | /*send value to PC. Line below is used to prevent buffer overrun */ |
| Hooglugt | 23:8d9a623dd713 | 52 | if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) |
| Hooglugt | 27:54167d54b0c5 | 53 | //pc.printf("%u\n",emg_value); |
| Hooglugt | 27:54167d54b0c5 | 54 | |
| Hooglugt | 27:54167d54b0c5 | 55 | scope.set(0,emg_bivalue); |
| Hooglugt | 27:54167d54b0c5 | 56 | scope.set(1,emg_bifloat.read()); |
| Hooglugt | 27:54167d54b0c5 | 57 | scope.set(2,emg_trivalue); |
| Hooglugt | 27:54167d54b0c5 | 58 | scope.set(3,emg_trifloat.read()); |
| Hooglugt | 23:8d9a623dd713 | 59 | scope.send(); |
| Hooglugt | 27:54167d54b0c5 | 60 | |
| Hooglugt | 23:8d9a623dd713 | 61 | } |
| Hooglugt | 23:8d9a623dd713 | 62 | |
| Hooglugt | 23:8d9a623dd713 | 63 | |
| Hooglugt | 23:8d9a623dd713 | 64 | |
| Hooglugt | 23:8d9a623dd713 | 65 | int main() |
| Hooglugt | 23:8d9a623dd713 | 66 | { |
| Hooglugt | 25:ec41b972b250 | 67 | pc.baud(115200); //baudrate instellen |
| Hooglugt | 27:54167d54b0c5 | 68 | emg_bifloat.period_ms(2); //sets period for the PWM to the emgfloat PTD4 |
| Hooglugt | 27:54167d54b0c5 | 69 | emg_trifloat.period_ms(2); |
| Hooglugt | 25:ec41b972b250 | 70 | log_timer.attach(looper, 0.001); // The looper() function will be called every 0.001 seconds (with the ticker object) |
| Hooglugt | 26:9b43d9cb1fb2 | 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 | 25:ec41b972b250 | 82 | if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 25:ec41b972b250 | 83 | direction = 1; |
| Hooglugt | 24:c6073b9efd5b | 84 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 85 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 86 | red=1; |
| 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 | 23:8d9a623dd713 | 100 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 25:ec41b972b250 | 101 | direction = 2; |
| Hooglugt | 24:c6073b9efd5b | 102 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 103 | green = 1; |
| Hooglugt | 24:c6073b9efd5b | 104 | red=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 | 23:8d9a623dd713 | 118 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 119 | direction = 3; |
| Hooglugt | 24:c6073b9efd5b | 120 | blue = 1; |
| Hooglugt | 24:c6073b9efd5b | 121 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 122 | red=0; |
| 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 | 26:9b43d9cb1fb2 | 140 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 141 | if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 142 | force = 1; |
| Hooglugt | 24:c6073b9efd5b | 143 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 144 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 145 | red=1; |
| Hooglugt | 26:9b43d9cb1fb2 | 146 | pc.printf("zwak "); |
| Hooglugt | 25:ec41b972b250 | 147 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 148 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 149 | } else { |
| Hooglugt | 23:8d9a623dd713 | 150 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 151 | } |
| Hooglugt | 23:8d9a623dd713 | 152 | } |
| Hooglugt | 23:8d9a623dd713 | 153 | } |
| Hooglugt | 23:8d9a623dd713 | 154 | if(i==2) { //green |
| Hooglugt | 23:8d9a623dd713 | 155 | red =1; |
| Hooglugt | 23:8d9a623dd713 | 156 | green=0; |
| Hooglugt | 23:8d9a623dd713 | 157 | blue=1; |
| Hooglugt | 26:9b43d9cb1fb2 | 158 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 159 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 160 | force = 2; |
| Hooglugt | 24:c6073b9efd5b | 161 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 162 | green = 1; |
| Hooglugt | 24:c6073b9efd5b | 163 | red=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 | 26:9b43d9cb1fb2 | 176 | for (int lag=0; lag<TIMEBETWEENBLINK; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 177 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 178 | force = 3; |
| Hooglugt | 24:c6073b9efd5b | 179 | blue = 1; |
| Hooglugt | 24:c6073b9efd5b | 180 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 181 | red=0; |
| Hooglugt | 26:9b43d9cb1fb2 | 182 | pc.printf("sterk "); |
| Hooglugt | 25:ec41b972b250 | 183 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 184 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 185 | } else { |
| Hooglugt | 23:8d9a623dd713 | 186 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 187 | } |
| Hooglugt | 23:8d9a623dd713 | 188 | } |
| Hooglugt | 23:8d9a623dd713 | 189 | } |
| Hooglugt | 23:8d9a623dd713 | 190 | } |
| Hooglugt | 23:8d9a623dd713 | 191 | } |
| Hooglugt | 25:ec41b972b250 | 192 | choicesmade: |
| Hooglugt | 25:ec41b972b250 | 193 | |
| Hooglugt | 26:9b43d9cb1fb2 | 194 | red = 0; |
| Hooglugt | 26:9b43d9cb1fb2 | 195 | green = 0; |
| Hooglugt | 26:9b43d9cb1fb2 | 196 | blue = 0; // wit lampje |
| Hooglugt | 26:9b43d9cb1fb2 | 197 | |
| Hooglugt | 25:ec41b972b250 | 198 | if(direction == 0 || force == 0) { |
| Hooglugt | 25:ec41b972b250 | 199 | pc.printf("error"); |
| Hooglugt | 25:ec41b972b250 | 200 | } |
| Hooglugt | 25:ec41b972b250 | 201 | if(direction == 1 && force == 1) { // links zwak |
| Hooglugt | 25:ec41b972b250 | 202 | pc.printf("links zwak"); |
| Hooglugt | 25:ec41b972b250 | 203 | } |
| Hooglugt | 25:ec41b972b250 | 204 | if(direction == 1 && force == 2) { // links normaal |
| Hooglugt | 25:ec41b972b250 | 205 | pc.printf("links normaal"); |
| Hooglugt | 25:ec41b972b250 | 206 | } |
| Hooglugt | 25:ec41b972b250 | 207 | if(direction == 1 && force == 3) { // links sterk |
| Hooglugt | 25:ec41b972b250 | 208 | pc.printf("links sterk"); |
| Hooglugt | 25:ec41b972b250 | 209 | } |
| Hooglugt | 25:ec41b972b250 | 210 | if(direction == 2 && force == 1) { // mid zwak |
| Hooglugt | 25:ec41b972b250 | 211 | pc.printf("mid zwak"); |
| Hooglugt | 25:ec41b972b250 | 212 | } |
| Hooglugt | 25:ec41b972b250 | 213 | if(direction == 2 && force == 2) { // mid normaal |
| Hooglugt | 25:ec41b972b250 | 214 | pc.printf("mid normaal"); |
| Hooglugt | 25:ec41b972b250 | 215 | } |
| Hooglugt | 25:ec41b972b250 | 216 | if(direction == 2 && force == 3) { // mid sterk |
| Hooglugt | 25:ec41b972b250 | 217 | pc.printf("mid sterk"); |
| Hooglugt | 25:ec41b972b250 | 218 | } |
| Hooglugt | 25:ec41b972b250 | 219 | if(direction == 3 && force == 1) { // rechts zwak |
| Hooglugt | 25:ec41b972b250 | 220 | pc.printf("rechts zwak"); |
| Hooglugt | 25:ec41b972b250 | 221 | } |
| Hooglugt | 25:ec41b972b250 | 222 | if(direction == 3 && force == 2) { // rechts normaal |
| Hooglugt | 25:ec41b972b250 | 223 | pc.printf("rechts normaal"); |
| Hooglugt | 25:ec41b972b250 | 224 | } |
| Hooglugt | 25:ec41b972b250 | 225 | if(direction == 3 && force == 3) { // rechts sterk |
| Hooglugt | 25:ec41b972b250 | 226 | pc.printf("rechts sterk"); |
| Hooglugt | 25:ec41b972b250 | 227 | } |
| Hooglugt | 25:ec41b972b250 | 228 | } |