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@24:c6073b9efd5b, 2014-10-03 (annotated)
- Committer:
- Hooglugt
- Date:
- Fri Oct 03 10:43:22 2014 +0000
- Revision:
- 24:c6073b9efd5b
- Parent:
- 23:8d9a623dd713
- Child:
- 25:ec41b972b250
Starting to implement enum and switches in the script (rewriting script)
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 | 23:8d9a623dd713 | 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 | 23:8d9a623dd713 | 54 | /*setup baudrate. Choose the same in your program on PC side*/ |
| Hooglugt | 23:8d9a623dd713 | 55 | pc.baud(115200); |
| Hooglugt | 23:8d9a623dd713 | 56 | /*set the period for the PWM to the emgfloat PTD4*/ |
| Hooglugt | 23:8d9a623dd713 | 57 | emgfloat.period_ms(2); |
| Hooglugt | 23:8d9a623dd713 | 58 | /**Here you attach the 'void looper(void)' function to the Ticker object |
| Hooglugt | 23:8d9a623dd713 | 59 | * The looper() function will be called every 0.001 seconds. |
| Hooglugt | 23:8d9a623dd713 | 60 | * Please mind that the parentheses after looper are omitted when using attach. |
| Hooglugt | 23:8d9a623dd713 | 61 | */ |
| Hooglugt | 23:8d9a623dd713 | 62 | log_timer.attach(looper, 0.001); |
| Hooglugt | 24:c6073b9efd5b | 63 | |
| Hooglugt | 24:c6073b9efd5b | 64 | enum Direction {LEFT, MID, RIGHT,UNSET}; |
| Hooglugt | 24:c6073b9efd5b | 65 | Direction direc = UNSET; |
| Hooglugt | 24:c6073b9efd5b | 66 | enum Force {WEAK, NORMAL, STRONG,UNSET}; |
| Hooglugt | 24:c6073b9efd5b | 67 | Force force = UNSET; |
| Hooglugt | 24:c6073b9efd5b | 68 | |
| Hooglugt | 23:8d9a623dd713 | 69 | goto directionchoice; |
| Hooglugt | 23:8d9a623dd713 | 70 | while(1) { //Loop keuze DIRECTION |
| Hooglugt | 23:8d9a623dd713 | 71 | directionchoice: |
| Hooglugt | 23:8d9a623dd713 | 72 | for(int i=1; i<4; i++) { |
| Hooglugt | 23:8d9a623dd713 | 73 | if(i==1) { //red |
| Hooglugt | 23:8d9a623dd713 | 74 | red=0; |
| Hooglugt | 23:8d9a623dd713 | 75 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 76 | blue=1; |
| Hooglugt | 24:c6073b9efd5b | 77 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 78 | if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 24:c6073b9efd5b | 79 | direc = LEFT; |
| Hooglugt | 24:c6073b9efd5b | 80 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 81 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 82 | red=1; |
| Hooglugt | 24:c6073b9efd5b | 83 | pc.printf("LEFT"); |
| Hooglugt | 24:c6073b9efd5b | 84 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 85 | goto forcechoice; |
| Hooglugt | 23:8d9a623dd713 | 86 | } else { |
| Hooglugt | 23:8d9a623dd713 | 87 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 88 | } |
| Hooglugt | 23:8d9a623dd713 | 89 | } |
| Hooglugt | 23:8d9a623dd713 | 90 | } |
| Hooglugt | 23:8d9a623dd713 | 91 | if(i==2) { //green |
| Hooglugt | 23:8d9a623dd713 | 92 | red =1; |
| Hooglugt | 23:8d9a623dd713 | 93 | green=0; |
| Hooglugt | 23:8d9a623dd713 | 94 | blue=1; |
| Hooglugt | 24:c6073b9efd5b | 95 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 96 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 24:c6073b9efd5b | 97 | direction = MID; |
| Hooglugt | 24:c6073b9efd5b | 98 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 99 | green = 1; |
| Hooglugt | 24:c6073b9efd5b | 100 | red=0; |
| Hooglugt | 24:c6073b9efd5b | 101 | pc.printf("MID"); |
| Hooglugt | 24:c6073b9efd5b | 102 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 103 | goto forcechoice; |
| Hooglugt | 23:8d9a623dd713 | 104 | } else { |
| Hooglugt | 23:8d9a623dd713 | 105 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 106 | } |
| Hooglugt | 23:8d9a623dd713 | 107 | } |
| Hooglugt | 23:8d9a623dd713 | 108 | } |
| Hooglugt | 23:8d9a623dd713 | 109 | if(i==3) { //blue |
| Hooglugt | 23:8d9a623dd713 | 110 | red=1; |
| Hooglugt | 23:8d9a623dd713 | 111 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 112 | blue=0; |
| Hooglugt | 24:c6073b9efd5b | 113 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 114 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 115 | direction = 3; |
| Hooglugt | 24:c6073b9efd5b | 116 | blue = 1; |
| Hooglugt | 24:c6073b9efd5b | 117 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 118 | red=0; |
| Hooglugt | 23:8d9a623dd713 | 119 | pc.printf("C"); |
| Hooglugt | 24:c6073b9efd5b | 120 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 121 | goto forcechoice; |
| Hooglugt | 23:8d9a623dd713 | 122 | } else { |
| Hooglugt | 23:8d9a623dd713 | 123 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 124 | } |
| Hooglugt | 23:8d9a623dd713 | 125 | } |
| Hooglugt | 23:8d9a623dd713 | 126 | } |
| Hooglugt | 23:8d9a623dd713 | 127 | } |
| Hooglugt | 23:8d9a623dd713 | 128 | } |
| Hooglugt | 23:8d9a623dd713 | 129 | while(1) { //Loop keuze FORCE |
| Hooglugt | 23:8d9a623dd713 | 130 | forcechoice: |
| Hooglugt | 23:8d9a623dd713 | 131 | for(int i=1; i<4; i++) { |
| Hooglugt | 23:8d9a623dd713 | 132 | if(i==1) { //red |
| Hooglugt | 23:8d9a623dd713 | 133 | red=0; |
| Hooglugt | 23:8d9a623dd713 | 134 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 135 | blue=1; |
| Hooglugt | 24:c6073b9efd5b | 136 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 137 | if(emgfloat.read()>0.8) { // 0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 138 | force = 1; |
| Hooglugt | 24:c6073b9efd5b | 139 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 140 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 141 | red=1; |
| Hooglugt | 24:c6073b9efd5b | 142 | pc.printf("D"); |
| Hooglugt | 24:c6073b9efd5b | 143 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 144 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 145 | } else { |
| Hooglugt | 23:8d9a623dd713 | 146 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 147 | } |
| Hooglugt | 23:8d9a623dd713 | 148 | } |
| Hooglugt | 23:8d9a623dd713 | 149 | } |
| Hooglugt | 23:8d9a623dd713 | 150 | if(i==2) { //green |
| Hooglugt | 23:8d9a623dd713 | 151 | red =1; |
| Hooglugt | 23:8d9a623dd713 | 152 | green=0; |
| Hooglugt | 23:8d9a623dd713 | 153 | blue=1; |
| Hooglugt | 24:c6073b9efd5b | 154 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 155 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 156 | force = 2; |
| Hooglugt | 24:c6073b9efd5b | 157 | blue = 0; |
| Hooglugt | 24:c6073b9efd5b | 158 | green = 1; |
| Hooglugt | 24:c6073b9efd5b | 159 | red=0; |
| Hooglugt | 24:c6073b9efd5b | 160 | pc.printf("E"); |
| Hooglugt | 24:c6073b9efd5b | 161 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om paars lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 162 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 163 | } else { |
| Hooglugt | 23:8d9a623dd713 | 164 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 165 | } |
| Hooglugt | 23:8d9a623dd713 | 166 | } |
| Hooglugt | 23:8d9a623dd713 | 167 | } |
| Hooglugt | 23:8d9a623dd713 | 168 | if(i==3) { //blue |
| Hooglugt | 23:8d9a623dd713 | 169 | red=1; |
| Hooglugt | 23:8d9a623dd713 | 170 | green=1; |
| Hooglugt | 23:8d9a623dd713 | 171 | blue=0; |
| Hooglugt | 24:c6073b9efd5b | 172 | for (int lag=0; lag<20; lag++) { |
| Hooglugt | 23:8d9a623dd713 | 173 | if(emgfloat.read()>0.8) { //0.8 klopt niet als grenswaarde. #nofilter |
| Hooglugt | 23:8d9a623dd713 | 174 | force = 3; |
| Hooglugt | 24:c6073b9efd5b | 175 | blue = 1; |
| Hooglugt | 24:c6073b9efd5b | 176 | green = 0; |
| Hooglugt | 24:c6073b9efd5b | 177 | red=0; |
| Hooglugt | 24:c6073b9efd5b | 178 | pc.printf("F"); |
| Hooglugt | 24:c6073b9efd5b | 179 | wait(TIMEB4NEXTCHOICE); // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie |
| Hooglugt | 23:8d9a623dd713 | 180 | goto choicesmade; |
| Hooglugt | 23:8d9a623dd713 | 181 | } else { |
| Hooglugt | 23:8d9a623dd713 | 182 | wait(0.1); |
| Hooglugt | 23:8d9a623dd713 | 183 | } |
| Hooglugt | 23:8d9a623dd713 | 184 | } |
| Hooglugt | 23:8d9a623dd713 | 185 | } |
| Hooglugt | 23:8d9a623dd713 | 186 | } |
| Hooglugt | 23:8d9a623dd713 | 187 | } |
| Hooglugt | 23:8d9a623dd713 | 188 | choicesmade: |
| Hooglugt | 23:8d9a623dd713 | 189 | red = 0; |
| Hooglugt | 23:8d9a623dd713 | 190 | green = 0; |
| Hooglugt | 23:8d9a623dd713 | 191 | blue = 0; |
| Hooglugt | 23:8d9a623dd713 | 192 | } |