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@25:ec41b972b250, 2014-10-03 (annotated)
- Committer:
- Hooglugt
- Date:
- Fri Oct 03 12:27:01 2014 +0000
- Revision:
- 25:ec41b972b250
- Parent:
- 24:c6073b9efd5b
- Child:
- 26:9b43d9cb1fb2
alles in for/while loops met if en else statements - alle mogelijkheden met ints en geen enums of switches
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 | 24:c6073b9efd5b | 4 | #define TIMEB4NEXTCHOICE 2 |
| Hooglugt | 23:8d9a623dd713 | 5 | |
| Hooglugt | 23:8d9a623dd713 | 6 | //Define objects |
| Hooglugt | 23:8d9a623dd713 | 7 | AnalogIn emg0(PTB1); //Analog input |
| Hooglugt | 23:8d9a623dd713 | 8 | PwmOut emgfloat(PTD4);//Float voor EMG-waarde |
| Hooglugt | 23:8d9a623dd713 | 9 | PwmOut red(LED_RED); //PWM output |
| Hooglugt | 23:8d9a623dd713 | 10 | PwmOut green(LED_GREEN); |
| Hooglugt | 23:8d9a623dd713 | 11 | PwmOut blue(LED_BLUE); |
| Hooglugt | 23:8d9a623dd713 | 12 | int direction = 0; |
| Hooglugt | 23:8d9a623dd713 | 13 | int force = 0; |
| Hooglugt | 23:8d9a623dd713 | 14 | |
| Hooglugt | 23:8d9a623dd713 | 15 | Ticker log_timer; |
| Hooglugt | 23:8d9a623dd713 | 16 | MODSERIAL pc(USBTX,USBRX); |
| Hooglugt | 23:8d9a623dd713 | 17 | HIDScope scope(2); |
| Hooglugt | 23:8d9a623dd713 | 18 | |
| Hooglugt | 23:8d9a623dd713 | 19 | /** Looper function |
| Hooglugt | 23:8d9a623dd713 | 20 | * functions used for Ticker and Timeout should be of type void <name>(void) |
| Hooglugt | 23:8d9a623dd713 | 21 | * i.e. no input arguments, no output arguments. |
| Hooglugt | 23:8d9a623dd713 | 22 | * if you want to change a variable that you use in other places (for example in main) |
| Hooglugt | 23:8d9a623dd713 | 23 | * you will have to make that variable global in order to be able to reach it both from |
| Hooglugt | 23:8d9a623dd713 | 24 | * the function called at interrupt time, and in the main function. |
| Hooglugt | 23:8d9a623dd713 | 25 | * To make a variable global, define it under the includes. |
| Hooglugt | 23:8d9a623dd713 | 26 | * variables that are changed in the interrupt routine (written to) should be made |
| Hooglugt | 23:8d9a623dd713 | 27 | * 'volatile' to let the compiler know that those values may change outside the current context. |
| Hooglugt | 23:8d9a623dd713 | 28 | * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value" |
| Hooglugt | 23:8d9a623dd713 | 29 | * in the example below, the variable is not re-used in the main function, and is thus declared |
| Hooglugt | 23:8d9a623dd713 | 30 | * local in the looper function only. |
| Hooglugt | 23:8d9a623dd713 | 31 | **/ |
| Hooglugt | 23:8d9a623dd713 | 32 | void looper() |
| Hooglugt | 23:8d9a623dd713 | 33 | { |
| Hooglugt | 23:8d9a623dd713 | 34 | /*variable to store value in*/ |
| Hooglugt | 23:8d9a623dd713 | 35 | uint16_t emg_value; |
| Hooglugt | 23:8d9a623dd713 | 36 | /*put raw emg value both in emgfloat and in emg_value*/ |
| Hooglugt | 23:8d9a623dd713 | 37 | emgfloat.write(emg0.read()); // read float value (0..1 = 0..3.3V) |
| Hooglugt | 23:8d9a623dd713 | 38 | emg_value = emg0.read_u16(); // read direct ADC result (0..4096 = 0..3.3V) |
| Hooglugt | 23:8d9a623dd713 | 39 | /*send value to PC. Line below is used to prevent buffer overrun */ |
| Hooglugt | 23:8d9a623dd713 | 40 | if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) |
| Hooglugt | 24:c6073b9efd5b | 41 | //pc.printf("%u\n",emg_value); |
| Hooglugt | 25:ec41b972b250 | 42 | scope.set(0,emg_value); |
| Hooglugt | 23:8d9a623dd713 | 43 | scope.set(1,emgfloat.read()); |
| Hooglugt | 23:8d9a623dd713 | 44 | scope.send(); |
| Hooglugt | 23:8d9a623dd713 | 45 | /**When not using the LED, the above could also have been done this way: |
| Hooglugt | 23:8d9a623dd713 | 46 | * pc.printf("%u\n", emg0.read_u16()); |
| Hooglugt | 23:8d9a623dd713 | 47 | */ |
| Hooglugt | 23:8d9a623dd713 | 48 | } |
| Hooglugt | 23:8d9a623dd713 | 49 | |
| Hooglugt | 23:8d9a623dd713 | 50 | |
| Hooglugt | 23:8d9a623dd713 | 51 | |
| Hooglugt | 23:8d9a623dd713 | 52 | int main() |
| Hooglugt | 23:8d9a623dd713 | 53 | { |
| Hooglugt | 25:ec41b972b250 | 54 | pc.baud(115200); //baudrate instellen |
| Hooglugt | 25:ec41b972b250 | 55 | emgfloat.period_ms(2); //sets period for the PWM to the emgfloat PTD4 |
| Hooglugt | 25:ec41b972b250 | 56 | log_timer.attach(looper, 0.001); // The looper() function will be called every 0.001 seconds (with the ticker object) |
| Hooglugt | 24:c6073b9efd5b | 57 | |
| Hooglugt | 25:ec41b972b250 | 58 | goto directionchoice; // goes to first while(1) for the deciding the direction |
| Hooglugt | 24:c6073b9efd5b | 59 | |
| Hooglugt | 23:8d9a623dd713 | 60 | while(1) { //Loop keuze DIRECTION |
| Hooglugt | 25:ec41b972b250 | 61 | directionchoice: |
| Hooglugt | 23:8d9a623dd713 | 62 | for(int i=1; i<4; i++) { |
| Hooglugt | 23:8d9a623dd713 | 63 | if(i==1) { //red |
| Hooglugt | 23:8d9a623dd713 | 64 | red=0; |
| Hooglugt | 23:8d9a623dd713 | 65 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 66 | blue=1; |
| Hooglugt | 24:c6073b9efd5b | 67 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 25:ec41b972b250 | 68 | if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 25:ec41b972b250 | 69 | direction = 1; |
| Hooglugt | 24:c6073b9efd5b | 70 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 71 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 72 | red=1; |
| Hooglugt | 25:ec41b972b250 | 73 | pc.printf("A"); |
| Hooglugt | 25:ec41b972b250 | 74 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie |
| Hooglugt | 25:ec41b972b250 | 75 | goto forcechoice; // goes to second while(1) for the deciding the force |
| Hooglugt | 23:8d9a623dd713 | 76 | } else { |
| Hooglugt | 23:8d9a623dd713 | 77 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 78 | } |
| Hooglugt | 23:8d9a623dd713 | 79 | } |
| Hooglugt | 23:8d9a623dd713 | 80 | } |
| Hooglugt | 23:8d9a623dd713 | 81 | if(i==2) { //green |
| Hooglugt | 23:8d9a623dd713 | 82 | red =1; |
| Hooglugt | 23:8d9a623dd713 | 83 | green=0; |
| Hooglugt | 23:8d9a623dd713 | 84 | blue=1; |
| Hooglugt | 24:c6073b9efd5b | 85 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 86 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 25:ec41b972b250 | 87 | direction = 2; |
| Hooglugt | 24:c6073b9efd5b | 88 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 89 | green = 1; |
| Hooglugt | 24:c6073b9efd5b | 90 | red=0; |
| Hooglugt | 25:ec41b972b250 | 91 | pc.printf("B"); |
| Hooglugt | 25:ec41b972b250 | 92 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 93 | goto forcechoice; |
| Hooglugt | 23:8d9a623dd713 | 94 | } else { |
| Hooglugt | 23:8d9a623dd713 | 95 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 96 | } |
| Hooglugt | 23:8d9a623dd713 | 97 | } |
| Hooglugt | 23:8d9a623dd713 | 98 | } |
| Hooglugt | 23:8d9a623dd713 | 99 | if(i==3) { //blue |
| Hooglugt | 23:8d9a623dd713 | 100 | red=1; |
| Hooglugt | 23:8d9a623dd713 | 101 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 102 | blue=0; |
| Hooglugt | 24:c6073b9efd5b | 103 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 104 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 105 | direction = 3; |
| Hooglugt | 24:c6073b9efd5b | 106 | blue = 1; |
| Hooglugt | 24:c6073b9efd5b | 107 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 108 | red=0; |
| Hooglugt | 23:8d9a623dd713 | 109 | pc.printf("C"); |
| Hooglugt | 25:ec41b972b250 | 110 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 111 | goto forcechoice; |
| Hooglugt | 23:8d9a623dd713 | 112 | } else { |
| Hooglugt | 23:8d9a623dd713 | 113 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 114 | } |
| Hooglugt | 23:8d9a623dd713 | 115 | } |
| Hooglugt | 23:8d9a623dd713 | 116 | } |
| Hooglugt | 25:ec41b972b250 | 117 | } |
| Hooglugt | 23:8d9a623dd713 | 118 | } |
| Hooglugt | 23:8d9a623dd713 | 119 | while(1) { //Loop keuze FORCE |
| Hooglugt | 25:ec41b972b250 | 120 | forcechoice: |
| Hooglugt | 23:8d9a623dd713 | 121 | for(int i=1; i<4; i++) { |
| Hooglugt | 23:8d9a623dd713 | 122 | if(i==1) { //red |
| Hooglugt | 23:8d9a623dd713 | 123 | red=0; |
| Hooglugt | 23:8d9a623dd713 | 124 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 125 | blue=1; |
| Hooglugt | 24:c6073b9efd5b | 126 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 127 | if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 128 | force = 1; |
| Hooglugt | 24:c6073b9efd5b | 129 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 130 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 131 | red=1; |
| Hooglugt | 24:c6073b9efd5b | 132 | pc.printf("D"); |
| Hooglugt | 25:ec41b972b250 | 133 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 134 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 135 | } else { |
| Hooglugt | 23:8d9a623dd713 | 136 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 137 | } |
| Hooglugt | 23:8d9a623dd713 | 138 | } |
| Hooglugt | 23:8d9a623dd713 | 139 | } |
| Hooglugt | 23:8d9a623dd713 | 140 | if(i==2) { //green |
| Hooglugt | 23:8d9a623dd713 | 141 | red =1; |
| Hooglugt | 23:8d9a623dd713 | 142 | green=0; |
| Hooglugt | 23:8d9a623dd713 | 143 | blue=1; |
| Hooglugt | 24:c6073b9efd5b | 144 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 145 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 146 | force = 2; |
| Hooglugt | 24:c6073b9efd5b | 147 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 148 | green = 1; |
| Hooglugt | 24:c6073b9efd5b | 149 | red=0; |
| Hooglugt | 24:c6073b9efd5b | 150 | pc.printf("E"); |
| Hooglugt | 25:ec41b972b250 | 151 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 152 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 153 | } else { |
| Hooglugt | 23:8d9a623dd713 | 154 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 155 | } |
| Hooglugt | 23:8d9a623dd713 | 156 | } |
| Hooglugt | 23:8d9a623dd713 | 157 | } |
| Hooglugt | 23:8d9a623dd713 | 158 | if(i==3) { //blue |
| Hooglugt | 23:8d9a623dd713 | 159 | red=1; |
| Hooglugt | 23:8d9a623dd713 | 160 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 161 | blue=0; |
| Hooglugt | 24:c6073b9efd5b | 162 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 163 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 164 | force = 3; |
| Hooglugt | 24:c6073b9efd5b | 165 | blue = 1; |
| Hooglugt | 24:c6073b9efd5b | 166 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 167 | red=0; |
| Hooglugt | 24:c6073b9efd5b | 168 | pc.printf("F"); |
| Hooglugt | 25:ec41b972b250 | 169 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 170 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 171 | } else { |
| Hooglugt | 23:8d9a623dd713 | 172 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 173 | } |
| Hooglugt | 23:8d9a623dd713 | 174 | } |
| Hooglugt | 23:8d9a623dd713 | 175 | } |
| Hooglugt | 23:8d9a623dd713 | 176 | } |
| Hooglugt | 23:8d9a623dd713 | 177 | } |
| Hooglugt | 25:ec41b972b250 | 178 | choicesmade: |
| Hooglugt | 25:ec41b972b250 | 179 | |
| Hooglugt | 25:ec41b972b250 | 180 | if(direction == 0 || force == 0) { |
| Hooglugt | 25:ec41b972b250 | 181 | pc.printf("error"); |
| Hooglugt | 25:ec41b972b250 | 182 | } |
| Hooglugt | 25:ec41b972b250 | 183 | if(direction == 1 && force == 1) { // links zwak |
| Hooglugt | 25:ec41b972b250 | 184 | pc.printf("links zwak"); |
| Hooglugt | 25:ec41b972b250 | 185 | } |
| Hooglugt | 25:ec41b972b250 | 186 | if(direction == 1 && force == 2) { // links normaal |
| Hooglugt | 25:ec41b972b250 | 187 | pc.printf("links normaal"); |
| Hooglugt | 25:ec41b972b250 | 188 | } |
| Hooglugt | 25:ec41b972b250 | 189 | if(direction == 1 && force == 3) { // links sterk |
| Hooglugt | 25:ec41b972b250 | 190 | pc.printf("links sterk"); |
| Hooglugt | 25:ec41b972b250 | 191 | } |
| Hooglugt | 25:ec41b972b250 | 192 | if(direction == 2 && force == 1) { // mid zwak |
| Hooglugt | 25:ec41b972b250 | 193 | pc.printf("mid zwak"); |
| Hooglugt | 25:ec41b972b250 | 194 | } |
| Hooglugt | 25:ec41b972b250 | 195 | if(direction == 2 && force == 2) { // mid normaal |
| Hooglugt | 25:ec41b972b250 | 196 | pc.printf("mid normaal"); |
| Hooglugt | 25:ec41b972b250 | 197 | } |
| Hooglugt | 25:ec41b972b250 | 198 | if(direction == 2 && force == 3) { // mid sterk |
| Hooglugt | 25:ec41b972b250 | 199 | pc.printf("mid sterk"); |
| Hooglugt | 25:ec41b972b250 | 200 | } |
| Hooglugt | 25:ec41b972b250 | 201 | if(direction == 3 && force == 1) { // rechts zwak |
| Hooglugt | 25:ec41b972b250 | 202 | pc.printf("rechts zwak"); |
| Hooglugt | 25:ec41b972b250 | 203 | } |
| Hooglugt | 25:ec41b972b250 | 204 | if(direction == 3 && force == 2) { // rechts normaal |
| Hooglugt | 25:ec41b972b250 | 205 | pc.printf("rechts normaal"); |
| Hooglugt | 25:ec41b972b250 | 206 | } |
| Hooglugt | 25:ec41b972b250 | 207 | if(direction == 3 && force == 3) { // rechts sterk |
| Hooglugt | 25:ec41b972b250 | 208 | pc.printf("rechts sterk"); |
| Hooglugt | 25:ec41b972b250 | 209 | } |
| Hooglugt | 25:ec41b972b250 | 210 | |
| Hooglugt | 23:8d9a623dd713 | 211 | red = 0; |
| Hooglugt | 23:8d9a623dd713 | 212 | green = 0; |
| Hooglugt | 23:8d9a623dd713 | 213 | blue = 0; |
| Hooglugt | 25:ec41b972b250 | 214 | |
| Hooglugt | 25:ec41b972b250 | 215 | } |